Search

Search papers and lessons

VC Dimension

Level 3 · 11 min read

The VC dimension of a family of classifiers is the largest number of points it can label in every possible way, and that number controls how much training error can mislead you.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • Learning basics
  • Linear algebra
  • Classical models
  • ReadReady to readNot yet

TL;DR

A classifier family is a set of rules, such as all straight lines in the plane, that each label points as positive or negative. Its VC dimension is the largest number of points that the family can label in every possible way. This number measures how flexible the family is, and it controls how far the error on training examples can drift from the error on new examples. A family with a small VC dimension cannot memorise much, so good training results are more trustworthy.

Builds on: Linear Classifier, Counting Principles · Leads to: Rademacher Complexity, Overfitting

Where it's used

  • Choosing how complex a model may be. Before training a Supervised Learning spam filter on a few thousand emails, a team can count the features and ask whether a linear rule over them is flexible enough to memorise the training set. The VC dimension gives a number for that flexibility, so the team can compare it with the size of the dataset.
  • Explaining why large models can still generalise. Theory based on VC dimension was one of the first general accounts of why learning from finite samples works. It set the vocabulary, the words "shattering" and "capacity", that later generalisation bounds still use.
  • Support vector machines. A Support Vector Machine is a linear classifier with a margin. The margin-based generalisation bounds for it are built on the VC analysis of linear classifiers, whose VC dimension is the number of features plus one.
  • Comparing model families on paper. Before running an experiment, a researcher can say that a Feedforward Neural Network with many weights has a VC dimension that grows with its number of weights, so the bound on its gap between train and test error is correspondingly loose.

How it works

We use the simplest family that has a non-trivial answer: threshold classifiers on the number line. A threshold tt labels a point xx as positive if x≥tx \ge t and negative otherwise.

  1. Pick one point. Put x1=5x_1 = 5 on the line. Can a threshold label it positive? Yes, with t=4t = 4. Can it label it negative? Yes, with t=6t = 6. Both labelings occur, so the family handles this one point in every way. Check: there are 21=22^1 = 2 labelings for one point, and both are achievable.
  2. Add a second point. Put x2=9x_2 = 9 to the right, so x1<x2x_1 < x_2. The four labelings are (negative, negative), (negative, positive), (positive, positive) and (positive, negative). Try the last one. To make x1x_1 positive and x2x_2 negative we would need x1≥t>x2x_1 \ge t > x_2, which is impossible when x1<x2x_1 < x_2. So three of the four labelings occur. Check: the family handles two points in only three of the four ways.
  3. Conclude the definition. A set of points is shattered if the family realises every labeling of it. One point is shattered, and no pair is. The VC dimension of thresholds is 1.
  4. Add the general counting idea. For mm points, a family H\mathcal{H} can realise at most 2m2^m labelings. The number it realises on a specific set of mm points is the count from step 2. The largest such count over all sets of mm points is its growth function. A set is shattered exactly when its count reaches 2m2^m.
  5. Why it matters. When the growth function stays small as mm grows, the family cannot fit random labels on many points, so the gap between training and test error shrinks as data grows.
  6. Check against the definition. Thresholds on a line: one point shattered, two points not, so the VC dimension is 1.
flowchart TD
  A[Choose m points] --> B[List every labeling of the m points]
  B --> C{Does the family produce every labeling?}
  C -- yes --> D[The set is shattered, so the VC dimension is at least m]
  C -- no --> E[Try another set of m points]
  E --> B

The diagram is the test in step 3: for each candidate set, list the labelings and check whether the family produces all of them. The VC dimension is the largest mm for which some set passes.

The math

Let H\mathcal{H} be a family of functions h:X→{−1,+1}h : \mathcal{X} \to \{-1, +1\}. For a finite set S={x1,…,xm}S = \{x_1, \ldots, x_m\}, the set of labelings that H\mathcal{H} produces is

ΠH(S)={(h(x1),…,h(xm)):h∈H}.\Pi_{\mathcal{H}}(S) = \{ (h(x_1), \ldots, h(x_m)) : h \in \mathcal{H} \}.

Step 3 in symbols: shattering and the VC dimension

SS is shattered by H\mathcal{H} when ∣ΠH(S)∣=2m|\Pi_{\mathcal{H}}(S)| = 2^m. The VC dimension is

VC⁡(H)=max⁡{m:some set of m points is shattered by H}.\operatorname{VC}(\mathcal{H}) = \max\{ m : \text{some set of } m \text{ points is shattered by } \mathcal{H} \}.

