Generalization - Overfitting Vs Underfitting - Bias Vs Variance

 

🧠 1. What is Generalization?

πŸ”Ή Definition

Generalization is the ability of a machine learning model to perform well on unseen data, not just the data it was trained on.

Formally:

A model generalizes well if its prediction error on new data is low.


πŸ“Š Training vs Generalization Error

  • Training error → error on training data
  • Generalization error (test error) → error on unseen data

πŸ‘‰ A good model:

  • Low training error ✅
  • Low generalization error ✅

🎯 Core Objective of Machine Learning

We are NOT trying to minimize training error alone.
We want to minimize:

Expected Error on New Data\text{Expected Error on New Data}

This is often called:

  • True risk
  • Expected loss

⚠️ 2. What is Overfitting?

πŸ”Ή Definition

Overfitting occurs when a model learns:

  • the underlying pattern ✅
  • AND noise/random fluctuations ❌

πŸ” Intuition

Imagine fitting a curve:

  • Simple model → misses pattern → underfitting
  • Moderate model → captures pattern → good generalization
  • Very complex model → fits every point → overfitting

πŸ“‰ Key Symptom

MetricBehavior
Training errorVery low
Validation/Test errorHigh

πŸ“ˆ Conceptual Visualization

  • Training error ↓ continuously
  • Validation error:
    • ↓ initially
    • then ↑ (after a point)

πŸ‘‰ That turning point = start of overfitting


🧩Why Overfitting Happens

πŸ”Ή 1. Model too complex

  • Too many parameters
  • High flexibility

Examples:

  • Deep neural networks
  • High-degree polynomial regression

πŸ”Ή 2. Small dataset

  • Not enough data to represent true distribution

πŸ”Ή 3. Noise in data

  • Model learns random variations

πŸ”Ή 4. Too much training

  • Especially in neural networks

πŸ”Ή 5. Data leakage

  • Model accidentally sees test information

πŸ§ͺDetecting Overfitting

πŸ” Using validation set:

  • Training accuracy ↑
  • Validation accuracy ↓

πŸ” Learning curves:

Plot:

  • Training error
  • Validation error

Interpretation:

  • Large gap → overfitting
  • Both high → underfitting

🧠 3. What is Underfitting?

πŸ”Ή Definition

Underfitting occurs when a model is too simple to capture the underlying structure of the data.

The model fails to learn even the basic patterns.


πŸ“Š  Key Characteristics

Metric            Behavior
Training error                High ❌
Validation/Test error            High ❌

πŸ‘‰ Unlike overfitting, the model performs poorly everywhere.


πŸ” Intuition

Imagine trying to fit a straight line to data that clearly follows a curve:

  • The model is too rigid
  • It cannot adapt to the pattern
  • Result → large errors

πŸ“‰ Conceptual Visualization

Think in terms of model complexity:

Model ComplexityBehavior
Very low            Underfitting
Moderate            Good generalization
Very high            Overfitting



🧩  Causes of Underfitting


πŸ”Ή 1. Model Too Simple

Examples:

  • Linear model for nonlinear data
  • Shallow decision tree

πŸ”Ή 2. Insufficient Features

  • Important variables missing
  • Poor feature representation

πŸ”Ή 3. Too Much Regularization

  • Model is overly constrained

πŸ”Ή 4. Inadequate Training

  • Not trained long enough
  • Optimization not converged

πŸ”Ή 5. Poor Data Representation

  • Lack of feature engineering
  • Raw data not informative enough

πŸ§ͺ  Detecting Underfitting


πŸ” Learning Curves

  • Training error → high
  • Validation error → high
  • Gap between them → small

πŸ‘‰ Key insight:

Model cannot even fit training data


πŸ” Performance Indicators

  • Low accuracy on training data
  • High loss values
  • Predictions too simplistic

πŸ› ️ How to Fix Underfitting


πŸ”Ή 1. Increase Model Complexity

  • Use more powerful models:
    • Polynomial regression instead of linear
    • Deeper neural networks

πŸ”Ή 2. Add Better Features

  • Feature engineering
  • Domain knowledge

πŸ”Ή 3. Reduce Regularization

  • Lower penalty strength

πŸ”Ή 4. Train Longer

  • Increase epochs (for neural networks)

πŸ”Ή 5. Use Nonlinear Models

  • Kernel methods
  • Tree-based models

🧠Intuitive Analogy (Teaching Friendly)

Student analogy:

  • Underfitting = student didn’t study enough
  • Cannot answer even basic questions

⚖️Bias–Variance Tradeoff

This is the theoretical backbone of generalization.


πŸ”Ή Bias

  • Error due to overly simple assumptions
  • High bias → underfitting

πŸ”Ή Variance

  • Error due to sensitivity to training data
  • High variance → overfitting

πŸ“Š Tradeoff

Model Type        Bias        Variance
Simple        High        Low
Complex        Low        High

🎯 Goal:

