Modern Portfolio Theory and Mean-Variance Optimization
Covers Modern Portfolio Theory and mean-variance optimization. Topics include efficient frontier, diversification mathematics.
Quantitative FinanceAll chapters Modern Portfolio Theory and Mean-Variance OptimizationFull chapter · Sections in this chapter1 / 12 ↓
Reading tools Definitions, links & notes
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
Modern Portfolio Theory and Mean-Variance Optimization
In 1952, Harry Markowitz published "Portfolio Selection," developing a foundational mathematical framework for mean-variance portfolio selection. Investors already recognized risk and diversification; Markowitz made the relationship between portfolio risk, individual asset risks, and covariances operational.
In the mean-variance framework, investors prefer higher expected return and lower variance, all else equal. Portfolio risk is measured by return variance or standard deviation. Efficient portfolios offer undominated risk-return combinations; minimizing variance at an arbitrary exact target can also produce portfolios on the inefficient lower branch.
This chapter develops the mathematical machinery of mean-variance optimization from first principles. We'll see how diversification arises naturally from the mathematics of combining assets with less-than-perfect correlation, and we'll implement practical algorithms for finding optimal portfolios. The framework we develop here supplies the covariance and mean-variance tools used in CAPM and factor models. The next two chapters in Part IX cover CAPM and multi-factor models, respectively.
Portfolio Returns and Risk
Before we can optimize portfolios, we need precise definitions of portfolio return and risk. These definitions form the foundation for Modern Portfolio Theory. We need accurate measures of portfolio return and risk to compare investment strategies and identify optimal allocations.
Suppose we allocate wealth across assets. The fundamental question is: how should we combine individual asset characteristics to describe the portfolio as a whole? Let denote the fraction of wealth invested in asset , where the weights must sum to one:
where:
- : fraction of wealth invested in asset
- : total number of assets in the portfolio
This constraint ensures we account for all invested capital. A weight of 0.3 means 30% of the portfolio is allocated to that asset. We allow short selling, meaning weights can be negative (representing borrowed positions). Shorting an asset involves selling borrowed shares with the obligation to return them later, creating a negative position that profits when the price falls.
Expected Portfolio Return
The expected return of a portfolio follows naturally from the linearity of expectation, a concept we explored in our probability foundations. If each asset has expected return , the portfolio's expected return is simply the weighted average:
where:
- : expected return of the portfolio
- : expected value of portfolio return
- : weight of asset
- : expected return of asset
- : vector of portfolio weights,
- : vector of expected returns,
Portfolio expected return is linear in the weights. Doubling an asset's weight doubles that asset's contribution ; the change in total portfolio return also depends on which holdings fund the increase. Vector notation provides a compact way to express this relationship for many assets.
Portfolio Variance
The portfolio variance requires more care and reveals how diversification works mathematically. Unlike expected return, variance does not simply average across assets. The portfolio return is:
where:
- : return of the portfolio
- : weight of asset
- : return of asset
- : number of assets
To find the variance of this weighted sum, we must account for how each pair of assets moves together. Taking the variance and using bilinearity of covariance:
where:
- : variance of the portfolio
- : weights allocated to assets and
- : covariance between returns of assets and
The double summation captures all pairwise interactions between assets. When , the covariance term becomes the variance of asset , contributing to portfolio variance. When , the covariance measures how assets and move together, and this is where diversification benefits emerge.
Let denote the covariance matrix, where . Then the portfolio variance is:
where:
- : variance of the portfolio
- : vector of portfolio weights
- : covariance matrix of asset returns
This compact matrix notation, which we developed in Part I's linear algebra chapter, makes optimization tractable. The quadratic form appears throughout financial mathematics because it elegantly encapsulates how portfolio risk depends on individual asset variances (the diagonal entries ) and pairwise covariances (the off-diagonal entries for ).
The covariance matrix is symmetric () and positive semi-definite, meaning for all weight vectors . This ensures portfolio variance is always non-negative. When is positive definite, the portfolio variance is strictly positive for any non-trivial portfolio.
The Two-Asset Case
The two-asset case provides intuition before we tackle the general problem. By working through this simplified scenario, we can develop a geometric understanding of how risk and return interact, seeing clearly how correlation shapes the set of achievable portfolios. Consider a portfolio with weights in asset 1 and in asset 2.
The expected return is:
where:
- : expected return of the portfolio
- : weight invested in asset 1
- : expected returns of assets 1 and 2
This formula shows that expected return varies linearly with the weight . As we shift allocation from asset 2 toward asset 1, expected return moves along a straight line between (when ) and (when ). There are no surprises here: return blending is straightforward.
The variance, however, tells a richer story:
where:
- : variance of the portfolio
- : weight invested in asset 1
- : standard deviations (volatility) of assets 1 and 2
- : correlation coefficient between returns of assets 1 and 2
The first two terms are the weighted individual-variance contributions. Relative to the same weights at zero correlation, the cross-term raises or lowers portfolio variance according to its sign. Whether a combination has less risk than either asset alone depends on all three terms, the weights, and the covariance.
The Diversification Effect
For fixed long-only weights , lowering correlation cannot increase portfolio variance; it decreases when both weights and volatilities are positive: . When correlation is negative, the cross-term is nonpositive. If one weight is negative, the derivative instead becomes negative, so the long-only correlation intuition reverses.
To understand this intuitively, imagine two assets that tend to move in opposite directions. When one zigs, the other zags. With positive holdings in both, these opposite movements partially cancel each other, smoothing the overall portfolio return and reducing its volatility. The lower the correlation, the more pronounced this cancellation effect.
Let's see this numerically:
PythonIn [2]Expand
import numpy as np
# Two assets with different risk-return profiles
mu1, sigma1 = 0.10, 0.15 # Asset 1: 10% return, 15% volatility
mu2, sigma2 = 0.06, 0.08 # Asset 2: 6% return, 8% volatility
# Portfolio weights for asset 1
weights = np.linspace(-0.5, 1.5, 100)
def portfolio_stats(w, mu1, mu2, sigma1, sigma2, rho):
"""Calculate portfolio return and standard deviation."""
mu_p = w * mu1 + (1 - w) * mu2
var_p = (
w**2 * sigma1**2
+ (1 - w) ** 2 * sigma2**2
+ 2 * w * (1 - w) * rho * sigma1 * sigma2
)
return mu_p, np.sqrt(var_p)VisualizationOut [3]Expand
Matplotlib is building the font cache; this may take a moment.

