Angad Sandhu
Search
Browse
On this page

2. Bias-Variance Tradeoff & Regularization ⭐⭐⭐

2. Bias-Variance Tradeoff & Regularization ⭐⭐⭐


Key Concepts

2.1 Bias-Variance Decomposition

The expected prediction error can be decomposed into three components:

Expected Error = Bias² + Variance + Irreducible Noise

E[(y - ŷ)²] = [E[ŷ] - f(x)]² + E[(ŷ - E[ŷ])²] + σ²
               └────────┘     └──────────────┘   └─┘
                  Bias²          Variance       Noise

Bias

Definition: Error from incorrect assumptions in the learning algorithm.

Bias = E[ŷ] - f(x)

High bias → Model is too simple → Underfitting

Characteristics:

  • Model cannot capture the true relationship
  • Consistently wrong predictions (systematic error)
  • Training AND test error are both high

Examples of high-bias models:

  • Linear regression on non-linear data
  • Shallow decision trees
  • Models with too few features
True function: y = x²
High-bias model: ŷ = ax + b (linear)

     │    ·  ·
     │  ·      ·
     │·    ────── ← Linear fit (high bias)
     │  ·      ·
     │    ·  ·
     └──────────

Variance

Definition: Error from sensitivity to small fluctuations in training data.

Variance = E[(ŷ - E[ŷ])²]

High variance → Model is too complex → Overfitting

Characteristics:

  • Model fits training data too closely
  • Different training sets → very different models
  • Low training error, high test error

Examples of high-variance models:

  • Deep decision trees (no pruning)
  • High-degree polynomial regression
  • KNN with k=1
High-variance model: fits every training point

     │    ·  ·
     │  ·  ╱╲  ·
     │·──╱  ╲──· ← Wiggly fit (high variance)
     │  ·      ·
     │    ·  ·
     └──────────

The Tradeoff

Model Complexity →

Error
  │
  │╲                    ╱
  │ ╲    Total Error   ╱
  │  ╲      ╱╲        ╱
  │   ╲    ╱  ╲      ╱
  │    ╲  ╱    ╲    ╱
  │     ╲╱      ╲  ╱
  │   Bias²      ╲╱ Variance
  │              ·
  │         Optimal
  └─────────────────────────
        Model Complexity →

Simple models: High bias, low variance
Complex models: Low bias, high variance

2.2 Overfitting and Underfitting

Underfitting (High Bias)

Symptoms:
- High training error
- High test error
- Training ≈ Test error

     Training Loss    Test Loss
          ████          ████
          ████          ████
          ████          ████
          ████          ████

Causes:

  • Model too simple
  • Not enough features
  • Too much regularization
  • Not trained long enough

Solutions:

  • Increase model complexity
  • Add more features
  • Reduce regularization
  • Train longer

Overfitting (High Variance)

Symptoms:
- Low training error
- High test error
- Large gap between training and test

     Training Loss    Test Loss
          █             ████
          █             ████
          █             ████
          █             ████

Causes:

  • Model too complex
  • Not enough training data
  • Training too long
  • No regularization

Solutions:

  • Simplify model
  • Get more data
  • Early stopping
  • Add regularization
  • Dropout (neural networks)
  • Data augmentation

Learning Curves

                    Overfitting              Good Fit               Underfitting
Loss                                                               
  │  ─── Train      │  ─── Train           │  ─── Train          │  ─── Train
  │  --- Test       │  --- Test            │  --- Test           │  --- Test
  │                 │                      │                     │
  │───              │───                   │───                  │────────────
  │   ───           │   ───────            │   ─────────         │────────────
  │      ─────      │                      │                     │
  │           ───   │                      │                     │
  │-----------      │-------               │---------            │
  │     -------     │                      │                     │
  └─────────────    └──────────            └───────────          └────────────
     Epochs            Epochs                 Epochs                Epochs
     
   Gap increases      Gap small              Gap small             Both high

2.3 Regularization

Regularization adds a penalty term to the loss function to constrain model complexity.

