Portfolio Allocation: The Math Behind the Efficient Frontier
Master the quantitative core of Modern Portfolio Theory. Learn how to use Markowitz optimization and Python to build an efficient frontier for asset allocation.
In the previous posts, we have already dipped our toes into diversifying our portfolio. But since that post, we have looked at different financial investment instruments. We can invest in stock, government bonds, corporate bonds, and even options. We can also buy these on many markets in countries such as Japan, Germany, the United Kingdom, the United States, and Canada. With all these options, a sensible investor will probably try to diversify their investment portfolio.
When faced with all these investment choices, what do we do? In this post, we will explore a quantitative model for portfolio allocation. It is a model that provides the conceptual core for modern asset allocation. This model was developed by Markowitz and refined by Sharpe, and earned the Nobel Prize in Economics.
Foundations of Portfolio Allocation
Before we dive into the mechanics of quantitative allocation, we must establish the rules of the game. We begin with a set of simplifying assumptions that allow us to build a mathematically tractable model.
Core Assumptions
We assume a one-period investment decision. You invest your entire capital at and “cash out” entirely at time . Within this universe, we assume:
- Assets: The investment universe consists of N assets. This could be the entire market or some subset of the market after we have done some kind of screening (e.g., liquidity filters or ethical criteria).
- Joint Normality: The simple returns of these assets are jointly normally distributed.
- Mean Returns: Represented by an column .
- Risk: Represented by an variance-covariance matrix .
- Frictionless Trading: An investor with initial wealth can invest in any asset without restriction and without impacting market prices.
- Unrestricted Short Selling: Investors can short any asset and use the proceeds to fund other positions.
Why the One-Period Model?
We use a one-period model for its simplicity. This one-period model does ignore the reality that investors can rebalance their portfolios in response to new information. However, this approach dramatically simplifies the calculus, bypassing the headache of transaction costs, and still provides a clear baseline for understanding diversification.
Simple returns
In derivatives pricing, we often prefer log returns because they exhibit better long-term behavior. However, in portfolio construction, simple returns are superior because they are additive across assets. Suppose you invest A dollars in stock with an annualized simple return . Over time, :
- Value at
- Value at
If we split our wealth across two assets ( and ), our total portfolio value at time is:
If we define our weight as and , the total portfolio return becomes a clean linear combinations:
If we try the same thing with log returns ( ), the math gets ugly fast. The portfolio value would be:
Solving for the portfolio return :
This nonlinear relationship is incredibly difficult to optimize. Unless is so small that we can use a Taylor series approximation (where ), log returns make portfolio aggregation mathematically messy.
We assume returns are normally distributed primarily for mathematical tractability. While fat tails of real-world markets often challenge this assumption, the normal hypothesis allows us to define a portfolio’s entire return profile using the mean and the variance.
Building the Risk and Return Model
Now that we have established the core assumptions of our model, let’s move to the mathematical implementation. To optimize a portfolio, we must first define our variables and the mathematical model that balances the desire for profit with the reality of risk.
Our primary control variable is the allocation vector, denoted as column vector Each element represents the fraction of total wealth invested in asset Because we are dealing with proportions of a whole, our first physical constraint is that the sum of these fractions must equal 1 (100% of our capital). In matrix notation, this is expressed using a vector of ones:
While we could add a constraint that no short selling is allowed (), we will first solve the unconstrained version to see the market’s pure geometry.
We know we want a high Expected Return, calculated simply as . However, maximizing return alone is dangerous without considering risk; the model would simply tell you to put 100% of your money into the single most volatile, high-growth stock. To measure risk, we use the portfolio’s variance. If we have two stocks, the variance is influenced not just by their individual volatility ( ), but also by how they move together. So, for a portfolio of **assets, we capture these relationships in the Variance-Covariance Matrix (V). This symmetric matrix allows us to express the total portfolio risk as a quadratic form:
The goal of the Mean-Variance Optimization is to find the specific allocation that minimizes risk for a target level of return . So, mathematically, we want to:
- Minimize:
- Subject to:
- (The portfolio must hit our target return)
- (The budget constraint)
To solve this, we use the method of Lagrange Multipliers. By introducing two multipliers ( and ) for our two constraints, we can transform this into an unconstrained problem. Taking first-order derivatives and solving the linear system yields a crucial discovery: the relationship between risk and return is not linear; it is hyperbolic. The solution for the variance of the optimal portfolio can be simplified to the following form:
This equation describes a hyperbola in the risk-return space, famously known as the Efficient Frontier. Every point on this curve represents a portfolio that offers the lowest possible risk for that specific level of return. In the formula, , , and are so-called “summary statistics” of the entire market’s investment opportunity set. They depend strictly on the expected returns and the risk relationships () of the assets you’ve chosen.
| Constant | Formula | Conceptual Meaning |
|---|---|---|
| Information Density: Represents the sum of al elements in the inverse covariance matrix. It scales the total precision of the market. | ||
| Return-Weighted Risk: Measures the relationship between the assets expected returns and their risk structure. | ||
| Return Intensity: Indicates the potential for return relative to the risk structure of the assets. |
We can now translate the matrix algebra into functional code. We can perform all the matrix operations using numpy and pandas. In this script, we will use four hypothetical assets, calculate the , , and constants, and then derive the Optimal Allocation Vector () for a target return.
import numpy as np
import matplotlib.pyplot as plt
def optimize_portfolio(returns, cov_matrix, target_return):
"""
Calculates the optimal weights using the Lagrange Multiplier analytical solution.
"""
# Prepare the inputs
V = np.matrix(cov_matrix)
V_inv = V.I
mu = np.matrix(returns).T
ones = np.matrix(np.ones(len(returns))).T
R = target_return
# Calculate the Market Constants (a, b, c)
a = (ones.T * V_inv * ones).item()
b = (mu.T * V_inv * ones).item()
c = (mu.T * V_inv * mu).item()
det = a * c - b**2
# Solve for Lagrange Multipliers (lambda 1 and lambda 2)
# Based on the system:
# lambda1 * a + lambda2 * b = 1
# lambda1 * b + lambda2 * c = R
lambda_2 = (a * R - b) / det
lambda_1 = (c - b * R) / det
# Calculate Optimal Weights (X)
# Equation: X = V^-1 * (lambda_1 * 1 + lambda_2 * mu)
weights = V_inv * (lambda_1 * ones + lambda_2 * mu)
return np.array(weights).flatten(), a, b, c
def plot_efficient_frontier(cov_matrix, a, b, c):
"""
Plot the efficient frontier chart
"""
# Define the Frontier range (around the Global Min Variance Portfolio)
r_min = b / a
target_returns = np.linspace(r_min - 0.05, r_min + 0.1, 200)
# Analytical Variance: σ² = (aR² - 2bR + c) / (ac - b²)
variances = (a * target_returns**2 - 2 * b * target_returns + c) / b**2
volatilities = np.sqrt(variances)
plt.figure(figsize=(10, 6))
plt.plot(volatilities, target_returns, color='blue', linestyle='--', label='Min-Var Frontier')
# The Efficient Frontier is the upper branch (Returns >= r_min)
efficient_idx = target_returns >= r_min
plt.plot(volatilities[efficient_idx], target_returns[efficient_idx],
color='darkblue', linewidth=3, label='Efficient Frontier')
# Mark the Global Minimum Variance Portfolio (GMVP)
plt.scatter(np.sqrt(1/a), r_min, color='red', label='GMVP', zorder=5)
# Plot individual assets
plt.scatter(np.sqrt(np.diag(cov_matrix)), asset_returns, marker='X', color='black', label='Assets')
plt.title('The Markowitz Efficient Frontier')
plt.xlabel('Risk (Volatility)')
plt.ylabel('Expected Return')
plt.legend()
plt.grid(True, linestyle=':', alpha=0.6)
plt.savefig('efficient_frontier_plot.png')
# --- Example Usage ---
# Assume 4 assets with expected annual returns
asset_returns = np.array([0.08, 0.12, 0.15, 0.10])
# Assume a stylized Covariance Matrix
cov_data = [
[0.005, 0.002, 0.001, 0.001],
[0.002, 0.008, 0.003, 0.002],
[0.001, 0.003, 0.012, 0.002],
[0.001, 0.002, 0.002, 0.007]
]
target = 0.11 # We want an 11% return
weights, a, b, c = optimize_portfolio(asset_returns, cov_data, target)
print(f"Optimal Weights for {target*100}% Return:")
for i, w in enumerate(weights):
print(f" Asset {i+1}: {w*100:.2f}%")
# Calculate resulting portfolio variance
portfolio_variance = (a * target**2 - 2 * b * target + c) / (a * c - b**2)
print(f"\nPortfolio Volatility (Std Dev): {np.sqrt(portfolio_variance)*100:.2f}%")
# Plot the chart
plot_efficient_frontier(cov_data, a, b, c)
In addition to this output, we also visualized the relationship between risk and return. By plotting the analytical solution derived from our constants , , and , we can see exactly where the best portfolios lie.
In this plot, the dashed line represents all portfolios that have the minimum possible variance for a given return. In red, the Global Minimum Variance Portfolio is marked. This is the single point on the graph with the lowest possible risk ( ). All the individual assets are marked with “” and fall inside the curve.
While the math produces the full hyperbola, an investor would never choose a portfolio on the lower branch. Because why take the same risk for a lower return? The true Efficient Frontier is the upper part of the blue curve. This chart visually proves the point that a diversified portfolio can provide a better risk-return profile than any single asset held in isolation.
⚠️ Financial Education Disclaimer
The quantitative models and Python code provided in this post (including the Markowitz Mean-Variance Optimization) are for educational and research purposes only.
- Not Financial Advice: This content does not constitute professional financial or investment advice.
- Model Assumptions: The Markowitz model relies on historical data and the assumption of “Joint Normality,” which often fails during real-market “Black Swan” events. Past performance is not indicative of future results.
- Risk of Loss: Quantitative trading and portfolio allocation involve significant risk. You should consult with a certified financial advisor before making any investment decisions.