S145 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S14 · Lesson 5 · Generalization bounds

How far a measured error rate can sit from the true one, Hoeffding's inequality and the test examples a guarantee takes, zero-failure bounds, the price of choosing among many models, and sample complexity with the VC dimension

A classifier makes no mistakes on 300 test examples. Is its error rate zero? Certainly not, but how large could it be? And if you tried a hundred hyperparameter settings and kept the one that scored best on the validation set, how much should you trust that best score? Learning theory answers both with bounds that hold whatever the data's distribution, and turns them into the number of examples a guarantee requires.

  • The curves show how far, at worst, a measured error rate can sit from the true one, against the number of test examples N, holding with probability 1 − δ.
  • The solid curve is for one fixed model; the dashed one for the best of M models picked on the same data.
  • The gap shrinks as 1/N. Choosing among many models costs only ln⁡M inside the square root, so even a million candidates need only a few times more data.

Predict

A classifier makes no errors on 300 independent test examples. With 95% confidence, its true error rate is below about

Name it

Measured against true error

A model's true error rate μ is the probability it errs on a new example; on N independent test examples it errs a fraction ν of the time. Hoeffding's inequality bounds how far apart they can be, for any distribution:

P(|ν−μ|>ϵ)≤2e−2Nϵ2

Setting the right side to δ, with probability at least 1 − δ,

|ν−μ|≤ln⁡(2/δ)2N,N≥ln⁡(2/δ)2ϵ2

The bound is distribution-free and so conservative; when the error rate is known to be small, the binomial distribution or its normal approximation gives tighter intervals (lesson 20).

Zero failures

With no errors in N trials, the one-sided bound at confidence 1 − δ is p<1−δ1/N≈ln⁡(1/δ)/N, which is 3/N at 95%.

Choosing among models

If M models are scored on the same data and the best kept, the bound must hold for all of them at once. The union bound splits δ among them:

|ν−μ|≤ln⁡(2M/δ)2N

Tuning many hyperparameters on one validation set therefore makes its best score optimistic, and a separate test set, used once, is needed for an honest number.

Sample complexity

A finite class of M hypotheses that contains a perfect one: any hypothesis consistent with

N≥ln⁡M+ln⁡(1/δ)ϵ

training examples has error below ε with probability 1 − δ. Infinite classes are measured by their VC dimension dVC, the largest number of points they can label in every possible way: d+1 for linear classifiers in d dimensions. The VC bound replaces ln⁡M by about dVCln⁡N; in practice, about ten examples per unit of VC dimension is a common rule of thumb.

Worked example

How many test examples does it take to measure an error rate to within ±1 percentage point with 95% confidence? Compare Hoeffding's answer with the normal approximation for a classifier whose error is near 5%.

Show the solution
  1. Hoeffding's bound. N≥ln⁡(2/0.05)2×0.012=18,445
  2. Normal approximation. It uses the actual variance: N=1.962×0.05×0.950.012=1825 ten times fewer.
  3. Why they differ. Hoeffding assumes the worst case, a variance of 1/4 at an error rate of 50%; when the error rate is known to be small, the binomial calculation is the one to use.

Answer Hoeffding asks for 18,445 examples, and the normal approximation for 1825, ten times fewer.

Worked example

A team tries 100 hyperparameter settings and keeps the one that scores best on a 2000-example validation set. How optimistic might that score be, at 95% confidence, compared with scoring a single model?

Show the solution
  1. One model. ln⁡40/4000=0.030
  2. Best of 100. ln⁡4000/4000=0.046
  3. What selection does. It widens the possible gap by half, and the selected model's score is biased low, because it partly won by luck.
  4. The cure. The cure is a test set held back from all the tuning.

Answer the best of 100 settings may look better than it is by up to 0.046, against 0.030 for a single model.

Worked example

A lookup table can represent any Boolean function of 10 binary inputs. How many training examples guarantee error below 5% with 95% confidence for a table consistent with them? Compare a linear threshold unit on the same inputs.

Show the solution
  1. Count the functions. There are 21024 such functions, so ln⁡M=1024ln⁡2≈710
  2. Examples needed. With δ = 0.05, ln⁡(1/δ)=ln⁡20=3.0, and N≥ln⁡M+ln⁡200.05=14,256 more examples than the table has entries (1024).
  3. What the bound says. It says, rightly, that a table learns nothing about inputs it hasn't seen.
  4. Threshold unit. It has VC dimension 11, and about ten examples per unit, a hundred or so, is typically enough.
  5. Why the class matters. Restricting the hypothesis class is what makes generalization possible.

Answer the table needs 14,256 examples, more than its 1024 entries; a threshold unit, with VC dimension 11, typically needs a hundred or so.

Check

An error rate must be measured to within ±0.01 with probability 0.01 of failure, using Hoeffding's inequality.

(a)

How many test examples does it need?

Check

A model is scored on 1000 examples, and the bound must hold with failure probability 0.01.

(a)

How far can its measured error sit from the true error?

(b)

And for the best of 1000000 models scored on the same examples?

Check

A system passes 300 independent trials with no failures.

(a)

What upper bound on its failure rate holds with confidence 0.9?

Check

A classifier's error rate is about 0.05, and it must be measured to within ±0.005 at 95% confidence.

(a)

How many test examples does the normal approximation call for?

(b)

How many times more does Hoeffding's bound ask for?

Check

A learner picks any Boolean function of 16 binary inputs that fits its training data.

(a)

How many training examples guarantee error below 0.01 with failure probability 0.05?

Check

A linear classifier with a bias works on 10-dimensional inputs.

(a)

What is its VC dimension?

(b)

About how many training examples does the ten-per-dimension rule of thumb suggest?

Check

Why is a model's score on the validation set used to tune it an optimistic estimate of its error?

Check

A model's error is measured on N examples, and the bound must fail with probability at most δ.

Step 1

How wide is Hoeffding's bound for one model?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Check

Write hoeffding_n(eps, delta) (the examples needed, rounded up), zero_failure_bound(n, conf) and best_of_m(n, m, delta) (the bound's width for the best of m models).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does choosing the best of many models on one data set weaken the guarantee its score carries?

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.