Regularized Loss = Original Loss + λ × Penalty

L_reg = L_data + λ × R(θ)

Where λ (lambda) controls regularization strength.

L2 Regularization (Ridge)

L_ridge = L_data + λ Σ wᵢ²

Penalty: Sum of squared weights

Effect on weights:

# Gradient update with L2
w_new = w - α(∂L/∂w + 2λw)
      = w(1 - 2αλ) - α(∂L/∂w)
        └────────┘
        Weight decay (shrinks toward 0)

Characteristics:

  • Shrinks all weights toward zero
  • Weights become small but rarely exactly zero
  • Handles correlated features by spreading weight among them
  • Closed-form solution exists

L1 Regularization (Lasso)

L_lasso = L_data + λ Σ |wᵢ|

Penalty: Sum of absolute weights

Effect on weights:

# Gradient update with L1
w_new = w - α(∂L/∂w + λ·sign(w))

# sign(w) = +1 if w > 0, -1 if w < 0

Characteristics:

  • Drives some weights exactly to zero
  • Performs feature selection
  • Produces sparse models
  • No closed-form solution (requires iterative methods)

Elastic Net

Combines L1 and L2:

L_elastic = L_data + λ₁ Σ |wᵢ| + λ₂ Σ wᵢ²

Or equivalently:
L_elastic = L_data + λ(α Σ |wᵢ| + (1-α) Σ wᵢ²)

Where α ∈ [0,1] balances L1 and L2

When to use:

  • Many correlated features
  • Want sparsity but also grouping effect
  • L1 alone selects one from correlated group; Elastic Net selects all

2.4 Why L1 Induces Sparsity (Geometric Intuition)

The key insight comes from the geometry of the constraint regions.

Constrained Optimization View

Regularization is equivalent to constrained optimization:

Minimize L_data(w)
Subject to: ||w||_p ≤ t

L1: ||w||₁ = Σ|wᵢ| ≤ t  (diamond constraint)
L2: ||w||₂ = √(Σwᵢ²) ≤ t  (circle constraint)

Geometric Picture (2D)

L2 Constraint (Circle):              L1 Constraint (Diamond):

      w₂                                   w₂
       │                                    │
       │    ╭───╮                          │    /\
       │   ╱     ╲                         │   /  \
       │  │   ·   │ ← Optimal              │  /    \
       │   ╲     ╱   (rarely on axis)      │ /   ·  \ ← Optimal
       │    ╰───╯                          │/        \  (often on axis!)
  ─────┼─────────── w₁                ─────┼──────────── w₁
       │                                   │\        /
       │                                   │ \      /
                                           │  \    /
                                           │   \  /
                                           │    \/

· = Loss function contours (ellipses)
Optimal = where contours touch constraint

Why the difference?

  • L2 (circle): Smooth boundary, contours typically touch at non-axis points
  • L1 (diamond): Corners on axes, contours likely to touch at corners where some wᵢ = 0

The diamond has "corners" exactly where coordinates are zero. The loss contours are more likely to first touch these corners, resulting in sparse solutions.

Mathematical Intuition

At the optimal point, the gradient of the loss must be balanced by the constraint:

L1: Gradient must overcome a constant "force" (λ·sign(w))
    - If gradient < λ, weight goes to exactly 0
    
L2: Gradient balanced by proportional force (2λw)
    - Weight shrinks but never reaches exactly 0

2.5 Curse of Dimensionality

As dimensions increase, several counterintuitive phenomena occur.

Volume Concentration

Most of the volume of a high-dimensional hypercube is in its corners:

Dimension d    Volume in corners (outside inscribed sphere)
    2              21%
    10             99.7%
    100            ~100%

Distance Concentration

In high dimensions, distances between points become nearly equal:

As d → ∞:
max_distance / min_distance → 1

All points are approximately equidistant!

Data Sparsity

The amount of data needed grows exponentially with dimensions:

