Skip to content
VibeFormer
Advanced34 min

VC Dimension and PAC Learning

Shattering, VC dimension, sample complexity bounds, and the theory behind how much data is enough.

VC Dimension and PAC Learning

Intuition first

Everyone knows more data helps and simpler models generalise better. This lesson makes both statements quantitative: given a model family and a tolerance, how many examples do you actually need?

The key move is finding the right measure of model complexity. Parameter count will not do — a one-parameter model can be infinitely flexible, as we will see. What works is asking how many points the family can label in every possible way. A straight line in the plane can realise all 23=82^3 = 8 labellings of three well-placed points, but not all 16 labellings of four. That number, three, is its VC dimension, and it turns out to be exactly what controls the sample size you need.

The resulting bounds are far too loose for practical use. Their value is structural: they tell you what the generalisation gap depends on, and what it does not.

Notation used in this lesson
SymbolMeaning
Hypothesis class
VC(ℋ)Vapnik–Chervonenkis dimension of ℋ
R(h)True risk of hypothesis h
R̂(h)Empirical risk on n samples
εAccuracy tolerance — how close to optimal
δFailure probability — confidence is 1 − δ

Shattering and VC dimension

A hypothesis class H\mathcal{H} shatters a set of mm points if, for every one of the 2m2^m possible label assignments, some hHh \in \mathcal{H} realises it.

VC(H)=the largest m such that some set of m points is shattered\text{VC}(\mathcal{H}) = \text{the largest } m \text{ such that some set of } m \text{ points is shattered}

Note "some set", not "every set" — one shatterable configuration suffices.

3 points — shattered4 points — impossible
A line in the plane shatters three points: all eight labellings are achievable. With four points no line can produce the diagonal labelling shown on the right, so the VC dimension is exactly 3.

Standard values:

Hypothesis classVC dimension
Thresholds on R\mathbb{R}, 1[x>a]\mathbb{1}[x > a]1
Intervals on R\mathbb{R}2
Linear separators in Rd\mathbb{R}^dd+1d + 1
Axis-aligned rectangles in R2\mathbb{R}^24
Sine classifiers 1[sin(ωx)>0]\mathbb{1}[\sin(\omega x) > 0]\infty
Neural network with WW weights (ReLU)O(WlogW)O(W \log W)
1-nearest-neighbour\infty

The PAC framework

Probably Approximately Correct: we cannot demand a perfect model with certainty, so we demand a nearly-correct one with high probability.

A class is PAC-learnable if there is an algorithm that, for any ϵ,δ>0\epsilon, \delta > 0 and enough samples, returns hh with

P(R(h)R(h)+ϵ)1δ\Prob\Big(R(h) \leq R(h^*) + \epsilon\Big) \geq 1 - \delta

Two knobs: ϵ\epsilon is how much error you tolerate, δ\delta is how often you accept failure.

The generalisation bound

With probability at least 1δ1 - \delta, simultaneously for all hHh \in \mathcal{H}:

R(h)    R^(h)  +  VC(H)(ln2nVC(H)+1)+ln4δnR(h) \;\leq\; \hat{R}(h) \;+\; \sqrt{\frac{\text{VC}(\mathcal{H})\big(\ln\frac{2n}{\text{VC}(\mathcal{H})} + 1\big) + \ln\frac{4}{\delta}}{n}}

Ignoring logarithms, the gap scales as

R(h)R^(h)  =  O ⁣(VC(H)n)R(h) - \hat{R}(h) \;=\; O\!\left(\sqrt{\frac{\text{VC}(\mathcal{H})}{n}}\right)

Sample complexity

Inverting the bound gives the number of examples needed:

n  =  O ⁣(VC(H)+ln1δϵ2)n \;=\; O\!\left(\frac{\text{VC}(\mathcal{H}) + \ln\frac{1}{\delta}}{\epsilon^2}\right)

Solved problem 1 · How much data does a linear classifier need?

You want a linear classifier in R20\mathbb{R}^{20} with generalisation gap at most ϵ=0.05\epsilon = 0.05 and confidence 1δ=0.951 - \delta = 0.95. Estimate the sample complexity, then compare with ϵ=0.01\epsilon = 0.01.

Step 1 — VC dimension

Linear separators in Rd\mathbb{R}^d have

VC=d+1=20+1=21\text{VC} = d + 1 = 20 + 1 = 21

Step 2 — apply the sample complexity form

Using the standard constant-free version nVC+ln(1/δ)ϵ2n \approx \dfrac{\text{VC} + \ln(1/\delta)}{\epsilon^2}:

ln1δ=ln10.05=ln203.00\ln\frac{1}{\delta} = \ln\frac{1}{0.05} = \ln 20 \approx 3.00n21+3.000.052=24.00.0025=9,600n \approx \frac{21 + 3.00}{0.05^2} = \frac{24.0}{0.0025} = 9{,}600

Step 3 — tighten the tolerance to 0.01

n21+3.000.012=24.00.0001=240,000n \approx \frac{21 + 3.00}{0.01^2} = \frac{24.0}{0.0001} = 240{,}000

Step 4 — note the two scalings separately

Reducing ϵ\epsilon from 0.050.05 to 0.010.01 — a factor of 5 — multiplied the requirement by 52=255^2 = 25. The ϵ2\epsilon^{-2} dependence is brutal, and it is why the last few points of accuracy are so much more expensive than the first.