The figure reveals several key insights about how correlation shapes investment opportunities:
- Perfect positive correlation (): For long-only weights, portfolio standard deviation is the weighted average of individual volatilities and cannot fall below the less volatile asset. With unrestricted weights, it is ; unequal volatilities permit a zero-variance long-short hedge.
- Imperfect correlation (): The displayed frontier can curve leftward. Whether a long-only portfolio has lower risk than either asset alone depends on the weights and covariance, not on alone. At fixed long-only weights, lowering correlation cannot increase variance.
- Perfect negative correlation (): A risk-free portfolio exists. When , the portfolio variance is exactly zero. This extreme case illustrates the theoretical maximum of diversification: if two assets move in perfectly opposite directions, we can combine them to eliminate all uncertainty.
Minimum Variance Portfolio
For the two-asset case, we can find the minimum variance portfolio analytically. This portfolio represents the allocation that achieves the lowest possible risk, regardless of the expected return it delivers. To find the minimum variance portfolio, we differentiate the variance with respect to and set the result to zero:
The derivative includes three components corresponding to the three terms in the variance formula. Setting this derivative to zero identifies a stationary point with no first-order change in variance. For , the variance has positive second derivative , so the point is its unique unrestricted minimum; any nonzero finite change in raises variance by . Rearranging the first-order condition to isolate :
For , the unrestricted minimizer is:
where:
- : optimal weight for asset 1 to minimize portfolio variance
- : volatilities of assets 1 and 2
- : correlation between assets 1 and 2
This unrestricted-weight formula requires . If , the centered asset returns coincide and every budget allocation has the same variance, so there is no unique variance-minimizing weight. For long-only weights and , clip the unrestricted minimizer to . When correlation is zero, the formula allocates more to the lower-volatility asset.
PythonIn [4]Expand
def minimum_variance_weight(sigma1, sigma2, rho):
"""Unrestricted two-asset minimizer; requires a positive variance denominator.
With identical centered returns, every budget allocation has the same
variance and this function raises rather than reporting a unique optimum.
A positive common volatility scale cancels from the weight ratio.
For a long-only portfolio, clip a nondegenerate result to [0, 1] separately.
"""
if not np.all(np.isfinite([sigma1, sigma2, rho])):
raise ValueError("Volatilities and correlation must be finite")
if sigma1 < 0 or sigma2 < 0 or not -1 <= rho <= 1:
raise ValueError("Require nonnegative volatilities and correlation in [-1, 1]")
if sigma1 == sigma2 and (sigma1 == 0 or rho == 1):
raise ValueError("No unique minimum-variance weight: centered returns coincide")
scale = max(sigma1, sigma2)
s1, s2 = sigma1 / scale, sigma2 / scale
numerator = s2 * (s2 - rho * s1)
# D / scale², formed only after scaling to avoid overflow and underflow.
denominator = (s1 - s2) ** 2 + 2 * (1 - rho) * s1 * s2
if denominator == 0:
raise ValueError(
"Positive variance denominator is unresolved at working precision"
)
return numerator / denominator
# Example with zero correlation
rho = 0.0
w_mv = minimum_variance_weight(sigma1, sigma2, rho)
mu_mv, sigma_mv = portfolio_stats(w_mv, mu1, mu2, sigma1, sigma2, rho)ConsoleOut [5]Expand
Minimum Variance Portfolio (ρ = 0.0): Weight in Asset 1: 0.2215 Weight in Asset 2: 0.7785 Expected Return: 0.0689 (6.89%) Standard Deviation: 0.0706 (7.06%) Compare to individual assets: Asset 1: σ = 0.1500, Asset 2: σ = 0.0800
For the zero-correlation inputs used here, the minimum-variance portfolio has lower risk than either asset alone. Imperfect correlation by itself does not guarantee a strict reduction below both individual risks; the outcome depends on covariance, volatilities, and permitted weights.
VisualizationOut [6]Expand

