Skip to content

Deriving Mean–Variance Portfolio Weights With Lagrange Multipliers

Original question

Can you explain mean-variance portfolio optimisation and derive the mathematical solution using Lagrange multipliers?
Mean-variance optimisation finds the weights with the smallest variance for a specified expected return. With a fully invested portfolio, unrestricted short selling, and a positive-definite covariance matrix, two Lagrange multipliers give the exact solution. One enforces the target return and the other enforces that the weights sum to one. Long-only limits turn the problem into a constrained quadratic programme.

Set up the two constraints and solve the multipliers

Let ww be the weight vector, μ\mu the expected returns, Σ\Sigma the covariance matrix and mm the target return. Solve

min⁡w12w⊤Σw,1⊤w=1,μ⊤w=m.\min_w \frac12 w^\top\Sigma w,\qquad \mathbf{1}^\top w=1,\quad \mu^\top w=m.
The Lagrangian is 12w⊤Σw−λ(μ⊤w−m)−γ(1⊤w−1)\tfrac12w^\top\Sigma w-\lambda(\mu^\top w-m)-\gamma(\mathbf{1}^\top w-1). Stationarity gives
w=Σ−1(λμ+γ1).w=\Sigma^{-1}(\lambda\mu+\gamma\mathbf{1}).

Define A=1⊤Σ−11A=\mathbf{1}^\top\Sigma^{-1}\mathbf{1}, B=1⊤Σ−1μB=\mathbf{1}^\top\Sigma^{-1}\mu, and C=μ⊤Σ−1μC=\mu^\top\Sigma^{-1}\mu. Substitution into the constraints yields Aγ+Bλ=1A\gamma+B\lambda=1 and Bγ+Cλ=mB\gamma+C\lambda=m. With D=AC−B2>0D=AC-B^2>0,

γ=C−BmD,λ=Am−BD,σp2(m)=Am2−2Bm+CD.\gamma=\frac{C-Bm}{D},\quad \lambda=\frac{Am-B}{D},\qquad \sigma_p^2(m)=\frac{Am^2-2Bm+C}{D}.
The global minimum-variance portfolio has return B/AB/A. The efficient branch consists of targets m≥B/Am\geq B/A. Below that return, moving towards the minimum-variance point increases expected return and decreases variance. When all assets have the same expected return, the return constraint is either redundant or infeasible and D=0D=0.
Differentiating 12w⊤Σw\tfrac12 w^\top\Sigma w gives Σw\Sigma w because Σ\Sigma is symmetric. The two linear constraints contribute multiples of 1\mathbf 1 and μ\mu. Solving for ww at fixed multipliers therefore gives a linear combination of Σ−11\Sigma^{-1}\mathbf 1 and Σ−1μ\Sigma^{-1}\mu. Substituting that combination back into the budget and return equations produces a two-by-two system. Its determinant is positive when μ\mu is not proportional to 1\mathbf 1 and Σ\Sigma is positive definite. In computation, solve the linear systems rather than forming an explicit matrix inverse.

There is a useful boundary case before you invert the two-by-two matrix. If all assets have the same expected return, the return constraint is proportional to the budget constraint. Every fully invested portfolio then has the same expected return. A target at that return leaves only the global minimum-variance problem, while any other target is infeasible. This is why a zero determinant needs interpretation. Adding a small numerical constant to force the two-multiplier formula through would hide the fact that the two constraints are carrying the same information.

Check the solution on a two-asset target

Suppose equities have an expected return of 8% and volatility of 18%, bonds have an expected return of 4% and volatility of 8%, and their correlation is 0.20. The 6% return target forces an even split. The covariance is 0.20(0.18)(0.08)=0.002880.20(0.18)(0.08)=0.00288. The resulting variance is 0.011140.01114 and volatility is about 10.55%. The global minimum-variance portfolio allocates about 10.65% to equities, giving an expected return of roughly 4.43%. The 6% target sits on the efficient branch.

Solve the two constraints in Python

Solve the linear systems directly to avoid forming a matrix inverse.

import numpy as np mu = np.array([0.08, 0.04]) cov = np.array([[0.0324, 0.00288], [0.00288, 0.0064]]) constraints = np.column_stack([np.ones(mu.size), mu]) basis = np.linalg.solve(cov, constraints) multipliers = np.linalg.solve(constraints.T @ basis, [1.0, 0.06]) weights = basis @ multipliers print(weights, np.sqrt(weights @ cov @ weights))

Interactive example

See how correlation changes two-asset portfolio risk

Asset A has 15% annual volatility and asset B has 25%. Move the correlation or the long-only weight in A.

Selected portfolio volatility 15.03% · minimum-risk weight in A 80.19%
Portfolio volatility as the weight in asset A changes1520250255075100Weight in asset A (%)Annual volatility (%)
  • Portfolio volatility
  • Selected weight

References and next steps