QP Portfolio › Formula
Machine Learning from ScratchAll chapters QP Portfolio › FormulaOne section · Sections in this chapter2 / 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
Building Intuition: Why Portfolio Risk is More Than the Sum of Its Parts
Imagine you're investing in two assets: a technology stock and a utility stock. If you put 50% of your money in each, you might naively think your portfolio risk is simply the average of their individual risks. But this intuition misses something important: how these assets move together matters just as much as how risky each one is individually.
When technology stocks crash, utility stocks often remain stable (or even rise slightly) because they represent different economic sectors. This negative correlation means your portfolio is less risky than the simple average would suggest, as the losses in one asset are partially offset by stability in the other. Conversely, if both assets tend to move together (positive correlation), your portfolio risk is amplified beyond the simple average.
This fundamental insight, that portfolio risk depends on both individual asset risks and their relationships, is why we need a mathematical framework that captures these interactions. Linear models simply cannot represent this reality, which is why portfolio optimization requires quadratic programming.

From Intuition to Mathematics: The Portfolio Variance Formula
Now that we've seen how correlation shapes portfolio risk, let's build the mathematical framework that captures this insight. We'll proceed step by step, starting with the most intuitive representation and gradually moving toward the matrix formulation that powers modern optimization algorithms.
Step 1: Understanding What We're Measuring
Portfolio variance measures how much the portfolio's return fluctuates over time. A high variance means the portfolio value experiences large swings, while low variance indicates more stable returns. Our goal is to construct a portfolio that achieves our desired return while minimizing this variance.
Step 2: The Building Blocks
Before we can express portfolio variance mathematically, we need to define our components:
- : The weight (proportion) of asset in the portfolio, where . If , then 30% of our capital is invested in asset . These weights must satisfy (100% of capital allocated).
- : The covariance between assets and , where . This measures how the returns of these two assets move together. When , we have , which is the variance of asset (how much asset 's return fluctuates on its own). Note that (covariance is symmetric).
- : The expected return of asset (the average return we anticipate), where .
- : The total number of assets in the portfolio.
Step 3: Why a Double Summation?
The portfolio variance formula uses a double summation to capture every possible interaction between assets:
Let's break this down to understand why both summations are necessary:
-
When : We're looking at terms like , where is the variance of asset . This captures how each asset's own variance contributes to portfolio risk, weighted by the square of its portfolio weight. Notice the squared weight. This is important because doubling your position in an asset quadruples its contribution to portfolio variance (risk grows quadratically with position size).
-
When : We're looking at terms like where . These cross-terms capture the diversification effects. If (negative correlation), this term reduces portfolio variance. If (positive correlation), it increases variance. If (uncorrelated assets), the term contributes zero to portfolio variance.
The double summation includes all possible asset pairs in addition to the individual assets. Accounting for both types of terms makes the formula mathematically complete and useful in practice.

Step 4: The Expected Return Formula
While portfolio variance requires the complexity of a double summation, expected portfolio return is simple. It's just the weighted average:
where:
- : The expected return of the portfolio (a scalar)
- : The weight of asset in the portfolio
- : The expected return of asset
This linear relationship follows directly from the definition of expected value. Consider a two-asset example:
- Asset 1: weight (30%), expected return (10%)
- Asset 2: weight (70%), expected return (5%)
The portfolio's expected return is:
which equals 6.5%. Unlike risk, expected returns combine linearly with no cross-terms or interaction effects.
The Matrix Formulation
The double summation notation reveals the structure of portfolio variance, but writing out terms becomes unwieldy as the number of assets grows. Matrix notation provides the compact representation that optimization algorithms require while preserving the same mathematical content.
Defining Our Vectors and Matrices
We organize our problem into three mathematical objects:
- : The column vector of portfolio weights: , where is the weight of asset and .
- : The column vector of expected returns: , where is the expected return of asset .
- : The covariance matrix where entry for .
The covariance matrix is symmetric (since , which implies ) and positive semi-definite (required for valid covariance matrices). The diagonal elements are the individual asset variances, while off-diagonal elements (for ) capture the relationships between different assets.
The Matrix Form of Portfolio Variance
In matrix notation, the portfolio variance becomes compact:
This single matrix multiplication is mathematically equivalent to the double summation, but it's far more efficient to compute. To see the equivalence, we expand the matrix multiplication step by step:
- Compute (an vector): The -th element of this vector is:
- Compute (a scalar): This is the dot product of with :
- Distribute the outer sum: Since scalar multiplication distributes over addition:
This confirms that the matrix form equals the summation form . The matrix notation makes the quadratic nature explicit: we're computing a quadratic form, which is why this is a quadratic programming problem rather than a linear one.
The Matrix Form of Expected Return
Similarly, the expected return becomes a simple dot product:
The Complete Quadratic Programming Formulation
With both variance () and expected return () expressed in matrix form, we can now state the complete optimization problem. The goal is to find portfolio weights that minimize risk while achieving our investment objectives:
subject to:
Understanding the Objective Function
The factor of in the objective function is a mathematical convenience that simplifies derivatives without changing the optimal solution. To see why, consider the gradient (vector of partial derivatives):
where:
- : The gradient operator with respect to (produces an vector of partial derivatives)
- : The result of multiplying the covariance matrix by the weight vector
We use the fact that for symmetric . The factor of 2 from the gradient cancels with the in the objective, giving us the clean result . Without the factor, we would have , which is less convenient. Since we're minimizing, multiplying the objective by a positive constant () doesn't change the optimal solution. It only scales the objective value.
Understanding the Constraints
Each constraint serves a specific practical purpose:
-
Budget constraint (): Ensures we invest exactly 100% of our available capital, no more and no less. This prevents the optimizer from suggesting we hold cash or borrow money (unless we explicitly allow it). The constraint can be written in matrix form as , where is a vector of ones.
-
Return constraint (): Ensures our portfolio achieves a specific expected return target . In matrix form, this is . By varying , we can trace out the efficient frontier, which is the set of portfolios that offer the best risk-return trade-off.
-
Non-negativity constraints ( for all ): Prevents short selling (negative weights). In practice, many investors cannot or prefer not to short sell, so this constraint reflects real-world limitations. If we remove this constraint, we allow short selling, which can lead to more aggressive portfolios.

Mathematical Properties: Why This Problem is Well-Behaved
The quadratic programming formulation for portfolio optimization has several important mathematical properties that make it both theoretically sound and practically solvable:
Convexity: The Guarantee of Global Optimality
The objective function is convex when the covariance matrix is positive semi-definite, which is always true for valid covariance matrices. This convexity property guarantees that:
- Any local minimum is also a global minimum, so we can't get "stuck" in a suboptimal solution
- The optimization problem is well-behaved and can be solved efficiently
- The solution is unique (or the set of optimal solutions is convex)
This convexity makes portfolio optimization computationally tractable. Instead of a complex non-convex function with many local minima, we have a smooth, bowl-shaped function with a single optimal solution.
The Quadratic Nature: Capturing Non-Linear Risk
The quadratic form captures the essential non-linearity of portfolio risk. Unlike expected returns (which combine linearly), risk grows quadratically with position sizes. This means:
- Doubling your position in an asset quadruples its contribution to portfolio variance
- The cross-terms (covariances) create complex interactions that linear models cannot represent
- Diversification benefits emerge naturally from the mathematical structure
This quadratic relationship is why simple linear approaches fail for portfolio optimization. The problem fundamentally requires a quadratic framework to capture the true nature of portfolio risk.