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.
Este exercício faz parte do curso
Machine Learning for Finance in Python
Instruções do exercício
- 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 be0.1
.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# 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()