How do I find minimum-variance weights?

Estimate a covariance matrix, compute the fully invested minimum-variance weights with np.linalg.solve, and see how a small change in the returns moves the weights.

After this lesson, you can compute the weights of the fully invested portfolio with the lowest estimated variance and see how sensitive they are to the data.

How do I compute the weights?

The minimum-variance weights solve the covariance matrix times a vector \(x\) equals a vector of ones, scaled so that they add up to 1. For two made-up stocks over four days, they are 70.03% in AAA and 29.97% in BBB, and the portfolio’s variance of 1.332134 is below the 1.332670 of equal weights.

\[ w = \frac{\Sigma^{-1}\mathbf{1}}{\mathbf{1}^\top \Sigma^{-1} \mathbf{1}}, \qquad V_p = w^\top \Sigma w \]

Here \(\Sigma\) is the covariance matrix of the returns, \(\mathbf{1}\) a vector of ones, one per stock, \(w\) the weights and \(V_p\) the portfolio’s variance. The weights add up to 1 and may be negative, a short sale.

The returns are in percent, one row per day and one column per stock. np.cov(returns, rowvar=False) estimates the covariance matrix of the columns, as in the lesson on correlation. np.linalg.solve(covariance, np.ones(...)) solves \(\Sigma x = \mathbf{1}\) without computing the inverse, and dividing by x.sum() gives the weights. weights @ covariance @ weights computes \(w^\top \Sigma w\), as in the lesson on portfolio returns.

The following box computes the weights and the two variances.

import numpy as np

returns = np.array([
    [-1.0, -0.897],
    [-1.0, -1.097],
    [1.0, 1.097],
    [1.0, 0.897],
])

def min_variance_weights(returns):
    covariance = np.cov(returns, rowvar=False)
    x = np.linalg.solve(covariance, np.ones(len(covariance)))
    return x / x.sum()

weights = min_variance_weights(returns)
covariance = np.cov(returns, rowvar=False)
equal = np.array([0.5, 0.5])
print(np.round(weights, 6))
print(round(weights @ covariance @ weights, 6), round(equal @ covariance @ equal, 6))
[0.70027 0.29973]
1.332134 1.33267

The two variances differ little, because the stocks move closely together.

Why can a small change in the returns move the weights so much?

When two stocks move almost together, the variance of their difference is small, and it is the denominator of the weights. Raising BBB’s return on day 4 by 0.04 percentage points moves the weights from 70.03% and 29.97% to 184.86% in AAA and −84.86% in BBB.

\[ w_A = \frac{\sigma_B^2 - \sigma_{AB}}{\sigma_A^2 + \sigma_B^2 - 2\,\sigma_{AB}} \]

Here \(\sigma_A^2\) and \(\sigma_B^2\) are the variances of the two stocks and \(\sigma_{AB}\) their covariance. The denominator is the variance of AAA’s return minus BBB’s.

.copy() makes a separate array, as in the lesson on views and copies, and revised[3, 1] is the cell of day 4 and BBB.

The following box computes the weights with the revised return.

revised = returns.copy()
revised[3, 1] = 0.937
print(np.round(min_variance_weights(revised), 6))
[ 1.848588 -0.848588]

A real covariance matrix is estimated with error, so a minimum-variance portfolio of similar stocks can take large positions that the next sample reverses.

Your turn

Compute the variance of the revised portfolio with the revised covariance matrix. You should see 1.325413.

The revised weights and the revised covariance matrix go into the same formula.

revised_weights = min_variance_weights(revised)
print(round(revised_weights @ np.cov(revised, rowvar=False) @ revised_weights, 6))
1.325413