Least squares in matrix form
The same least-squares fit as a straight line, but with many signals at once. Solving the normal equations gives every coefficient together, each one measured holding the others fixed.
01The formula
- the design matrix: one row per observation, one column per signal, plus a column of ones for the intercept
- the column of outcomes, for example next-day returns
- how the signals vary and overlap with each other
- how each signal moves with the outcome
- the fitted coefficients, one per column of X
02See it move
03Where it is used
- Quant researcherFits a return forecast on several signals at once, so a signal only gets credit for what the others do not already explain.
- Portfolio managerRuns factor regressions of a fund's returns on market, size and value factors to see where its returns come from.
- Risk managerBuilds multi-factor risk models whose exposures are regression coefficients.
04Why traders use it
Signals overlap. The matrix form untangles them: a coefficient is the effect of one signal after the others have been accounted for, which is what you need before adding a signal to a model.
05Worked example
With true coefficients 0.80 and −0.50, noise 0.50 and 80 observations, the fit gives β̂₁ = 0.745, β̂₂ = −0.449 and intercept 0.471. The leftover noise is estimated at 0.540.
06How it is built
- Stack the model for every observation: .
- The squared error is .
- Its gradient is ; setting it to zero gives the normal equations .
- Solve them: . In practice you solve the system rather than invert the matrix.
07The common mistake
Reading a coefficient as the effect of that signal alone. Each coefficient is conditional on the others in the model; add or drop a correlated signal and the remaining coefficients can change sign.
08In Python and LaTeX
import numpy as np rng = np.random.default_rng(7) n = 80 x1 = 1.1 * rng.standard_normal(n) x2 = 0.3 * x1 + rng.standard_normal(n) y = 0.5 + 0.8 * x1 - 0.5 * x2 + 0.5 * rng.standard_normal(n) X = np.column_stack([np.ones(n), x1, x2]) beta = np.linalg.solve(X.T @ X, X.T @ y) print(np.allclose(beta, np.linalg.lstsq(X, y, rcond=None)[0])) # True
\hat{\boldsymbol\beta} = (X^\top X)^{-1} X^\top \mathbf{y}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.