Now vary the confidence instead. Tightening δ\delta from 0.050.05 to 0.0010.001:

ln10.001=ln10006.91\ln\frac{1}{0.001} = \ln 1000 \approx 6.91n21+6.910.0025=27.90.002511,160n \approx \frac{21 + 6.91}{0.0025} = \frac{27.9}{0.0025} \approx 11{,}160

A 50-fold reduction in failure probability costs only 16% more data, because δ\delta enters logarithmically. High confidence is cheap; high accuracy is expensive.

Step 5 — reality check

In practice a 20-feature logistic regression performs well with a few thousand examples, not 9,600 — and far better than the bound's worst case. The bound is distribution-free: it must hold for the most adversarial data distribution imaginable, so it is loose by a wide margin on real data.

Answer

About 9,600 examples for ϵ=0.05\epsilon = 0.05 and about 240,000 for ϵ=0.01\epsilon = 0.01. The ϵ2\epsilon^{-2} scaling dominates everything; the ln(1/δ)\ln(1/\delta) term is nearly free. Treat these as upper bounds on the worst case, not as forecasts.

Why the bounds are loose, and what replaced them

VC bounds are distribution-free — they hold for any data distribution, including maliciously constructed ones. That generality is exactly what makes them loose on benign real data.

They also fail spectaculary on modern networks. A network with 101110^{11} parameters has an astronomically large VC dimension, so the bound is vacuous — it permits a generalisation gap above 1. Yet such models generalise. Worse, deep networks can perfectly fit randomly labelled data, which means their capacity genuinely is enormous, and yet they still generalise on real labels.

Refinements that do better:

  • Margin bounds — depend on 1/margin21/\text{margin}^2 rather than dimension, which is why SVMs work in very high dimensions.
  • Rademacher complexity — data-dependent, measuring how well the class fits random noise on your sample rather than in the worst case.
  • PAC-Bayes — bounds in terms of the distance between a learned posterior over hypotheses and a prior; currently the most successful route for deep networks.
  • Algorithmic stability — bounds based on how much the output changes when one training point is swapped. Applies to SGD directly.

What to actually take away

The bounds are not calculators. They are a correct account of what the generalisation gap depends on:

  1. Quadruple the data to halve the gap. This sets realistic expectations for data-collection projects.
  2. Confidence is cheap, accuracy is not. ln(1/δ)\ln(1/\delta) versus 1/ϵ21/\epsilon^2.
  3. What matters is complexity relative to sample size, not complexity alone.
  4. Regularisation works by reducing effective complexity — a claim the theory makes precise.

Exercise 1

Show that the VC dimension of axis-aligned rectangles in R2\mathbb{R}^2 is exactly 4.

Show solution

Two parts: exhibit 4 shattered points, and show 5 can never be.

At least 4. Place four points in a diamond: top (0,1)(0,1), bottom (0,1)(0,-1), left (1,0)(-1,0), right (1,0)(1,0). For any subset SS of these four, take the smallest axis-aligned rectangle containing exactly the points of SS. Because each point is the unique extreme point in one of the four directions, the bounding box of SS contains no point outside SS — removing the topmost point, for instance, lowers the top edge below it. All 24=162^4 = 16 subsets are realisable, so these four are shattered.

Not 5. Take any five points. Identify the point with the largest yy, smallest yy, largest xx, and smallest xx — at most four points are selected this way, so at least one point pp is not extreme in any direction. Then pp lies inside the bounding box of the other four.

Consider the labelling that marks the other four positive and pp negative. Any rectangle containing all four positives must contain their bounding box, which contains pp — so pp is included and labelled positive. That labelling is unachievable, so no set of 5 points is shattered.

Hence VC=4\text{VC} = 4.

Sanity check against the general pattern: an axis-aligned rectangle in Rd\mathbb{R}^d is specified by 2d2d numbers and has VC dimension 2d2d, giving 4 for d=2d = 2. ✓

Exercise 2

A model class has VC dimension 500. You have 1,000 training examples and zero training error. Should you trust the model?

Show solution

No. The ratio VC/n=500/1000=0.5\text{VC}/n = 500/1000 = 0.5 is far too high.

Plugging into the asymptotic form, the generalisation gap is on the order of

5001000=0.50.71\sqrt{\frac{500}{1000}} = \sqrt{0.5} \approx 0.71

A bound permitting a gap of 0.71 is vacuous for a classifier whose error is between 0 and 1 — it tells you the true error could be anywhere.

The intuition behind the formalism: a class with VC dimension 500 can shatter 500 points, meaning it can produce any labelling of 500 points. With only 1,000 examples, achieving zero training error is weak evidence of learning, because the class is flexible enough to fit a large fraction of arbitrary label patterns. Zero training error is close to guaranteed whether or not signal exists.

What to do:

  1. Hold out a test set and measure. The theory is loose; the empirical gap is what matters.
  2. Reduce effective complexity — regularise, constrain, or use a simpler class. Aim for VCn\text{VC} \ll n.
  3. Run the randomisation check. Shuffle the labels and refit. If training error is still near zero, the model is memorising, and the zero error on real labels means nothing.
  4. Get more data. For a meaningful bound you would want nn in the tens of thousands at VC=500\text{VC} = 500.

That completes Machine Learning Foundations. Next module: Supervised Learning, where these principles are applied to specific algorithms, starting with Simple Linear Regression.