The efficient frontier in Python

We can calculate an efficient frontier using scipy.optimize.minimize and the historical estimates for asset returns, standard deviations, and the covariance matrix. The code can be found in the efficient_frontier subfolder of the repo for this chapter and implements the following sequence of steps:

  1. The simulation generates random weights using the Dirichlet distribution, and computes the mean, standard deviation, and SR for each sample portfolio using the historical return data:
def simulate_portfolios(mean_ret, cov, rf_rate=rf_rate, short=True):    alpha = np.full(shape=n_assets, fill_value=.01)    weights = dirichlet(alpha=alpha, size=NUM_PF)    weights *= choice([-1, 1], size=weights.shape) returns = weights @ ...

Get Hands-On Machine Learning for Algorithmic Trading now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.