Module contents
Lesson 7 of 20Core 110 min

Rademacher complexity

Build Rademacher complexity as a measure of correlation with pure noise. Derive a generalization bound through McDiarmid and ghost-sample symmetrization, Massart’s finite-class lemma and structural properties including Talagrand’s contraction lemma.

After this lesson you can

  • Define empirical and expected Rademacher complexity for a function class and its loss class.
  • Prove E[sup_h (R(h) - R̂(h))] ≤ 2ℜ_n(loss ∘ H) using symmetrization with a ghost sample.
  • Use McDiarmid to prove a high-probability version including computable empirical complexity.
  • Prove Massart’s lemma and apply it to finite classes.
  • Use scaling, sums, convex hulls and contraction to bound composite classes.
  • Estimate empirical Rademacher complexity of concrete classes using Monte Carlo.

Before you start

Capacity to fit noise

A class may contain infinitely many functions and still be unable to follow arbitrary labels on a sample. VC dimension counts possible labelings; Rademacher complexity asks a quantitative question: how much correlation can the class achieve with independent signs carrying no signal? Noise fitting is a property of the class on observed points, not a direct measure of one function’s error.

The supremum is chosen after seeing the signs. If F\mathcal F contains one fixed function, its sum has expectation zero; capacity arises because one can choose among alternatives. For binary functions h∈{−1,1}h\in\{-1,1\}, maximum correlation equals 22 times optimal sign accuracy minus 11. The class of all functions on distinct points attains correlation 1. A class’s ability to fit noise does not show all its algorithms fail to generalize; the algorithm and distribution matter too.

InteractiveFit pure noise
++++++++++++

Circles above: + sign; below: − sign. Symbols on the line show the optimal prediction. Polynomials are represented by up to three sign changes.

Class
12
1 rounds
Current correlation0.333
Average0.333
Massart for thresholds0.654
In each round find the class’s best labeling on the selected points. The observed average estimates an expectation over signs.

Oriented thresholds produce n+1n+1 patterns, intervals produce 1+n(n+1)/21+n(n+1)/2, and a polynomial of degree at most three has at most three sign changes on ordered points. Conversely, placing roots between changes yields every pattern with up to three changes. Constants and empty output are included. For this finite class on the sample, Massart gives R^≤2log⁡(n+1)/n\widehat{\mathfrak R}\le\sqrt{2\log(n+1)/n} for thresholds. An average over a few rounds can exceed that expectation bound without contradicting it.

A sharper bound gives order 1/n1/\sqrt n without the logarithm. If Tk=∑i=1kσiT_k=\sum_{i=1}^k\sigma_i with T0=0T_0=0, the best threshold has sum Tn−2min⁡0≤k≤nTkT_n-2\min_{0\le k\le n}T_k. The mean of TnT_n is zero and, by Doob’s maximal inequality in L2L^2, Emax⁡k∣Tk∣≤2n\mathbb E\max_k|T_k|\le2\sqrt n. Therefore threshold complexity is at most 4/n4/\sqrt n. This order bound uses the ordered structure, not Massart alone.

Each function corresponds to vector (f(z1),…,f(zn))(f(z_1),\ldots,f(z_n)). The supremum is the support function of the vector set evaluated in direction σ\sigma, divided by nn. Its average is a normalized random width. If the class is not symmetric, this mean support should not be confused with a bilateral geometric width without an additional factor.

InteractiveWidth of the class on three points

Blue points: class; orange: maximum in the green direction. The projection rotates, but products are computed in three dimensions.

Class
35
Signs σ-1, -1, -1
Maximum product / 31.000
Exact mean0.667
Rotate the cube, change class and explore all eight directions. The highlighted point maximizes the dot product in the chosen direction.

Symmetrization and a ghost sample

Assume ZiZ_i are i.i.d. and F⊆[0,1]Z\mathcal F\subseteq[0,1]^\mathcal Z. Write Pf=Ef(Z)Pf=\mathbb E f(Z) and PSf=n−1∑if(Zi)P_Sf=n^{-1}\sum_i f(Z_i). Let S′S' be an independent copy of SS. For the one-sided deviation Φ(S)=sup⁡f(Pf−PSf)\Phi(S)=\sup_f(Pf-P_Sf),

