Plot efficient frontier with best Sharpe ratio
Let's now plot the efficient frontier again, but add a marker for the portfolio with the best Sharpe index. Visualizing our data is always a good idea to better understand it.
Recall the efficient frontier is plotted in a scatter plot of portfolio volatility on the x-axis, and portfolio returns on the y-axis. We'll get the latest date we have in our data from covariances.keys()
, although any of the portfolio_returns
, etc, dictionaries could be used as well to get the date. Then we get volatilities and returns for the latest date we have from our portfolio_volatility
and portfolio_returns
. Finally we get the index of the portfolio with the best Sharpe index from max_sharpe_idxs[date]
, and plot everything with plt.scatter()
.
This exercise is part of the course
Machine Learning for Finance in Python
Exercise instructions
- Set
cur_volatility
to be the portfolio volatilities for the latestdate
. - Construct the "efficient frontier" plot by plotting volatility on the x-axis and returns on the y-axis.
- Get the best portfolio index for the latest
date
frommax_sharpe_idxs
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Get most recent (current) returns and volatility
date = sorted(covariances.keys())[-1]
cur_returns = portfolio_returns[date]
cur_volatility = ____
# Plot efficient frontier with sharpe as point
plt.scatter(x=____, y=____, alpha=0.1, color='blue')
best_idx = max_sharpe_idxs[____]
# Place an orange "X" on the point with the best Sharpe ratio
plt.scatter(x=cur_volatility[best_idx], y=cur_returns[best_idx], marker='x', color='orange')
plt.xlabel('Volatility')
plt.ylabel('Returns')
plt.show()