Notes on portfolio optimization
Thu 07 Aug 2025 - 14 min read | 47 view(s)In the realm of finance, portfolio optimization is the process of selecting the
optimal allocation of assets to maximize returns while minimizing risk. This
blog post shares my notes on the mathematics of portfolio optimization,
starting with single-asset optimization and progressing to more complex
scenarios.
Just a heads-up!
While I've explored portfolio optimization and have gained a decent grasp of
the maths involved, these notes are more for my understanding than expert
advice. So, take it all with a grain of salt! ;)
Let's begin with the simplest problem:
Optimizing the allocation to a single asset.
Suppose we have an asset with a expected return of and a
variance of . Our goal is to find the optimal allocation
that minimizes the variance while maximizing the return. This
is described using an objective function as follows:
There’s a lot happening here, so let’s break it down.
You might notice the quadratic function form ; this is intentional.
The term represents our risk, scaled by the square of the allocation
.
The squared allocation can be a bit confusing, but here’s why it
matters: When we scale the asset return by , we get a new 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 :
Thus, we see that the variance of the scaled return is proportional to the
square of the allocation.
In short: Increasing your investment in an asset not only boosts returns
but also raises your exposure to return variability. For example, doubling
your allocation doesn’t just double the return; it quadruples your risk. This
quadratic relationship explains the squared term in the variance calculation.
Now, back to our objective function: the coefficient is
, representing the asset's risk.
The in front of the risk term is really just to give us a
cleaner derivative as we'll see later
The term, on the other hand, represents the return of the
asset, scaled by the allocation . Here, is equal to ,
which is the negative of the return of the asset. The negation is
used because we want to maximize the return, and the minimization
problem will try to minimize the negative return, effectively
maximizing the actual return.
The term is absent in this case, as there is no constant term in our
objective function.
Now, to find the optimal allocation , we need to minimize this quadratic
function. We can do this by taking the derivative of the function with respect
to , setting it to zero, and solving for . This will give us the value of
that minimizes the function.
Let's do that:
So, the optimal allocation to the single asset is given by the ratio of the
return to the variance. This makes intuitive sense, as we want to allocate more
to assets with high returns and low risk, and less to assets with low returns
and high risk.
But, what if we have multiple assets to choose from? How do we optimize the
allocation across multiple assets?
Optimizing a Portfolio of multiple assets
We’ve seen how to optimize the allocation to a single asset, where the
trade-off is between return and risk. The optimal allocation was determined by
balancing the expected return against the variance (the risk) of
the asset. But what happens when we have two or more assets to choose from?
Extending to Two Assets
Let's start small, with just two assets, Asset A and Asset B. Each
asset has its own expected return , and variance
, , just like the single asset case.
If we want to allocate our capital between these two assets, we'll
assign weights and , where the expected return of
the portfolio is a simple weighted average of the expected returns
of the individual assets:
This is straightforward: the more you allocate to an asset, the more its return
contributes to the portfolio's overall return.
Here's where things start to get interesting. You might assume that the
portfolio risk is also just the weighted average of the individual risks:
At first glance, this seems reasonable. But it's incomplete.
Why? Because it doesn't take into account how the two assets
interact. We need to consider how the returns of Asset A and
Asset B move in relation to each other. This is where
covariance comes in.
Why Covariance Matters
Imagine that Asset A and Asset B are highly correlated -- when
Asset A goes up, Asset B tends to go up too. In this case, the
portfolio's risk will be higher because both assets are likely to
experience gains and losses together, reinforcing each other's
volatility.
On the other hand, if 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 volatility of the portfolio.
Deriving the formula for variance of multiple assets
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 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 asset returns. Specifically, if and
represent the weights of Asset A and Asset B, respectively, then the portfolio
return is, like described before:
Substituting this into the variance formula, we get:
Since and are constants, we can factor them out of the expectation,
giving us:
At this stage, we expand the square inside the expectation, which gives us
three terms:
The first two terms correspond to the individual variances of each asset,
scaled by the square of their respective weights like we showed earlier with
the single-asset case. Specifically, simplifies to
.
The third term, ,
measures how the returns of the two assets move together—specifically, this is
the covariance between the returns of Asset A and Asset B. By definition:
The factor of 2 comes from the expansion of , where the
covariance term appears twice: once for paired with and once for
paired with .
Thus, the cross-term becomes:
Finally, combining all the terms, the variance of the portfolio is:
This formula shows that the portfolio variance depends not only on the
individual variances of each asset but also on how the returns of the assets
interact, as captured by the covariance term. This is why diversification works
— if the assets are not perfectly correlated, the covariance term can reduce
overall portfolio risk, even while the portfolio maintains a positive return.
This formula for two assets is already getting a bit long. Imagine trying to write it for a portfolio of 100 assets! This is where the elegance of linear algebra comes to our rescue, allowing us to express these concepts in a much cleaner way.
Generalizing to N Assets with Matrix Notation
To handle a portfolio with any number of assets, we switch to vectors and matrices. Let's define the key components for a portfolio of assets:
-
The Weights Vector (): A column vector containing the allocation to each asset.
-
The Returns Vector (): A column vector of the expected returns for each asset.
-
The Covariance Matrix (): An matrix that captures the variance of each asset and the covariance between each pair of assets.
Note that the diagonal of contains the individual asset variances (), and the off-diagonal elements contain the covariances. Since , the matrix is symmetric.
With this notation, the portfolio's expected return and variance are expressed very neatly:
- Portfolio Return:
- Portfolio Variance:
This is the exact same math as before, just generalized for assets. The term is the dot product of the weights and returns, giving us a weighted average. The term is a quadratic form that perfectly captures all the variance and covariance terms.
The N-Asset Optimization Problem
Now we can formulate the core problem of portfolio optimization. We want to find the best weight vector . A common way to frame this is as maximizing a utility function that balances return and risk:
This should look familiar! It's the multi-asset version of our single-asset objective function. The new term, (lambda), is a risk-aversion parameter.
What is ?
represents how much you dislike risk.
- A high means you are very risk-averse. The optimization will heavily penalize variance, leading to a safer, lower-return portfolio.
- A low means you are more willing to take on risk for potentially higher returns.
By varying , we can trace out a whole set of optimal portfolios.
We also typically add the constraint that all weights must sum to one: .
Solving the Optimization Problem
So we have our objective function, but how do we actually find the optimal weight vector that maximizes it?
The process is remarkably similar to the single-asset case. We take the derivative with respect to our variable (), set it to zero, and solve. The only difference is that we're now using matrix calculus.
Our objective function is:
Taking the derivative with respect to the vector and setting it to zero gives us:
Now, we just need to solve for . Rearranging the equation, we get:
Let's pause and appreciate how elegant this solution is. It tells us that the optimal allocation is proportional to .
- This is the direct multi-asset equivalent of our single-asset solution, .
- Instead of dividing by the variance (), we multiply by the inverse of the covariance matrix (). This matrix inversion is the magic step that accounts for all the complex interactions between every asset in the portfolio.
- The result is then scaled by our risk aversion, .
A note on constraints: The solution above is for the unconstrained case. Adding the constraint that all weights must sum to one () makes the math a bit more involved (it requires a technique called Lagrange multipliers), but the core concept remains the same: we are finding the weights that give us the best risk-return trade-off.
By solving this problem for different values of , we can generate a whole family of optimal portfolios. This whole set of optimal portfolios is actually called the efficient frontier. It's something that I'll dive into in a future post, but for now, let's solve an example portfolio.
A Simple Worked Example
Let's make this real with a simple two-asset portfolio. Suppose we have two stocks:
- TechCorp (T): A high-growth tech stock.
- GlobalGoods (G): A stable, global consumer goods company.
We've estimated their financial characteristics as follows:
- Expected Returns (): TechCorp: 10%, GlobalGoods: 6%
- Standard Deviations (): TechCorp: 20%, GlobalGoods: 15%
- Correlation (): The returns have a low positive correlation of 0.3.
First, let's assemble our vectors and matrices.
The returns vector is straightforward:
For the covariance matrix , we need the variances and the covariance.
- Variance of TechCorp:
- Variance of GlobalGoods:
- Covariance:
So, our covariance matrix is:
Now, let's solve for the optimal weights using our formula: . We'll assume a moderate risk-aversion parameter of .
-
Find the inverse of the covariance matrix ():
For a 2x2 matrix, this is a standard procedure. The result is:
-
Multiply by the returns vector ():
-
Scale by risk aversion ():
With , our scaling factor is .
So our solution is to allocate 104.4% of our capital to TechCorp and 91.5% to GlobalGoods.
Wait, 104.4% + 91.5% = 195.9%? How is that possible?
Welcome to the concept of leverage. The unconstrained solution we just calculated doesn't assume our weights must sum to 100%. A total allocation over 100% implies borrowing money to invest more than you have. In this case, you would borrow 95.9% of your capital to achieve this high-return (and high-risk!) portfolio. This is a strategy an investor with very low risk aversion might take.
What if you can't borrow?
Most investors operate under the constraint that their weights must sum to 1 (). While the formal solution involves more complex math (Lagrange multipliers), the unconstrained solution we found still gives us something incredibly useful.
If we normalize our unconstrained weights so they sum to 1, we get the portfolio with the best possible risk-return trade-off.
- Total Weight =
- Normalized weight for TechCorp:
- Normalized weight for GlobalGoods:
This particular portfolio (53.3% TechCorp, 46.7% GlobalGoods) is special. It's the portfolio that maximizes our return for the risk we are taking.
And that’s pretty much the core of it! We've walked through the math, starting with a single asset and building up to a neat way of handling a whole portfolio with matrices. By solving for the weights and then normalizing them, we landed on a specific mix: about 53% in TechCorp and 47% in GlobalGoods.
It's interesting that no matter what our personal risk aversion λ
was, the ratio between the assets stayed the same. Normalizing them just gives us a tangible portfolio to look at.
But this definitely opens up more questions. What's so special about this specific 53/47 mix? And what about the constraint that most of us have: that our investments must add up to 100%? We kind of sidestepped that with our normalization trick.
These are deeper topics in portfolio theory, and we'll definitely dig into them in a future post. For now, hopefully, this gives a solid feel for the fundamental math involved. Thanks for reading