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 msml610.tutorials.msml610_utils as ut
import L05_02_01_bias_variance_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: Approximation¶
Goal:
- Demonstrate the concept of approximation - how well different models can fit a target function
- Visualize how a constant model (horizontal line) and a linear model (diagonal line) approximate a sinusoidal target function for
- Compare approximation capability between simple (constant) and more complex (linear) models
Plots:
- Display three plots:
- True function vs constant model: Shows (mean of ), with orange shading for approximation error
- True function vs linear model: Shows (fitted using least squares), with orange shading for approximation error
- Comments: Displays approximation errors and observations
Parameters:
- None (this is a static visualization)
Key observations:
- The constant model has high approximation error - it cannot capture any variation in the target function
- The linear model has lower approximation error - it can capture the general trend, though not the curvature
- Lower approximation error means better fit, but doesn’t always mean better learning (as we’ll see with bias-variance tradeoff)
# Display approximation comparison between constant and linear models.
utils.cell1_approximation()
Cell 2: Learning Once¶
Goal:
- Demonstrate the difference between learning and approximation
- Show how models trained on a limited training set (learning) perform differently than models that approximate the full function
- Illustrate the key difference between in-sample error () and out-of-sample error ()
Plots:
- Display three plots:
- Constant model : Shows true function , fitted model, and training points (red dots), with and displayed
- Linear model : Shows true function, fitted model, and training points, with and displayed
- Comments: Displays errors and observations about learning vs approximation
Parameters:
seed: Random seed controlling which training points are sampledN_samples: Number of random points in the training set (default: 2)
Key observations:
- measures how well the model fits the training data
- measures how well the model generalizes to the full function
- With very few samples (e.g., ), a linear model can achieve (perfect fit on training data) but still have high
- This demonstrates that learning from limited data is fundamentally different from approximation
- Try different seeds to see how training set selection affects performance
# Display learning from N random samples with interactive controls.
utils.cell2_learning_once()Loading...
Loading...
Loading...
Cell 3: Learning (Bias-Variance Decomposition)¶
Goal:
- Visualize the bias-variance tradeoff by showing how models trained on different random training sets vary around the true function
- Demonstrate bias and variance decomposition by running multiple learning experiments with different training sets
- Show how model complexity affects both bias (systematic error) and variance (sensitivity to training data)
Plots:
- Display three plots:
- Constant models: True function and all fitted constant models (green lines with transparency), with dashed line showing average model
- Linear models: True function and all fitted linear models (magenta lines with transparency), with dashed line showing average model
- Comments: Average errors and explanation of bias-variance tradeoff
Parameters:
seed: Random seed for reproducibilityN_samples: Number of training points per experiment (default: 2)N_experiments: Number of different training sets to generate (default: 100)
Key observations:
- Constant model (): Low variance (all lines very similar), high bias (far from true function) - the model is too simple to capture the pattern
- Linear model (): Higher variance (lines spread out more), lower bias (average model closer to true function) - the model is more flexible but sensitive to training data
- This illustrates the bias-variance tradeoff: simpler models have low variance but high bias; more complex models have lower bias but higher variance
- The total out-of-sample error decomposes as:
- Try increasing
N_samplesto see how more data reduces variance - Try increasing
N_experimentsto get more stable estimates of bias and variance
# Display bias-variance decomposition over multiple experiments.
utils.cell3_learning_bias_variance()Loading...
Loading...
Loading...
Loading...
Cell 4: Learning Plots (Bias-Variance as Function of Training Set Size)¶
Goal:
- Show how bias, variance, and overall error change as we increase the number of training samples
- Visualize the bias-variance decomposition as a function of training set size ()
- Demonstrate the classic bias-variance curves and how more data affects both components of the learning error
Plots:
- Display three plots:
- Constant model (): Shows , , , and as functions of
- Linear model (): Shows , , , and as functions of
- Comments: Error decomposition formula and observations
- Show error decomposition: For a deterministic target function (no noise),
Parameters:
seed: Random seed for reproducibility (fixed to ensure consistent comparison)N_experiments: Number of experiments to average over for each valuemax_N_samples: Maximum number of training samples to test
Key observations:
- Constant model: Very low variance (almost constant across ) because it’s insensitive to training data, but high bias because it cannot capture the sinusoidal pattern
- Linear model: Higher variance (especially with few samples) because it’s more flexible and sensitive to training data, but lower bias because it can better approximate the target function
- As increases: Variance decreases for both models (more data leads to more stable fits), while bias remains relatively constant (determined by model capacity)
- decomposition: You can verify that by comparing the curves
- The bias-variance tradeoff: Simpler models (constant) have low variance but high bias; more complex models (linear) have higher variance but lower bias
- Try increasing
N_experimentsto get smoother, more stable curves
# Display bias-variance decomposition as a function of N_samples.
utils.cell4_learning_plots()Loading...
Loading...
Loading...
Loading...
Cell 5: Learning with Noise (Bias-Variance Decomposition)¶
Goal:
- Extend Cell 3 by adding Gaussian noise to the training data
- Demonstrate how noise affects the bias-variance tradeoff
- Show how adding noise to training labels affects both the variance and out-of-sample error of learned models
Plots:
- Display three plots:
- Constant models: True function and all fitted constant models (green lines with transparency), with dashed line showing average model
- Linear models: True function and all fitted linear models (magenta lines with transparency), with dashed line showing average model
- Comments: Average errors and explanation of noise effects
Parameters:
seed: Random seed for reproducibilityN_samples: Number of training points per experiment (default: 2)N_experiments: Number of different training sets to generate (default: 100)noise_std: Standard deviation of Gaussian noise added to training labels (default: 0.0)
Key observations:
- With : Same behavior as Cell 3 (no noise case)
- With : Training data is corrupted by Gaussian noise
- Models try to fit the noisy observations instead of the true function
- This increases variance for both models (more sensitivity to data)
- increases because models partially fit the noise
- The error decomposition becomes: (noise variance)
- Constant model: Still has low variance, but noise increases
- Linear model: Variance increases significantly with noise (tries to fit noise)
- Try increasing
noise_stdto see how noise affects the spread of fitted models - Try increasing
N_samplesto see how more data helps average out the noise
# Display bias-variance decomposition with noise over multiple experiments.
utils.cell5_learning_with_noise()Loading...
Loading...
Loading...
Loading...
Loading...
Cell 6: Learning Plots with Noise (Bias-Variance as Function of Training Set Size)¶
Goal:
- Extend Cell 4 by adding Gaussian noise to the training data
- Visualize how , , , and change as a function of training set size when training data is corrupted by Gaussian noise
- Demonstrate how more data helps mitigate the effects of noise
Plots:
- Display three plots:
- Constant model (): Shows error components as functions of
- Linear model (): Shows error components as functions of
- Comments: Error decomposition with noise formula and observations
- Show error decomposition with noise: (noise variance is irreducible error)
Parameters:
seed: Random seed for reproducibility (fixed to ensure consistent comparison)N_experiments: Number of experiments to average over for each valuemax_N_samples: Maximum number of training samples to testnoise_std: Standard deviation of Gaussian noise added to training labels (default: 0.0)
Key observations:
- With : Same behavior as Cell 4 (deterministic case)
- With : Training data includes random noise
- Variance increases for both models compared to the no-noise case
- increases by approximately (the irreducible error from noise)
- As increases, variance decreases (more data averages out noise)
- Bias remains relatively constant (determined by model capacity, not noise)
- The noise term: Represents the best possible error - even a perfect model cannot do better than when learning from noisy data
- More data helps: Increasing reduces the variance component but cannot reduce the noise component
- Try setting
noise_std = 0.1or0.2to see the noise effect - Try increasing
max_N_samplesto see how variance continues to decrease with more data - Compare with Cell 4 () to see the additional error from noise
# Display bias-variance decomposition with noise as a function of N_samples.
utils.cell6_learning_plots_with_noise()Loading...
Loading...
Loading...
Loading...
Loading...