The Claude Quant PlaybookDownload ebookDownload ebook

Ridge regression

Least squares with a charge on the size of the coefficients. It pulls every coefficient towards zero, most of all along directions where the data say little.

Build a trading system with Claude.Download ebook

01The formula

the penalty: how much you charge for large coefficients (0 gives ordinary least squares)
the sum of squared coefficients
the signals and the outcomes, as in least squares
the identity matrix: adds λ to each signal's own variance

02See it move

03Where it is used

04Why traders use it

When signals are correlated, plain least squares can give huge offsetting coefficients that fit the past and fail out of sample. Ridge trades a little bias for a big cut in variance.

05Worked example

Here the OLS estimates are 1.5 and 0.5 and the signals have correlation 0.60. With λ = 1.00 (per observation) ridge gives 0.758 and 0.473: a coefficient vector 57% as long, for an extra squared error of only 0.575.

06How it is built

  1. Add the penalty to the squared error: .
  2. Set the gradient to zero: .
  3. Collect terms: .
  4. Adding to the diagonal makes the matrix easy to invert even when signals are almost identical, which is why the answer is stable.

07The common mistake

Penalising signals measured in different units. Ridge charges every coefficient the same, so standardise each signal first or the penalty falls mostly on whichever one happens to have small units.

08In Python and LaTeX

import numpy as np
rho, lam = 0.6, 1.0
S = np.array([[1, rho], [rho, 1]])   # X'X / n
b_ols = np.array([1.5, 0.5])
b = np.linalg.solve(S + lam * np.eye(2), S @ b_ols)
print(b.round(3))  # [0.758 0.473]
\hat{\boldsymbol\beta}_{\text{ridge}} = \arg\min_{\boldsymbol\beta} \|\mathbf y - X\boldsymbol\beta\|^2 + \lambda\|\boldsymbol\beta\|^2 = (X^\top X + \lambda I)^{-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 AgenKit

The worked example and the model run the same code, so every number matches. Models simplify markets on purpose. This is education, not financial advice.