Find a balance:

Low bias + Low variance = Good generalization



 



πŸ“Mathematical View 

The terms bias and variance come from statistics, and the names describe what type of error the model is making.

Bias: Why is it called "bias"?

Bias means a systematic tendency toward a particular direction. A model with high bias consistently makes similar mistakes because of strong assumptions.

Mathematically:

Bias=E[f^(x)]f(x)Bias = E[\hat{f}(x)] - f(x)

where:

  • f(x)f(x) = true function
  • f^(x)\hat{f}(x) = predicted function
  • E[f^(x)]E[\hat{f}(x)] = average prediction over many training datasets

If the average prediction is consistently away from the true value, the model is biased.

Example

Suppose actual relationship:

y=x2y=x^2

but we force a linear model:

y=mx+by=mx+b

No matter how many datasets we use, the model keeps trying to fit a straight line to a curve.

The error occurs because the model is biased toward linearity.

Hence it is called bias.

Think of a dartboard:

  • All arrows land together but away from the center.
  • Predictions are consistently shifted in one direction.
Center (true value)

X X X
X X X ← tightly grouped
O ← true value

The model repeatedly misses the target in the same way.


Variance: Why is it called "variance"?

Variance means how much something changes or spreads out.

Mathematically:

Variance=E[(f^(x)E[f^(x)])2]Variance = E[(\hat{f}(x)-E[\hat{f}(x)])^2]

It measures how much predictions vary if we train the model on different datasets.

Example

Suppose we collect slightly different training samples:

Dataset 1 → model predicts 20
Dataset 2 → model predicts 35
Dataset 3 → model predicts 12
Dataset 4 → model predicts 40

Predictions change drastically.

The model is sensitive to small changes in data.

Hence it is called variance because predictions have large variability.

Dartboard analogy:

X       X

O

X X

Predictions are scattered around.


Why both terms together?

The total prediction error can be decomposed as:

Error=Bias2+Variance+Irreducible ErrorError = Bias^2 + Variance + Irreducible\ Error

High bias:

  • Model too simple
  • Underfitting
  • Consistent errors

High variance:

  • Model too complex
  • Overfitting
  • Sensitive to training data

A simple way to remember:

  • Bias = wrong assumptions → consistently wrong
  • Variance = sensitivity to data → unstable predictions

The names come directly from their statistical meanings: bias = systematic deviation, variance = spread/variability.



πŸ”¬ Generalization in Practice

πŸ”Ή Empirical Risk Minimization (ERM)

Model minimizes:

Training Loss\text{Training Loss}

BUT we actually care about:

Expected Loss\text{Expected Loss}

πŸ‘‰ Overfitting happens when:

Training LossTest Loss\text{Training Loss} \ll \text{Test Loss}

πŸ› ️Techniques to Improve Generalization


πŸ”Ή 1. More Data

  • Best solution (if possible)

πŸ”Ή 2. Regularization

Adds penalty to model complexity:

Examples:

  • L1 regularization (Lasso)
  • L2 regularization (Ridge)

πŸ”Ή 3. Early Stopping

  • Stop training when validation error increases

πŸ”Ή 4. Cross-Validation

  • More reliable performance estimate

πŸ”Ή 5. Simpler Model

  • Reduce parameters

πŸ”Ή 6. Dropout (Neural Networks)

  • Randomly disable neurons during training

πŸ”Ή 7. Data Augmentation

  • Create more training examples

🧠 Intuitive Analogy (Good for Teaching)

Student analogy:

  • Generalization → ability to solve new questions
  • Overfitting → memorizing past exam answers

πŸ‘‰ A student who memorizes:

  • Scores high on practice questions
  • Fails new questions

πŸ“Š Model Complexity vs Performance

Complexity        Training Error        Test Error
Low        High        High
Medium        Low        Lowest
High        Very Low        High

πŸ” Relation to Training / Validation / Test Sets

  • Training set → learning
  • Validation set → detect overfitting
  • Test set → measure generalization

πŸ“ŠUnderfitting vs Overfitting

Aspect    Underfitting    Overfitting
Model complexity    Too low    Too high
Training error    High    Very low
Test error    High    High
Generalization    Poor    Poor

🎯 Relationship to Generalization

  • Underfitting → poor generalization because model is too simple
  • Overfitting → poor generalization because model is too complex

πŸ‘‰ Both are undesirable
πŸ‘‰ Goal: balanced model

🎯 Key Takeaways 

  • Underfitting = model too simple
  • Cannot capture patterns
  • High error everywhere
  • Fixed by increasing model capacity or improving features
  • Low training error ≠ good model
  • Generalization is the real goal
  • Overfitting = learning noise
  • Test error will be high is the model overfits
  • Validation set is essential
  • Balance model complexity carefully

Comments

Popular posts from this blog

Machine Learning PCCST503 Semester5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Machine Learning (ML)

Distinguishing Machine Learning from Traditional Programming