1. Vectors and matrices explained with portfolios
Level 7: Linear algebra and portfolio optimization
When you work out how much your portfolio gained last month, you are already doing linear algebra without knowing it.
Nobody has told you so under that name, but the operation is exactly that. To understand why portfolio optimization needs linear algebra, it is worth starting there: with something you already know under a different name.
Your portfolio is already a vector
A vector is nothing more than an ordered list of numbers. And a multi-asset portfolio is already, though nobody has told you so until now, exactly that.
If you have a portfolio with Apple at 40%, Microsoft at 30%, Johnson & Johnson at 20% and Exxon at 10%, that portfolio is the vector (0.40, 0.30, 0.20, 0.10).
Order matters: the first position always corresponds to Apple, the second to Microsoft, and so on. Changing any of those numbers, even one of them, takes you to a different portfolio, that is, to a different vector.
How many dimensions a portfolio has
The dimension of that vector, that is, how many numbers it contains, is simply the number of assets in the portfolio:
- A portfolio of 4 stocks lives in a 4-dimensional space.
- One that replicates the 500 companies of the S&P 500 lives in a 500-dimensional space.
You do not need to visualize 500 dimensions, because nobody can, and there is no need. The algebra works just as well conceptually even though the geometry stops being drawable beyond the third dimension.
The rule weights usually satisfy
That brings us to the first rule a weight vector usually satisfies. If there is no leverage and no uninvested cash, its components must add up to 1, or to 100%, which is the same thing.
If you allow short positions, some component can be negative, because you are betting that asset falls, and the vector is still perfectly valid.
The algebra does not require weights to be positive: that is a constraint added by the investor, not by the mathematics.
The return is not the simple average
Now comes the operation that really matters, which is computing the portfolio return.
Suppose that in one month Apple rises 5%, Microsoft falls 2%, J&J rises 1% and Exxon rises 3%. The portfolio return is NOT the simple average of those four numbers, which would be 1.75%.
It is the weighted average using the weights: 0.40 times 5, plus 0.30 times minus 2, plus 0.20 times 1, plus 0.10 times 3. That gives 2 minus 0.6 plus 0.2 plus 0.3, that is, 1.9%.
This operation has its own name in linear algebra. Multiplying each component of one vector by the corresponding component of another vector and adding all the results is called the scalar product, or dot product, between the weight vector and the return vector.
Every time you compute how much your portfolio gained, you are doing linear algebra without knowing it.
Rebalancing is moving within the same space
Rebalancing a portfolio, that is, going from 50/50 between two assets to, say, 70/30, is, in this language, moving from one point to another within the space of all possible portfolios.
Note carefully what does not change when you make that move within the space:
- You do not change the number of assets, because the dimension is still 2.
- You do not touch how those assets move relative to each other, because that lives in another object: the covariance matrix, which is the subject of the next module.
You simply choose a different weight vector within the same space.
Multiplying the whole vector by a number is equivalent to levering the portfolio. For example, multiplying (0.5, 0.5) by 1.5 to get (0.75, 0.75), adding up to 1.5 instead of 1: the same relative split between assets, but greater total exposure.
Comparing two portfolios is a subtraction
This way of thinking also serves to compare portfolios with one another.
Suppose your current portfolio is (0.40, 0.30, 0.20, 0.10) and your target portfolio after a rebalance is (0.25, 0.25, 0.25, 0.25), an equal split across the four assets.
The difference between the two vectors, component by component, tells you exactly how much you have to buy or sell of each:
- Minus 0.15 in Apple.
- Minus 0.05 in Microsoft.
- Plus 0.05 in J&J.
- Plus 0.15 in Exxon.
This is not a metaphor, it is a vector subtraction, and it is literally what any automatic rebalancing tool does under the hood.
From vectors to matrices
A matrix, in turn, is simply a table of numbers organized into rows and columns, and you can think of it as several vectors stacked together.
If you store the daily return of your 4 assets over 252 market days, you have a table with 252 rows, one per day, and 4 columns, one per asset. That is a 252 by 4 matrix.
It is from that table of raw data that, in the next module, the covariance matrix is extracted, summarizing at a glance how all pairs of assets move at once.
The reason this language matters is not aesthetic, but purely practical.
As soon as you have more than two or three assets, it is the only manageable way to describe precisely what a portfolio is, how its return is computed and, in the following modules, how its risk is measured and optimized.