Vilhelm Bergsøe

Notes on portfolio optimization

Thu 07 Aug 2025 - 12 min read

Starting with a Single Asset

Suppose we have some asset with a random return 𝑟. Its expected return is 𝜇=𝔼[𝑟], and its variance is 𝜎2=Var(𝑟). Our goal is to find the optimal allocation of capital 𝑤 that balances expected return against variance.

This is described using our objective function:

max𝑤𝑤𝜇−𝜆2𝜎2𝑤2

There’s quite a lot happening here, so let’s break it down.

The squared exposure 𝑤2 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:

Var(𝑤𝑟)=𝔼[(𝑤𝑟−𝔼[𝑤𝑟])2]

Since 𝑤 is a constant, we can factor it out:

=𝔼[(𝑤(𝑟−𝔼[𝑟]))2]=𝑤2⋅𝔼[(𝑟−𝔼[𝑟])2]

The last term is simply the variance of 𝑟:

=𝑤2𝜎2

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 𝜆2𝜎2𝑤2 represents our variance penalty with a risk-aversion parameter 𝜆 which controls how heavily variance is penalized.

The 12 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 𝑤:

𝜕𝜕𝑤(𝑤𝜇−𝜆2𝜎2𝑤2)=0𝜇−𝜆𝜎2𝑤=0𝑤∗=𝜇𝜆𝜎2

So the optimal exposure to our single asset is given by 𝜇𝜆𝜎2. 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 𝜎2 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 𝜎𝐴2 and 𝜎𝐵2, 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:

𝜎𝑝2=𝑤𝐴2𝜎𝐴2+𝑤𝐵2𝜎𝐵2

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:

𝜎𝑝2=𝔼[(𝑟𝑝−𝔼[𝑟𝑝])2]

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:

𝜎𝑝2=𝔼[(𝑤𝐴𝑟𝐴+𝑤𝐵𝑟𝐵−𝔼[𝑤𝐴𝑟𝐴+𝑤𝐵𝑟𝐵])2]

Since 𝑤𝐴 and 𝑤𝐵 are constants, we can factor them out of the expectation, giving us:

𝜎𝑝2=𝔼[(𝑤𝐴(𝑟𝐴−𝔼[𝑟𝐴])+𝑤𝐵(𝑟𝐵−𝔼[𝑟𝐵]))2]

At this point, we can expand the square inside the expectation, which gives us three terms:

𝜎𝑝2=𝔼[𝑤𝐴2(𝑟𝐴−𝔼[𝑟𝐴])2+𝑤𝐵2(𝑟𝐵−𝔼[𝑟𝐵])2+2𝑤𝐴𝑤𝐵(𝑟𝐴−𝔼[𝑟𝐴])(𝑟𝐵−𝔼[𝑟𝐵])]

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, 2𝑤𝐴𝑤𝐵(𝑟𝐴−𝔼[𝑟𝐴])(𝑟𝐵−𝔼[𝑟𝐵]), 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:

Cov(𝐴,𝐵)=𝔼[(𝑟𝐴−𝔼[𝑟𝐴])(𝑟𝐵−𝔼[𝑟𝐵])]

Therefore our cross-term becomes:

2𝑤𝐴𝑤𝐵Cov(𝐴,𝐵)

Now combining everything we get:

𝜎𝑝2=𝑤𝐴2𝜎𝐴2+𝑤𝐵2𝜎𝐵2+2𝑤𝐴𝑤𝐵Cov(𝐴,𝐵)

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:

𝐫=[𝑟1𝑟2⋮𝑟𝑁],𝐰=[𝑤1𝑤2⋮𝑤𝑁]

Taking the expected value of each return gives us the expected-return vector:

𝛍=𝔼[𝐫]=[𝜇1𝜇2⋮𝜇𝑁]

We then collect all the variances and covariances in the covariance matrix 𝚺, whose entries are:

𝚺𝑖𝑗=Cov(𝑟𝑖,𝑟𝑗)

The diagonal entries are the individual asset variances, while the off-diagonal entries show how each pair of assets moves together. Since Cov(𝑟𝑖,𝑟𝑗)=Cov(𝑟𝑗,𝑟𝑖), the covariance matrix is symmetric.

From this, our portfolio equations follow:

𝑟𝑝=𝐰𝑇𝐫,𝔼[𝑟𝑝]=𝐰𝑇𝛍Var(𝑟𝑝)=𝐰𝑇𝚺𝐰

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:

max𝐰𝐰𝑇𝛍−𝜆2𝐰𝑇𝚺𝐰

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:

𝛍−𝜆𝚺𝐰=0

Assuming the covariance matrix is invertible, solving for the weights gives:

𝐰∗=1𝜆𝚺−1𝛍

This is the multi-asset equivalent of our single-asset solution 𝑤∗=𝜇𝜆𝜎2.

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:

𝟏𝑇𝐰=1

To include this constraint, we introduce a Lagrange multiplier 𝛾:

ℒ︀(𝐰,𝛾)=𝐰𝑇𝛍−𝜆2𝐰𝑇𝚺𝐰+𝛾(1−𝟏𝑇𝐰𝟏)

We then differentiate with respect to both 𝐰 and 𝛾:

𝜕ℒ︀𝜕𝐰=𝛍−𝜆𝚺𝐰−𝛾𝟏=0𝜕ℒ︀𝜕𝛾=1−𝟏𝑇𝐰=0

The first equation gives us:

𝐰∗=1𝜆𝚺−1(𝛍−𝛾𝟏)

We choose 𝛾 so that the weights satisfy our full-investment constraint:

𝛾=𝟏𝑇𝚺−1𝛍−𝜆𝟏𝑇𝚺−1𝟏

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 𝑤𝑖≥0, 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:

Cov(𝐴,𝐵)=requires JavaScript𝛍=requires JavaScript𝚺=requires JavaScript

The inverse covariance matrix is:

𝚺−1=requires JavaScript

The remaining values in the constrained solution are:

𝚺−1𝛍=requires JavaScript𝚺−1𝟏=requires JavaScript𝛾=requires JavaScript

Finally, the fully invested portfolio is:

𝐰∗=requires JavaScript

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.

tags: [economics, investment, optimization]