TradeLabs AILearn

Linear Algebra for Traders

Linear algebra handles many assets at once using vectors and matrices. Learn portfolio variance with covariance matrices, PCA, regressions and the Python tools.

Advanced3 min readUpdated 3 Oct 2026
Markdown
Lesson 43 of 46

When you work with one asset, simple formulas are enough. With dozens or thousands of assets, you need a way to handle all of them at once. Linear algebra, the mathematics of vectors and matrices, does exactly that. It powers portfolio risk calculations, factor models, regressions, principal component analysis and much of machine learning. You do not need to be a mathematician, but understanding a few core ideas makes quantitative finance far easier to follow.

Vectors and matrices#

ObjectDescriptionTrading example
VectorA list of numbersPortfolio weights; expected returns of several assets
MatrixA table of numbersReturns of many assets over many days; a covariance matrix
TransposeFlipping rows and columnsTurning a column of weights into a row
Matrix multiplicationCombining matrices according to rulesComputing portfolio returns from asset returns

Portfolio return and risk#

With weights w (a vector), expected returns μ (a vector) and covariance matrix Σ:

portfolio expected return = wᵀ μ
portfolio variance = wᵀ Σ w

Covariance matrices#

A covariance matrix collects the variances of all assets and their pairwise covariances. For 500 stocks, it has 500 × 500 entries. Estimating it reliably is hard, because there are so many parameters relative to the data. Techniques such as shrinkage and factor models help. See Bayesian Statistics and Factor Models.

Eigenvalues and principal component analysis#

Principal component analysis (PCA) finds the directions in which a set of assets moves together most. It uses the eigenvalues and eigenvectors of the covariance or correlation matrix.

ApplicationWhat PCA finds
Stock returnsThe first component is usually the overall market
Yield curvesLevel, slope and curvature explain most yield movements. See Yield Curves
Risk modelsA few factors explain much of portfolio risk
Statistical arbitrageResiduals after removing common components. See Statistical Arbitrage

Regression in matrix form#

Multiple regression can be written compactly:

β = (Xᵀ X)⁻¹ Xᵀ y

where X is the matrix of explanatory variables and y the vector of outcomes. This formula gives the ordinary least squares estimates for any number of variables. See Regression Analysis.

Optimisation#

Mean variance portfolio optimisation finds weights that minimise wᵀ Σ w for a target return, subject to constraints. Matrix maths makes these problems solvable for large portfolios. See Portfolio Optimization and Optimization.

Practical tools#

In Python, NumPy handles vectors and matrices efficiently:

import numpy as np
w = np.array([0.5, 0.3, 0.2])
vol = np.array([0.16, 0.06, 0.15])
corr = np.array([[1.0, -0.2, 0.1],
                 [-0.2, 1.0, 0.3],
                 [0.1, 0.3, 1.0]])
cov = np.outer(vol, vol) * corr
port_vol = np.sqrt(w @ cov @ w)
print(round(port_vol, 4))

See NumPy and Pandas for Traders.

Common pitfalls#

  • Ill conditioned matrices: nearly collinear assets make inverses unstable.
  • Estimation error: small errors in Σ or μ produce extreme optimised weights.
  • Shape mismatches in code.

Where to learn more#

A practical route is to learn NumPy array operations, then work through covariance matrices, regression and PCA on real return data. Seeing how changing one correlation changes portfolio volatility builds intuition faster than formulas alone. See Python for Trading.

Frequently asked questions#

Why do traders need linear algebra?#

To handle many assets at once in portfolio risk, factor models, regressions, PCA and optimisation.

How is portfolio variance calculated with matrices?#

As wᵀ Σ w, where w is the vector of weights and Σ is the covariance matrix.

What is PCA used for in finance?#

To find the main common drivers of a set of assets, such as the market factor in stocks or level, slope and curvature in yield curves.

Next, learn the calculus behind sensitivities in Calculus for Traders.

Check your understanding

3 quick questions on this lesson. Get them all right to finish it.

Turn on JavaScript to take the quiz.

Finished this lesson?Sign in to save your progress across devices.
Next lessonCalculus for TradersCalculus describes how quantities change. Learn derivatives, integrals and Taylor approximations, and how they underpin Greeks, duration and optimisation.

Mentioned in