Multi-Asset Portfolio Optimization
With more than two assets, we need the full matrix formulation. The principles remain the same, but the algebra becomes more complex, requiring linear algebra tools to express and solve the optimization problem efficiently. Let be the vector of weights, the vector of expected returns, and the covariance matrix.
The optimization problem has two related formulations, each emphasizing a different perspective on the objective:
Formulation 1: Minimize risk for a target return
This formulation starts with a desired level of expected return and asks: what is the least risky way to achieve it?
where:
- : vector of portfolio weights
- : covariance matrix of asset returns
- : vector of expected returns
- : required level of expected return
- : vector of ones (sum constraint)
The factor of in the objective is a mathematical convenience that simplifies the derivatives. It does not change the optimal solution because multiplying an objective by a positive constant does not alter which portfolio minimizes it.
Formulation 2: Maximize utility (mean-variance utility)
This formulation takes a different approach, directly modeling investor preferences through a utility function that trades off return against risk:
where:
- : risk aversion parameter ()
- : vector of portfolio weights
- : vector of expected returns
- : covariance matrix of asset returns
- : vector of ones
The parameter captures how much you dislike risk. If you are highly risk-averse (large ), you penalize variance heavily and will choose a conservative, low-volatility portfolio. If you are risk-tolerant (small ), you care more about expected return and will accept higher volatility to chase higher gains. The target-return formulation traces the minimum-variance frontier, including its inefficient lower branch. For targets above the GMV return, its solutions also maximize mean-variance utility for a corresponding . Varying positive traces the efficient upper branch and approaches the GMV portfolio as tends to infinity.
Analytical Solution: Lagrangian Approach
For the target-return closed forms below, assume that is positive definite and that is not proportional to . These conditions make invertible and . If the covariance matrix is singular, the inverse formulas do not apply as written; the constrained system must be treated directly.
For the first formulation, we form the Lagrangian using the techniques from Part I. Its equality multipliers are free variables, not fixed penalties. With differentiable functions and linearly independent constraint gradients, a constrained optimum satisfies stationarity together with the original constraints. We solve that system rather than minimize jointly over weights and multipliers:
where:
- : the Lagrangian function
- : vector of portfolio weights
- : covariance matrix of asset returns
- : Lagrange multipliers enforcing the return and budget constraints
- : vector of expected returns
- : target portfolio return
- : vector of ones
When the optimal value varies differentiably with a constraint's right-hand side, the multiplier gives its signed local rate of change. With the sign convention used here, for the optimal half-variance . A finite change is generally only approximated by this rate times the change. Taking the derivative with respect to and setting it to zero:
where:
- : covariance matrix
- : vector of portfolio weights
- : Lagrange multipliers
- : vector of expected returns
- : vector of ones
This first-order condition states that at the optimum, the marginal increase in risk from adjusting any weight must be exactly offset by the value of that weight in meeting the return and budget constraints. Rearranging terms to solve for explicitly:
where:
- : optimal portfolio weight vector
- : inverse of the covariance matrix
- : vector of expected returns
- : vector of ones
- : Lagrange multipliers
The vector is a covariance-adjusted return direction, while is proportional to the GMV portfolio. Their coefficients and normalization matter: both terms jointly satisfy the target-return and budget constraints, and can be negative on the lower branch.
The Lagrange multipliers and are determined by the constraints. Substituting back:
where:
- : target return
- : Lagrange multipliers
- : vector of expected returns
- : inverse covariance matrix
- : vector of ones
These two equations form a linear system in the two unknowns and . We can solve for and in terms of scalar constants derived from the problem parameters:
where:
- : target portfolio return
The constants , , and depend only on the assets' expected returns and covariance structure, not on the target return. This means we can precompute them once and then quickly find optimal portfolios for any target return, making efficient frontier construction computationally efficient.
The Global Minimum Variance Portfolio
With positive-definite covariance, setting (ignoring the return target) gives the global minimum variance (GMV) portfolio. This formula does not require distinct expected returns:
where:
- : weights of the global minimum variance portfolio
- : inverse of the covariance matrix
- : vector of ones
This formula represents risk minimization in its most basic form. The term identifies the portfolio structure that minimizes variance without regard for expected returns, while the denominator normalizes the weights to sum to one. Each weight depends on the full covariance matrix through ; lower standalone variance or lower correlations do not by themselves guarantee a higher weight. With short selling allowed, some GMV weights can be negative.
This portfolio minimizes variance within the stated asset universe subject to the budget constraint. If variance alone is the objective, it is optimal regardless of expected return. A short investment horizon does not by itself imply that objective; cash needs, liabilities, and available risk-free investments can also matter.
PythonIn [7]Expand
import numpy as np
def global_minimum_variance_portfolio(cov_matrix):
"""
Compute the global minimum variance portfolio weights.
Parameters
----------
cov_matrix : ndarray
n x n positive-definite covariance matrix of asset returns
Returns
-------
weights : ndarray
One-dimensional array of optimal weights with shape (n,)
"""
n = cov_matrix.shape[0]
ones = np.ones(n)
# Compute inverse covariance matrix
cov_inv = np.linalg.inv(cov_matrix)
# GMV weights: Σ⁻¹1 / (1'Σ⁻¹1)
weights = cov_inv @ ones / (ones @ cov_inv @ ones)
return weightsThe Efficient Frontier
The efficient frontier is the undominated part of the upper-left boundary of the feasible risk-return set. Upper-boundary points on a flat-return segment can still be dominated by lower-risk portfolios with the same return.
To understand the efficient frontier, imagine plotting every possible portfolio in risk-return space. The feasible set is defined by the budget constraint and any position limits. With unrestricted short selling, the budget equality alone does not bound leverage. Some portfolios lie on the boundary of this region, while others lie in the interior. Interior portfolios are dominated by boundary portfolios that offer either higher return for the same risk or lower risk for the same return. The efficient frontier consists of all portfolios that are not dominated by any other portfolio.
A portfolio is efficient if no other feasible portfolio offers at least as high an expected return and no more risk, with at least one strict improvement. The collection of all efficient portfolios forms the efficient frontier.
Two-Fund Separation Theorem
A property of the unrestricted-weight model with positive-definite covariance and expected returns not proportional to is the two-fund separation theorem: any efficient portfolio can be expressed as a linear combination of two distinct efficient portfolios. The combination must remain on the efficient upper branch; arbitrary proportions need not be efficient. Position limits can invalidate this two-fund representation.
Within those assumptions, two efficiently managed funds span the minimum-variance curve. An investor can select an efficient combination according to risk tolerance, subject to the borrowing or short-selling needed to implement those proportions.
Mathematically, if and are two distinct efficient portfolios in this model, then any scalar gives another portfolio on the minimum-variance curve:
where:
- : the new minimum-variance portfolio
- : weighting scalar
- : two distinct efficient portfolios
The parameter determines the combined expected return. When , we hold only portfolio 1; when , we hold only portfolio 2. The combination is efficient only when its combined expected return is at least the GMV return. Extrapolating outside requires short selling one fund and can reach either the efficient upper branch or the inefficient lower branch.
Computing the Frontier
Let's compute the minimum-variance curve at exact target returns. Unlike the unrestricted analytical model, allow_short=True below still bounds every asset weight between -1 and 2; False uses [0,1]. The curve includes its inefficient lower branch, and binding bounds can make weight paths piecewise smooth.
PythonIn [8]Expand
import numpy as np
from scipy.optimize import minimize
def efficient_frontier(mu, cov_matrix, n_points=50, allow_short=True):
"""Compute a bounded minimum-variance curve, including its lower branch.
Requires finite 1-D expected returns and a compatible symmetric PSD
covariance matrix. Each weight is bounded to [-1, 2] when allow_short is
True, or [0, 1] otherwise; shorting is not unrestricted here.
n_points is an integer >= 2 specifying the requested exact-return targets.
If every expected return is exactly equal, the return constraint is
redundant: return one budget-constrained bounded GMV point instead.
Returns arrays with shapes (m,), (m,), and (m, n_assets), where m is
n_points in the nondegenerate case and 1 in the equal-mean case.
A failed endpoint or target solve raises RuntimeError instead of silently
omitting portfolios. Feasibility is checked to absolute tolerance 1e-7;
The variance objective is divided by a positive covariance scale for the
solver; reported risk still uses the original unscaled covariance. This
preserves the mathematical minimizer across covariance magnitudes.
Numerical solver success is not an optimality certificate.
"""
mu = np.asarray(mu, dtype=float)
cov_matrix = np.asarray(cov_matrix, dtype=float)
if mu.ndim != 1 or mu.size == 0 or not np.all(np.isfinite(mu)):
raise ValueError("mu must be a nonempty finite one-dimensional array")
n_assets = mu.size
if cov_matrix.shape != (n_assets, n_assets) or not np.all(np.isfinite(cov_matrix)):
raise ValueError("cov_matrix must be finite with shape (n_assets, n_assets)")
if not np.allclose(cov_matrix, cov_matrix.T, rtol=1e-10, atol=1e-12):
raise ValueError("cov_matrix must be symmetric")
if np.linalg.eigvalsh(cov_matrix).min() < -1e-12:
raise ValueError("cov_matrix must be positive semidefinite")
if (
isinstance(n_points, (bool, np.bool_))
or not isinstance(n_points, (int, np.integer))
or n_points < 2
):
raise ValueError("n_points must be an integer >= 2")
lower, upper = (-1.0, 2.0) if allow_short else (0.0, 1.0)
bounds = [(lower, upper)] * n_assets
x0 = np.full(n_assets, 1.0 / n_assets)
constraints = [{"type": "eq", "fun": lambda w: np.sum(w) - 1.0}]
feasibility_tolerance = 1e-7
options = {"maxiter": 500, "ftol": 1e-10}
# Scale the entire variance objective by a positive constant.
# This changes numerical conditioning, not the chosen covariance model.
objective_scale = float(np.max(np.abs(cov_matrix)))
if objective_scale == 0:
objective_scale = 1.0
optimization_covariance = cov_matrix / objective_scale
def scaled_portfolio_variance(w):
return float(w @ optimization_covariance @ w)
def portfolio_variance(w):
# Returned variances/risk retain the original units and covariance.
return float(w @ cov_matrix @ w)
def checked_weights(result, label, target=None):
if not result.success:
raise RuntimeError(f"{label} optimization failed: {result.message}")
weights = np.asarray(result.x, dtype=float)
if (
weights.shape != (n_assets,)
or not np.all(np.isfinite(weights))
or abs(weights.sum() - 1.0) > feasibility_tolerance
or np.any(weights < lower - feasibility_tolerance)
or np.any(weights > upper + feasibility_tolerance)
or (
target is not None
and abs(float(weights @ mu) - target) > feasibility_tolerance
)
):
raise RuntimeError(
f"{label} optimization returned invalid or infeasible weights"
)
return weights
def result_arrays(weights):
weights = np.stack(weights)
returns = weights @ mu
variances = np.array([portfolio_variance(w) for w in weights])
if not np.all(np.isfinite(variances)) or np.any(variances < -1e-12):
raise RuntimeError("Frontier optimization returned invalid variances")
return returns, np.sqrt(np.maximum(variances, 0.0)), weights
if np.all(mu == mu[0]):
result = minimize(
scaled_portfolio_variance,
x0,
method="SLSQP",
bounds=bounds,
constraints=constraints,
options=options,
)
return result_arrays([checked_weights(result, "Equal-mean GMV")])
min_result = minimize(
lambda w: float(w @ mu),
x0,
method="SLSQP",
bounds=bounds,
constraints=constraints,
options=options,
)
max_result = minimize(
lambda w: -float(w @ mu),
x0,
method="SLSQP",
bounds=bounds,
constraints=constraints,
options=options,
)
min_weights = checked_weights(min_result, "Minimum-return endpoint")
max_weights = checked_weights(max_result, "Maximum-return endpoint")
min_ret, max_ret = float(min_weights @ mu), float(max_weights @ mu)
if min_ret > max_ret:
raise RuntimeError("Frontier endpoint returns are out of order")
target_returns = np.linspace(min_ret, max_ret, n_points)
frontier_weights = []
for index, target in enumerate(target_returns):
# A convex combination of endpoints is a feasible starting allocation.
fraction = index / (n_points - 1)
start = (1 - fraction) * min_weights + fraction * max_weights
target_constraints = constraints + [
{"type": "eq", "fun": lambda w, t=target: float(w @ mu) - t}
]
result = minimize(
scaled_portfolio_variance,
start,
method="SLSQP",
bounds=bounds,
constraints=target_constraints,
options=options,
)
frontier_weights.append(checked_weights(result, f"Target {index + 1}", target))
return result_arrays(frontier_weights)Worked Example: Three-Asset Portfolio
Let's construct an efficient frontier for a portfolio of stocks, bonds, and commodities. This example illustrates how different asset classes with distinct risk-return profiles and correlations combine to form a diversified investment opportunity set:
PythonIn [9]Expand
import numpy as np
# Asset parameters (annualized)
# Illustrative annual parameters for stocks, bonds, and commodities
asset_names = ["Stocks", "Bonds", "Commodities"]
# Expected returns
mu = np.array([0.08, 0.03, 0.05]) # 8%, 3%, 5%
# Volatilities
sigma = np.array([0.18, 0.05, 0.20]) # 18%, 5%, 20%
# Correlation matrix
corr = np.array(
[
[1.0, 0.1, 0.3], # Stocks
[0.1, 1.0, 0.0], # Bonds
[0.3, 0.0, 1.0], # Commodities
]
)
# Convert to covariance matrix: Σ = diag(σ) × Corr × diag(σ)
cov_matrix = np.diag(sigma) @ corr @ np.diag(sigma)ConsoleOut [10]Expand
Covariance Matrix: [[0.0324 0.0009 0.0108] [0.0009 0.0025 0. ] [0.0108 0. 0.04 ]] Asset Statistics: Stocks: μ = 8.0%, σ = 18.0% Bonds: μ = 3.0%, σ = 5.0% Commodities: μ = 5.0%, σ = 20.0%
The covariance matrix confirms the relationships between our assets: stocks and commodities have higher volatility and a moderate positive correlation (0.3), while bonds give stability with low volatility and low correlation to the other assets. The near-zero correlations between bonds and the other asset classes suggest that bonds will play a central role in diversifying portfolio risk.
VisualizationOut [11]Expand

