Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

ECDF and histograms

The atom of Gaussianization, from rank to uniform to normal, and where the empirical CDF breaks

00 · ECDF and histograms

Open In Colab

Part 0 described a density destructor as marginal Gaussianizations (2) interleaved with rotations (3). Part 1 builds the marginal piece, the map that turns one coordinate’s distribution into a standard normal. Every method in the curriculum, from RBIG to coupling and spline flows, stacks these one-dimensional maps, so getting them right is most of the work; the rest is composition (1).

Python implementation: CPython
Python version       : 3.13.12
IPython version      : 9.12.0

jax             : 0.12.1
numpy           : 2.4.3
scipy           : 1.17.1
matplotlib      : 3.11.2
rbig            : 0.1.16
gauss_flows_maps: 0.3.0

Compiler    : Clang 21.1.4 
OS          : Linux
Release     : 6.8.0-1044-azure
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

Throughout, the data are N=6000N = 6000 draws of one coordinate x∈R≥0x \in \mathbb{R}_{\ge 0} from a unit exponential, a skewed marginal with a hard edge at zero and a long right tail, far from Gaussian.

x: shape (6000, 1), min 0.0001, max 9.68

1. The probability integral transform

Everything rests on one classical fact. If a continuous variable xx has a strictly increasing CDF FF, then FF applied to its own draws is uniform,

x∼px  ⟹  τ=F(x)∼U(0,1)x \sim p_x \implies \tau = F(x) \sim \mathcal{U}(0, 1)

which is the probability integral transform. The probit Φ−1\Phi^{-1} then sends that uniform level to a standard normal, and the two steps together are the marginal map z=Φ−1(F(x))z = \Phi^{-1}(F(x)) of (3). We write it T=Φ−1∘FT = \Phi^{-1} \circ F when we need its derivative.

In one dimension this Gaussianizes exactly, provided we know FF. We never do, so we estimate it from the samples, and the two simplest estimators, the empirical CDF and the histogram, show where the difficulty lies.

2. The empirical CDF and the DKW rate

The empirical CDF of the samples x1,…,xNx_1, \ldots, x_N counts how many fall at or below a point,

F^N(x)=1N∑n=1N1[xn≤x]\hat{F}_N(x) = \frac{1}{N} \sum_{n=1}^{N} \mathbf{1}[x_n \le x]

a right-continuous staircase that jumps by 1/N1/N at each sample. The Glivenko–Cantelli theorem says that the worst-case gap sup⁡x∣F^N(x)−F(x)∣\sup_x \vert \hat{F}_N(x) - F(x) \vert tends to zero almost surely, and the Dvoretzky–Kiefer–Wolfowitz inequality Dvoretzky et al., 1956, with the constant 2 that Massart later showed is tight, says how fast: for any gap λ>0\lambda > 0,

Pr⁡(sup⁡x∣F^N(x)−F(x)∣>λ)≤2exp⁡(−2Nλ2)\Pr(\sup_x \vert \hat{F}_N(x) - F(x) \vert > \lambda) \le 2 \exp(-2 N \lambda^2)

Setting the right-hand side to 0.05 gives a band λN=log⁡(40)/(2N)\lambda_N = \sqrt{\log(40) / (2 N)} that holds the whole empirical CDF with probability at least 0.95, and it shrinks like N−1/2N^{-1/2}. We measure the gap on a grid for growing subsets of the data.

We evaluate it on a grid over [0,6][0, 6], for 15 subset sizes NN from 10 to 5000, and compare each with the true CDF F(x)=1−e−xF(x) = 1 - e^{-x}.

fitted slope of log gap against log N: -0.45
every gap inside the 95% DKW band: True
<Figure size 1200x450 with 2 Axes>

Figure 1:Left: the empirical CDF of 20, 200 and 2000 samples closes in on the exponential CDF. Right: the worst-case gap against the sample size NN stays inside the 95% DKW band λN\lambda_N and falls at about its N−1/2N^{-1/2} rate.

The fitted slope is close to −1/2-1/2 and every measured gap sits inside the band, so the empirical CDF is a consistent estimate of FF with a known rate. That is the guarantee behind every rank-based Gaussianization.

3. Rank, uniform, normal

Putting F^N\hat{F}_N into (1) gives the rank transform: the uniform level of a sample is close to its rank divided by NN, and the probit of that level is its Gaussianized value. rbig.MarginalUniformize does the first step and rbig.MarginalGaussianize both. In gauss_flows the same map is a chain of two bijections, a HistogramCDF that sends data to uniform and an InverseGaussCDF that applies the probit, so it also carries a log-determinant (section 5).

The gauss_flows version chains two bijections, so the same map also returns its log-determinant.

We compare rbig’s levels with the rank over NN, the two estimators’ levels with each other, and the moments of both latents.

rbig level against rank / N: max gap 8.3e-05
rbig against HistogramCDF levels (sorted): max gap 0.035
        rbig: mean +0.000, std 1.000, range [-3.76, 3.76]
HistogramCDF: mean +0.013, std 0.964, range [-1.81, 3.71]
<Figure size 1800x450 with 3 Axes>

Figure 2:The marginal map in three stages on exponential data. Left: the data. Middle: the uniform levels τ\tau from rbig’s empirical CDF. Right: the Gaussianized values from rbig and from a 64-bin HistogramCDF, against N(0,1)\mathcal{N}(0, 1); the histogram cannot resolve the steep density next to zero, so its lower tail stops near -1.8.

rbig’s level is the rank over NN to within 10-4, so it really is the empirical CDF, and its output has mean zero and unit standard deviation. The 64-bin histogram is coarser. Its first bin is wider than the region where the exponential density is steepest, and its edges are padded past the smallest sample, so the smallest samples land at a level of a few percent instead of near zero and the lower tail of zz stops near -1.8. A histogram trades the empirical CDF’s jumps for a resolution set by its bin width.

