TExcelTAD
Home › Optimization › Portfolio Optimization

Portfolio Optimization

Harry Markowitz's mean-variance model (1952) is one of the most famous applications of optimization: choose how to split money among assets to balance expected return against risk. It is a convex quadratic program, and its extensions (transaction costs, position limits, robust and cardinality constraints) touch most of this course.

The model

Let μ\boldsymbol{\mu} be the vector of expected returns, Σ\Sigma the covariance matrix of returns, and w\mathbf{w} the fractions invested (∑iwi=1\sum_iw_i = 1). The portfolio's expected return is μTw\boldsymbol{\mu}^T\mathbf{w} and its variance (risk) is wTΣw\mathbf{w}^T\Sigma\mathbf{w}. Three equivalent ways to trade them off:

min⁡w wTΣws.t. μTw≥rmin⁡, 1Tw=1, w≥0max⁡w μTw−γ wTΣws.t. 1Tw=1, w≥0max⁡w μTws.t. wTΣw≤σmax⁡2, 1Tw=1, w≥0\begin{aligned} &\min_{\mathbf{w}}\ \mathbf{w}^T\Sigma\mathbf{w} && \text{s.t. } \boldsymbol{\mu}^T\mathbf{w} \ge r_{\min},\ \mathbf{1}^T\mathbf{w} = 1,\ \mathbf{w} \ge 0 \\ &\max_{\mathbf{w}}\ \boldsymbol{\mu}^T\mathbf{w} - \gamma\,\mathbf{w}^T\Sigma\mathbf{w} && \text{s.t. } \mathbf{1}^T\mathbf{w} = 1,\ \mathbf{w} \ge 0 \\ &\max_{\mathbf{w}}\ \boldsymbol{\mu}^T\mathbf{w} && \text{s.t. } \mathbf{w}^T\Sigma\mathbf{w} \le \sigma_{\max}^2,\ \mathbf{1}^T\mathbf{w} = 1,\ \mathbf{w} \ge 0 \end{aligned}

The first two are QPs; the third has a convex quadratic constraint (an SOCP). The constraint w≥0\mathbf{w} \ge 0 forbids short selling.

Minimum variance and the efficient frontier

python · try it yourself
import numpy as np
from scipy.optimize import minimize

mu = np.array([0.06, 0.08, 0.11, 0.14])                      # expected annual returns
vol = np.array([0.10, 0.14, 0.20, 0.28])
corr = np.array([[1.0, 0.3, 0.2, 0.1], [0.3, 1.0, 0.4, 0.3], [0.2, 0.4, 1.0, 0.5], [0.1, 0.3, 0.5, 1.0]])
Sigma = np.outer(vol, vol) * corr

def min_risk(r_min):
    cons = [{"type": "eq", "fun": lambda w: w.sum() - 1}, {"type": "ineq", "fun": lambda w: mu @ w - r_min}]
    r = minimize(lambda w: w @ Sigma @ w, np.full(4, 0.25), jac=lambda w: 2 * Sigma @ w,
                 constraints=cons, bounds=[(0, 1)] * 4, method="SLSQP", options={"ftol": 1e-12})
    return r.x
for target in (0.07, 0.09, 0.11, 0.13):
    w = min_risk(target)
    print(f"target {target:.0%}: weights {np.round(w, 3) + 0}, risk (std) {np.sqrt(w @ Sigma @ w):.2%}")
target 7%: weights [0.686 0.222 0.059 0.033], risk (std) 9.06%
target 9%: weights [0.374 0.233 0.202 0.191], risk (std) 11.37%
target 11%: weights [0.063 0.245 0.344 0.349], risk (std) 16.29%
target 13%: weights [0.    0.    0.333 0.667], risk (std) 22.74%

Each line is a point on the efficient frontier: no portfolio has higher return for the same risk. Notice how diversification lowers the risk below that of the safest single asset at the low end: correlations below 1 let risks partly cancel.

The KKT view: unconstrained minimum variance

Without the w≥0\mathbf{w} \ge 0 and return constraints, the minimum-variance portfolio has a closed form from the Lagrange conditions 2Σw=ν12\Sigma\mathbf{w} = \nu\mathbf{1}:

wmv=Σ−111TΣ−11\mathbf{w}_{\text{mv}} = \frac{\Sigma^{-1}\mathbf{1}}{\mathbf{1}^T\Sigma^{-1}\mathbf{1}}
python · try it yourself
import numpy as np

