Trading Dev AcademyFree quant education

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:

rp=𝐰⊤𝐫=∑i=1Nwiri
Algebra and arithmetic

Expand the portfolio dot product

  1. Capital allocated to asset i is wiV0. Its gain is wiV0ri.
  2. Sum gains and divide by V₀: rp=∑iwiri=wTr.
Work it by hand

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:

σp2=𝐰⊤Σ𝐰=∑i∑jwiwjσij,σij=ρijσiσj
Algebra and arithmetic

Expand the covariance quadratic form

  1. Write the centred portfolio return as ∑iwi(ri−μi). Square it and take expectations: ∑i∑jwiwjE[(ri−μi)(rj−μj)].
  2. The expectation is covariance σᵢⱼ. Substitute σij=ρijσiσj. With two assets, variance is w12σ12+w22σ22+2w1w2ρσ1σ2.
Work it by hand

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 w12σ12+w22σ22+2w1w2ρσ1σ2; 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?

σp2=0.25(0.04)+0.25(0.04)+0=0.02, so σp=0.141. 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
  1. Start with lists: addition, scaling and a dot product
  2. Your portfolio is a vector; your risk is a matrix
  3. Eigenvalues: where the risk actually lives
  4. Regression, conditioning, and regularisation

Quantitative finance and development glossary · Python resources and libraries · Research sources and limitations