Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Overfitting

Imports

%load_ext autoreload
%autoreload 2

import logging

import matplotlib.pyplot as plt
import seaborn as sns

# Set plotting style.
sns.set_style("whitegrid")
plt.rcParams["figure.figsize"] = (12, 6)
import helpers.htutorial as ut
import L05_02_02_overfitting_utils as utils

ut.config_notebook()

# Initialize logger.
logging.basicConfig(level=logging.INFO)
_LOG = logging.getLogger(__name__)
WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
vim support installed: restart the notebook, if needed
Python 3.12.3
Linux 589569fe8102 6.12.67-linuxkit #1 SMP Sun Jan 25 02:26:28 UTC 2026 aarch64 aarch64 aarch64 GNU/Linux

Cell 1: True Target Function and Data Sampling

Goal:

  • Visualize the true target function that we want to learn in a machine learning problem
  • Understand that in real-world scenarios, we don’t have access to the complete target function f(x)f(x)
  • Demonstrate the process of sampling noisy observations from the true function
  • Show the train/test split: in-sample (training) data vs out-of-sample (test) data
  • Build intuition for the fundamental ML setup: learning from limited, noisy samples

Plots:

  • Display four panels:
    • True Target Function: Complete unknown function f(x)f(x) we want to learn
      • Blue solid line: Noiseless true function
      • Blue transparent line: Noisy version (if ϵ>0\epsilon > 0)
    • In-Sample Data (80%): Green points representing training data
      • These ntrainn_{\text{train}} points are used to fit the model
    • Out-of-Sample Data (20%): Red points representing test data
      • These ntestn_{\text{test}} points are used to evaluate generalization
    • Comments: Text summary of parameters and key observations

Parameters:

  • Function: Select the true target function from available options:
    • Slow Sinusoid: f(x)=sin(0.5πx)f(x) = \sin(0.5\pi x)
    • Fast Sinusoid: f(x)=sin(2πx)f(x) = \sin(2\pi x)
    • Parabola: f(x)=2x21f(x) = 2x^2 - 1
    • Constant: f(x)=0f(x) = 0
    • Linear: f(x)=xf(x) = x
  • epsilon (ϵ\epsilon): Standard deviation of Gaussian noise added to observations (noise level)
  • N (total samples) (NN): Total number of data points to sample from the function
  • seed: Random seed for reproducibility of sampling

Key observations:

  • The complete curve represents the unknown target function f(x)f(x) that we wish to learn
  • In practice, we only have access to a finite set of noisy samples: (xi,yi)(x_i, y_i) where yi=f(xi)+ϵiy_i = f(x_i) + \epsilon_i
  • Data is split into:
    • Training set (80%): Used to learn the model parameters
    • Test set (20%): Used to evaluate how well the model generalizes
  • The fundamental ML challenge: Learn from green (training) points to predict well on red (test) points
  • Increasing NN provides more information, while increasing ϵ\epsilon makes learning harder due to noise
# Display the true target function with interactive controls.
utils.cell1_plot_true_target_function()
Loading...

Cell 2: Model Comparison - Constant vs Linear

Goal:

  • Compare two hypothesis classes: constant model h(x)=bh(x) = b vs linear model h(x)=ax+bh(x) = ax + b
  • Understand the bias-variance tradeoff through concrete examples
  • Observe how model complexity affects approximation quality and stability
  • Visualize in-sample error (EinE_{\text{in}}) vs out-of-sample error (EoutE_{\text{out}})
  • Demonstrate how different models fit the same training data and generalize to test data

Note: This cell uses the same configuration as Cell 1. All parameters (function type, ϵ\epsilon, NN, seed) are synchronized with Cell 1. To change the setup, adjust the parameters in Cell 1.

Models:

  • Constant model: h(x)=bh(x) = b
    • Parameter bb is the mean of training yy-values: b=1ni=1nyib = \frac{1}{n}\sum_{i=1}^{n} y_i
    • Hypothesis class: H0={h(x)=b:bR}\mathcal{H}_0 = \{h(x) = b : b \in \mathbb{R}\}
    • Characteristics: High bias (limited expressiveness), low variance (stable across datasets)
  • Linear model: h(x)=ax+bh(x) = ax + b
    • Parameters a,ba, b are fit using least squares regression
    • Hypothesis class: H1={h(x)=ax+b:a,bR}\mathcal{H}_1 = \{h(x) = ax + b : a, b \in \mathbb{R}\}
    • Characteristics: Lower bias (more expressive), higher variance (sensitive to training data)

Plots:

  • Display four panels:
    • In-Sample Data: Green training points with fitted model
      • Shows model fit on training data
      • Displays EinE_{\text{in}} (training error)
    • Out-of-Sample Data: Red test points with fitted model
      • Shows model generalization on test data
      • Displays EoutE_{\text{out}} (test error)
    • True Function vs Model: Comparison between target function and learned model
      • Blue line: True target function f(x)f(x)
      • Model line: Fitted hypothesis h(x)h(x)
      • Orange shaded area: Approximation error between f(x)f(x) and h(x)h(x)
    • Comments: Text summary showing learned parameters, errors, and observations

Parameters:

  • Model Type: Dropdown to select between Constant and Linear models
  • Resample and Relearn: Button to generate new training data (increments seed) and refit the model

Key observations:

  • Constant model (H0\mathcal{H}_0):
    • HIGH BIAS: Poor approximation of complex target functions (large orange shaded area)
    • LOW VARIANCE: Very stable - produces similar h(x)h(x) across different training sets
    • Simple model (1 parameter) cannot capture patterns in data
    • EinE_{\text{in}} and EoutE_{\text{out}} are typically close (low variance)
  • Linear model (H1\mathcal{H}_1):
    • LOWER BIAS: Better approximation capability (smaller orange shaded area for linear targets)
    • HIGHER VARIANCE: More sensitive to specific training points - h(x)h(x) changes more with resampling
    • More complex model (2 parameters) can capture linear trends
    • EinE_{\text{in}} and EoutE_{\text{out}} may differ more (higher variance)
  • Bias-variance tradeoff:
    • Constant model: Underfits (high bias) but is stable (low variance)
    • Linear model: Better fit but less stable (bias-variance tradeoff)
    • Click “Resample and Relearn” multiple times to observe variance: how much does h(x)h(x) change?
    • Compare EinE_{\text{in}} vs EoutE_{\text{out}} to assess generalization
# Display model learning with interactive controls.
utils.cell2_plot_model()
Loading...