vol = np.array([0.10, 0.14, 0.20, 0.28])
corr = np.array([[1.0, 0.3, 0.2, 0.1], [0.3, 1.0, 0.4, 0.3], [0.2, 0.4, 1.0, 0.5], [0.1, 0.3, 0.5, 1.0]])
Sigma = np.outer(vol, vol) * corr
z = np.linalg.solve(Sigma, np.ones(4))
w = z / z.sum()
print("minimum-variance weights:", np.round(w, 4), " risk:", f"{np.sqrt(w @ Sigma @ w):.2%}")
minimum-variance weights: [0.6972 0.2215 0.0542 0.027 ]  risk: 9.05%

All four weights happen to be positive, so adding w≥0\mathbf{w} \ge 0 would change nothing here, and the risk of 9.05% sits just below the 7% point of the frontier above. With strongly correlated assets the formula often produces negative weights (short positions), and then the long-only QP gives a different answer.

Practical extensions

Extension Effect on the problem
transaction costs κ∥w−wold∥1\kappa\lVert \mathbf{w} - \mathbf{w}_{\text{old}} \rVert_1 convex, LP-representable (absolute value trick)
position limits wi≤0.3w_i \le 0.3 bounds
at most KK assets binary variables: a mixed-integer QP
uncertain μ\boldsymbol{\mu} (robust) SOCP (Second-Order Cone and Semidefinite Programs)
CVaR (tail risk) instead of variance an LP over return scenarios

Estimation error is the real enemy: mean returns are hard to estimate, and optimizers amplify their errors, producing extreme, unstable weights. Shrinking the covariance estimate, adding weight bounds or using only the minimum-variance portfolio (which ignores μ\boldsymbol{\mu}) are common defences.

In software

  • CVXPY's portfolio examples, PyPortfolioOpt, riskfolio-lib; commercial risk systems use MOSEK or Gurobi with factor risk models (Σ=BFBT+D\Sigma = BFB^T + D) to keep the QP sparse.

Exercises

Exercise 1: Two assets

Two uncorrelated assets with standard deviations 0.1 and 0.2. Which fully invested mix has the smallest variance?

Answer

80% in the first, 20% in the second.

Solution

With Σ=diag⁡(0.01,0.04)\Sigma = \operatorname{diag}(0.01, 0.04), Σ−11=(100,25)\Sigma^{-1}\mathbf{1} = (100, 25), normalized to (0.8,0.2)(0.8, 0.2). The variance is 0.64⋅0.01+0.04⋅0.04=0.0080.64 \cdot 0.01 + 0.04 \cdot 0.04 = 0.008, below either asset alone.

Exercise 2: Which form?

You must earn at least 9% and want the least risk. Which formulation do you use, and is it convex?

Answer

The first (minimize variance subject to a return floor); it is a convex QP.

Solution

Σ⪰0\Sigma \succeq 0 makes wTΣw\mathbf{w}^T\Sigma\mathbf{w} convex, and every constraint is linear.

Exercise 3: Risk aversion (code)

Solve max⁡μTw−γwTΣw\max \boldsymbol{\mu}^T\mathbf{w} - \gamma\mathbf{w}^T\Sigma\mathbf{w} (long-only, fully invested) for γ=1\gamma = 1 and γ=10\gamma = 10. Which is riskier?

Answer

γ=1\gamma = 1 takes more risk.

Solution
python · try it yourself
import numpy as np
from scipy.optimize import minimize
mu = np.array([0.06, 0.08, 0.11, 0.14]); vol = np.array([0.10, 0.14, 0.20, 0.28])
corr = np.array([[1.0, 0.3, 0.2, 0.1], [0.3, 1.0, 0.4, 0.3], [0.2, 0.4, 1.0, 0.5], [0.1, 0.3, 0.5, 1.0]])
S = np.outer(vol, vol) * corr
for g in (1.0, 10.0):
    r = minimize(lambda w: -(mu @ w - g * w @ S @ w), np.full(4, 0.25), bounds=[(0, 1)] * 4,
                 constraints=[{"type": "eq", "fun": lambda w: w.sum() - 1}], method="SLSQP", options={"ftol": 1e-12})
    w = r.x
    print(f"gamma {g:4}: weights {np.round(w, 3) + 0}, return {mu @ w:.2%}, risk {np.sqrt(w @ S @ w):.2%}")
gamma  1.0: weights [0.    0.241 0.375 0.384], return 11.43%, risk 17.49%
gamma 10.0: weights [0.627 0.224 0.086 0.063], return 7.38%, risk 9.18%

Test yourself

The Markowitz minimum-variance problem with a return floor is…

  1. a convex quadratic program
  2. an integer program
  3. non-convex
  4. a linear program

Diversification lowers risk because…

  1. asset returns are not perfectly correlated
  2. returns are always positive
  3. variance is linear in the weights
  4. the solver rounds weights

Limiting a portfolio to at most KK assets turns the QP into…

  1. an LP
  2. a mixed-integer QP
  3. an unconstrained problem
  4. an SDP