Now let's compute and visualize the efficient frontier:
PythonIn [12]Expand
# Compute the minimum-variance frontier, including its inefficient lower branch
frontier_returns, frontier_risks, frontier_weights = efficient_frontier(
mu, cov_matrix, n_points=100, allow_short=True
)
# Find global minimum variance portfolio
gmv_weights = global_minimum_variance_portfolio(cov_matrix)
gmv_return = gmv_weights @ mu
gmv_risk = np.sqrt(gmv_weights @ cov_matrix @ gmv_weights)VisualizationOut [13]Expand

The minimum-variance curve gives the lowest risk at each exact target return. Only the branch at or above the GMV return is efficient. The individual assets (red dots) lie inside the frontier, confirming that portfolios combining imperfectly correlated assets offer superior efficiency compared to holding single securities. Notice how the frontier bows outward to the left, showing that diversification creates portfolios with better risk-return characteristics than any individual asset.
ConsoleOut [14]Expand
Global Minimum Variance Portfolio: Stocks: 2.95% Bonds: 92.03% Commodities: 5.02% Expected Return: 3.25% Standard Deviation: 4.82%
The GMV portfolio is heavily weighted toward bonds because of their low volatility. Notice that the GMV portfolio achieves lower risk than any individual asset, showing the power of diversification. Even the lowest-risk asset (bonds at 5% volatility) has higher risk than the optimally diversified GMV portfolio.
Portfolio Composition Along the Frontier
Understanding how portfolio weights change along the efficient frontier provides insight into the risk-return tradeoff. As you move up the frontier seeking higher returns, you must accept more risk, and the portfolio composition shifts accordingly:
VisualizationOut [15]Expand