ESΦ(S)=ESsup⁡fES′(PS′f−PSf)≤ES,S′sup⁡f(PS′f−PSf)=ES,S′,σsup⁡f1n∑iσi[f(Zi′)−f(Zi)]≤Rn(F)+Rn(F)=2Rn(F).\begin{aligned} \mathbb E_S\Phi(S) &=\mathbb E_S\sup_f\mathbb E_{S'}(P_{S'}f-P_Sf)\\ &\le\mathbb E_{S,S'}\sup_f(P_{S'}f-P_Sf)\\ &=\mathbb E_{S,S',\sigma}\sup_f\frac1n\sum_i\sigma_i[f(Z_i')-f(Z_i)]\\ &\le\mathfrak R_n(\mathcal F)+\mathfrak R_n(\mathcal F)=2\mathfrak R_n(\mathcal F). \end{aligned}

The first inequality exchanges supremum and expectation. In the following equality, each pair (Zi,Zi′)(Z_i,Z_i') has the same distribution when swapped; introducing independent signs performs those swaps. The last line separates the supremum of a sum and uses sign symmetry. This is the symmetrization argument; it does not remove the factor 2.

InteractiveSwap with the ghost sample
Pair 1
σ = +1
z1 | z′1
pending
Pair 2
σ = −1
z2 | z′2
pending
Pair 3
σ = −1
z3 | z′3
pending
Pair 4
σ = +1
z4 | z′4
pending
0 of 4 pairs processed
Pairs processed0/4
Swaps0
Replay a sign sequence: negative signs swap the pairs. The equality is in distribution, not individual values for one fixed sample.

A simultaneous guarantee

Changing one observation of SS changes Φ(S)\Phi(S) by at most 1/n1/n because functions lie between 0 and 1. By McDiarmid and symmetrization, with probability at least 1−δ1-\delta, simultaneously for every f∈Ff\in\mathcal F,

Pf≤PSf+2Rn(F)+log⁡(1/δ)2n.Pf\le P_Sf+2\mathfrak R_n(\mathcal F)+\sqrt{\frac{\log(1/\delta)}{2n}}.

R^S\widehat{\mathfrak R}_S also changes by at most 1/n1/n when one observation is replaced. A second one-sided application of McDiarmid gives Rn≤R^S+log⁡(2/δ)/(2n)\mathfrak R_n\le\widehat{\mathfrak R}_S+\sqrt{\log(2/\delta)/(2n)} with failure at most δ/2\delta/2. Applying the first inequality with δ/2\delta/2 and taking a union over failures gives the empirical generalization bound

Pf≤PSf+2R^S(F)+3log⁡(2/δ)2n.Pf\le P_Sf+2\widehat{\mathfrak R}_S(\mathcal F)+3\sqrt{\frac{\log(2/\delta)}{2n}}.

It applies to the loss class in [0,1][0,1] and holds for a hypothesis chosen after seeing the sample because the event is simultaneous. For losses in [a,b][a,b], concentration terms are multiplied by b−ab-a. To use a Monte Carlo complexity estimate inside the guarantee, add an upper bound on its error and allocate an additional part of δ\delta; the average alone is not a certified bound.

Massart: a maximum of sub-Gaussian sums

Let finite A⊂RnA\subset\mathbb R^n and r=max⁡a∈A∥a∥2r=\max_{a\in A}\|a\|_2. For λ>0\lambda>0, Jensen, the exponential sum and Eeλσiai=cosh⁡(λai)≤eλ2ai2/2\mathbb E e^{\lambda\sigma_i a_i}=\cosh(\lambda a_i)\le e^{\lambda^2a_i^2/2} give

Eσmax⁡a∈A⟨σ,a⟩≤λ−1log⁡∑a∈AEeλ⟨σ,a⟩≤log⁡∣A∣λ+λr22.\begin{aligned} \mathbb E_\sigma\max_{a\in A}\langle\sigma,a\rangle &\le\lambda^{-1}\log\sum_{a\in A}\mathbb E e^{\lambda\langle\sigma,a\rangle}\\ &\le\frac{\log|A|}{\lambda}+\frac{\lambda r^2}{2}. \end{aligned}

Minimizing at λ=2log⁡∣A∣/r\lambda=\sqrt{2\log|A|}/r, Massart’s lemma gives

R^S(F)≤r2log⁡∣A∣n,A={(f(z1),…,f(zn)):f∈F}.\widehat{\mathfrak R}_S(\mathcal F)\le \frac{r\sqrt{2\log|A|}}n, \quad A=\{(f(z_1),\ldots,f(z_n)):f\in\mathcal F\}.

If r=0r=0 or ∣A∣=1|A|=1, complexity is zero and the limiting case is used. For values in [−1,1][-1,1], r≤nr\le\sqrt n. In binary classification, ∣A∣|A| is the number of sample traces and is at most the growth function ΠH(n)\Pi_{\mathcal H}(n). This recovers control by log⁡∣H∣\log|\mathcal H| or by Sauer when VC dimension is finite. The bound may exceed 1: still true, but uninformative.

Properties for building bounds

For nonempty classes and c≥0c\ge0, structural properties of empirical complexity include:

  • Scaling: R^S(cF)=cR^S(F)\widehat{\mathfrak R}_S(c\mathcal F)=c\widehat{\mathfrak R}_S(\mathcal F). For c<0c<0 use sign symmetry and factor ∣c∣|c|.
  • Class sums: R^S(F+G)=R^S(F)+R^S(G)\widehat{\mathfrak R}_S(\mathcal F+\mathcal G)=\widehat{\mathfrak R}_S(\mathcal F)+\widehat{\mathfrak R}_S(\mathcal G) if all pairs (f,g)(f,g) are independently allowed. For a coupled sum only an upper bound is guaranteed.
  • Convex hull: R^S(conv⁡F)=R^S(F)\widehat{\mathfrak R}_S(\operatorname{conv}\mathcal F)=\widehat{\mathfrak R}_S(\mathcal F), because a linear functional has the same supremum over a class and its convex combinations.
  • Translation by a fixed function: adding it does not change the expectation, although it changes each realization of the supremum.

If each ϕi\phi_i is LL-Lipschitz, the contraction lemma states

Eσsup⁡f1n∑iσiϕi(f(zi))≤LR^S(F).\mathbb E_\sigma\sup_f\frac1n\sum_i\sigma_i\phi_i(f(z_i))\le L\widehat{\mathfrak R}_S(\mathcal F).

To see the mechanism, condition on all signs except one and group the remaining terms as afa_f. Averaging the last sign gives half of sup⁡f(af+ϕi(uf))+sup⁡g(ag−ϕi(ug))\sup_f(a_f+\phi_i(u_f))+\sup_g(a_g-\phi_i(u_g)). Combining suprema, ϕi(uf)−ϕi(ug)≤L∣uf−ug∣\phi_i(u_f)-\phi_i(u_g)\le L|u_f-u_g|. Swapping ff and gg when necessary permits replacement by the average with LuLu and the corresponding sign. Repeating coordinate by coordinate proves the result. Constants ϕi(0)\phi_i(0) vanish in expectation; they need not be zero in this version without an exterior absolute value. The 0-1 loss is not Lipschitz as a function of a real margin, so this step cannot be applied directly.

For HB={x↦w⊤x:∥w∥2≤B}\mathcal H_B=\{x\mapsto w^\top x:\|w\|_2\le B\}, Cauchy-Schwarz resolves the supremum:

R^S(HB)=BnEσ∥∑iσixi∥2≤Bn∑i∥xi∥22.\widehat{\mathfrak R}_S(\mathcal H_B)=\frac Bn\mathbb E_\sigma\left\|\sum_i\sigma_i x_i\right\|_2\le\frac Bn\sqrt{\sum_i\|x_i\|_2^2}.

The inequality uses Jensen and zero expected cross terms. It does not explicitly depend on dimension if input norms are controlled. Applying contraction to ramp loss ℓy(t)=min⁡(1,max⁡(0,1−yt))\ell_y(t)=\min(1,\max(0,1-yt)) gives a bound for ramp loss, which in turn dominates 0-1 error when y∈{−1,1}y\in\{-1,1\}.

InteractiveWhen does the bound become nonvacuous?
  • ramp bound
  • test error
  • trivial bound
Dimension
Maximum norm B
First n with bound < 1200
Test error at n=30000.016
Bound at n=30000.562
Reproducible synthetic data: compare the ramp-loss bound with independent test error and the trivial bound 1.

The figure uses one nested training sample and 2000 independent test points generated with seed 1307. The learned vector points in the mean direction of yixiy_i x_i and is scaled to norm BB; it is not exact ERM. The curve uses the deterministic empirical bound B∑i∥xi∥2/nB\sqrt{\sum_i\|x_i\|^2}/n, not an uncorrected sign average. The test line is an observed proportion, not population risk. δ=.05\delta=.05 is fixed for one chosen class; exploring many classes and selecting one after observing bounds requires splitting confidence or another argument.

Estimating complexity

For a finite class stored as a matrix, each draw calculates an exact maximum. For the linear ball use the formula above; for one-dimensional stumps enumerate thresholds and orientations. The standard error below quantifies only Monte Carlo variation conditional on SS, not sampling uncertainty of SS or a guaranteed upper bound.

[ ]Monte Carlo for three classesBrowser
import numpy as np
rng=np.random.default_rng(1307)
n=40; draws=4000
x=np.sort(rng.uniform(-1,1,n))
stumps=np.array([s*np.where(x>=t,1,-1) for s in (-1,1) for t in np.r_[-2,(x[:-1]+x[1:])/2,2]])
signs=rng.choice([-1,1],size=(draws,n))
values=np.max(signs@stumps.T,axis=1)/n
massart=np.sqrt(2*np.log(len(stumps))/n)
print('stumps:',round(values.mean(),4),'error estándar MC:',round(values.std(ddof=1)/np.sqrt(draws),4),'Massart:',round(massart,4))
X=np.column_stack((x,x*x)); B=2
linear=B*np.linalg.norm(signs@X,axis=1)/n
print('bola lineal:',round(linear.mean(),4),'error estándar MC:',round(linear.std(ddof=1)/np.sqrt(draws),4),'cota Jensen:',round(B*np.sqrt((X*X).sum())/n,4))
assert values.mean() <= massart + .03

Gaussian complexity replaces each sign with a standard normal and measures the same kind of supremum in Gaussian directions. Comparisons exist under appropriate conditions and constants; they are not identical sample by sample. To reproduce an estimate, fix sample and seed, report draw count and standard error, and check that the maximum is actually solved for the chosen class.

The next experiment uses random labels and scikit-learn models. Training accuracy transformed into correlation measures the fitting procedure’s capacity in those rounds; if the optimizer does not find its class maximum, it is a lower estimate, not necessarily exact complexity. It does not turn random training error into a universal generalization diagnostic.

[ ]Trees and neighbors versus noiseBrowser
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
rng=np.random.default_rng(1308)
X=rng.normal(size=(120,4)); scores={str(k):[] for k in (1,3,8)}; scores['1-NN']=[]
for trial in range(20):
    y=rng.choice([-1,1],size=len(X))
    for depth in (1,3,8):
        model=DecisionTreeClassifier(max_depth=depth,random_state=trial).fit(X,y)
        scores[str(depth)].append(2*model.score(X,y)-1)
    model=KNeighborsClassifier(n_neighbors=1).fit(X,y)
    scores['1-NN'].append(2*model.score(X,y)-1)
for name,v in scores.items(): print(name,'correlación media',round(np.mean(v),3),'error estándar',round(np.std(v,ddof=1)/np.sqrt(len(v)),3))

Exercises

Proof
Prove: Prove symmetrization of the one-sided deviation.
0
Introduce an independent ghost sample and then Rademacher signs.

Step 1 of 2. Why does the supremum move after the ghost expectation?

Proof
Prove: Prove Massart’s lemma.
0
Bound the maximum with the sum of exponentials and use the sign moment-generating function.

Step 1 of 2. What bound holds for the moment-generating function of a vector a?

Numeric
If A contains 1024 vectors in R¹⁰⁰ with norm at most 10, calculate Massart’s bound r√(2 ln|A|)/n.
Quiz
Which identity holds for nonempty classes with well-defined suprema and expectations?
CodeEmpirical complexity from signs
Implement empirical_rademacher(loss_matrix, n_draws, rng) for a matrix with one function per row and one datum per column, and linear_rademacher(X, B, n_draws, rng) for a data matrix with one observation per row. Both return the Monte Carlo mean of the normalized supremum. Use rng.choice([-1,1], size=...) and numpy.
def empirical_rademacher(loss_matrix, n_draws, rng):
  pass
def linear_rademacher(X, B, n_draws, rng):
  pass
Shift+Enter to run

Summary

Key idea
Rademacher complexity measures optimal fitting to independent signs. The ghost sample produces the factor 2; McDiarmid turns that expectation into a uniform guarantee. Massart and contraction control specific classes, always using the relevant loss class.

The proofs and conventions draw on Bartlett and Mendelson and Chapter 3 of Mohri, Rostamizadeh and Talwalkar.