Read it as the largest size at which at least one set of points can be labelled every way. One shattered set of size mm is enough to count, but the VC dimension is the largest mm for which that happens. So no set of d+1d + 1 points can be shattered when the VC dimension is dd.

Step 4 in symbols: the growth function

The growth function is ΠH(m)=max⁡∣S∣=m∣ΠH(S)∣\Pi_{\mathcal{H}}(m) = \max_{|S| = m} |\Pi_{\mathcal{H}}(S)|, the most labelings any mm points can produce. By the counting principle from Counting Principles, it is at most 2m2^m. The Sauer–Shelah lemma says that once mm exceeds the VC dimension dd, it grows only polynomially:

ΠH(m)≤∑i=0d(mi).\Pi_{\mathcal{H}}(m) \le \sum_{i=0}^{d} \binom{m}{i}.

The right side is a polynomial in mm of degree dd, which is far smaller than 2m2^m when mm is large.

Beyond thresholds: linear classifiers

A linear classifier in Rd\mathbb{R}^d is h(x)=sign⁡(w⊤x+b)h(\mathbf{x}) = \operatorname{sign}(\mathbf{w}^{\top}\mathbf{x} + b). Its VC dimension is d+1d + 1. In the plane (d=2d = 2), lines have VC dimension 3. This matches the examples below.

The generalisation bound, in order of magnitude

Let the family have VC dimension dd and let the training set hold mm examples drawn at random. With probability at least 1−δ1 - \delta over the random draw of the examples (the usual Probability statement), every hh in the family has test error at most its training error plus a term of order

dlog⁡m+log⁡(1/δ)m.\sqrt{\frac{d \log m + \log(1/\delta)}{m}}.

The constants are omitted here on purpose. The point is the shape: the gap is governed by dd and shrinks like 1/m1/\sqrt{m}. The proof uses tail bounds such as Concentration Inequalities. Empirical Risk Minimization trains a model to make the training error small, and this bound controls the rest.

Worked examples

Example
Example 1: thresholds, the tiny case
  1. Family: thresholds ht(x)=+1h_t(x) = +1 if x≥tx \ge t, else −1-1.
  2. One point x1x_1: labelings (+1)(+1) and (−1)(-1) both occur (choose tt below or above x1x_1). Shattered.
  3. Two points x1<x2x_1 < x_2: the labeling (+1,−1)(+1, -1) would need x1≥t>x2x_1 \ge t > x_2, impossible. Not shattered.
  4. So the largest shattered size is 1.
  5. Interpretation: a threshold rule can only switch once along the line, so it cannot represent an alternating pattern on two points. Its VC dimension is 1.
Example
Example 2: lines in the plane, the standard case
  1. Family: all lines in the plane, labeling points on one side +1+1 and the other side −1-1.
  2. Three points that are not on one line form a triangle. Each of the 8 labelings can be drawn: all positive or all negative by a line far away; one positive point by a line cutting off one corner; two positive points by a line separating that pair from the third. So the triangle is shattered.
  3. Four points: either one point lies inside the triangle of the other three, or the four form a convex quadrilateral. In the first case, labeling the inner point +1+1 and the three outer points −1-1 cannot be done by a line. In the second case, labeling the two diagonal pairs with opposite signs (the XOR pattern) cannot be done by a line.
  4. So no four points are shattered, and the VC dimension of lines in the plane is 3.
  5. Interpretation: 3 equals d+1d + 1 with d=2d = 2, matching the formula in the math section.
Example
Example 3: a realistic spam filter

A linear spam filter uses 10 word-count features, so d=10d = 10 and its VC dimension is d+1=11d + 1 = 11. It is trained on m=1000m = 1000 emails.

  1. The VC dimension is 11, so the flexibility term in the bound scales with d=11d = 11.
  2. The term is of order (11log⁡1000+log⁡(1/δ))/1000\sqrt{(11 \log 1000 + \log(1/\delta)) / 1000}. With log⁡1000≈6.9\log 1000 \approx 6.9, the numerator is about 76+log⁡(1/δ)76 + \log(1/\delta), and dividing by 1000 and taking the square root gives a term of order 0.3 before the constant.
  3. Interpretation: the bound on the train-test gap is loose at this sample size, because the constant is large and the guarantee is worst-case over all linear rules. It still tells the team that more emails shrink the gap, and that adding features without more data widens it.
  4. A practical response is to validate on held-out emails rather than rely on the bound.

