Bias and Variance
Bias and variance decompose a model's reducible prediction error into two competing sources. High bias stems from overly simplistic model assumptions that cause underfitting, whereas high variance stems from excessive sensitivity to training sample fluctuations that causes overfitting, making total generalization error lowest at an intermediate model complexity.
Study Card: Bias and Variance
Direct Answer
Bias and variance are the two reducible components of a supervised learning model’s expected prediction error. Bias reflects error introduced by approximating a complex target function with overly restrictive assumptions—high bias leads to underfitting, performing poorly on both training and test data. Variance reflects how much a model’s predictions fluctuate when trained on different finite samples from the same distribution—high variance leads to overfitting, fitting training noise closely while generalizing poorly. Model selection aims to balance bias and variance to minimize total expected error on unseen data.
For squared-error regression where with and , the expected test error of a learned predictor at point decomposes cleanly as:
where and .
Key Terms
- Bias: The systematic difference between a model’s average prediction across training sets and the true underlying target function.
- Variance: The variability of the model’s prediction at across different random training samples drawn from the same data distribution.
- Irreducible Error (): Inherent observation noise in the data-generating process that no model can eliminate.
- Underfitting: When a model lacks sufficient capacity to capture the underlying signal, yielding high bias and high error on both training and test sets.
- Overfitting: When a model fits sample noise and training idiosyncrasies, yielding low training error alongside high variance and high test error.
Example
Imagine predicting house prices from living area when the true relationship is curved due to diminishing marginal returns on very large homes. A simple straight-line model (high bias, low variance) misses the curvature systematically across every dataset draw, underfitting both training and test houses. Conversely, fitting a degree-15 polynomial to a small noisy sample (low bias, high variance) weaves wildly through random price outliers; on a new house, its prediction error explodes. An intermediate model (e.g., a quadratic curve or regularized spline) balances both error terms to minimize test MSE.
Code Implementation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# Generate sample data with a non-linear relationship
np.random.seed(0)
X = np.sort(np.random.rand(100, 1), axis=0)
y = np.sin(2 * np.pi * X).ravel() + np.random.normal(0, 0.2, X.shape[0])
# Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
# High Bias Model (Linear Regression)
model_high_bias = LinearRegression()
model_high_bias.fit(X_train, y_train)
# High Variance Model (High-degree Polynomial Regression)
model_high_variance = make_pipeline(PolynomialFeatures(degree=15), LinearRegression())
model_high_variance.fit(X_train, y_train)
# Generate points for plotting smooth model curves
X_plot = np.linspace(0, 1, 100).reshape(-1, 1)
plt.figure(figsize=(10, 6))
plt.scatter(X_train, y_train, s=20, label="Training points")
plt.plot(X_plot, model_high_bias.predict(X_plot), color='red', label="High Bias Model (degree 1)")
plt.plot(X_plot, model_high_variance.predict(X_plot), color='green', label="High Variance Model (degree 15)")
plt.ylim(-2, 2)
plt.title("Bias-Variance Tradeoff")
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.show()
# Print Train vs Test MSE
print("High Bias Model - MSE Train:", round(mean_squared_error(y_train, model_high_bias.predict(X_train)), 4))
print("High Bias Model - MSE Test:", round(mean_squared_error(y_test, model_high_bias.predict(X_test)), 4))
print("High Variance Model - MSE Train:", round(mean_squared_error(y_train, model_high_variance.predict(X_train)), 4))
print("High Variance Model - MSE Test:", round(mean_squared_error(y_test, model_high_variance.predict(X_test)), 4))
Related Concepts
- Overfitting and Underfitting: High training and validation error indicates high bias (underfitting), whereas low training error with a large generalization gap indicates high variance (overfitting).
- Regularization: L1/L2 penalties shrink parameter magnitudes, trading a small increase in bias for a substantial reduction in variance.
- Cross-Validation: Estimates out-of-sample error across multiple data splits to select hyperparameters (polynomial degree, regularization , tree depth) at the optimal bias-variance sweet spot.
- Ensembles: Bagging (e.g., Random Forests) reduces variance by averaging high-variance trees; boosting reduces bias by sequentially fitting residual errors.
- Double Descent: In heavily overparameterized deep neural networks, test error can descend again past the interpolation threshold due to implicit smoothness regularization from gradient-based optimization.