Sample Complexity: How Many Examples a Guarantee Requires
Compute how many examples classic learning bounds demand, see what drives the numbers, and learn why practice often needs fewer.
Key takeaways
- If some candidate model is perfect, the data needed grows like 1/ε; if none is, it grows like 1/ε².
- The number of candidate models matters only through its logarithm, so wide searches are cheaper than intuition suggests.
- Halving the error gap in the noisy setting takes four times as many examples.
- Testing one fixed model needs far less data than guaranteeing a whole class at once.
- Worst-case bounds guide scaling, while learning curves on your own data give practical estimates.
Sample complexity is the number of training examples needed to guarantee a stated level of performance with a stated probability. For a finite set of candidate models, the classic answer grows with the logarithm of the number of candidates, with the logarithm of 1/δ, and with either 1/ε or 1/ε², where ε is the allowed error gap and δ the allowed chance of failure.
Those few facts explain a lot about why some requests for accuracy are cheap and others ruinous.
Setting up the question
Fix a class H of classifiers and assume examples are drawn independently from one distribution. Training error is the fraction of mistakes on the n examples; true error is the probability of a mistake on a new example.
Two settings behave differently.
- In the realizable setting, some classifier in H makes no errors at all. You return any classifier with zero training error and want its true error to be at most ε.
- In the agnostic setting, you assume nothing about whether H contains a good classifier. You want every classifier's training error to be within ε of its true error.
Both guarantees must hold with probability at least 1 − δ. The PAC learning article derives the first with a threshold example.
The two basic formulas
For finite H:
realizable: n ≥ ( ln|H| + ln(1/δ) ) / ε
agnostic: n ≥ ln( 2·|H| / δ ) / (2ε²) The agnostic formula comes from Hoeffding's inequality, which bounds how far an average of bounded independent values strays from its mean, followed by a union bound over all |H| classifiers. Concentration inequalities explains that tool.
In the agnostic setting, if every training error is within ε of the truth, then the classifier with the lowest training error has true error within 2ε of the best in the class. To promise a gap of ε between your pick and the best, substitute ε/2 into the formula.
Worked numbers
The table uses δ = 0.05 and rounds up.
| Candidates in H | ε | Realizable n | Agnostic n |
|---|---|---|---|
| 1,000 | 0.05 | 199 | 2,120 |
| 1,000,000 | 0.05 | 337 | 3,501 |
| 1,000 | 0.01 | 991 | 52,984 |
Check the first row by hand. ln(1000) ≈ 6.908 and ln(20) ≈ 2.996, so the realizable bound is 9.904/0.05 ≈ 198.1. For the agnostic bound, ln(2 × 1000/0.05) = ln(40,000) ≈ 10.597, divided by 2 × 0.0025 = 0.005, gives about 2,119.3.
Multiplying the candidates by a thousand raised the requirement by about two thirds. Tightening ε from 0.05 to 0.01 in the agnostic case raised it twenty-five fold, exactly the square of five.
Hypothetical example: the numbers are made up to show the method, not a quote or an average.
Why noise changes the exponent
In the realizable case, a bad classifier is exposed by a single mistake. If its true error exceeds ε, the chance that n examples never catch it is at most (1 − ε)ⁿ, which falls quickly.
With noise, no single mistake rules anything out. You have to estimate error rates and compare them. The spread of an estimated rate shrinks like 1/√n, so distinguishing rates that differ by ε needs n on the order of 1/ε².
Real problems often sit between the two. When the best classifier's error is small, variance is small too, and sharper bounds give rates closer to 1/ε. This is the idea behind Bernstein-type and "fast rate" results.
Infinite classes and VC dimension
Most useful classes have infinitely many members, since weights are real numbers. The VC dimension d takes over the role of ln|H|. Up to constant factors:
| Setting | Examples sufficient, order of magnitude |
|---|---|
| Realizable | ( d·ln(1/ε) + ln(1/δ) ) / ε |
| Agnostic | ( d + ln(1/δ) ) / ε² |
There are matching lower bounds, apart from logarithmic factors in the realizable case. For any learning algorithm, some distribution forces it to use on the order of at least d/ε examples (realizable) or d/ε² (agnostic) once ε and δ are small. So the dependence on d and ε is not just an artifact of loose proofs in the worst case.
A rough finite-class estimate is also possible for a model stored with p parameters of 32 bits each. It can represent at most 2^(32p) different functions, so ln|H| ≤ 32p·ln 2. With p = 1,000, that is about 22,181. In the realizable formula with ε = 0.05 and δ = 0.05, it asks for roughly 444,000 examples. The bound is valid and very pessimistic, since many bit patterns compute nearly identical functions.
Testing one model is much cheaper
Once training is finished and you hold one fixed model, set |H| = 1. To estimate its error within ±0.05 with 95% confidence, the Hoeffding formula needs ln(40)/0.005 ≈ 737.8, so 738 fresh examples. For ±0.01 it needs about 18,445.
This is why held-out evaluation is so powerful. It avoids paying for the whole class. The condition is that the model was chosen without looking at those examples.
If you compare 20 finished models on one test set, use |H| = 20. The requirement for ±0.05 becomes ln(800)/0.005 ≈ 1,337.
Rare classes change the arithmetic
The formulas count the examples relevant to the quantity you care about. If you need recall, the share of true positives that the model finds, only positive examples inform it.
Say positives make up 1% of the data and you want recall within ±0.05 with 95% confidence for a finished model. The Hoeffding calculation asks for about 738 positive examples. At a 1% rate, you would expect to need roughly 73,800 randomly sampled examples to collect that many.
You can often do better by sampling positives deliberately, since recall is computed within the positive class. Quantities that depend on the class mix, such as precision, then need the original proportions restored through weighting.
The same logic applies to small subgroups. A test set that is large overall may hold only a few dozen examples from one region or device type, and any claim about that slice carries a correspondingly wide margin. Plan the collection around the smallest group you intend to report on.
Absolute and relative gaps
An error gap of 0.01 means something different for a model with 30% error and one with 0.5% error. For rare failures, you usually care about relative precision, and additive bounds like Hoeffding's are wasteful.
A useful special case: if you observe zero failures in n independent trials, an approximate 95% upper limit on the failure rate is 3/n. This is often called the rule of three. To support a claim that the rate is below 0.001, you need about 3,000 failure-free trials. It follows from solving (1 − p)ⁿ = 0.05 for small p, since −ln(0.05) ≈ 3.0.
The claim holds only for inputs drawn the same way as those trials. It says nothing about conditions that were never sampled.
A small calculator
import math
def realizable(H, eps, delta):
return math.ceil((math.log(H) + math.log(1 / delta)) / eps)
def agnostic(H, eps, delta):
return math.ceil(math.log(2 * H / delta) / (2 * eps ** 2))
for H, eps in [(1, 0.05), (1000, 0.05), (1000, 0.01)]:
print(H, eps, realizable(H, eps, 0.05), agnostic(H, eps, 0.05)) You can also turn the formulas around. With n examples, the agnostic gap is ε = √( ln(2|H|/δ) / (2n) ). This form is handy when the data set is what it is and you want to know what can be claimed.
From theory to a data collection plan
Bounds answer "what can be promised for any distribution." A project usually needs "how much data will my problem need." Those are different questions.
For the second, train on growing subsets of your data, for instance 1/8, 1/4, 1/2 and all of it, and plot validation error against n. Error often falls roughly like a power of n toward a floor. Extrapolate cautiously, repeat with several seeds, and check rare classes separately, since they run out of examples first. Estimating whether you have enough data walks through that process.
Rules of thumb such as a fixed number of examples per parameter should not be treated as guarantees. The need depends on noise, feature quality, the prior knowledge built into the model and whether you start from a pretrained network.
Several things can lower the practical requirement far below a worst-case figure. A large margin between classes makes the boundary easy to locate. Features that encode domain knowledge leave less for the model to discover. A pretrained network starts close to a useful solution, so fine-tuning explores a much smaller effective set. Letting the learner choose which examples to label, known as active learning, can reduce labeling needs in favorable problems, though not in every one.
Other things push the requirement up. Label noise hides the signal. Many near-duplicate rows add less information than their count suggests, as do repeated measurements of the same person or machine.
Theory still earns its keep here. It tells you that cutting an estimation gap in half will probably cost several times the data, that a bigger search is less expensive than a tighter target, and that data from a different distribution does not count toward the total in any of these formulas.
Common questions
Do the bounds apply if I tuned hyperparameters?
They apply if H includes every model your procedure could have returned. Adaptive choices made after seeing results make that set hard to count, which is one reason to keep an untouched test set.
Are more features always more expensive?
In worst-case terms, adding features raises capacity. If a new feature carries strong signal, the error floor drops and you may need less data to reach a target.
Why does δ matter so little?
It sits inside a logarithm. Going from δ = 0.05 to δ = 0.0005 adds ln(100) ≈ 4.6 to the numerator.