4. The boundary problem

The empirical CDF puts the largest sample at a level near one and the smallest near zero, and it knows nothing outside the observed range. The probit of a level near zero or one is huge, and the inverse has no information about where between the extreme samples a tail point should go, so tail samples do not survive the round trip. This is the saturated-CDF failure of Part 0, notebook 05 seen from the estimator’s side. rbig’s remedy is support extension, pdf_extension, which pads the estimated density a fraction of the data range beyond the extremes so the inverse has room to place tail samples.

pdf_extension = 0.0: worst round-trip error 7.62e-01 at x = 9.68
pdf_extension = 0.1: worst round-trip error 1.17e-02 at x = 6.79
the worst error falls by a factor of 65
<Figure size 1200x450 with 1 Axes>

Figure 3:Round-trip error of rbig’s uniformization at each sample, without and with a 10% support extension. Without it the error spikes at the largest samples; with it the worst error is far smaller, though not zero.

Support extension cuts the worst error by the printed factor, but it is a heuristic, a guess about mass beyond the data. The HistogramCDF makes the opposite choice and refuses to guess: a point outside its fitted range is clamped to the boundary level and gets a log-determinant of −∞-\infty, which says the point is outside the support of the fitted CDF.

x =   -0.5: z = -4.75, log-det = -inf
x =  100.0: z = +4.75, log-det = -inf

The z=±4.75z = \pm 4.75 there is the clamp of (2) at ε=10−6\varepsilon = 10^{-6}. Neither estimator has a model of the tail, which is the first reason Part 1 moves on to smooth CDFs.

5. The log-determinant of a marginal map

A marginal map earns its place in a flow only if we can compute its log-determinant, the term that change of variables (4) adds to log⁡pz\log p_z. For T=Φ−1∘FT = \Phi^{-1} \circ F the chain rule, with F′=pxF' = p_x and the derivative of Φ−1\Phi^{-1} at τ\tau equal to 1/N(z;0,1)1 / \mathcal{N}(z; 0, 1), gives

T′(x)=px(x)/N(T(x);0,1)T'(x) = p_x(x) / \mathcal{N}(T(x); 0, 1)

and so, per coordinate,

log⁡∣T′(x)∣=log⁡px(x)−log⁡N(z;0,1)\log \vert T'(x) \vert = \log p_x(x) - \log \mathcal{N}(z; 0, 1)

with z=T(x)z = T(x). The log-determinant of a marginal map is its own log-density re-referenced to the standard normal, and a DxD_x-dimensional marginal Gaussianization sums it over coordinates, because its Jacobian is diagonal. With an estimated CDF, pxp_x is replaced by the density of the estimate, p^x=F^′\hat{p}_x = \hat{F}'.

This exposes the second flaw of the empirical CDF. F^N\hat{F}_N is a staircase, so p^x\hat{p}_x is a sum of point masses: zero between samples and undefined at them. The log-determinant (5) is then −∞-\infty almost everywhere, so the rank transform Gaussianizes but is not a density model. A histogram with LL bins of width hh, where bin ll holds NlN_l samples, has the piecewise-constant density

p^x(x)=Nl/(Nh), for x in bin l\hat{p}_x(x) = N_l / (N h), \text{ for } x \text{ in bin } l

which gives the first finite log-determinant. We check that the gauss_flows chain returns exactly (5), then compare it with the true log-determinant of the exponential, whose CDF 1−e−x1 - e^{-x} we know.

chain log-det against log p - log N(z): max gap 0.0e+00

On a grid over (0,6](0, 6], where the exponential’s CDF 1−e−x1 - e^{-x} gives the true log-determinant, we compare it with the histogram’s.

The true log-determinant follows from (5) with the exponential’s own density and CDF.

histogram log-det against the truth on (0, 6]: median gap 0.12
<Figure size 1200x450 with 2 Axes>

Figure 4:Left: the density of the 64-bin HistogramCDF, constant within each bin, against the exponential density. The first bin starts below the smallest sample, so its density is too low next to zero. Right: its log-determinant against the true one; it follows the truth on average but inherits the bin structure, and its jumps grow in the right tail, where each bin holds few samples.

The two agree to machine precision on the identity, so the log-determinant a gauss_flows marginal reports is the formula above. Against the truth it is finite and right on average, with a median gap of about 0.1 nats, but jagged: it moves smoothly within each bin and jumps at the edges, more so in the sparse right tail. A flow trained by gradient descent wants a smooth p^x\hat{p}_x, so that the log-determinant and its gradients are smooth too, and that is what the kernel, mixture and spline CDFs of the next notebooks provide.

Recap

StepEquationIn code
data to uniform(1) with F^N\hat{F}_N, (2)rbig.MarginalUniformize, HistogramCDF
uniform to normal(3)InverseGaussCDF
full marginal mapT=Φ−1∘F^NT = \Phi^{-1} \circ \hat{F}_Nrbig.MarginalGaussianize, Chain([hist, InverseGaussCDF(...)])
consistency(3)Figure 1
tail fixsupport extensionpdf_extension
log-determinant(5), (6)transform_and_log_det

Next. The empirical CDF is consistent but jagged and blind to the tails. The next notebook replaces the staircase with a smooth density estimate, a kernel density or a Gaussian mixture, whose CDF and log-determinant are analytic and differentiable.

References
  1. Dvoretzky, A., Kiefer, J., & Wolfowitz, J. (1956). Asymptotic Minimax Character of the Sample Distribution Function and of the Classical Multinomial Estimator. The Annals of Mathematical Statistics, 27(3), 642–669. 10.1214/aoms/1177728174