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 be the vector of expected returns, the covariance matrix of returns, and the fractions invested (). The portfolio's expected return is and its variance (risk) is . Three equivalent ways to trade them off:
The first two are QPs; the third has a convex quadratic constraint (an SOCP). The constraint forbids short selling.
Minimum variance and the efficient frontier
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 and return constraints, the minimum-variance portfolio has a closed form from the Lagrange conditions :
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 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 | convex, LP-representable (absolute value trick) |
| position limits | bounds |
| at most assets | binary variables: a mixed-integer QP |
| uncertain (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 ) are common defences.
In software
- CVXPY's portfolio examples,
PyPortfolioOpt,riskfolio-lib; commercial risk systems use MOSEK or Gurobi with factor risk models () 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 , , normalized to . The variance is , 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
makes convex, and every constraint is linear.
Exercise 3: Risk aversion (code)
Solve (long-only, fully invested) for and . Which is riskier?
Answer
takes more risk.
Solution
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…
- a convex quadratic program
- an integer program
- non-convex
- a linear program
Diversification lowers risk because…
- asset returns are not perfectly correlated
- returns are always positive
- variance is linear in the weights
- the solver rounds weights
Limiting a portfolio to at most assets turns the QP into…
- an LP
- a mixed-integer QP
- an unconstrained problem
- an SDP