VivaPrep
← Jaber Notes

Jaber Notes · 2 of 16

Learning Theory

ERM, bias-variance, regularization, cross-validation, VC dimension, PAC.

Why learning generalizes at all: empirical risk minimization, the full bias-variance decomposition, the theory behind L1/L2 and early stopping, honest cross-validation, and the leakage traps that fake good scores.

Visual reference

Bias–variance tradeoff

sweet spotunderfitoverfitmodel complexity →errortrainval
As model complexity grows, training error keeps falling — but validation error bottoms out then rises again. The gap between the two curves after the sweet spot is overfitting.
The "why" behind every practical ML decision: why we hold out test sets, why we regularize, what "learning" formally means.

Table of contents

  1. The learning problem
  2. Generalization and train/val/test split
  3. Bias-variance decomposition
  4. Overfitting and underfitting
  5. Regularization: theory and methods
  6. Cross-validation
  7. VC theory and PAC learning (overview)
  8. Model complexity and the no-free-lunch theorem
  9. Data splits and common mistakes

1. The learning problem

1.1 Setup

  • Input space X=Rp\mathcal{X} = \mathbb{R}^p (features).
  • Output space Y\mathcal{Y} (regression: R\mathbb{R}; classification: {1,,C}\{1,\ldots,C\}).
  • Unknown distribution P(x,y)P(\mathbf{x},y) that generates the data.
  • Training set D={(xi,yi)}i=1n\mathcal{D} = \{(\mathbf{x}_i, y_i)\}_{i=1}^n sampled i.i.d. from PP.
  • Hypothesis class H\mathcal{H}: set of candidate functions h:XYh: \mathcal{X} \to \mathcal{Y} the algorithm searches over.
  • Loss function (h(x),y)\ell(h(\mathbf{x}), y): penalty for a prediction.

1.2 Expected and empirical risk

Expected (true) risk:

R(h)=E(x,y)P[(h(x),y)].R(h) = \mathbb{E}_{(\mathbf{x},y)\sim P}[\ell(h(\mathbf{x}), y)].

This is what we ultimately care about: performance on *new* unseen data.

Empirical risk (training loss):

R^(h)=1ni=1n(h(xi),yi).\hat{R}(h) = \frac{1}{n}\sum_{i=1}^n \ell(h(\mathbf{x}_i), y_i).

This is observable. Empirical risk minimization (ERM): find h^=argminhHR^(h)\hat{h} = \arg\min_{h\in\mathcal{H}} \hat{R}(h).

The fundamental problem: R^(h^)\hat{R}(\hat{h}) underestimates R(h^)R(\hat{h}) because we optimized on the same data we are evaluating on. The gap R(h^)R^(h^)R(\hat{h}) - \hat{R}(\hat{h}) is the generalization gap.


2. Generalization and train/val/test split

2.1 Why three splits?

SplitPurposeMust not be touched until
TrainFit model parameters
ValidationTune hyperparameters, compare modelsNever used for final fit
TestUnbiased estimate of final model's performanceOne time only, after all decisions are made

Using the test set to make *any* decision (including "I'll keep this model because it worked on test") inflates your estimate of generalization. This is called test set contamination.

2.2 Typical splits

  • 60/20/20 (train/val/test) or 70/15/15 — common for moderate datasets.
  • k-fold CV — when data is small, no permanent val set (see §6).
  • Temporal splits for time series — future data never used to predict past (see note 10).

3. Bias-variance decomposition

3.1 Derivation (regression)

Fix a test point x\mathbf{x} and assume y=f(x)+εy = f(\mathbf{x}) + \varepsilon, E[ε]=0\mathbb{E}[\varepsilon]=0, Var(ε)=σ2\text{Var}(\varepsilon)=\sigma^2.

For a learning algorithm A\mathcal{A} trained on dataset D\mathcal{D}, let f^D(x)\hat{f}_\mathcal{D}(\mathbf{x}) be its prediction. Expected MSE over random training sets:

ED ⁣[(yf^D(x))2]=(ED[f^D(x)]f(x))2Bias2+ED ⁣[(f^D(x)ED[f^D(x)])2]Variance+σ2.\mathbb{E}_\mathcal{D}\!\left[(y - \hat{f}_\mathcal{D}(\mathbf{x}))^2\right] = \underbrace{(\mathbb{E}_\mathcal{D}[\hat{f}_\mathcal{D}(\mathbf{x})] - f(\mathbf{x}))^2}_{\text{Bias}^2} + \underbrace{\mathbb{E}_\mathcal{D}\!\left[(\hat{f}_\mathcal{D}(\mathbf{x}) - \mathbb{E}_\mathcal{D}[\hat{f}_\mathcal{D}(\mathbf{x})])^2\right]}_{\text{Variance}} + \sigma^2.

Derivation sketch: add and subtract fˉ:=ED[f^D(x)]\bar{f} := \mathbb{E}_\mathcal{D}[\hat{f}_\mathcal{D}(\mathbf{x})]:

(yf^)2=(yf+ffˉ+fˉf^)2.(y - \hat{f})^2 = (y - f + f - \bar{f} + \bar{f} - \hat{f})^2.

Expand and take expectations; cross terms vanish.

TermInterpretation
Bias²Systematic error from wrong model assumptions (underfitting).
VarianceSensitivity of model to training set fluctuations (overfitting).
Irreducible noise σ2\sigma^2Cannot be reduced by any model.

3.2 Bias-variance tradeoff

As model complexity increases:

Bias:        ████████░░░░░░░░  (decreases)
Variance:    ░░░░░░░░████████  (increases)
Total error: ╲__________/      (U-shaped curve)
  • High bias (underfitting): model too simple (e.g., fitting a line to curved data). Both train and test error are high.
  • High variance (overfitting): model too complex (e.g., degree-100 polynomial on 20 points). Train error low, test error high.
  • Sweet spot: right model complexity minimizes sum of bias² + variance.

3.3 What affects each term?

Bias is increased by:

  • Small hypothesis class (e.g., only linear functions when truth is nonlinear).
  • Heavy regularization (shrinks model toward a biased baseline).
  • Wrong model assumptions.

Variance is increased by:

  • Large model class (many free parameters).
  • Small training set.
  • Little regularization.

Practical rules:

  • If train error ≈ test error and both are high → high bias → more model complexity, less regularization, more features.
  • If train error ≪ test error → high variance → more data, regularization, fewer features.

4. Overfitting and underfitting

4.1 Overfitting

Model memorizes training data, including noise. Signs:

  • Loss on training set continues to decrease but validation loss plateaus or increases.
  • Large train/validation loss gap.

Causes:

  • Too many parameters relative to data.
  • Training too long (in iterative methods).
  • Noisy features, irrelevant features.

4.2 Underfitting

Model too simple to capture the true pattern. Signs:

  • Both train and validation loss are high.
  • Adding more training data does not significantly improve either.

Causes:

  • Too few parameters.
  • Heavily constrained model class.
  • Excessive regularization.

4.3 Learning curves

Plot train and validation loss vs. training set size nn:

Loss
│
│ train ────────────────────
│                       \
│                        \________  validation
│
└──────────────────────────────── n
  • If both curves are high and converging: high bias (add complexity).
  • If large gap between them: high variance (add data or regularize).

5. Regularization: theory and methods

5.1 The regularization idea

Add a penalty term to the training loss to control complexity:

Regularized loss=R^(h)data fit+λΩ(h)complexity penalty.\text{Regularized loss} = \underbrace{\hat{R}(h)}_\text{data fit} + \underbrace{\lambda \Omega(h)}_\text{complexity penalty}.

Higher λ\lambda → stronger regularization → smaller model → higher bias, lower variance.

5.2 L2 regularization (Ridge / weight decay)

Ω(w)=w22=wj2\Omega(\mathbf{w}) = \|\mathbf{w}\|_2^2 = \sum w_j^2.

