Get startedGet started for free

Plot efficient frontier

We can finally plot the results of our MPT portfolios, which shows the "efficient frontier". This is a plot of the volatility vs the returns. This can help us visualize our risk-return possibilities for portfolios. The upper left boundary of the points is the best we can do (highest return for a given risk), and that is the efficient frontier.

To create this plot, we will use the latest date in our covariances dictionary which we created a few exercises ago. This has dates as keys, so we'll get the sorted keys using sorted() and .keys(), then get the last entry with Python indexing ([-1]). Lastly we'll use matplotlib to scatter variance vs returns and see the efficient frontier for the latest date in the data.

This exercise is part of the course

Machine Learning for Finance in Python

View Course

Exercise instructions

  • Get the latest date from the covariances dictionary -- remember the dates are the keys.
  • Plot the volatility vs returns (portfolio_returns) for the latest date in a scatter plot, and set the alpha value for transparency to be 0.1.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Get latest date of available data
date = sorted(covariances.____)[____]  

# Plot efficient frontier
# warning: this can take at least 10s for the plot to execute...
plt.scatter(x=portfolio_volatility[date], y=____,  alpha=____)
plt.xlabel('Volatility')
plt.ylabel('Returns')
plt.show()
Edit and Run Code