1. Learn
  2. /
  3. Courses
  4. /
  5. Machine Learning for Finance in Python

Exercise

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().

Instructions

100 XP
  • Set cur_volatility to be the portfolio volatilities for the latest date.
  • 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 from max_sharpe_idxs.