Path: blob/master/05_strategy_evaluation/04_mean_variance_optimization.ipynb
2923 views
Mean-Variance Optimization
MPT solves for the optimal portfolio weights to minimize volatility for a given expected return, or maximize returns for a given level of volatility. The key requisite input are expected asset returns, standard deviations, and the covariance matrix.
Diversification works because the variance of portfolio returns depends on the covariance of the assets and can be reduced below the weighted average of the asset variances by including assets with less than perfect correlation. In particular, given a vector, ω, of portfolio weights and the covariance matrix, , the portfolio variance, is defined as:
Markowitz showed that the problem of maximizing the expected portfolio return subject to a target risk has an equivalent dual representation of minimizing portfolio risk subject to a target expected return level, . Hence, the optimization problem becomes:
We can calculate an efficient frontier using scipy.optimize.minimize
and the historical estimates for asset returns, standard deviations, and the covariance matrix.
Imports & Settings
Prepare Data
We select historical data for tickers included in the S&P500 (according to Wikipedia) from 1998-2017.
Compute Inputs
Compute Returns
Create month-end monthly returns and drop dates that have no observations:
Set Parameters
Annualization Factor
Compute Mean Returns, Covariance and Precision Matrix
The precision matrix is the inverse of the covariance matrix:
Risk-Free Rate
Load historical 10-year Treasury rate:
Simulate Random Portfolios
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:
Plot Simulated Portfolios
Compute Annualize PF Performance
Now we'll set up the quadratic optimization problem to solve for the minimum standard deviation for a given return or the maximum SR.
To this end, define the functions that measure the key metrics:
Max Sharpe PF
Define a target function that represents the negative SR for scipy's minimize function to optimize, given the constraints that the weights are bounded by [-1, 1], if short trading is permitted, and [0, 1] otherwise, and sum to one in absolute terms.
Compute Efficient Frontier
The solution requires iterating over ranges of acceptable values to identify optimal risk-return combinations
The mean-variance frontier relies on in-sample, backward-looking optimization. In practice, portfolio optimization requires forward-looking input. Unfortunately, expected returns are notoriously difficult to estimate accurately.
The covariance matrix can be estimated somewhat more reliably, which has given rise to several alternative approaches. However, covariance matrices with correlated assets pose computational challenges since the optimization problem requires inverting the matrix. The high condition number induces numerical instability, which in turn gives rise to Markovitz curse: the more diversification is required (by correlated investment opportunities), the more unreliable the weights produced by the algorithm.
Min Volatility Portfolio
Run Calculation
Get random PF
Get Max Sharpe PF
From simulated pf data
Get Min Vol PF
Get Efficent PFs
Plot Result
The simulation yields a subset of the feasible portfolios, and the efficient frontier identifies the optimal in-sample return-risk combinations that were achievable given historic data.
The below figure shows the result including the minimum variance portfolio and the portfolio that maximizes the SR and several portfolios produce by alternative optimization strategies. The efficient frontier