Try it: intervals [a,b][a, b] on the line, labelled +1+1 inside and −1-1 outside. What is the VC dimension? Answer: 2. Two points x1<x2x_1 < x_2 can be labelled in all four ways by choosing the interval to contain either, both or neither. For three points x1<x2<x3x_1 < x_2 < x_3, the labeling (+1,−1,+1)(+1, -1, +1) would need an interval containing x1x_1 and x3x_3 but not x2x_2, which is impossible. So the largest shattered size is 2.

Common mistakes and tradeoffs

  • Thinking VC dimension counts parameters. It is tempting to say a model with 100 weights has VC dimension 100. In fact VC dimension measures the labelings a family can produce, which can be far smaller or larger than the parameter count. Linear classifiers happen to have d+1d + 1 parameters (weights plus bias) and VC dimension d+1d + 1, but that match does not carry over to other families.
  • Confusing "some set" with "every set". It is tempting to think a family fails to shatter as soon as one set cannot be shattered. In fact the definition needs only one set of size mm that is shattered. Four points that are not shattered do not rule out a different set of three that is.
  • Assuming a large VC dimension means overfitting. It is tempting to conclude that a high VC dimension always overfits. In fact the bound is worst-case, and a large family can still generalise when the data are well structured and training stops early (see Early Stopping). VC dimension tells you what is possible, not what happens.
  • Trusting the bound's numbers. The bound gives a valid guarantee but is often loose enough to be useless for real sample sizes. Use it to compare families and to see how error scales, not to predict test error.

Use VC dimension when you want a worst-case, distribution-free statement about a family of classifiers, or when you want to compare the flexibility of two families. For a number on a specific problem, look to a held-out set (see Train, Validation and Test Sets) or to Cross-Validation, which measure the real error instead of a bound. Look to Rademacher Complexity when you want a data-dependent bound that is often tighter.

History

Vladimir Vapnik and Alexey Chervonenkis introduced the idea in the late 1960s and published their uniform convergence results in 1971. Their work showed that, for many families, the difference between training and test frequencies can be controlled uniformly over the whole family, through the shattering property. The field was slow to take it up in practice, partly because the bounds were loose. In the 1990s, Vapnik's support vector machines brought the ideas into machine learning practice, and his 1998 book on statistical learning theory made the framework standard.

Recap

  • A classifier family is a set of rules that each give labels to points.
  • A set of points is shattered when the family produces every labeling of it.
  • The VC dimension is the largest size at which some set can be shattered.
  • Thresholds on a line have VC dimension 1, lines in the plane have 3, and linear classifiers in Rd\mathbb{R}^d have d+1d + 1.
  • The VC dimension controls a bound on the gap between training and test error, of order d/m\sqrt{d / m} up to logs.
  • Next, read Rademacher Complexity for a data-dependent alternative, and Overfitting for what the gap looks like in practice.

Check yourself

QuestionIn your own words, what does it mean for a set of points to be shattered by a family of classifiers?Show answer

A set is shattered when the family can produce every possible labeling of its points. For mm points there are 2m2^m labelings, so shattering means the family realises all of them.

QuestionWhat is the VC dimension of intervals on the number line that are labelled positive inside?Show answer

Two points can be shattered, as shown in the Try-it example, and three points cannot, because the labeling that is positive at the ends and negative in the middle needs a non-interval. So the answer is 2.

QuestionWhy does a model with a small VC dimension generalise more reliably from a fixed amount of data?Show answer

A family with a small VC dimension can produce few labelings on any set of points, so it cannot fit random labels well. That keeps the gap between training and test error small, because the bound on the gap grows with the VC dimension and shrinks with the number of examples.

QuestionLines in the plane have VC dimension 3. How many features does a linear classifier need to have VC dimension 11, and why does that matter for a dataset of 50 examples?Show answer

VC dimension d+1=11d + 1 = 11 needs d=10d = 10 features. With only 50 examples the bound on the train-test gap is large, because the flexibility term is big compared with the sample size, so training error alone says little about test error.

QuestionGive one reason a high VC dimension does not guarantee overfitting in practice.Show answer

The VC bound is a worst-case statement over all rules in the family. Real training uses a specific algorithm, often with early stopping or regularisation, that picks a rule far from the worst case, so the actual gap can be small.

Sources