To maintain same density:
- 1D: 10 points
- 2D: 10² = 100 points
- 10D: 10¹⁰ points!

Impact on ML Algorithms

AlgorithmImpact
KNNDistances meaningless, all neighbors equidistant
Decision TreesNeed exponentially more data to split
ClusteringClusters become indistinguishable
Density EstimationImpossible without massive data

Solutions:

  • Dimensionality reduction (PCA, t-SNE)
  • Feature selection
  • Regularization
  • Domain knowledge to select relevant features

2.6 Multicollinearity

Multicollinearity occurs when features are highly correlated.

Problems Caused

If x₁ ≈ x₂, then:
y = w₁x₁ + w₂x₂ + b

Many solutions work equally well:
- w₁ = 10, w₂ = 0
- w₁ = 5, w₂ = 5
- w₁ = 0, w₂ = 10

Results in:
- Unstable coefficients
- High variance in estimates
- Inflated standard errors
- Unreliable feature importance

Detection Methods

  1. Correlation matrix: Check pairwise correlations
  2. Variance Inflation Factor (VIF):
    VIF_j = 1 / (1 - R²_j)
    
    Where R²_j = R² from regressing xⱼ on all other features
    
    VIF > 5-10 indicates problematic collinearity

Solutions

MethodDescription
Remove featuresDrop one of correlated pair
PCATransform to uncorrelated components
Ridge regressionL2 handles collinearity well
Domain knowledgeCombine correlated features meaningfully

Questions & Answers

⭐ Q1: What is the bias-variance tradeoff? How does it affect model performance?

Answer:

The bias-variance tradeoff describes the fundamental tension between two sources of error:

Bias: Error from oversimplified assumptions

  • Model cannot capture true patterns
  • Leads to underfitting
  • High bias → consistently wrong predictions

Variance: Error from sensitivity to training data

  • Model captures noise as if it were signal
  • Leads to overfitting
  • High variance → predictions vary wildly with different training sets

The tradeoff:

Total Error = Bias² + Variance + Irreducible Noise
  • Simple models: High bias, low variance
  • Complex models: Low bias, high variance
  • Optimal model: Balances both

How it affects performance:

ScenarioTraining ErrorTest ErrorGap
High biasHighHighSmall
High varianceLowHighLarge
Good balanceModerateModerateSmall

Practical implications:

  • If train and test errors are both high → increase complexity
  • If train error low but test error high → reduce complexity or get more data
  • Goal: minimize test error, not training error

⭐ Q2: What are the differences between L1 and L2 regularization?

Answer:

AspectL1 (Lasso)L2 (Ridge)
PenaltyΣ|wᵢ|Σwᵢ²
GeometryDiamondCircle
SparsityYes (some weights = 0)No (weights → 0 but ≠ 0)
Feature selectionBuilt-inNo
Correlated featuresPicks one arbitrarilySpreads weight among all
Closed-form solutionNoYes
ComputationalHarder (non-differentiable at 0)Easier

When to use L1:

  • Want automatic feature selection
  • Believe only few features are relevant
  • Need interpretable sparse model

When to use L2:

  • All features likely relevant
  • Correlated features present
  • Want stable, smooth solution

When to use Elastic Net:

  • Many correlated features
  • Want sparsity but also grouping
  • Unsure which to choose

⭐ Q3: Why does L1 regularization favor sparse parameters?

Answer:

Two complementary explanations:

1. Geometric Intuition:

L1 constraint region is a diamond with corners on the axes. The optimal solution is where the loss contours first touch the constraint region. Due to the diamond's shape, this contact point is likely at a corner where some weights are exactly zero.

L1 Diamond:        L2 Circle:
    /\                ╭─╮
   /  \              │   │
  /    \             │   │
 /      \            ╰─╯
 
Corners on axes    No corners
→ sparse solutions  → dense solutions

2. Gradient Intuition:

L1 gradient is constant (±λ), while L2 gradient is proportional to weight:

L1: ∂R/∂w = λ·sign(w)  (constant push toward 0)
L2: ∂R/∂w = 2λw        (push proportional to w)

