Portfolio optimisation: choosing the weights
On this page
Portfolio optimisation is how you decide how much money to put in each asset. This post explores the idea by drawing thousands of random portfolios, which shows the shape of the problem and makes the intuition easier to see than an optimiser would.
This post pulls the whole Finance stats series together: it needs expected returns, standard deviations, correlations and the Sharpe ratio.
The question
You have money to split across five tickers. Each has an expected yearly return, a standard deviation, and correlations with the others. What mix should you hold?
The inputs are the 2019 to 2023 figures from the earlier posts, annualised, with a 2% risk-free rate:
| Ticker | Expected return | Standard deviation | Sharpe |
|---|---|---|---|
| SPY | 16.7% | 21.0% | 0.70 |
| AAPL | 37.8% | 32.2% | 1.11 |
| JPM | 18.9% | 31.9% | 0.53 |
| XOM | 18.2% | 34.3% | 0.47 |
| GLD | 10.2% | 14.9% | 0.55 |
The stocks move together (correlations 0.3 to 0.8, from the correlation post). Gold is nearly unrelated to the rest (0.1 or less). Keep one thing in mind throughout: “expected return” here means “what happened in those five years”, which is the standard shortcut and, as the end of the post shows, the weak point.
The two formulas you need
For weights (one per asset, adding up to 1), expected returns and covariance matrix . Romero and Balch’s What Hedge Funds Really Do (chapter 10) writes these as and ; in vector form:
the weights we choosereturnsrisk
The return is just the weighted average of the assets’ returns. The risk is not a weighted average: as the correlation post showed, the correlations inside decide how much of the risk cancels out. That asymmetry is the reason optimisation is worth doing at all.
50,000 random portfolios
I drew 50,000 random long-only mixes (all weights between 0 and 1, adding up to 1) and computed the return and risk of each:
Three things to notice:
- The cloud has a sharp upper-left edge. No mix gets above it. That edge is the efficient frontier: for each level of risk, the highest expected return available. Anything below the edge is beaten by a portfolio straight above it, with the same risk and more return.
- The lowest-risk mix (blue) has 12.7% risk, less than gold alone (14.9%). Adding some SPY and a little JPM to gold lowers risk, because they don’t move together.
- The best mix beats every single asset on Sharpe ratio. The best of the 50,000 has a Sharpe of , higher than AAPL alone (1.11), the best single asset.
| Portfolio | Return | Risk | Sharpe | Mix |
|---|---|---|---|---|
| Best Sharpe found | 25.3% | 19.3% | 1.21 | 53% AAPL, 42% GLD, 5% XOM, nothing in SPY or JPM |
| Lowest risk found | 12.4% | 12.7% | 0.82 | 70% GLD, 22% SPY, 7% JPM |
| Equal weights | 20.4% | 19.8% | 0.93 | 20% each |
Notice what the best mix is: the stock with the best past return plus the asset that moves independently of it. Nothing in SPY, even though SPY is “the market”: it’s just a worse version of AAPL in these years, and AAPL plus gold diversifies better.
(Random search only finds points near the true optimum. An optimiser such as SciPy’s
minimize on the negative Sharpe ratio would find the exact edge. With 50,000 draws in five
assets, we’re close.)
What “optimal” means
Romero and Balch state the problem like this: choose the weights with the least risk that still meets a target return , fully invested:
Solve it once for every possible target , and the answers trace out the efficient frontier: the lowest-risk portfolio for each achievable return. In practice you add limits too, such as no more than 5% in any one asset. Two special cases come up again and again:
Minimum variance: the mix with the least risk, whatever the return.
If short selling is allowed (weights can be negative), this has an exact answer:
where is a vector of ones. Note that it doesn’t use expected returns at all, which turns out to be an advantage (see below).
Maximum Sharpe ratio: the mix with the most excess return per unit of risk.
Without the long-only limit, the answer points in the direction , rescaled so the weights add up to 1. With limits like there’s no neat formula, and you need a numerical optimiser.
The capital market line
The dashed line on the chart starts at the risk-free rate (2%, zero risk) and just touches the cloud at the best-Sharpe portfolio. Its slope is that Sharpe ratio:
Every point on the line is a mix of the best-Sharpe portfolio and cash. Want less risk? Hold some cash. More? Borrow (if you can). The line lies above the frontier everywhere except where it touches, so in theory everyone should hold the same risky mix, and only change how much cash they hold beside it. It is a surprising result, and it is the foundation of index investing.
Why real optimisers disappoint
The maths is exact. The inputs are not. Expected returns are very hard to estimate: a few years of data tell you almost nothing reliable about a stock’s average return. AAPL’s 37.8% a year is what happened in 2019 to 2023, not what anyone could have forecast in 2018, and nobody should expect it to repeat. And the optimiser takes your guesses completely literally.
I changed one input: AAPL expected to return 32.8% instead of 37.8%, still a great stock. Everything else the same.
| Item | SPY | AAPL | JPM | XOM | GLD |
|---|---|---|---|---|---|
| Original best mix | 0% | 53% | 0% | 5% | 42% |
| After −5 points on AAPL | 0% | 40% | 3% | 7% | 50% |
A five-point change in a number nobody can forecast to five points moved an eighth of the portfolio. Nudge JPM’s return up by two points instead and JPM goes from 0% to 7%. Optimisers concentrate money in whatever assets have the most optimistic estimates, which are often the ones with the biggest estimation errors. Richard Michaud called mean-variance optimisers “error maximizers”.
What practitioners do about it:
- Prefer minimum variance, which doesn’t need expected returns at all.
- Add limits: no asset above some maximum, or close to a benchmark.
- Shrink estimates towards something sensible, like equal returns, before optimising.
- Check stability: if a small change to the inputs flips the answer, don’t trust the answer.
The code
This only scores random portfolios. It doesn’t search for the best one.
import numpy as np
import pandas as pd
prices = pd.read_csv("prices.csv", index_col="date", parse_dates=True) # SPY, AAPL, JPM, XOM, GLD
r = (prices / prices.shift(1) - 1).dropna()
mu = (r.mean() * 252).to_numpy() # expected yearly returns (the five-year averages)
cov = (r.cov() * 252).to_numpy() # yearly covariance matrix
W = np.random.default_rng(17).dirichlet(np.ones(5), 50_000) # random long-only weights
ret = W @ mu
risk = np.sqrt(np.einsum("ij,jk,ik->i", W, cov, W)) # sqrt(w' cov w) for each row
sharpe = (ret - 0.02) / risk
W[sharpe.argmax()].round(2) # best mix among the random ones
Summary
| Idea | Formula | Remember it as |
|---|---|---|
| Portfolio return | weighted average | |
| Portfolio risk | correlations decide how much cancels | |
| Efficient frontier | best return for each level of risk | the upper-left edge of the cloud |
| Minimum variance | (shorts allowed) | needs no return forecasts |
| Max Sharpe | max | where the line from cash touches the cloud |
| Capital market line | one risky mix, plus or minus cash | |
| The catch | outputs swing with small input changes | bad forecasts in, confident nonsense out |
That completes the Finance stats series: probability, expected value, mean and standard deviation, covariance and correlation, the risk-adjusted ratios, and portfolio optimisation.
References
- Philip J. Romero and Tucker Balch, What Hedge Funds Really Do: An Introduction to Portfolio Management (Business Expert Press, 2014), chapter 10: modern portfolio theory, the efficient frontier and the optimisation problem
- Harry Markowitz, “Portfolio Selection”, The Journal of Finance 7(1), 77–91, 1952
- Richard O. Michaud, “The Markowitz Optimization Enigma: Is ‘Optimized’ Optimal?”, Financial Analysts Journal 45(1), 31–42, 1989