Updated: Mar 29, 2026
| 9 min

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.

Robot explaining finance with statistics

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 t=0t=0 and “cash out” entirely at time TT. Within this universe, we assume:

  • NN 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 N×1N \times 1 column μ\mu.
    • Risk: Represented by an N×NN \times N variance-covariance matrix VV.
  • Frictionless Trading: An investor with initial wealth WW 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 S1S_{1} with an annualized simple return μ1\mu_{1}. Over time, TT:

  • Value at t=0:V0=At=0: V_{0} = A
  • Value at t=T:Vt=A(1+μ1T)t=T: V_{t} = A(1+\mu_{1}T)

If we split our wealth across two assets (A1A_{1} and A2A_{2}), our total portfolio value at time TT is:

Vt=A1(1+μ1T)+A2(1+μ2T)V_{t} = A_{1}(1+\mu_{1}T) + A_{2}(1+\mu_{2}T)

If we define our weight as x1=A1Ax_{1} = \frac{A_{1}}{A} and x2=A2Ax_{2} = \frac{A_{2}}{A}, the total portfolio return RpR_{p} becomes a clean linear combinations:

Rp=x1μ1+x2μ2R_{p} = x_{1}\mu{1} + x_{2}\mu_{2}

If we try the same thing with log returns ( β\beta ), the math gets ugly fast. The portfolio value would be:

AeβT=A1eβ1T+A2eβ2TAe^{\beta T} = A_{1}e^{\beta_{1}T}+A_{2}e^{\beta_{2}T}

Solving for the portfolio return β\beta:

β=12ln(x1eβ1T+x2eβ2T)\beta = \frac{1}{2}ln(x_{1}e^{\beta_{1}T} + x_{2}e^{\beta_{2}T})

This nonlinear relationship is incredibly difficult to optimize. Unless TT is so small that we can use a Taylor series approximation (where ex1+xe^x \approx 1 + x ), 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 N×1N \times 1 column vector X.X. Each element x1x_{1}represents the fraction of total wealth invested in asset i.i. 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:

1TX=11^TX=1

While we could add a constraint that no short selling is allowed (xi0x_{i} \ge 0), 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 Rp=μTXR_{p} = \mu^TX. 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 ( σ1,σ2\sigma_{1}, \sigma_{2}), but also by how they move together. So, for a portfolio of NN **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:

PortfolioVariance=XTVXPortfolio Variance = X^TVX

The goal of the Mean-Variance Optimization is to find the specific allocation XX that minimizes risk for a target level of return RR. So, mathematically, we want to:

  • Minimize: XTVXX^TVX
  • Subject to:
    • μTX=R\mu^TX = R (The portfolio must hit our target return)
    • 1TX=11^TX=1 (The budget constraint)

To solve this, we use the method of Lagrange Multipliers. By introducing two multipliers ( λ1\lambda_{1} and λ2\lambda_{2}) 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:

σp2=aR22bR+cacb2\sigma_{p}^2 = \frac{aR^2 - 2bR + c}{ac - b^2}

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, aa, bb, and cc are so-called “summary statistics” of the entire market’s investment opportunity set. They depend strictly on the expected returns and the risk relationships (V1V^{-1}) of the assets you’ve chosen.

ConstantFormulaConceptual Meaning
aa1TV111^TV^{-1}1Information Density: Represents the sum of al elements in the inverse covariance matrix. It scales the total precision of the market.
bbμTV11\mu^TV^{-1}1Return-Weighted Risk: Measures the relationship between the assets expected returns and their risk structure.
ccμTV1μ\mu^TV^{-1}\muReturn 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 aa, bb, and cc constants, and then derive the Optimal Allocation Vector (XX) 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 aa, bb, and cc , 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 ( 1a\frac{1}{\sqrt{a}}). All the individual assets are marked with “XX” 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.