Collinearity & Multicollinearity Explained: Correlation Matrix, VIF & Dummy Variable Trap
Why redundant features flip regression weight signs and explode standard errors, how to compute VIF, and how Ridge/Lasso/PCA fix it
Collinearity occurs when two input features move in lockstep (like height in cm and height in inches), while Multicollinearity happens when one feature can be predicted accurately from a combination of two or more other features.
In Linear and Logistic Regression, multicollinearity makes learned weights wildly unstable, flips coefficient signs (saying bigger houses cost less!), and destroys statistical significance — even when overall accuracy looks fine.
Multicollinearity is the #1 reason Linear/Logistic Regression coefficients look nonsensical to business stakeholders.
Perfect multicollinearity makes XᵀX singular (non-invertible), crashing unregularized closed-form OLS solvers.
Whenever you report feature weights ('each extra bedroom adds $25k'), checking VIF first prevents embarrassing sign-flip errors.
Top Data Science interview question: 'Does multicollinearity hurt predictions, or interpretability? How do you detect 3-way multicollinearity that a correlation heatmap misses?'
Feature C equals Feature A + Feature B, yet pairwise correlations corr(A, C) and corr(B, C) are only 0.70. Which diagnostic catches this hidden 3-way multicollinearity?
- Distinguish pairwise Collinearity (2 correlated columns) from Multicollinearity (3+ columns forming a hidden linear combination).
- Compute and interpret the Variance Inflation Factor (VIF = 1 / (1 − R_j²)) using standard thresholds (<5 safe, 5–10 caution, >10 severe).
- Recognize the One-Hot Dummy Variable Trap as a special case of perfect multicollinearity.
- Know which models suffer (OLS Linear/Logistic Regression) vs. which are mostly unaffected in prediction (Ridge, Lasso, Random Forests, XGBoost).
When two features carry the same information, OLS Linear Regression has infinitely many ways to split the credit between them (+10 and 0, or +1,000 and -990). Tiny noise in your training sample picks a wild arbitrary split — predictions stay accurate, but individual weights become garbage!
1. Collinearity vs. Multicollinearity & The Dummy Variable Trap
Suppose a dataset has salary_usd and salary_eur, or One-Hot columns is_male and is_female alongside an intercept 1. Because is_male + is_female = 1 on every row, one column is 100% redundant!
- Collinearity: Two features are strongly correlated (). Caught by a correlation matrix.
- Multicollinearity: One feature is a linear combination of 2 or more other features (e.g.,
total_spend = food_spend + rent_spend + fun_spend). Pairwise correlations can look moderate while VIF explodes to ! - Dummy Variable Trap: Including all One-Hot columns plus an intercept creates exact multicollinearity. Fix: set
drop_first=Truefor unregularized OLS.
2. How to Detect & Fix Multicollinearity
VIF Thresholds
- VIF = 1: Zero correlation with other features.
- VIF 1–5: Moderate, usually harmless.
- VIF > 5–10: High multicollinearity — take action if interpreting coefficients!
4 Proven Fixes
- Drop or Combine Redundant Columns: Keep
sqftand dropsquare_meters, or create a single ratio/sum. - Use Ridge (L2) Regularization: Adds to , splitting weights smoothly across correlated features (next chapter!).
- Use Lasso (L1) or ElasticNet: Automatically zeros out redundant duplicates.
- Apply PCA: Rotates correlated columns into 100% orthogonal (VIF = 1) principal components.
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Predictions are almost completely unaffected because trees just pick one of the correlated columns at each split. However, feature importance gets diluted/split across the correlated duplicates!
Sweep λ from 0 → 3: Watch Lasso Drop Noisy Weights to Exact Zero
Toggle Lasso (L1 Sparsity) vs. Ridge (L2 Smooth Shrinkage) while the path sweeps live!
1 / (1 − R_j²)) to catch hidden multi-feature redundancy, drop duplicate columns or apply drop_first=True for OLS, or switch to Ridge/ElasticNet (coming up next!).import numpy as np
# Compute VIF (Variance Inflation Factor) From Scratch for Every Feature
rng = np.random.default_rng(0)
N = 200
x1 = rng.normal(size=N)
x2 = rng.normal(size=N)
x3 = 0.9 * x1 + 0.4 * x2 + rng.normal(scale=0.05, size=N) # multicollinear combo!
X = np.column_stack([x1, x2, x3])
def compute_vif(X_mat: np.ndarray) -> list[float]:
vifs = []
for j in range(X_mat.shape[1]):
y_j = X_mat[:, j]
X_other = np.column_stack([np.ones(len(y_j)), np.delete(X_mat, j, axis=1)])
beta, *_ = np.linalg.lstsq(X_other, y_j, rcond=None)
pred = X_other @ beta
r2 = 1.0 - np.sum((y_j - pred) ** 2) / np.sum((y_j - y_j.mean()) ** 2)
vifs.append(round(float(1.0 / max(1e-9, 1.0 - r2)), 2))
return vifs
for name, vif in zip(["x1", "x2", "x3 (combo!)"], compute_vif(X)):
print(f"{name:12s} | VIF = {vif}")