QP Portfolio › Example
Machine Learning from ScratchAll chapters QP Portfolio › ExampleOne section · Sections in this chapter4 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
A Concrete Walkthrough: From Data to Optimal Portfolio
The efficient frontier visualization shows us what optimal portfolios look like, but how do we find them? In this section, we'll work through the complete mathematical derivation for the minimum variance portfolio, solving the optimization problem by hand using Lagrange multipliers. This walkthrough reveals the structure underlying portfolio optimization and shows exactly how the covariance matrix determines optimal allocations.
Setting Up the Problem: Our Five Assets
Suppose we're constructing a portfolio from five asset classes, each with distinct risk-return characteristics:
Expected Returns (annualized):
- Stocks: 12% (highest return, highest risk)
- Bonds: 4% (low return, low risk)
- Commodities: 8% (moderate return, high volatility)
- REITs: 10% (good return, moderate risk)
- Cash: 2% (lowest return, essentially no risk)
Covariance Matrix (annualized):
Stocks Bonds Commodities REITs Cash
Stocks 0.04 0.01 0.02 0.01 0.00
Bonds 0.01 0.01 0.00 0.00 0.00
Commodities 0.02 0.00 0.06 0.01 0.00
REITs 0.01 0.00 0.01 0.03 0.00
Cash 0.00 0.00 0.00 0.00 0.00
Let's interpret what this covariance matrix tells us:
-
Diagonal elements (variances): The diagonal elements represent individual asset variances. For example:
- Stocks: (20% volatility)
- Commodities: (24.5% volatility)
- Cash: (essentially risk-free)
-
Off-diagonal elements (covariances): The off-diagonal elements (for ) measure how asset returns move together:
- Stocks-Bonds: (tend to move together)
-
Commodities-Bonds: (uncorrelated, so they can diversify each other)
Our Goal: Finding the Minimum Variance Portfolio
The minimum variance portfolio is the portfolio with the lowest possible risk (variance) that still satisfies our constraints. This represents the leftmost point on the efficient frontier, which is the safest possible portfolio we can construct from these assets.
Step 1: Setting Up the Optimization Problem
For the minimum variance portfolio, we want to minimize portfolio variance subject to our constraints:
subject to:
Notice we don't have a return constraint here. We're purely minimizing risk. This gives us the absolute minimum risk portfolio, which we can then use as a starting point for constructing portfolios with higher returns.
Step 2: Understanding the Gradient
To solve this optimization problem, we need to find where the gradient (slope) of our objective function equals zero. The gradient tells us the direction of steepest increase, so setting it to zero finds the minimum.
The gradient of with respect to is:
This makes intuitive sense: the gradient is a linear function of the weights, where each component tells us how sensitive the portfolio variance is to changes in each asset's weight.
Step 3: Using Lagrange Multipliers to Handle Constraints
Since we have a constraint (weights must sum to 1), we use the method of Lagrange multipliers. This technique converts our constrained optimization problem into an unconstrained one by introducing a Lagrange multiplier .
We form the Lagrangian:
where:
- : The Lagrangian function, which combines the objective function and the constraint
- : The vector of portfolio weights (decision variables)
- : The Lagrange multiplier (also a decision variable to be determined)
- : The original objective function (portfolio variance)
- : The budget constraint written in standard form
The second term penalizes solutions that violate our constraint. The multiplier will be determined as part of the solution and represents the "shadow price" of the constraint, which is how much the objective function would improve if we relaxed the constraint by one unit.
Step 4: Finding the Optimality Conditions
Taking partial derivatives and setting them to zero gives us the optimality conditions (first-order necessary conditions for optimality):
where:
- : The gradient of the Lagrangian with respect to (a vector)
- : The partial derivative of the Lagrangian with respect to (a scalar)
- : A vector of ones:
- : A vector of zeros
Rearranging, we get the system of equations:
The first equation tells us that at the optimum, the gradient must be proportional to the vector of ones. The second equation enforces our budget constraint.
Step 5: Deriving the Closed-Form Solution
From the first optimality condition , we can solve for by left-multiplying both sides by (assuming is invertible, which is true for positive definite covariance matrices):
Since (the identity matrix), we get:
This tells us the optimal weights are proportional to , which is the inverse covariance matrix times a vector of ones. The inverse covariance matrix appears because we're essentially "dividing out" the covariance structure to find the optimal allocation.
Substituting this expression for into the budget constraint :
Since is a scalar, we can factor it out:
Solving for :
Note that is a scalar: it equals the sum of all elements in . This scalar is always positive for positive definite covariance matrices.
Substituting this value of back into , we get the optimal weights:
where:
- : The optimal portfolio weights (an vector)
- : The inverse of the covariance matrix (an matrix)
- : A vector of ones (an vector)
- : A scalar equal to the sum of all elements in
What This Solution Tells Us
This closed-form solution shows that the minimum variance portfolio weights are determined entirely by the covariance structure, not by expected returns. The solution is proportional to the inverse of the covariance matrix, scaled so the weights sum to one.
The denominator is a normalization constant that ensures our weights sum to 1. The numerator tells us the "raw" optimal allocation before normalization.
This solution has several advantages:
- It's analytical: we can compute it directly without iterative optimization.
- It's unique: the convexity of the problem guarantees a single optimal solution.
- It's interpretable: the inverse covariance matrix captures how assets should be weighted to minimize risk.
In practice, we would compute this using numerical linear algebra (solving the system of equations rather than explicitly inverting the matrix), but this closed-form expression gives us deep insight into the mathematical structure of the problem.