Back to Community
Beta Constrained Markowitz Minimum Variance Portfolio

There was an interesting question on StackExchange recently ( asking how we could use Markowitz to find a portfolio that not only minimizes correlations but also minimizes beta (in response to the Quantopian open).

I took a quick stab at it and posted an example here. For more information, see the full NB at

Loading notebook preview...

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

2 responses

You should generate the market return time series and stock residual time series separately and correlate them by using the betas. This creates a simulated world where CAPM is the model that generated the data. As the code is written, the stocks are independent and your estimate of covariance is pure statistical noise. Also, when taking the covariance matrix S = opt.matrix(np.cov(returns)) you should not be using the full matrix of stock returns, that defeats the purpose of a factor model. The covariance matrix should be calculated as $\Sigma = \beta\sigma^2\beta^\prime + \text{COV}[\epsilon]$ where $\beta$ is the beta vector, $\sigma^2$ is the variance of the market, and $\epsilon$ is the matrix of residuals.

Hopefully this makes sense.


thanks for your response. I completely agree -- would be a more realistic example if there actually was any correlation between the returns. Before I used this code to generate correlated returns:

def gen_cov(n_assets=5, var=1, covar=0, var_vec=None, noise=0):  
    X = np.random.randn(n_assets, n_assets) * noise + covar  
    if var_vec is None:  
        np.fill_diagonal(X, var)  
        np.fill_diagonal(X, var_vec)  
    return X

def gen_returns(mean=0., cov=None, size=100, var=1, covar=0, n_assets=5):  
    if cov is None:  
        cov = gen_cov(var=var, covar=covar, n_assets=n_assets)  
    if np.isscalar(mean):  
        mean = np.ones((cov.shape[0],), np.float32) * mean  
    return pd.DataFrame(np.random.multivariate_normal(mean, cov, size=size))  

I posted the code and some more simulation studies here: