Mean-Variance Optimization is an approach to find the optimal allocation of assets in a portfolio based on Modern Portfolio Theory. We want to minimize the portfolio risk for a given expected return, or equivalently, maximize the return for a given level of risk. Mathematically, we can define the optimization problem as follows:
Given the expected returns vector $\mathbf{E}(R)=\begin{bmatrix} E(R_1) \\ E(R_2) \\ \vdots \\ E(R_n) \end{bmatrix}$ and the covariance matrix ∑, the optimization problem is to find:
$$\begin{aligned}
\min_{\mathbf{w}} & \quad \frac{1}{2}\mathbf{w}^\top\boldsymbol{\Sigma}\mathbf{w} \\
\text{s.t.} & \quad \mathbf{w}^\top\mathbf{E}(R)=\mu \\
& \quad \mathbf{w}^\top\mathbf{1} = 1 \\
\end{aligned}$$
$$\begin{aligned}
\min_{\mathbf{w}} & \quad \frac{1}{2}\mathbf{w}^\top\boldsymbol{\Sigma}\mathbf{w} \\
\text{s.t.} & \quad \mathbf{w}^\top\mathbf{E}(R)=\mu \\
& \quad \mathbf{w}^\top\mathbf{1} = 1 \\
\end{aligned}$$
Here, we have the following notations:
- $\mathbf{w} = \begin{bmatrix} w_1 \\ w_2 \\ \vdots \\ w_n \end{bmatrix}$ is the vector of asset weights in the portfolio that we want to find.
- μ is the target expected return.
- 1 denotes an n-dimensional vector of ones.
To solve this optimization problem, we can use the method of Lagrange multipliers. The Lagrangian is given by:
$$\mathcal{L}(\mathbf{w}, \lambda_1, \lambda_2) = \frac{1}{2}\mathbf{w}^\top\boldsymbol{\Sigma}\mathbf{w} - \lambda_1 (\mathbf{w}^\top\mathbf{E}(R)-\mu) - \lambda_2 (\mathbf{w}^\top\mathbf{1} -1)$$
Now we take the first-order conditions with respect to w, λ1, and λ2:
$$\begin{aligned}
\frac{\partial \mathcal{L}}{\partial \mathbf{w}} &= \boldsymbol{\Sigma} \mathbf{w} - \lambda_1 \mathbf{E}(R) - \lambda_2 \mathbf{1} = 0 \\
\frac{\partial \mathcal{L}}{\partial \lambda_1} &= \mathbf{w}^\top\mathbf{E}(R)-\mu = 0 \\
\frac{\partial \mathcal{L}}{\partial \lambda_2} &= \mathbf{w}^\top\mathbf{1} -1 = 0 \\
\end{aligned}$$
Solving the system of linear equations above, we get the optimal weight vector w. Now, let’s illustrate this with a concrete example using 4 assets.
Suppose we have the expected returns vector:
$$\mathbf{E}(R) = \begin{bmatrix} 0.05 \\ 0.1 \\ 0.15 \\ 0.2 \end{bmatrix}$$
and the covariance matrix:
$$\boldsymbol{\Sigma} = \begin{bmatrix} 0.01 & 0.001 & 0.005 & 0.002 \\ 0.001 & 0.02 & 0.001 & 0.001 \\ 0.005 & 0.001 & 0.03 & 0.002 \\ 0.002 & 0.001 & 0.002 & 0.04 \end{bmatrix}$$
To solve the optimization problem, we will use some numerical optimization package, e.g., ‘cvxpy‘ in Python for different target returns μ:
import numpy as np
import cvxpy as cp
expected_returns = np.array([0.05, 0.1, 0.15, 0.2])
cov_matrix = np.array([[0.01, 0.001, 0.005, 0.002],
[0.001, 0.02, 0.001, 0.001],
[0.005, 0.001, 0.03, 0.002],
[0.002, 0.001, 0.002, 0.04]])
n = expected_returns.shape[0]
w = cp.Variable(n)
mu = cp.Parameter()
risk = cp.quad_form(w, cov_matrix)
constraints = [cp.sum(w) == 1, w @ expected_returns == mu, w >= 0]
objective = cp.Minimize(risk)
problem = cp.Problem(objective, constraints)
# Target return
target_return = 0.1
mu.value = target_return
problem.solve()
optimized_weights = w.value
print("Optimized weights for target return {:.2f}: n{}".format(target_return, optimized_weights))
By changing the ‘target_return‘ parameter, you can find the optimal weight allocations for different target returns. To visualize the result, you can generate a plot of the efficient frontier as a curve in the expected return–volatility space for the optimal portfolios.