Effect: shrinks all weights uniformly toward zero. Does not produce exact zeros (smooth penalty). Closed form for linear models. Probabilistic interpretation: Gaussian prior on weights.

Ridge regression: minwyXw2+λw2\min_\mathbf{w} \|\mathbf{y}-\mathbf{X}\mathbf{w}\|^2 + \lambda\|\mathbf{w}\|^2, solution: w=(XX+λI)1Xy\mathbf{w}^\star = (\mathbf{X}^\top\mathbf{X}+\lambda\mathbf{I})^{-1}\mathbf{X}^\top\mathbf{y}.

Adding λI\lambda\mathbf{I} makes the matrix strictly positive definite → always invertible → solves the ill-conditioned / collinear case.

Effective degrees of freedom: tr(X(XX+λI)1X)\operatorname{tr}(\mathbf{X}(\mathbf{X}^\top\mathbf{X}+\lambda\mathbf{I})^{-1}\mathbf{X}^\top) — decreases as λ\lambda increases.

5.3 L1 regularization (Lasso)

Ω(w)=w1=wj\Omega(\mathbf{w}) = \|\mathbf{w}\|_1 = \sum |w_j|.

Effect: promotes sparsity — many weights become exactly zero → automatic feature selection.

Why sparsity? Geometric argument: L1 ball has corners on axes; optimization tends to land on a corner where some coordinates are exactly zero.

Proximal operator (soft thresholding): the 1D Lasso proximal update for coordinate jj:

wjsign(zj)max(zjλ/(2n),0),w_j \leftarrow \text{sign}(z_j)\max(|z_j| - \lambda/(2n), 0),

where zjz_j is the unconstrained coordinate update. This is used in coordinate descent for Lasso.

When to use Lasso: you believe few features are truly relevant (sparse signal).

5.4 Elastic Net

Combines both:

minwyXw2+λ1w1+λ2w22.\min_\mathbf{w} \|\mathbf{y}-\mathbf{X}\mathbf{w}\|^2 + \lambda_1\|\mathbf{w}\|_1 + \lambda_2\|\mathbf{w}\|_2^2.

Advantages over pure Lasso: handles correlated features better (Lasso arbitrarily picks one from a correlated group; ElasticNet can keep all), always selects at most nn features.

5.5 Regularization via early stopping

In gradient descent, stopping before convergence implicitly regularizes. The number of iterations controls model complexity. Equivalent to a form of L2 regularization for some models (gradient flow argument).

5.6 Dropout (preview of DL)

During training, randomly zero out each neuron with probability pp. Prevents co-adaptation. Equivalently trains an ensemble of 2N2^N sub-networks.

5.7 Data augmentation as regularization

Expanding the training set with label-preserving transformations (flips, crops, noise) reduces effective variance without adding parameters.


6. Cross-validation

6.1 Purpose

