Sub topic 14
Correlated features and unstable coefficients
This chapter assumes you can fit a LinearRegression, read coef_, and score a model on held-out data. It also assumes you know what a bootstrap resample is: a random sample of rows drawn with replacement.
What multicollinearity is
Multicollinearity means two or more features carry nearly the same information. Examples are height in centimetres and height in inches, or a total and one of its parts. Ordinary least squares (OLS) tries to give each feature its own effect while holding the others fixed. When two features move together, the data cannot say how to split the credit between them.
The effect is on the individual coefficients, not on the predictions. Many different coefficient pairs fit the training data almost equally well. A tiny change in the sample can flip the fit from one pair to another. Coefficients may become very large, with opposite signs that cancel each other out.
Seeing the instability
The code below builds two almost identical features. The target depends only on x1, with a true weight of 3. It refits OLS and Ridge on 200 bootstrap resamples and reports how much each coefficient moves.
import numpy as np
from sklearn.linear_model import LinearRegression, Ridge
rng = np.random.default_rng(0)
n = 100
x1 = rng.normal(size=n)
x2 = x1 + rng.normal(0, 0.01, size=n)
X = np.column_stack([x1, x2])
y = 3 * x1 + rng.normal(0, 1.0, size=n)
print("corr:", np.corrcoef(x1, x2)[0, 1])
models = {
"OLS": LinearRegression,
"Ridge": lambda: Ridge(alpha=1.0),
}
for name, make in models.items():
coefs = []
for _ in range(200):
idx = rng.integers(0, n, size=n)
coefs.append(make().fit(X[idx], y[idx]).coef_)
coefs = np.array(coefs)
print(name, "coef std:", coefs.std(axis=0))
print(name, "mean of coef sum:", coefs.sum(axis=1).mean())You should see three things:
- The OLS coefficients have a large standard deviation across resamples. Each one can land far from 3, and they often have opposite signs.
- The OLS coefficients still sum to roughly 3. The model reliably learns the combined effect, but not how it is divided.
- The Ridge coefficients have a much smaller standard deviation. Ridge tends to give each correlated feature a similar share, roughly 1.5 each.
Detecting it
| Check | What to look for |
|---|---|
| Correlation matrix | Pairs with absolute correlation near 1 |
| Coefficients across folds or resamples | Large swings in sign or size |
| Variance inflation factor (VIF) | Values above about 5 to 10 suggest a problem |
A VIF measures how well one feature can be predicted from all the others. Pairwise correlation can miss a feature that is a combination of three others, which VIF catches.
Fixing it
- Drop or merge redundant features. This is the simplest fix and keeps the model easy to explain.
- Use Ridge, which shrinks coefficients and spreads weight across correlated features. Scale features first, because the penalty depends on feature size.
- Use Lasso or ElasticNet if you want some features set to zero. Lasso tends to pick one feature from a correlated group arbitrarily, and the pick can change between samples. ElasticNet is steadier because its Ridge part keeps the group together.
- Collect more data that breaks the correlation, if that is possible.
Exercise
In the experiment above, the OLS coefficients for x1 and x2 swing widely between resamples, yet their sum stays near 3. Why does the sum stay stable, and why does Ridge give smaller swings?
Show answer
Because x2 is nearly a copy of x1, only the sum of the two coefficients is pinned down by the data. Any split of that sum gives almost the same predictions, so OLS can pick very different splits on different samples. Ridge adds a penalty on the squared coefficients. For a fixed sum, that penalty is smallest when the weight is split evenly, so Ridge settles on a similar split each time.