Ridge Regression for Correlated Predictors
By MCP Analytics Team | Published , updated
Multicollinearity can make regression coefficients unstable. Ridge regression with L2 regularization addresses this, but only if you avoid the implementation pitfalls that lead to the wrong regularization strength. The worked example below uses made-up data and prints its numbers, including a case where ridge did not beat ordinary least squares.
Worked Example (Simulated): Does Ridge Lower Test Error When Two Inputs Share a Signal?
Question: Does ridge lower test error when two input features share a signal?
Inputs: Simulated data, seed 17. The script makes 120 made-up rows with three input features. The first two share a hidden signal. The third is made on its own. The target is 2 times the first input plus 2 times the second, minus the third, plus random noise (SD 1.5). The script keeps 90 rows to fit and tune the model, then uses 30 held-out rows to compare error. It needs numpy only.
Method: The script scales each input using the fit rows. It picks alpha (the penalty strength, called lambda elsewhere on this page) by five-fold cross-validation on those rows. It leaves the intercept out of the penalty; with centred inputs the intercept equals the target mean. It then fits OLS and ridge on the fit rows and compares mean squared error on the held-out rows. Mean squared error is the mean of squared gaps between each guess and the true value.
import numpy as np
SEED = 17
rng = np.random.default_rng(SEED)
n = 120
z = rng.normal(size=n)
x1 = z + 0.25 * rng.normal(size=n)
x2 = 0.9 * z + 0.35 * rng.normal(size=n)
x3 = rng.normal(size=n)
X = np.column_stack([x1, x2, x3])
y = 2.0 * x1 + 2.0 * x2 - 1.0 * x3 + rng.normal(scale=1.5, size=n)
perm = rng.permutation(n)
train_idx, test_idx = perm[:90], perm[90:]
X_train, X_test = X[train_idx], X[test_idx]
y_train, y_test = y[train_idx], y[test_idx]
# Use five fixed folds to pick alpha. Scale within each fold.
folds = np.array_split(np.arange(len(train_idx)), 5)
alphas = [0.01, 0.1, 1.0, 10.0, 100.0]
cv_mse = []
for alpha in alphas:
fold_errors = []
for val in folds:
fit = np.setdiff1d(np.arange(len(train_idx)), val)
mean_fold = X_train[fit].mean(axis=0)
scale_fold = X_train[fit].std(axis=0)
A = (X_train[fit] - mean_fold) / scale_fold
V = (X_train[val] - mean_fold) / scale_fold
mu_y = y_train[fit].mean()
beta = np.linalg.solve(
A.T @ A + alpha * np.eye(A.shape[1]),
A.T @ (y_train[fit] - mu_y),
)
pred = mu_y + V @ beta
fold_errors.append(np.mean((y_train[val] - pred) ** 2))
cv_mse.append(np.mean(fold_errors))
best_alpha = alphas[int(np.argmin(cv_mse))]
# Refit after scaling with all fit rows. The intercept is the fit target mean.
mean = X_train.mean(axis=0)
scale = X_train.std(axis=0)
Z_train = (X_train - mean) / scale
Z_test = (X_test - mean) / scale
mu_y = y_train.mean()
ols_beta = np.linalg.lstsq(Z_train, y_train - mu_y, rcond=None)[0]
ridge_beta = np.linalg.solve(
Z_train.T @ Z_train + best_alpha * np.eye(Z_train.shape[1]),
Z_train.T @ (y_train - mu_y),
)
ols_mse = np.mean((y_test - (mu_y + Z_test @ ols_beta)) ** 2)
ridge_mse = np.mean((y_test - (mu_y + Z_test @ ridge_beta)) ** 2)
print(f"seed={SEED}; rows={n}; predictors=3; train={len(train_idx)}; "
f"test={len(test_idx)}")
print("predictors: x1 and x2 share a latent signal; x3 is independent")
print("target: 2*x1 + 2*x2 - x3 + Gaussian noise (SD 1.5)")
print(f"selected alpha={best_alpha:g}; 5-fold CV MSE={min(cv_mse):.3f}")
print(f"held-out test MSE: OLS={ols_mse:.3f}; ridge={ridge_mse:.3f}")
print("ridge standardized coefficients="
+ np.array2string(ridge_beta, precision=3, separator=", "))
Printed output:
seed=17; rows=120; predictors=3; train=90; test=30
predictors: x1 and x2 share a latent signal; x3 is independent
target: 2*x1 + 2*x2 - x3 + Gaussian noise (SD 1.5)
selected alpha=10; 5-fold CV MSE=2.130
held-out test MSE: OLS=2.366; ridge=2.368
ridge standardized coefficients=[ 2.069, 2.15 , -0.922]
Result: Cross-validation chose alpha 10. On the 30 held-out rows ridge (2.368) did not beat OLS (2.366); with only 3 inputs and 90 fit rows there was little variance to remove. Ridge helps most when inputs are many or strongly linked.
Limit: This is one small simulated data set with a known setup. It does not show that ridge will do better, or worse, on real data.
Sources
- Hoerl A, Kennard R (1970). Ridge Regression: Biased Estimation for Nonorthogonal Problems. Technometrics. doi:10.1080/00401706.1970.10488634. Covers: ridge regression.
- James G, Witten D, Hastie T, Tibshirani R. An Introduction to Statistical Learning, 2nd ed. statlearning.com. Covers: ridge, the intercept left out of the penalty, standardizing inputs, bias and variance, and choosing the penalty by cross-validation.
What do we believe before looking at any data? That regularization helps prevent overfitting. The fuller picture is that the benefit depends on choosing the right lambda penalty. Get it wrong, and you're either back to unstable ordinary least squares (lambda too small) or you've shrunk all signal away (lambda too large).
Where Ordinary Regression Fails
Let's quantify our uncertainty about when ordinary least squares (OLS) breaks down. Three warning signs are worth checking:
To see how much your own drivers explain an outcome, and which matter most, try the free linear regression tool.
- Multicollinearity: A high variance inflation factor (VIF) can flag strong links among input features. There is no single cut-off that makes OLS estimates unreliable in every setting; read VIF in context. Ridge reduces coefficient variance in these cases by shrinking the estimates.
- High-dimensional curse: When the number of features is a sizable fraction of the number of observations (e.g., 30 features with 100 observations), OLS tends to overfit. Ridge's penalty limits how far the fit can chase noise.
- Coefficient instability: When adding or removing a single correlated feature flips coefficient signs, your model isn't learning generalizable patterns. Ridge makes the coefficients less sensitive to such changes.
Here's what these failures can look like in practice. Illustrative example with made-up numbers. Suppose an e-commerce team builds a conversion rate model with features for page load time, image count, product description length, and review score. Page load time and image count are highly correlated (more images = slower loading). Suppose OLS suggests that faster load times decrease conversions, a nonsensical result caused by multicollinearity. Ridge regression with a suitably chosen lambda would typically pull that coefficient back toward a plausible sign.
Ridge penalizes the magnitude of coefficients, so a feature measured in dollars gets penalized differently than one measured in percentages. Always transform features to mean=0, variance=1 before applying Ridge. The penalty term becomes meaningless without standardization.
How L2 Regularization Updates Your Coefficient Beliefs
Ridge regression modifies the ordinary least squares objective function. Instead of minimizing just the sum of squared residuals, it minimizes:
Loss = Sum of Squared Residuals + lambda * Sum of Squared Coefficients
This penalty term is called L2 regularization or "shrinkage." Here's the Bayesian interpretation: ridge is the posterior mean under a zero-centred Gaussian prior on the coefficients with Gaussian noise (ISLR). Lambda controls the prior's precision: higher lambda means stronger belief that coefficients should be small.
What does this mean for your posterior beliefs about coefficients? Ridge shrinks them toward zero, but never exactly to zero. Large coefficients pay a bigger penalty (squared term), so Ridge shrinks large coefficients more aggressively than small ones. This proportional shrinkage stabilizes estimates without eliminating any features entirely.
The Math Behind Shrinkage
In general, the Ridge coefficients are the matrix solution:
β_ridge = (XᵀX + lambda·I)⁻¹ Xᵀy
Each coefficient depends on the whole design matrix, including how the features correlate with each other, which is exactly how Ridge stabilizes correlated features. Only in the special case of orthogonal (uncorrelated) features, scaled so that XᵀX = I, does this simplify to a per-feature formula:
β_ridge[j] = β_ols[j] / (1 + lambda)
That special case shows the shrinkage factor plainly: as lambda increases, coefficients shrink toward zero. When lambda=0, Ridge becomes ordinary least squares. When lambda approaches infinity, all coefficients approach zero. The optimal lambda balances these extremes.
Ridge can lower the spread of estimates across samples when inputs are strongly linked, at the cost of bias (ISLR). How to build an interval for a ridge estimate is a separate question.
Three Common Lambda Selection Mistakes
Choosing lambda is where implementations commonly go wrong. Three mistakes to avoid:
Mistake 1: Using Default Lambda Values
A fixed default lambda may work, but the best lambda depends on the data and on how the inputs are scaled. Select lambda by cross-validation instead.
Mistake 2: Testing Too Narrow a Lambda Range
A narrow grid like [0.1, 0.5, 1, 5, 10] can miss the best value. Test a broad log-spaced range that fits your model and data. If the best value is at either end, widen the range and search again.
Mistake 3: Not Using Cross-Validation
Choosing lambda based on training set performance is circular reasoning: you're using the data to choose a penalty that affects how you use the data. Always use k-fold cross-validation (k=5 or k=10) to select lambda based on held-out prediction error.
Before cross-validation, your prior might be "lambda around 1 probably works." After testing lambdas [0.001, 0.01, 0.1, 1, 10, 100, 1000] with 10-fold CV, you might find lambda=10 minimizes prediction error. How confident should you be? Look at the cross-validation curve, which estimates held-out prediction error at each lambda. If error is similar for lambda in [1, 10], the data does not pin down a single best value, and any lambda in that flat region performs about as well. If there's a sharp minimum at 10, the choice matters more. Remember the curve is itself an estimate with noise across folds, so a small difference between neighboring lambdas may not be real. (Posterior distributions and credible intervals over lambda require a Bayesian model; see Bayesian Ridge below.)
Ridge vs Lasso vs Elastic Net: Which to Use
Should you use Ridge (L2), Lasso (L1), or Elastic Net (combination)? The choice follows from what you believe about your features:
| Scenario | Usually Favored | Why |
|---|---|---|
| Moderate or severe multicollinearity, features mostly relevant | Ridge | Keeps correlated features and stabilizes them |
| Many irrelevant features | Lasso | Sets some coefficients exactly to zero |
| High-dimensional (features > observations) | Elastic Net | Combines selection with stabilization of correlated groups |
| All features somewhat relevant | Ridge | Uses all the information, shrunk toward zero |
Ridge is the principled choice when you believe most features contribute something and multicollinearity is present. Lasso is better when you believe many features are pure noise and want automatic feature selection. Elastic Net hedges between these priors. Compare candidates on held-out data rather than trusting the table.
For business applications like pricing models, customer lifetime value prediction, and demand forecasting, features are typically correlated but relevant: price correlates with product quality, customer age correlates with tenure, past sales correlate with seasonality. You want to use all the information, just stabilized.
What Ridge Regression Looks Like in Practice
Illustrative example with made-up numbers. Let's walk through a concrete scenario: predicting customer churn for a subscription business. Suppose you have 15 features including account age, monthly spend, support tickets, feature usage metrics, and payment method. Several features are correlated: higher monthly spend correlates with more feature usage, longer tenure correlates with fewer support tickets.
Step 1: Check for Multicollinearity
Calculate variance inflation factors for each feature. Suppose several VIF values are high compared with the rest, which flags strongly linked features and points toward trying Ridge alongside OLS.
Step 2: Standardize Features
Transform all 15 features to have mean=0 and standard deviation=1. Without this step, features like "monthly spend" ($50-$500) would be penalized differently than "support tickets" (0-10), making the L2 penalty meaningless.
Step 3: Cross-Validate Lambda Selection
Test lambda values [0.001, 0.01, 0.1, 1, 10, 100] using 10-fold cross-validation, then refine around the best region with a finer grid such as [0.5, 1, 2.5, 5, 7.5]. Suppose prediction accuracy (AUC for classification) peaks at lambda=2.5 on the finer grid. The cross-validation curve shows similar performance for lambda in [1, 5], so any value in that range is a reasonable choice; it is a range of similar held-out performance, not a credible interval.
Step 4: Fit Final Model and Interpret
Fit Ridge regression with lambda=2.5 on the full training set. The coefficients are all non-zero (Ridge doesn't eliminate features), but they're shrunk compared to OLS. The coefficient for "monthly spend" is positive but smaller in magnitude than OLS suggested, because Ridge accounts for its correlation with feature usage.
Suppose that when you compare predictions on a held-out test set, Ridge achieves 0.82 AUC versus 0.75 for OLS. That would be a 7 percentage point improvement, and you would check whether it holds up on your own data before relying on it.
Cases Where Selective Penalization May Fit
Here's a nuance: you don't always want to penalize every coefficient equally. Three cases to think about:
1. Intercept Term
Ridge leaves the intercept out of the penalty (ISLR). Penalizing it would shift all predictions up or down with lambda. With centred inputs the intercept equals the target mean, as in the worked example above. Check how your own code handles it.
2. Causal Aims
A belief about cause alone does not show that a fitted coefficient is causal. Decide whether the aim is prediction or a causal effect, and whether the study design supports that aim. If a causal effect is the aim, use a method that fits the study design and the target.
3. Treatment Indicators in A/B Tests
When using Ridge to analyze experiment results with covariates, one option is to leave the treatment indicator out of the penalty so its estimate is not shrunk, and apply the penalty only to the control variables.
Selective penalization may need manual handling: exclude those terms from standardization and the penalty, or use a package that allows per-feature penalties.
When Ridge Regression Makes Wrong Predictions (And How to Detect It)
Ridge isn't magic. It can still fail. Three scenarios where Ridge predictions can be unreliable:
Scenario 1: Non-Linear Relationships
Ridge is still linear regression. If the true relationship is non-linear, Ridge won't capture it. On data with strong quadratic or interaction effects, tree-based methods can outperform it. Check for non-linearity with residual plots. If you see clear patterns (curved residuals vs. fitted values), add polynomial terms or interaction features before applying Ridge.
Scenario 2: Outliers and Leverage Points
Ridge minimizes squared errors, so outliers still have disproportionate influence. A single extreme value can shift all the Ridge coefficients. Use robust scaling (median and interquartile range instead of mean and standard deviation) or remove statistical outliers before fitting Ridge.
Scenario 3: Changing Relationships Over Time
Ridge assumes the links between inputs and outcomes stay the same. If they change over time, a model fit to past data may make poor new guesses. Check its error on newer data.
Ridge reduces variance (coefficient instability) but introduces bias (coefficients shrunk toward zero). For small datasets, this tradeoff often favors Ridge, because lower variance can outweigh small bias. For very large datasets with weak multicollinearity, OLS might be as good. With little multicollinearity and plenty of data, test both and compare cross-validation error.
Advanced Technique: Bayesian Ridge Regression
Standard Ridge requires you to choose lambda, for example by cross-validation. Bayesian Ridge is a related approach: it puts a prior distribution on the model terms, then uses the data to form a posterior distribution. A prior is an assumption made before the data; a posterior is the updated distribution after the data. The details depend on the model and software.
Ridge Regression for Business Decisions: Three Hypothetical Scenarios
Let's examine how Ridge regression could update beliefs and improve decisions in business contexts. These scenarios are hypothetical, and every number in them is made up for illustration.
Scenario 1: E-commerce Price Optimization
Suppose a fashion retailer has 28 features predicting conversion rate (price, discount, inventory level, seasonality, competitor prices, customer segment, etc.). Suppose OLS produces unstable coefficients: the price elasticity estimate ranges from -0.3 to -2.1 depending on which correlated features are included.
Suppose Ridge regression with lambda=4.2 stabilizes the price elasticity estimate at -0.85 with a bootstrap interval of [-1.1, -0.6]. That would mean a 10% price increase reduces conversion by about 8.5%, and the shrunk estimate stays roughly between 6% and 11% across resamples (a statement about stability, not an interval for the true effect), which is stable enough to look for a revenue-maximizing price point.
Scenario 2: SaaS Customer Lifetime Value Prediction
Suppose a B2B SaaS company wants to predict customer lifetime value (LTV) from signup features. They have 45 predictors including company size, industry, product tier, onboarding behavior, and initial usage metrics, many highly correlated.
Ridge with cross-validation would give stable coefficient estimates that a sales team could trust. If the coefficient for "completed onboarding checklist" stays positive across model variations, that supports investing in onboarding improvements, though a Ridge coefficient alone does not establish causation.
Scenario 3: Healthcare Risk Adjustment
Suppose a health insurance provider predicts patient costs from 120 diagnosis codes, demographics, and prior utilization. The diagnosis codes are heavily correlated (diabetes correlates with hypertension, cancer correlates with age, etc.).
OLS can produce negative coefficients for serious conditions, a nonsensical result from multicollinearity. Ridge regression with a lambda chosen by cross-validation would tend to produce coefficients with clinically sensible signs, which matters when predictions feed risk adjustment.
Implementation Checklist: Getting Ridge Regression Right
Here's a step-by-step checklist for reliable Ridge regression:
- Assess multicollinearity: Calculate VIF for each feature. If some are high, Ridge may help. If all are low and you have plenty of data, OLS may be sufficient; fit both and compare.
- Standardize features: Transform to mean=0, variance=1. Use the training set's mean and standard deviation, then apply the same transformation to test data.
- Exclude intercept from penalty: Verify your implementation doesn't regularize the intercept term.
- Test wide lambda range: Use a broad log-spaced grid, and widen it if the best value sits at an end.
- Use cross-validation: Use k-fold cross-validation (ISLR). Select the lambda that minimizes mean cross-validation error.
- Check residual plots: Plot residuals vs. fitted values. If you see patterns, add polynomial or interaction terms.
- Evaluate on held-out data: After selecting lambda, fit on training data and evaluate on a completely separate test set. Compare to OLS baseline.
- Interpret coefficients carefully: Remember they're shrunk toward zero. Relative magnitudes are more meaningful than absolute values.
- Monitor over time: Check error on newer data and retrain when prediction accuracy degrades.
Ridge is optimized for prediction accuracy, not unbiased coefficient estimation. The coefficients are deliberately biased toward zero. If your goal is causal inference (measuring the true effect of X on Y), Ridge may give misleading estimates. Use Ridge for prediction; use causal inference methods (instrumental variables, difference-in-differences, etc.) for measuring effects.
The Uncertainty Interval Around Your Ridge Coefficients
Let's quantify our uncertainty about Ridge coefficient estimates. Unlike OLS, which has simple closed-form standard errors, Ridge standard errors require more careful handling because the penalty introduces bias.
One way to look at stability is the bootstrap: resample your data many times, fit Ridge with your chosen lambda on each resample, and look at the spread of each coefficient across resamples.
Bootstrap intervals describe sampling variability around the shrunk estimates. Because Ridge is deliberately biased toward zero, they are not the same as intervals for the true coefficient, so treat them as a measure of stability rather than a guarantee of coverage.
Your turn
Bring your own data and the question you actually need answered.
MCP AnalyticsData science by email Send us your data and your question, and we will reply. ds@mcpanalytics.ai
Frequently Asked Questions
Ridge regression uses L2 regularization (sum of squared coefficients) which shrinks all coefficients toward zero but never exactly to zero. Lasso uses L1 regularization (sum of absolute coefficients) which can shrink coefficients to exactly zero, performing feature selection.
Ridge is better when you believe all features contribute something; Lasso when you suspect many features are irrelevant. With correlated predictors, correlated features still contain information, so Ridge keeps them and stabilizes them, whereas Lasso tends to keep one from a correlated group and drop the others.
Use cross-validation to find the optimal lambda. Test a broad log-spaced range and widen it if the best value is at either end. The best lambda minimizes cross-validation prediction error.
Your cross-validation results give the principled answer for your specific dataset. Look at the CV curve to judge how sharply the choice matters: if error is similar across [1, 10], any lambda in that range performs about as well on held-out data.
Use Ridge when you have multicollinearity (correlated predictors), when the number of predictors approaches the number of observations, or when ordinary least squares produces unstable coefficients.
If your variance inflation factors are high, or if coefficients flip signs when you add/remove variables, Ridge regression is the principled choice. For large datasets with weak multicollinearity, OLS might be sufficient.
Yes, absolutely. Ridge regression penalizes the magnitude of coefficients, so features on different scales will be penalized differently. Always standardize features (mean 0, variance 1) before applying Ridge.
Skipping this step is an easy implementation mistake. The penalty term becomes meaningless without standardization. A feature measured in dollars shouldn't be penalized differently than one measured in percentages just because of unit choice.
Yes, but encode them properly first. Use one-hot encoding or dummy variables to convert categories to numeric features. Then standardize all features including the encoded categorical variables.
Ridge will shrink the coefficients of all dummy variables together. For high-cardinality categoricals (many levels), Ridge's shrinkage helps prevent overfitting to rare categories: a common problem in customer segmentation models. Just remember to apply the same encoding and scaling to test data using the training set's parameters.
What to Take Away About Ridge Regression
Before reading this guide, you might have believed "regularization probably helps with overfitting." Here is what that belief should become:
- Ridge helps most when multicollinearity is present (a high VIF is one flag) or when features approach observations in number. Confirm the gain on your own held-out data.
- Lambda selection by cross-validation is essential. Default or arbitrary lambda values can leave prediction accuracy on the table.
- Feature standardization is non-negotiable. Without standardization, the penalty term is arbitrary.
- Ridge suits correlated but relevant features, as in pricing, LTV prediction, demand forecasting, and risk scoring. Lasso suits data with many irrelevant features.
- Ridge coefficients give stable magnitudes, unlike OLS coefficients which can flip signs and explode in magnitude under multicollinearity.
The practical conclusion: Ridge regression isn't a niche technique for machine learning experts. It's a sensible baseline for any business regression problem with correlated features. Fit it, tune lambda by cross-validation, and compare against OLS on held-out data before you trust it.