Estimate generalization performance when data is limited (can't afford a large held-out set) and to select hyperparameters.

6.2 k-fold CV

  1. Partition data into kk roughly equal folds.
  2. For i=1,,ki = 1, \ldots, k: train on all folds except fold ii, evaluate on fold ii.
  3. Average the kk evaluation scores.

Bias-variance of CV: large kk → low bias (trains on nearly all data), high variance (each fold's test set is small). Small kk → faster, more biased. k=5k=5 or k=10k=10 is standard.

Leave-one-out CV (LOOCV): k=nk = n. Unbiased but expensive and high variance.

6.3 Stratified k-fold

Preserve class proportions in each fold. Essential for imbalanced classification.

6.4 Nested CV (for hyperparameter tuning)

Inner loop: select hyperparameters (CV over train data). Outer loop: estimate test performance (CV over full data).

Without the outer loop, selecting hyperparameters via CV and reporting that CV score as your performance estimate is optimistic (you've peeked at which parameters "work").

6.5 Common CV mistakes

MistakeConsequence
Fit scaler/imputer on all data, then CVData leakage from test fold into training
Select features using all data, then CVFeature selection leakage
Time series with random foldsFuture leaks into past
Report inner CV score as finalOver-optimistic performance

Rule: every step of the pipeline that "learns" from data (scaling, encoding, feature selection) must be fit only on training folds and applied to validation/test folds.


7. VC theory and PAC learning (overview)

7.1 PAC learning

Probably Approximately Correct (PAC) framework: algorithm A\mathcal{A} PAC-learns H\mathcal{H} if for any distribution PP and any ε,δ>0\varepsilon, \delta > 0, using at most polynomially many samples:

P(R(h^)minhHR(h)+ε)1δ.P(R(\hat{h}) \leq \min_{h\in\mathcal{H}} R(h) + \varepsilon) \geq 1 - \delta.

"ε\varepsilon-close to the best in class, with probability at least 1δ1-\delta."

Sample complexity: how many samples are needed? For finite H|\mathcal{H}|:

n12ε2logHδ.n \geq \frac{1}{2\varepsilon^2}\log\frac{|\mathcal{H}|}{\delta}.

Grows logarithmically in H|\mathcal{H}| — even huge but finite classes are learnable.

7.2 VC dimension

For infinite hypothesis classes, VC dimension measures effective complexity.

Shattering: a set of mm points is shattered by H\mathcal{H} if for every labeling of those points, some hHh\in\mathcal{H} correctly classifies them.

VC dimension dVC(H)d_{VC}(\mathcal{H}): largest set size that H\mathcal{H} can shatter.

Examples:

  • Threshold classifiers on R\mathbb{R}: dVC=1d_{VC} = 1.
  • Linear classifiers in Rn\mathbb{R}^n: dVC=n+1d_{VC} = n + 1.
  • Neural networks: dVC=O(WlogW)d_{VC} = O(W \log W) where WW = number of weights.

Fundamental theorem of PAC learning (VC bound):

R(h^)R^(h^)+O ⁣(dVClogn+log(1/δ)n).R(\hat{h}) \leq \hat{R}(\hat{h}) + O\!\left(\sqrt{\frac{d_{VC}\log n + \log(1/\delta)}{n}}\right).

Takeaways:

  • More data nn → smaller generalization gap.
  • Higher VC dimension → larger gap (more complex model needs more data).
  • Tighter for models with lower VC dimension.

8. Model complexity and the no-free-lunch theorem

8.1 Inductive bias

Every learning algorithm makes assumptions about the structure of solutions. This is its inductive bias. Without inductive bias, a model cannot generalize beyond the training set.

Examples: linear models assume linearity; SVMs with RBF kernel assume smoothness; Naive Bayes assumes conditional independence.

8.2 No-free-lunch (NFL) theorem

Statement: Averaged over all possible data distributions, every learning algorithm performs exactly the same (as random guessing on unseen data).

Implication: there is no universally best algorithm. Every algorithm makes assumptions. The right model requires domain knowledge. This is why practitioners choose models with inductive biases that match their data.

Do not misuse: NFL does not say "all models are equally good in practice." Real problems are not uniform over all distributions — real data has structure, and some algorithms exploit it better.


9. Data splits and common mistakes

9.1 Temporal data

  • Always split by time: train on past, evaluate on future.
  • Never use future information to construct features for past samples (temporal leakage).

9.2 Group-based data

  • If samples within a group are correlated (e.g., multiple readings from one patient), do group k-fold: keep all samples from a group in the same fold.

9.3 Label leakage

  • A feature is derived from or correlated with the label after the fact (e.g., using a column that was computed using the answer). Classic cause of "too good to be true" results.

9.4 Preprocessing leakage (the most common mistake)

WRONG:
  1. StandardScaler.fit_transform(all_data)
  2. train_test_split(...)
  3. Train model on train, evaluate on test

CORRECT:
  1. train_test_split(...)
  2. StandardScaler.fit(train), transform(train) and transform(test)
  3. Train model on train, evaluate on test

Any fit call must only see training data.


*File: notes/02_learning_theory.md — next: notes/03_linear_models.md*