Principal component analysis
PCA finds the directions along which data vary most. The first component is the longest stretch of the cloud, the second the longest stretch at right angles to it, and so on.
01The formula
- the covariance matrix of the data
- the k-th principal component: a direction in the data
- the variance of the data along that direction (an eigenvalue of Σ)
- the total variance of all the variables
02See it move
03Where it is used
- Rates traderSummarises moves across a whole yield curve by three components: level, slope and curvature.
- Risk managerFinds the few hidden factors that drive most of a large book's risk.
- Quant researcherRemoves the market-wide component from a basket of stock returns to study what is left.
04Why traders use it
Many market variables move together. PCA tells you how many independent things are really going on, and lets you hedge or model those few instead of dozens of correlated ones.
05Worked example
With spreads of 1, 0.55 and 0.22 along the three axes, the first component explains 74.0% of the variance and the first two together 96.4%; flattening the cloud onto a plane loses only 3.6%.
06How it is built
- Centre the data and compute its covariance matrix .
- Look for the unit direction with the largest variance . With a Lagrange multiplier this gives : an eigenvector.
- Its eigenvalue is the variance along it; the next eigenvectors give the next-best directions, all at right angles.
- The eigenvalues add up to the total variance, so is the share each component explains.
07The common mistake
Running PCA on variables in different units. A price in dollars swamps a return in percent; standardise first, or use the correlation matrix, unless the units really are comparable.
08In Python and LaTeX
import numpy as np lam = np.linalg.eigvalsh(np.diag([1, 0.55**2, 0.22**2]))[::-1] print((lam / lam.sum()).round(3)) # [0.74 0.224 0.036]
\Sigma\,\mathbf v_k = \lambda_k \mathbf v_k, \qquad \text{share explained by PC}_k = \frac{\lambda_k}{\sum_j \lambda_j}09Where it fits
Rather not code the maths yourself? Describe your trading idea and let AI build the bot.
Build a bot with AgenKitThe worked example and the model run the same code, so every number matches. Models simplify markets on purpose. This is education, not financial advice.