Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.The shape of the data, one bar at a time
Sort measurements into bins and count them: a histogram shows where values are common and where they are rare. Its whole message depends on the bin width. Very thin bins make a jagged comb that mostly shows luck. Very wide bins merge everything into a few blocks and can hide a second peak. The right width depends on how much data you have and what the shape looks like, and there are principled rules for choosing it.
Histograms are the first look at every telemetry channel, test result and residual. They feed binned features, histogram-based detectors such as HBOS, and quantisation for embedded systems. A misleading bin width can hide a bimodal failure population or invent structure that is not there, so it is worth knowing how to choose it.
- 1. Very narrow bins make a histogram…
- 2. Very wide bins can…
- 3. With more data, the best bin width usually…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.The estimator and the rules by hand
Histogram density. With bins of width h starting at origin x₀, the estimate at x is (count in x’s bin)/(n h). It integrates to 1. For the data 0.1, 0.2, 0.7, 1.5 with h = 0.5 and origin 0, the density at 0.15 is 2/(4 · 0.5) = 1.
Sturges (1926). k = ⌈log₂ n⌉ + 1 bins across the range:
- n = 1000 gives 11 bins;
- even n = 10⁶ gives only 21.
Scott (1979). h = 3.49 s n^(−1/3), optimal if the data are normal. For s = 1 and n = 1000, h = 0.349. Multiplying n by 8 halves h.
Freedman–Diaconis (1981). h = 2 IQR n^(−1/3). For normal data IQR = 1.349σ, so h ≈ 2.70σ n^(−1/3), or 0.270 for n = 1000. The IQR ignores the tails, which makes the rule robust to outliers and heavy tails.
Leave-one-out cross-validation (Rudemo, 1982). Choose h to minimise J(h) = 2/((n − 1)h) − (n + 1)/((n − 1)h) Σⱼ p̂ⱼ², where p̂ⱼ is the fraction of points in bin j. This is an unbiased estimate of the integrated squared error, up to a constant.
The steps in plain English:
- Look at the data’s shape: skewed, heavy-tailed or multimodal?
- Start from Freedman–Diaconis, which is robust, and compare with cross-validation if n is moderate or large.
- Avoid Sturges for n beyond a few hundred.
- Check robustness by shifting the origin by half a bin and by halving or doubling h; trust only features that survive.
- 1. Sturges' bins for n = 100?
- 2. Freedman–Diaconis uses the…
- 3. Cross-validation for histograms estimates…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.Bias, variance and the optimal width
Variance. A bin with probability p holds a count with variance np(1 − p), so the density estimate’s variance is about f(x)/(nh). Integrated, it gives 1/(nh).
Bias. Within a bin, the estimate is flat while f slopes, and the squared bias integrates to about h² R(f′)/12, where R(f′) = ∫ f′(x)² dx measures roughness.
Optimum. MISE(h) ≈ 1/(nh) + h² R(f′)/12 is minimised at h* = (6/(n R(f′)))^(1/3), with MISE* ∝ n^(−2/3). For a normal density, R(f′) = 1/(4√π σ³), giving h* = (24√π)^(1/3) σ n^(−1/3) = 3.49 σ n^(−1/3): Scott’s rule.
Consequences.
- Rules derived for the normal oversmooth any density rougher than the normal (bimodal, sharply peaked), because those have larger R(f′) and need narrower bins.
- Sturges’ bin count grows only like log n, so its width shrinks far too slowly, and its error stops improving or even worsens as n grows.
Exercise 1 (1000 standard normal values). Widths, with the integrated squared error (ISE) against the true density:
- Sturges 0.633 (0.0069);
- Scott 0.341 (0.0042);
- Freedman–Diaconis 0.261 (0.0052);
- cross-validation 0.475 (0.0051).
For normal data, Scott’s rule was best, as theory predicts.
import numpy as np
edges = np.histogram_bin_edges(x, bins="fd") # also "scott", "sturges", "auto" (max of Sturges and FD), "doane"
counts, edges = np.histogram(x, bins=edges, density=True)- 1. Histogram MISE at the optimal width scales as…
- 2. Normal-reference rules oversmooth densities that are…
- 3. Where does the 3.49 in Scott's rule come from?
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Origin sensitivity and ASH. With h fixed, shifting all edges by a fraction of h changes the estimate, sometimes making a mode appear or vanish. Averaging m histograms with origins shifted by h/m gives the averaged shifted histogram (Scott, 1985). As m → ∞ it approaches a kernel density estimate with a triangular kernel, removing the origin artefact while keeping histogram speed.
Unequal widths. Variable-width bins, such as equal-frequency or Bayesian blocks, adapt to local density. Bayesian blocks (Scargle et al., 2013) choose change points optimally under a likelihood criterion and are popular in astrophysics for event data.
Beyond MISE. For visual mode detection, the bin width that minimises MISE may still show spurious bumps. Other criteria, such as Wand’s plug-in rule or rules targeting the number of modes, balance faithfulness against readability.
Interview questions.
- Which bin rule should be the default? Freedman–Diaconis, or numpy’s “auto”, which takes the smaller width of Sturges and FD. Check with cross-validation or a KDE overlay. Never trust a single histogram for a claim about multimodality.
- Why do histograms of the same data look different in two tools? They use different default rules and different origins. Fix both explicitly in reports.
- 1. Averaging shifted histograms approaches…
- 2. Bayesian blocks choose…
- 3. numpy's bins='auto' uses…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Which rule, for which shape?
History.
- K. Pearson (1895) coined “histogram”.
- H. Sturges (1926) proposed his rule.
- D. Scott (1979) derived the normal-reference rule.
- D. Freedman and P. Diaconis (1981) proposed the IQR-based rule.
- M. Rudemo (1982) introduced least-squares cross-validation.
- D. Scott (1985) introduced averaged shifted histograms.
Exercise 2. Integrated squared error ×1000, mean of 4 samples, against the true density.
| density | n | Sturges | Scott | Freedman–Diaconis | cross-validation |
|---|---|---|---|---|---|
| normal | 100 | 23.8 | 16.1 | 16.6 | 34.3 |
| 1000 | 4.9 | 4.0 | 4.9 | 5.9 | |
| 10 000 | 3.0 | 0.7 | 0.7 | 0.8 | |
| lognormal | 100 | 104.5 | 78.9 | 21.6 | 80.7 |
| 1000 | 69.8 | 18.3 | 12.8 | 13.1 | |
| 10 000 | 94.2 | 16.3 | 3.7 | 2.1 | |
| bimodal | 100 | 39.2 | 80.5 | 98.0 | 50.6 |
| 1000 | 19.1 | 22.9 | 29.7 | 7.1 | |
| 10 000 | 12.3 | 5.3 | 6.4 | 1.6 | |
| t (3 d.f.) | 100 | 43.7 | 29.5 | 15.5 | 21.3 |
| 1000 | 45.3 | 4.2 | 4.4 | 4.6 | |
| 10 000 | 71.8 | 1.0 | 0.7 | 1.0 |
- Sturges got worse with more data on skewed and heavy-tailed densities (lognormal 104 → 94, t₃ 44 → 72). Its few bins span a range stretched by extreme values, so the bulk is crushed into one or two bins.
- Scott’s rule was best for normal data but oversmoothed the bimodal density (5.3 against 1.6 for cross-validation at n = 10 000).
- Freedman–Diaconis was the most robust single rule for skewed and heavy-tailed data, but it also oversmoothed the bimodal density: its IQR spans both modes.
- Cross-validation adapted best with enough data (bimodal at n ≥ 1000, lognormal at 10 000), but it was the noisiest choice at n = 100, worst of all for normal data.
- No rule wins everywhere: robust rules for small samples, cross-validation for large ones, and always look at more than one width.
Histograms in AI, robotics and aerospace
- Telemetry dashboards: fix bin rules and origins so that histograms of the same channel are comparable across flights.
- Feature binning: quantile or FD-based bins for gradient-boosting histograms, HBOS, and embedded lookup tables.
- Test-data analysis: spotting bimodal failure populations, such as two suppliers or two failure mechanisms, which demands checking several widths before drawing conclusions.
References
- H. A. Sturges (1926). “The choice of a class interval.” Journal of the American Statistical Association 21(153).
- D. W. Scott (1979). “On optimal and data-based histograms.” Biometrika 66(3).
- D. Freedman, P. Diaconis (1981). “On the histogram as a density estimator: L₂ theory.” Zeitschrift für Wahrscheinlichkeitstheorie und verwandte Gebiete 57(4).
- M. Rudemo (1982). “Empirical choice of histograms and kernel density estimators.” Scandinavian Journal of Statistics 9(2).
- D. W. Scott (1985). “Averaged shifted histograms: effective nonparametric density estimators in several dimensions.” Annals of Statistics 13(3).