Notes on portfolio optimization
Thu 07 Aug 2025 - 12 min readStarting with a Single Asset
Suppose we have some asset with a random return . Its expected return is , and its variance is . Our goal is to find the optimal allocation of capital that balances expected return against variance.
This is described using our objective function:
There’s quite a lot happening here, so let’s break it down.
The squared exposure confused me quite a bit the first time ‘round, but here’s how it pops up: When we scale the random return by , we get a new random return . The variance of this scaled return is given by:
Since is a constant, we can factor it out:
The last term is simply the variance of :
The expected return scales linearly instead:
So doubling doubles our expected return but quadruples variance. In our model the reward grows linearly while the variance penalty grows quadratically which means that, at some point, taking on more exposure hurts the objective more than it helps.
Using variance as our measure of risk is a modelling choice from mean-variance optimization; it makes this a quadratic optimization problem with a nice solution
Now back to our objective function: the term represents our variance penalty with a risk-aversion parameter which controls how heavily variance is penalized.
The in front of the risk term is really just to give us a cleaner derivative, as we’ll see later.
Now to find the optimal exposure we need to take the derivative of the objective function with respect to , set it to zero, and solve for :
So the optimal exposure to our single asset is given by . This makes good intuitive sense, as we want more exposure to assets with high expected returns and low variance, and less exposure when we are more risk-averse.
But what if we instead have multiple assets to choose from? How do we optimize the allocation across multiple assets?
Adding a Second Asset
We’ve seen how to optimize the allocation to a single asset, where the trade-off is between expected return and variance. The optimal allocation was determined by balancing the expected return against the variance of the asset. But what happens when we have two or more assets to choose from?
Let’s start small, with just two assets, Asset A and Asset B. Their random returns are and , with expected returns and and variances and , just like the single-asset case. If we want to allocate our capital between these two assets, we’ll assign weights and . The random portfolio return is the weighted sum:
Its expected return is therefore:
This is pretty straightforward: the more you allocate to an asset, the more its expected return contributes to the portfolio’s overall expected return.
Here’s where things start to get fun. You might assume that the portfolio variance is just the sum of the individual variance terms:
And at first, this does seems reasonable. But it doesn’t tell us whether the two assets tend to move in relation to eachother.
Imagine that Asset A and Asset B are positively correlated: when Asset A goes up, Asset B will tend to go up too. This will result in a portfolio with a higher risk profile as both assets are likely to experience both gains, and losses at the same time, reinforcing each other’s variance.
Now imagine that Asset A and Asset B tend to move in opposite directions (i.e., they have negative correlation), the portfolio’s overall risk will be lower. This is because the losses in one asset might be offset by gains in the other, reducing the total variance of the portfolio.
To derive the formula for the variance of a portfolio with multiple assets, we begin with the definition of variance. For any random variable, the variance measures the spread of its possible outcomes around its mean. In the case of a portfolio, the variance of the random portfolio return is given by:
If our portfolio consists of two assets, the total return of the portfolio is a weighted sum of the individual random returns. Specifically, if and represent the weights of Asset A and Asset B respectively, then the portfolio return is, as described above:
Substituting this into the variance formula, we get:
Since and are constants, we can factor them out of the expectation, giving us:
At this point, we can expand the square inside the expectation, which gives us three terms:
The first two terms correspond to our individual variances of each asset scaled by the square of their respective weights like we showed earlier with the single-asset case.
The third term, , measures how the returns of the two assets move together. This is the covariance between the returns of Asset A and Asset B. By definition:
Therefore our cross-term becomes:
Now combining everything we get:
This formula shows that our portfolio variance depends on both the individual variances of each asset but also on how the returns of the assets interact, as captured by our covariance term.
This is why diversification works: If the assets are not perfectly correlated, the covariance term can reduce (or increase) overall portfolio risk, even while the portfolio maintains a positive expected return.
Generalizing to N Assets
Our formula for just two assets is already getting a bit long. Imagine trying to write this out for a portfolio of 100 assets! We’ll use some basic linear algebra to express these concepts in a much cleaner way.
For assets we collect their random returns and our chosen weights into two column vectors:
Taking the expected value of each return gives us the expected-return vector:
We then collect all the variances and covariances in the covariance matrix , whose entries are:
The diagonal entries are the individual asset variances, while the off-diagonal entries show how each pair of assets moves together. Since , the covariance matrix is symmetric.
From this, our portfolio equations follow:
Expanding gives us the same individual variance and pairwise covariance terms we derived above, only now the notation works for any number of assets.
Solving for the Weights
Now we can write the -asset optimization problem:
This should look familiar! It’s the multi-asset version of our earlier single-asset objective function. Just like the single-asset problem, controls how heavily we penalize variance.
If we leave the weights unrestricted, we can differentiate the objective and set the result to zero. Since is symmetric, this gives:
Assuming the covariance matrix is invertible, solving for the weights gives:
This is the multi-asset equivalent of our single-asset solution
This solution, however, is unrestricted meaning the weights can add up to more or less than one. That can be useful when forms of borrowing is allowed, but suppose we instead want a fully invested portfolio:
To include this constraint, we introduce a Lagrange multiplier :
We then differentiate with respect to both and :
The first equation gives us:
We choose so that the weights satisfy our full-investment constraint:
Together, these equations give us the portfolio that maximizes our mean-variance objective while investing exactly 100% of our capital.
The constraint still allows individual weights to be negative, meaning that short positions are permitted. Preventing short selling requires the additional constraints , which generally means solving the problem numerically.
Now, let’s put some numbers into this and see how the calculation works.
Try It Yourself
Let’s use two imaginary stocks. TechCorp has an expected return of 10.0% and a standard deviation of 20%. GlobalGoods has an expected return of 6.0% and a standard deviation of 15%. Their correlation is 0.30, and we’ll use a risk-aversion parameter of 2.0.
Drag any of the blue values to see the calculation update.
First, the covariance and our two input matrices are:
The inverse covariance matrix is:
The remaining values in the constrained solution are:
Finally, the fully invested portfolio is:
Its expected return is requires JavaScript and its variance is requires JavaScript.
This puts requires JavaScript in TechCorp and requires JavaScript in GlobalGoods. Changing any of the assumptions above runs the same calculation again immediately.
And that’s pretty much the core of it! We started with a single asset, introduced covariance for multiple assets, and ended up with a compact way to optimize an entire portfolio. Thanks for reading.