The plotted curve includes targets below the GMV return, on the inefficient lower branch. Near the GMV portfolio, bonds dominate the allocation. Above the GMV return, seeking higher returns along the efficient branch increases risk and shifts weight toward stocks. The bounded model produces piecewise smooth weight paths; kinks occur when a position limit binds.
Quadratic Programming for Portfolio Optimization
For practical applications with many assets and constraints, we use quadratic programming (QP). Quadratic programming is a mathematical optimization technique designed specifically for problems where the objective function is quadratic (involving squared terms) and the constraints are linear. Mean-variance optimization fits this structure perfectly because portfolio variance is a quadratic function of the weights.
The standard QP form is:
where:
- : vector of decision variables
- : symmetric matrix (quadratic term)
- : vector (linear term)
- : matrix and vector defining equality constraints
- : matrix and vector defining inequality constraints
The minimum-variance formulation uses and . For the defined utility , minimizing uses and . Equivalently, dividing the objective by positive gives and . The analytical exact-target formulation enforces both budget and target return as equalities; the utility formulation does not require a target-return equality. Inequalities can encode position limits and other practical requirements.
Adding Practical Constraints
Real portfolio optimization often includes additional constraints that reflect regulatory requirements, risk management policies, or investment mandates:
- No short selling: for all . This constraint prevents the portfolio from taking negative positions, which an investor cannot or chooses not to take.
- Maximum position size: (e.g., 10% per asset). This limits single-security concentration and can encourage diversification, but does not guarantee diversified sector or factor exposure.
- Sector constraints: Total exposure to a sector some limit. This prevents the portfolio from becoming overly exposed to a single industry or market segment.
- Turnover constraints: Changes from current portfolio are limited. This controls transaction costs and tax consequences by restricting how much trading occurs.
Let's implement a more realistic optimizer using SciPy's SLSQP method. Here target_return is a minimum return floor, imposed as an inequality, rather than the exact-target equality used in the analytical derivation. If the solver reports failure, the function raises an error instead of presenting a fallback allocation as optimal:
This implementation uses the ridge-regularized covariance , so its numerical candidate targets that model, not exactly the supplied unregularized covariance problem. The fixed ridge can matter substantially when covariance entries are small. For tiny objectives, we divide the entire chosen objective by a positive scale to avoid premature termination under an absolute stopping tolerance; this does not change its mathematical minimizer. A successful solver flag is not a proof of global optimality. After thresholding and normalization, we check finite weights, budget, bounds and the return floor to an absolute feasibility tolerance of ; this is a feasibility check, not an optimality certificate.
PythonIn [16]Expand
import numpy as np
from scipy.optimize import minimize
def mean_variance_optimize(
mu,
cov_matrix,
target_return=None,
risk_aversion=None,
max_weight=None,
min_weight=0.0,
allow_short=False,
):
"""
Find an approximate feasible candidate for a ridge-regularized model.
Requires finite, compatible inputs and a positive-semidefinite covariance;
the utility branch assumes positive risk_aversion. Uses Sigma + 1e-8 I.
Tiny entire objectives are positively rescaled; success is not an
optimality proof. Returned feasibility uses an absolute 1e-7 tolerance.
Parameters
----------
mu : ndarray
Expected returns
cov_matrix : ndarray
Covariance matrix
target_return : float, optional
Minimum portfolio return (a floor for min variance formulation)
risk_aversion : float, optional
Risk aversion parameter (for max utility formulation)
max_weight : float, optional
Maximum weight per asset
min_weight : float, optional
Minimum weight per asset when allow_short=False (default 0).
Ignored when allow_short=True, which sets the lower bound to -1.
allow_short : bool
Use lower bound -1 rather than min_weight; the default upper bound is 1.
Returns
-------
weights : ndarray
Approximate feasible weights for the ridge-regularized model
Raises
------
RuntimeError
If the optimizer reports failure or returned weights fail validation.
"""
# Convert to numpy arrays and ensure proper shape
mu_arr = np.asarray(mu).flatten()
cov_arr = np.asarray(cov_matrix)
n = len(mu_arr)
# Fixed ridge changes the supplied covariance model; see the stated scope.
cov_arr = cov_arr + np.eye(len(mu_arr)) * 1e-8
if target_return is not None:
objective = lambda w: float(w @ cov_arr @ w)
elif risk_aversion is not None:
objective = lambda w: (
-float(mu_arr @ w - (risk_aversion / 2) * (w @ cov_arr @ w))
)
else:
objective = lambda w: float(w @ cov_arr @ w)
objective_scale = float(np.max(np.abs(cov_arr)))
if target_return is None and risk_aversion is not None:
objective_scale = max(
float(np.max(np.abs(mu_arr))), abs(risk_aversion) * objective_scale
)
if 0 < objective_scale < 1e-6:
unscaled_objective = objective
objective = lambda w: unscaled_objective(w) / objective_scale
constraints = [{"type": "eq", "fun": lambda w: np.sum(w) - 1.0}]
if target_return is not None:
constraints.append(
{"type": "ineq", "fun": lambda w: float(mu_arr @ w - target_return)}
)
if allow_short:
lower = -1.0
else:
lower = min_weight
upper = 1.0 if max_weight is None else max_weight
bounds = [(lower, upper) for _ in range(n)]
x0 = np.full(n, 1.0 / n)
if not allow_short:
x0 = np.clip(x0, lower, upper)
x0 = x0 / x0.sum()
result = minimize(
objective,
x0,
method="SLSQP",
bounds=bounds,
constraints=constraints,
options={"maxiter": 500, "ftol": 1e-10},
)
if not result.success:
raise RuntimeError(f"Mean-variance optimization failed: {result.message}")
weights = result.x
weights = np.asarray(weights, dtype=float)
if not np.all(np.isfinite(weights)):
raise RuntimeError("Mean-variance optimization returned invalid weights")
weights[np.abs(weights) < 1e-10] = 0.0
total = weights.sum()
if not np.isfinite(total) or total == 0:
raise RuntimeError("Mean-variance optimization returned invalid weights")
weights = weights / total
feasibility_tolerance = 1e-7
if (
abs(weights.sum() - 1.0) > feasibility_tolerance
or np.any(weights < lower - feasibility_tolerance)
or np.any(weights > upper + feasibility_tolerance)
or (
target_return is not None
and mu_arr @ weights < target_return - feasibility_tolerance
)
):
raise RuntimeError("Mean-variance optimization returned infeasible weights")
return weightsPythonIn [17]Expand
# Unconstrained GMV (allows short selling)
w_unconstrained = global_minimum_variance_portfolio(cov_matrix)
# Constrained: no short selling
w_long_only = mean_variance_optimize(mu, cov_matrix, allow_short=False)
# Constrained: max 40% per asset, no short selling
max_pos = 0.4
w_max_40 = mean_variance_optimize(mu, cov_matrix, allow_short=False, max_weight=max_pos)
# Calculate performance metrics
ret_unconstrained = w_unconstrained @ mu
risk_unconstrained = np.sqrt(w_unconstrained @ cov_matrix @ w_unconstrained)
ret_long_only = w_long_only @ mu
risk_long_only = np.sqrt(w_long_only @ cov_matrix @ w_long_only)
ret_max_40 = w_max_40 @ mu
risk_max_40 = np.sqrt(w_max_40 @ cov_matrix @ w_max_40)ConsoleOut [18]Expand
Portfolio Weights Comparison: ------------------------------------------------------------ Asset Unconstrained Long-Only Max 40% ------------------------------------------------------------ Stocks 2.95% 2.95% 33.78% Bonds 92.03% 92.03% 40.00% Commodities 5.02% 5.02% 26.22% ------------------------------------------------------------ Unconstrained: Return: 3.25%, Risk: 4.82% Long-Only: Return: 3.25%, Risk: 4.82% Max 40%: Return: 5.21%, Risk: 9.49%
VisualizationOut [19]Expand

