Free lesson · Linear algebra
Your portfolio is a vector; your risk is a matrix
Open interactive lessonPractice calculationsExplore labs
Start with the idea
Vectors let you keep the sign and size of every position; matrices retain the dependence between every pair. The off-diagonal terms matter because assets can move together even if their individual volatilities look modest.
Symbols, units & horizon
- wᵢ: signed capital weight of asset i
- rᵢ: decimal return of asset i
- N: asset count
- r_p: portfolio return
- ⊤: transpose
- Σ: covariance matrix
- σᵢⱼ: covariance of returns i and j
- ρᵢⱼ: return correlation
- σᵢ: asset volatility
- σ_p²: portfolio variance
- ΣᵢΣⱼ: sum over every asset pair
When and why to use this
Use weighted returns in daily P&L attribution and the covariance quadratic form in risk forecasts, optimisation and hedge sizing.
A single number is a scalar: a price, a return, a weight. A list of numbers is a vector: the returns of five assets today, or the weights of five positions. A grid is a matrix: the returns of five assets over 250 days (250 × 5), or how each pair of assets co-moves (5 × 5).
Portfolio return is a weighted sum, which is a dot product:
Expand the portfolio dot product
- Capital allocated to asset i is . Its gain is .
- Sum gains and divide by V₀: .
Weights .6,.4 and returns 2%,−1% give .6(.02)+.4(−.01)=.008, or 0.8%.
That is the easy half. The hard half is variance, because variance of a sum is not the sum of variances. It picks up every pairwise covariance:
Expand the covariance quadratic form
- Write the centred portfolio return as . Square it and take expectations: .
- The expectation is covariance σᵢⱼ. Substitute . With two assets, variance is .
Equal weights, vols .1,.2, correlation .25: variance=.0025+.01+.0025=.015; volatility=√.015=12.247%.
is the covariance matrix: diagonal entries are variances, off-diagonal entries are covariances. For two assets the expression expands to ; for 50 assets it has 2,500 terms, and the matrix form is the only sane way to write it.
Two assets, each 20% vol, 50/50 weights, correlation 0. Portfolio vol?
, so . Two uncorrelated 20% assets make a 14% portfolio. With ρ = 1 you would get exactly 20%.
Python implementation
Self-contained teaching example. Python 3.10+; dependencies and input conventions are shown in the code and notation. Run in your own Python environment.
import numpy as np # dependency: numpy
def portfolio(weights, returns, covariance):
w, r, cov = map(np.asarray, (weights, returns, covariance))
variance = float(w @ cov @ w)
return float(w @ r), variance, np.sqrt(max(0, variance))
def covariance_from_correlation(volatilities, correlations):
vol = np.asarray(volatilities)
return np.outer(vol, vol)*np.asarray(correlations)
print(portfolio([.5,.5], [.02,-.01], [[.01,0],[0,.04]]))Continue learning
Linear Algebra — all lessons- Start with lists: addition, scaling and a dot product
- Your portfolio is a vector; your risk is a matrix
- Eigenvalues: where the risk actually lives
- Regression, conditioning, and regularisation
Quantitative finance and development glossary · Python resources and libraries · Research sources and limitations