With L1, if the data gradient is smaller than λ, the weight is pushed all the way to zero. With L2, the push weakens as w approaches zero, so it never quite reaches zero.


Q4: How do you identify if a model is overfitting or underfitting?

Answer:

Diagnostic approach:

MetricUnderfittingOverfittingGood Fit
Training errorHighLowModerate
Validation errorHighHighModerate
Gap (val - train)SmallLargeSmall

Visual diagnostics:

  1. Learning curves: Plot train/val loss vs epochs

    • Underfitting: Both curves plateau high
    • Overfitting: Train decreases, val increases (divergence)
  2. Validation curves: Plot error vs model complexity

    • Find the sweet spot where val error is minimized

Practical checks:

# Check for overfitting
if train_accuracy > 0.99 and val_accuracy < 0.80:
    print("Likely overfitting")
    
# Check for underfitting  
if train_accuracy < 0.70 and val_accuracy < 0.70:
    print("Likely underfitting")

⭐ Q5: What is overfitting and how can it be prevented?

Answer:

Overfitting: Model learns training data too well, including noise, and fails to generalize to new data.

Signs:

  • Excellent training performance
  • Poor test/validation performance
  • Model is too complex for the data

Prevention strategies:

StrategyHow it helps
More training dataHarder to memorize, must learn patterns
Regularization (L1/L2)Penalizes complex models
DropoutPrevents co-adaptation in neural networks
Early stoppingStop before overfitting occurs
Cross-validationBetter estimate of generalization
Data augmentationArtificially increase data diversity
Simpler modelReduce capacity to memorize
Ensemble methodsAverage out individual model errors
Batch normalizationRegularization effect

Practical workflow:

  1. Start with a model that can overfit (proves it can learn)
  2. Add regularization until validation improves
  3. Use early stopping based on validation loss
  4. If still overfitting, simplify architecture or get more data

Q6: What is the Curse of Dimensionality, and how does it impact ML algorithms?

Answer:

Definition: As dimensionality increases, data becomes increasingly sparse, and many intuitions from low dimensions break down.

Key phenomena:

  1. Volume explosion: Data points become isolated

    • Need exponentially more data to maintain density
    • 10 points per dimension → 10^d total points needed
  2. Distance concentration: All distances become similar

    • max_dist / min_dist → 1 as d → ∞
    • Nearest neighbor becomes meaningless
  3. Corner concentration: Most volume is in corners

    • Uniform sampling misses the "middle"

Impact on algorithms:

AlgorithmProblem
KNNAll points equidistant, neighbors meaningless
K-meansClusters indistinguishable
Density estimationRequires impossible amounts of data
Decision treesExponentially more splits needed

Solutions:

  • Dimensionality reduction (PCA, autoencoders)
  • Feature selection (remove irrelevant features)
  • Regularization (constrain model complexity)
  • Domain knowledge (select meaningful features)

Q7: How do you handle collinearity in features?

Answer:

Detection:

  1. Correlation matrix: |r| > 0.8 is concerning
  2. VIF > 5-10 indicates multicollinearity
  3. Unstable coefficients across different samples

Solutions:

MethodWhen to use
Remove one featureClear redundancy, interpretability needed
Ridge regressionWant to keep all features, prediction focus
PCATransform to uncorrelated components
Combine featuresDomain knowledge suggests meaningful combination
Elastic NetWant some sparsity with correlated features

Example:

# If height_cm and height_inches are both features:
# Option 1: Remove one
# Option 2: Use Ridge regression
# Option 3: Create single height feature

# VIF calculation
from statsmodels.stats.outliers_influence import variance_inflation_factor
vif = [variance_inflation_factor(X, i) for i in range(X.shape[1])]

Key insight: Collinearity doesn't hurt prediction accuracy, but makes coefficient interpretation unreliable. If you only care about predictions, Ridge regression handles it well. If you need interpretable coefficients, remove correlated features.

Browse the library