In the original covariance model, the unrestricted GMV optimum is already long-only, so that constraint is nonbinding. The 40% cap raises volatility substantially, from 4.82% to 9.49%. Adding constraints cannot improve the minimum objective when the objective itself is unchanged. Here the numerical constrained helper also adds a small ridge, so exact equality with the unrestricted calculation is only expected to displayed precision.
The Mathematics of Diversification
Diversification is often called the "free lunch" of finance because combining different exposures can improve risk-return tradeoffs. Common market or economic factors affect many assets with different exposure magnitudes and signs. Specific risk can diminish when holdings become sufficiently granular and residual dependence is weak; simply increasing the count of holdings is not enough.
Variance Decomposition
Consider an equally-weighted portfolio of assets with identical variance and pairwise correlation . While this is a simplification, it reveals the essential mechanics of diversification. The portfolio variance is:
where:
- : variance of the portfolio
- : number of assets
- : covariance between asset and asset
For the diagonal terms ():
For the off-diagonal terms ():
There are diagonal terms and off-diagonal terms. The diagonal terms represent each asset's own variance contribution, while the off-diagonal terms capture the covariance contributions from all pairs of different assets:
where:
- : variance of each individual asset
- : pairwise correlation between all assets
- : number of assets in the portfolio
The displayed terms are diagonal variance and off-diagonal covariance contributions. For a common-factor interpretation, assume and write centered returns as , where and the residuals have unit variance and are mutually uncorrelated. Then : common-factor variance is constant, while specific variance declines with . The finite equicorrelation matrix permits for , but a fixed negative correlation is not valid as tends to infinity.
For a family of homogeneous portfolios with fixed , as :
where:
- : limiting portfolio variance
- : pairwise correlation between assets
- : variance of individual assets
For fixed , the specific component tends to zero, leaving . The limit is positive when and ; it is zero when . Common-factor risk remains here because every holding retains the same factor exposure. In a market crash, many stocks can fall together, although individual stocks can move differently.
PythonIn [20]Expand
import numpy as np
def calculate_portfolio_variance(n, sigma, rho):
"""Calculate portfolio variance for an equally weighted portfolio."""
return (sigma**2 / n) + ((n - 1) / n) * rho * sigma**2
# Define parameters for visualization
sigma = 0.25
rho_values = [0.0, 0.2, 0.4, 0.6]
n_assets = np.arange(1, 101)VisualizationOut [21]Expand

VisualizationOut [22]Expand

