How do I solve for weights with scipy’s minimize?

Compute each stock’s share of portfolio risk and use scipy.optimize.minimize with bounds and a budget constraint to find long-only weights that give each stock an equal share.

After this lesson, you can find portfolio weights with a numerical optimiser and check that each stock carries an equal share of the risk.

How much risk does each stock contribute?

A stock’s share of portfolio risk is its weight times its covariance with the portfolio, divided by the portfolio’s variance, and the shares add up to 1. With equal weights in three made-up stocks, BBB contributes 66.67% of the risk, AAA 22.22% and CCC 11.11%.

\[ s_i = \frac{w_i\,(\Sigma w)_i}{w^\top \Sigma w} \]

Here \(w\) is the vector of weights, \(\Sigma\) the covariance matrix, \((\Sigma w)_i\) stock \(i\)’s covariance with the portfolio and \(s_i\) its share of the risk.

The returns are four days of three made-up stocks in percent, one column per stock. As in the lesson on minimum-variance weights, np.cov(returns, rowvar=False) estimates the covariance matrix. In risk_shares, covariance @ weights gives each stock’s covariance with the portfolio, and multiplying by the weights gives the parts that add up to the variance.

The following box computes the risk shares at equal weights.

import numpy as np
from scipy.optimize import minimize

returns = np.array([
    [-1.0, -3.0, -1.0],
    [-1.0, 1.0, 1.0],
    [1.0, -1.0, 1.0],
    [1.0, 3.0, -1.0],
])
covariance = np.cov(returns, rowvar=False)

def risk_shares(weights):
    parts = weights * (covariance @ weights)
    return parts / parts.sum()

start = np.ones(3) / 3
print(np.round(risk_shares(start), 4))
[0.2222 0.6667 0.1111]

How do I find weights with equal risk shares?

minimize searches for the weights that make a function as small as possible within bounds and constraints. Here the function is the sum of squared gaps between the risk shares and one third, and the solution puts 37.73% in AAA, 16.87% in BBB and 45.39% in CCC, which gives each stock a third of the risk.

\[ f(w) = \sum_{i=1}^{3} \Big(s_i(w) - \frac{1}{3}\Big)^2 \]

bounds=[(0, 1)] * 3 keeps each weight between 0 and 1, so no stock is sold short. The constraint {"type": "eq", "fun": budget} requires budget to return zero, so the weights add up to 1. method="SLSQP" selects an algorithm that handles both, and options={"ftol": 1e-12} sets a small stopping tolerance. result.success reports whether the solver met its stopping rule, and result.x holds the weights.

The following box runs the optimiser and checks the risk shares at its solution.

def objective(weights):
    return ((risk_shares(weights) - 1 / 3) ** 2).sum()

def budget(weights):
    return weights.sum() - 1

result = minimize(objective, start, method="SLSQP", bounds=[(0, 1)] * 3, constraints=[{"type": "eq", "fun": budget}], options={"ftol": 1e-12})
weights = result.x
print(result.success)
print(np.round(weights, 4))
print(np.round(risk_shares(weights), 4))
True
[0.3773 0.1687 0.4539]
[0.3333 0.3333 0.3333]

BBB gets the smallest weight, because it carries the most risk per dollar.

Your turn

Compute each stock’s contribution to the portfolio’s daily volatility, its risk share times the volatility \(\sqrt{w^\top \Sigma w}\). You should see 0.302617 for each stock and a sum of 0.907852.

np.sqrt(weights @ covariance @ weights) gives the volatility in percent.

contributions = risk_shares(weights) * np.sqrt(weights @ covariance @ weights)
print(np.round(contributions, 6), round(contributions.sum(), 6))
[0.302617 0.302617 0.302617] 0.907852