A histogram with m bins of width h is biased when the bins are wide and noisy when they are narrow. Its risk cannot be computed from the data, because it involves the unknown density, but the leave-one-out score Ĵ(h) estimates it (up to a constant) without bias, and it costs nothing: Ĵ(h) = 2/((n − 1)h) − (n + 1)/((n − 1)h) Σp̂j2, from the bin proportions alone. Move the number of bins and watch the histogram and the score; switch on the confidence envelope; and on simulated data, compare the score of one sample with the exact risk curve it estimates.
Bins are [a + (j − 1)h, a + jh) on a fixed interval: [0, 0.2] for the redshifts (as in the book), [−7, 16] for the glass refractive index, [−4, 4] for the simulated densities (Normal mixtures restricted to that interval). The flat range lists every m whose score is within 3% of the minimum. Simulated sample number r uses its own seeded generator, so the same settings give the same sample.
code/ch20_nonparametric_curve_estimation.py (hist_cv, hist_band, sample_truncated, hist_expected_cv).