Systematic risk comes from common market or economic factors and remains when the portfolio retains exposure to them. Specific risk can approach zero in increasingly granular portfolios with sufficiently weak residual dependence. The equal-loading model above makes these conditions explicit; the CAPM chapter develops the market-factor interpretation further.
How Many Assets Are Enough?
In this equal-weight, equal-variance model, 20 and 30 assets eliminate 95% and 96.7% of diversifiable variance. That normalized fraction is , independent of correlation when ; correlation still determines the remaining total variance. Real diversification needs depend on concentration, exposures, dependence, and the chosen risk criterion.
PythonIn [23]Expand
# Calculate percentage of diversification benefit captured
sigma = 0.25
rho = 0.3 # Moderate correlation
def pct_diversification(n, sigma, rho):
"""Percentage of diversifiable variance eliminated; n>=1, sigma>0, 0<=rho<1."""
if not np.all(np.isfinite([n, sigma, rho])):
raise ValueError("Portfolio size, volatility and correlation must be finite")
if n < 1 or sigma <= 0 or not 0 <= rho < 1:
raise ValueError("Require n>=1, sigma>0 and correlation in [0, 1)")
# Cancel the positive common factor (1-rho)*sigma² before numerical evaluation.
return 100.0 * (1.0 - 1.0 / n)
# Calculate benefits for various portfolio sizes
asset_counts = [5, 10, 20, 30, 50, 100]
diversification_benefits = {n: pct_diversification(n, sigma, rho) for n in asset_counts}ConsoleOut [24]Expand
Diversification Benefit Captured (ρ = 0.3):
----------------------------------------
5 assets: 80.00% of diversifiable variance eliminated
10 assets: 90.00% of diversifiable variance eliminated
20 assets: 95.00% of diversifiable variance eliminated
30 assets: 96.67% of diversifiable variance eliminated
50 assets: 98.00% of diversifiable variance eliminated
100 assets: 99.00% of diversifiable variance eliminatedVisualizationOut [25]Expand

The table illustrates diminishing marginal variance reduction in this model. Whether more holdings are worthwhile in practice depends on their distinct exposures, implementation costs, and investor objectives; the model does not calculate those costs or establish a universal sufficient portfolio size.
Practical Implementation with Simulated Return Data
Let's apply mean-variance optimization to a seeded five-asset Gaussian return sample with a specified covariance structure. The 1,260 observations represent five simulated trading years, not historical market observations.
PythonIn [26]Expand
import numpy as np
# Simulated daily returns for 5 assets (252 trading days x 5 simulated years)
np.random.seed(42)
n_days = 252 * 5
n_assets = 5
asset_names = ["Large Cap", "Small Cap", "Intl Stocks", "Bonds", "REITs"]
# True parameters for simulation
true_annual_returns = np.array([0.10, 0.12, 0.08, 0.04, 0.09])
true_annual_vols = np.array([0.16, 0.22, 0.18, 0.04, 0.20])
# Correlation structure
true_corr = np.array(
[
[1.00, 0.75, 0.65, 0.10, 0.55],
[0.75, 1.00, 0.60, 0.05, 0.50],
[0.65, 0.60, 1.00, 0.15, 0.45],
[0.10, 0.05, 0.15, 1.00, 0.20],
[0.55, 0.50, 0.45, 0.20, 1.00],
]
)
# Convert to daily parameters
daily_returns_mean = true_annual_returns / 252
daily_vols = true_annual_vols / np.sqrt(252)
true_cov_daily = np.diag(daily_vols) @ true_corr @ np.diag(daily_vols)
# Generate returns using Cholesky decomposition
L = np.linalg.cholesky(true_cov_daily)
z = np.random.randn(n_days, n_assets)
returns = daily_returns_mean + z @ L.TPythonIn [27]Expand
# Estimate annualized parameters from simulated daily returns (IID arithmetic scaling)
estimated_mu = returns.mean(axis=0) * 252 # Annualize
estimated_cov = np.cov(returns.T) * 252 # Annualize
# Compute the long-only minimum-variance curve, including its lower branch
frontier_ret, frontier_risk, frontier_w = efficient_frontier(
estimated_mu, estimated_cov, n_points=100, allow_short=False
)
# Select the best sampled Sharpe-ratio portfolio (approximate tangent), rf = 2%
rf = 0.02
sharpe_ratios = (frontier_ret - rf) / frontier_risk
max_sharpe_idx = np.argmax(sharpe_ratios)
tangent_weights = frontier_w[max_sharpe_idx]
tangent_return = frontier_ret[max_sharpe_idx]
tangent_risk = frontier_risk[max_sharpe_idx]
max_sharpe_ratio = sharpe_ratios[max_sharpe_idx]
# Calculate Capital Allocation Line (CAL)
cml_risks = np.linspace(0, tangent_risk * 1.5, 50)
cml_returns = rf + max_sharpe_ratio * cml_risksVisualizationOut [28]Expand

This chart uses simulated returns. The dashed capital-allocation line combines the 2% risk-free asset with the best Sharpe portfolio found on the finite grid. It approximates the tangency opportunity set in the stated mean-variance model; extending beyond the risky fund assumes borrowing at the same risk-free rate, without trading frictions or additional leverage constraints.
ConsoleOut [29]Expand
Approximate Tangent Portfolio (Best Sampled Sharpe Ratio): -------------------------------------------------- Large Cap : 14.49% Small Cap : 0.00% Intl Stocks : 0.00% Bonds : 7.02% REITs : 78.50% -------------------------------------------------- Expected Return: 13.45% Standard Deviation: 16.79% Sharpe Ratio: 0.682
VisualizationOut [30]Expand

