Ridge regression explained with a two-feature example
Ridge regression adds λ to the diagonal of XᵀX before solving, which keeps correlated or collinear features solvable. A three-molecule example shows every step.
Updated November 28, 2026
Ridge regression is least squares with a penalty on the size of the coefficients. It adds λ times the identity matrix to XᵀX before inverting it, which keeps the solve stable when features are correlated or when there are as many parameters as data points. MolBasis kernels use ridge with λ = 1e-3, a value so small that it changes coefficients in the fourth decimal yet still rescues a singular system. This guide shows the formula, works a two-feature example by hand, and explains what such a small λ does and does not protect against.
The formula and the trade it makes
Ordinary least squares chooses β to minimise the sum of squared residuals. Ridge adds a second term and minimises the squared residuals plus λ times the sum of βⱼ². Hoerl and Kennard introduced the method in 1970 for exactly the case where predictors are nearly collinear and least-squares coefficients swing wildly from sample to sample.
The closed form is β = (XᵀX + λI)⁻¹Xᵀy. For any λ above zero the matrix in the bracket is positive definite, so the inverse exists. The price is bias. Every coefficient is pulled toward zero by a factor that depends on λ and on the spread of its column. The gain is lower variance. For small, noisy datasets the trade often lowers the error on new molecules.
The penalty treats all coefficients alike, so the columns must share a scale. That is one reason kernels standardise each term before the solve. The guide on z-scoring before a fit covers the other reasons.
Two identical features, three molecules, solved by hand
Take three molecules and two standardised descriptor columns that happen to be identical. Each column reads z = −1.225, 0, 1.225. The centred property reads y = −1, 0, 1. The numbers are invented and sized so that the sums are round.
Build the pieces. ZᵀZ has 3 on the diagonal, because the sum of squares of a standardised column equals the number of molecules, and 3 off the diagonal, because the columns are the same. Zᵀy has 2.449 in both rows, since 1.225 × 1 + 1.225 × 1 = 2.449.
Least squares fails here. The matrix with rows 3, 3 and 3, 3 has determinant zero, so there are infinitely many β pairs that fit the data equally well. Any pair with β₁ + β₂ = 0.816 gives the same predictions.
Ridge with λ = 1e-3 adds 0.001 to the diagonal. By symmetry the two coefficients are equal, call them b, and the first row of the system reads 3.001·b + 3·b = 2.449. So b = 2.449/6.001 = 0.408. The two features share the effect equally, and their sum 0.816 is the single-feature least-squares coefficient 2.449/3 to three decimals.
Raise λ to 1 and the same row reads 4·b + 3·b = 2.449, so b = 0.350 and the sum drops to 0.700. That is shrinkage. The fit no longer reproduces the data as well, but it would change less if one molecule were swapped.
import numpy as np
Z = np.array([[-1.225, -1.225], [0.0, 0.0], [1.225, 1.225]])
y = np.array([-1.0, 0.0, 1.0])
for lam in (1e-3, 1.0):
beta = np.linalg.solve(Z.T @ Z + lam * np.eye(2), Z.T @ y)
print(lam, beta.round(3))Why λ = 1e-3 is a numerical choice, not a safety net
On standardised columns the diagonal of ZᵀZ equals the number of molecules n. The ridge factor on a lone coefficient is n divided by n + λ. For n = 40 that is 40/40.001, a shrinkage of 0.0025 %. For n = 400 it is ten times smaller still. At this level ridge is least squares with a guard against division by zero.
So the λ in a kernel does not regularise. It makes the solve well defined when a two-term equation picks two nearly identical columns, which can happen with the σ moments or with cavity area and segment count. The guide on descriptor correlations and redundancy shows which pairs are at risk.
The protection against overfitting has to come from elsewhere. In the MolBasis record it comes from the gate around the search, which the guide on overfitting with few parameters describes. If you read a kernel as a regularised model, you will credit λ with work it did not do.
What ridge cannot fix
Ridge stabilises coefficients for a given set of features. It knows nothing about how the features were chosen. If the descriptor and its exponent were picked by scoring hundreds of candidates on the same molecules, the winner's score is optimistic, and λ does not remove that optimism.
Ridge also cannot repair a leak. If near-duplicate molecules sit on both sides of a split, any model looks better than it is. And when two correlated terms share a coefficient, as in the example above, the individual βⱼ lose their meaning even though the predictions are fine. The guide on interpretability of short equations returns to that point.
If you fit ridge for real, choose λ by cross-validation that respects groups. Scikit-learn's RidgeCV accepts a GroupKFold splitter, and the guide on grouped cross-validation explains why the group should be the molecule or the scaffold, never the row.
Key points
- Ridge regression solves β = (XᵀX + λI)⁻¹Xᵀy. For any λ above zero the solve exists, even with identical features.
- With two identical columns ridge splits the effect equally. Least squares has no unique answer at all.
- On standardised columns λ = 1e-3 shrinks coefficients by thousandths of a percent. It is a numerical guard, not regularisation.
- Ridge stabilises coefficients for chosen features. It does nothing about how those features were chosen, and that choice is where kernels take their risk.
Questions people ask
- What does ridge regression do that least squares does not?
- It adds a penalty λ times the sum of squared coefficients to the least-squares objective. The closed form becomes β = (XᵀX + λI)⁻¹Xᵀy, which is always solvable for λ greater than zero, even when two features are identical.
- Is λ = 1e-3 a strong regularisation?
- No. On standardised columns the diagonal of XᵀX equals the number of molecules, so a λ of 0.001 shrinks coefficients by a few thousandths of a percent. It guards against a singular matrix and does nothing to prevent overfitting.
- How do I pick λ for my own fit?
- Cross-validate it on groups that keep one molecule or one scaffold on one side of the split. Scikit-learn offers RidgeCV with a cv argument that accepts GroupKFold. Report the chosen value with the coefficients.
Further reading
In practice: A kernel on this site is one or two descriptor terms with coefficients from a single ridge solve. The methodology page gives the exact form, the grid and the gate the search had to pass.
See the kernels themselves
Every kernel is shown with its held-out result, the number of molecules it rests on and what it cannot be trusted for.