The selected portfolio has the highest Sharpe ratio among the sampled targets. In the frictionless mean-variance model with a suitable tangency portfolio and unrestricted borrowing and lending at the same risk-free rate, efficient allocations combine that risky fund with the risk-free asset. Actual funding limits, objectives, and exposures can change this conclusion. The next chapter connects portfolio choice to CAPM equilibrium.
Limitations of Mean-Variance Optimization
While mean-variance optimization provides a rigorous framework for portfolio construction, several practical challenges limit its direct application.
Estimation Error
A major practical limitation is sensitivity to input-estimation errors. Small changes in expected returns, volatilities, or correlations can change allocations substantially. These parameters must be estimated or forecast, often using historical data alongside models and other information.
Mean-variance optimizers are often called "error maximizers" because favorable estimation errors can attract disproportionate weight. Expected-return forecasts are often noisy, and allocations can be more sensitive to mean errors than covariance errors in relevant settings; this is a sensitivity statement, not a comparison of raw errors measured in different units.
PythonIn [31]Expand
import numpy as np
# Demonstrate sensitivity to estimation error
np.random.seed(123)
# Add independent Gaussian mean noise with a one-percentage-point annual-return standard deviation
est_error = 0.01
n_simulations = 50
perturbed_weights = []
for _ in range(n_simulations):
perturbed_mu = estimated_mu + np.random.normal(0, est_error, n_assets)
w = mean_variance_optimize(
perturbed_mu, estimated_cov, target_return=0.08, allow_short=False
)
perturbed_weights.append(w)
perturbed_weights = np.array(perturbed_weights)
# Calculate statistics
mean_w = perturbed_weights.mean(axis=0)
std_w = perturbed_weights.std(axis=0)
min_w = perturbed_weights.min(axis=0)
max_w = perturbed_weights.max(axis=0)VisualizationOut [32]Expand

ConsoleOut [33]Expand
Weight Variability Statistics: ------------------------------------------------------------ Asset Mean Std Dev Min Max ------------------------------------------------------------ Large Cap 8.93% 7.64% 0.00% 28.09% Small Cap 0.06% 0.29% 0.00% 1.99% Intl Stocks 0.08% 0.56% 0.00% 4.02% Bonds 53.57% 7.57% 34.96% 69.15% REITs 37.36% 4.72% 26.69% 46.15%
With one-percentage-point standard deviation in the annual-mean perturbations, some allocations vary by more than 10 percentage points. Naive plug-in allocations can be unreliable under estimation error; constraints and appropriately chosen regularization can improve robustness. This experiment already uses long-only constraints and a small fixed ridge.
Solutions to Estimation Error
Several approaches address the estimation error problem:
- Shrinkage estimators: Combine noisy parameter estimates with structured targets, such as a sample covariance matrix with a constant-correlation target. Shrinking portfolio weights toward a benchmark is a separate procedure.
- Robust optimization: Explicitly account for parameter uncertainty in the optimization
- Resampling methods: Generate multiple parameter sets, optimize each, and average the results
- Constraints: Position limits and other constraints implicitly regularize the optimization
- Black-Litterman model: Combine market equilibrium returns with your views
We'll explore methods for estimation uncertainty and robust portfolio construction in later chapters of Portfolio Theory and Investment Analysis (Part IX).
Assumptions and Limitations
Beyond estimation error, the mean-variance framework rests on several assumptions worth examining:
-
Quadratic utility or normal returns: These are familiar sufficient settings for mean-variance analysis, subject to the relevant preference and domain conditions; they are not necessary in every possible model. The Stylized Facts of Financial Returns chapter in Part V discusses fat tails and skewness that depart from normality.
-
Single-period framework: The basic model ignores multi-period considerations like rebalancing costs, tax consequences, and changing investment horizons.
-
No transaction costs: Real portfolios face trading costs that mean-variance ignores.
-
Unlimited leverage and short selling: Many investors face constraints that prevent them from implementing the theoretically optimal portfolio.
Despite these limitations, mean-variance optimization remains the foundational framework for portfolio construction. Its insights about diversification, the risk-return tradeoff, and the efficient frontier guide both academic research and practical investment management.
Key Parameters
The key parameters for Mean-Variance Optimization are:
- (Expected Returns): The vector of forecast returns. Errors can strongly affect allocations; their impact relative to covariance errors depends on estimation precision, constraints, and conditioning.
- (Covariance Matrix): Captures the risk of individual assets (diagonal elements) and their co-movements (off-diagonal elements). At fixed long-only weights, increasing a pairwise correlation cannot reduce portfolio variance, within the valid covariance domain; short positions can reverse that response.
- (Risk-Free Rate): The return on a risk-free asset, used to calculate Sharpe ratios and the Capital Market Line.
- (Risk Aversion): Determines the trade-off between risk and return in the utility maximization formulation ().
- (Weights): The decision variables representing the fraction of capital allocated to each asset. Constraints typically require and (for long-only portfolios).
Summary
This chapter developed MPT as a mathematical framework for analyzing diversification and risk-return tradeoffs.
The key concepts we covered include:
-
Portfolio return and risk: Portfolio expected return is the weighted average of asset returns (), while portfolio variance depends on the full covariance structure ().
-
The efficient frontier: Feasible portfolios for which no other feasible portfolio offers at least as high an expected return and no more risk, with at least one strict improvement. A mean-variance investor who prefers higher expected return and lower risk should avoid dominated portfolios.
-
Global minimum variance portfolio: The portfolio with the lowest possible variance, given by .
-
Two-fund separation: In the stated unrestricted-weight model, two distinct efficient portfolios span the minimum-variance curve. Their combination is efficient only on the upper branch, at or above the GMV return.
-
Diversification benefits and limits: At fixed long-only weights, lowering correlation cannot increase variance; a combination need not have lower risk than both individual assets. In the identical-variance, common-positive-correlation example, the limiting variance is , rather than zero.
-
Practical optimization: Quadratic programming enables efficient frontier computation with realistic constraints on short selling, position sizes, and sector exposures.
-
Estimation error: Mean-variance optimization is highly sensitive to input estimation errors, particularly in expected returns. This sensitivity motivates the regularization techniques and robust methods covered in later chapters.
The framework developed here sets up the Capital Asset Pricing Model in the next chapter, where we'll see how equilibrium considerations determine the pricing of risk in the market portfolio.
Quiz
Ready to test your understanding? Take this quick quiz to reinforce what you've learned about Modern Portfolio Theory and mean-variance optimization.
Modern Portfolio Theory and Mean-Variance Optimization
Join the discussionAsk a question or share what you learned.
Keep exploring
Reference
Citation details
Cite or share this article.


Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!