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.

KDE and Gaussian-mixture CDFs

Smooth, differentiable, invertible marginal maps, and the one knob each of them has

01 · KDE and Gaussian-mixture CDFs

Open In Colab

The empirical CDF of notebook 00 is consistent but jagged, a staircase with no derivative, and it knows nothing beyond the largest and smallest sample. Both problems go away if we estimate a smooth density and integrate it into a CDF. Two estimators dominate in practice: the kernel density estimate, which is nonparametric, and the Gaussian mixture, which is parametric and is the marginal layer of gauss_flows.

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
sklearn         : 1.8.0
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 we use one bimodal sample of N=2000N = 2000 points, half from N(−2,0.52)\mathcal{N}(-2, 0.5^2) and half from N(1.5,0.82)\mathcal{N}(1.5, 0.8^2), so that the bandwidth and the number of components visibly matter.

N = 2000, sample mean = -0.247, sample std = 1.868

1. A KDE-based CDF

A kernel density estimate puts a smooth bump of width h>0h > 0, the bandwidth, on every sample xn∈Rx_n \in \mathbb{R}. With a Gaussian kernel it reads

p^h(x)=1N∑n=1NN(x;xn,h2)\hat{p}_h(x) = \frac{1}{N} \sum_{n=1}^{N} \mathcal{N}(x; x_n, h^2)

Integrating term by term turns every Gaussian bump into a normal CDF, so the CDF F^h\hat{F}_h of (1) is the mixture CDF (2) with M=NM = N components, equal weights wm=1/Nw_m = 1/N, means at the samples and the common scale hh. It is smooth and strictly increasing, which is exactly what the marginal map (3) needs, and rbig.KDEGaussianizer builds Φ−1(F^h(x))\Phi^{-1}(\hat{F}_h(x)) with Scott’s rule h=N−1/5sh = N^{-1/5} s for the bandwidth, where ss is the sample standard deviation.

We first build the same KDE with SciPy, to draw its density and its CDF F^h\hat{F}_h next to the empirical CDF F^N\hat{F}_N.

Scott's bandwidth h = 0.409

rbig.KDEGaussianizer builds the marginal map on the same KDE; we Gaussianize the sample, invert it back and test the result against N(0,1)\mathcal{N}(0, 1).

KDE Gaussianization: std of z = 0.915
round-trip error = 5.0e-13
KS test against N(0, 1): p = 0.027
<Figure size 1500x450 with 3 Axes>

Figure 1:Left: the Gaussian KDE with Scott’s bandwidth traces both modes of the histogram. Middle: its CDF is the smooth curve the ECDF staircase approximates. Right: the Gaussianized sample z=Φ−1(F^h(x))z = \Phi^{-1}(\hat{F}_h(x)) against the standard-normal density, slightly too narrow.

The KDE traces both modes, its CDF is smooth, and the inverse returns the data to better than 10-12. The output is not quite a standard normal, though: its standard deviation is below one and the KS test rejects at the 5 % level. This is the price of smoothing. F^h\hat{F}_h is the CDF of the data blurred by the kernel, a distribution with variance s2+h2s^2 + h^2, so the samples sit inside it and land too close to the centre. Shrinking hh removes the bias and brings back the roughness, which is the trade-off of section 3.

2. The Gaussian-mixture CDF

The parametric alternative fits a mixture of MM Gaussians, with weights wmw_m, means μm\mu_m and scales σm\sigma_m, whose density is

px(x)=∑m=1MwmN(x;μm,σm2)p_x(x) = \sum_{m=1}^{M} w_m \mathcal{N}(x; \mu_m, \sigma_m^2)

and whose CDF is (2). The KDE (1) is the special case with one component per sample; a mixture keeps MM small, so it has only 3M−13M - 1 free parameters, is differentiable in all of them and extrapolates into the tails through its outer components. Its forward map (3) is a closed-form sum and its inverse is the root find (4) of Part 0. This is the marginal layer MixtureGaussianCDF that every parametric Gaussianization flow in gauss_flows stacks.

A second helper builds an MM-component layer and swaps those three parameters in.

For each layer we Gaussianize the sample, invert it back by bisection, and score it by its mean negative log-likelihood, the change of variables (4) averaged over the samples.

from_data (initialisation)  std of z = 0.862, KS p = 2e-11, NLL = 1.7712, round trip = 1.6e-15
EM fit                      std of z = 1.000, KS p = 0.93, NLL = 1.6397, round trip = 6.8e-15

scikit-learn scores its own EM fit the same way, so its negative log-likelihood tells us whether the loaded layer computes the same density.

scikit-learn NLL of the EM fit         = 1.6397
EM weights [0.497 0.503], means [ 1.511 -1.986]

To draw the fitted mixture we run the EM-loaded layer over the grid and read its density off the change of variables, px(x)=N(z;0,1)∣T′(x)∣p_x(x) = \mathcal{N}(z; 0, 1) \vert T'(x) \vert.

<Figure size 1500x450 with 3 Axes>

Figure 2:Left: the two-component EM fit against the histogram. Middle: the mixture CDF of the layer, read off as Φ(z)\Phi(z). Right: the Gaussianized sample of the EM-loaded layer is a standard normal, while the from_data initialisation is too narrow.

Loaded from the EM fit, the layer turns the bimodal sample into a standard normal: unit standard deviation, a KS test that does not reject, and a mean negative log-likelihood identical to scikit-learn’s, so the layer computes the same density. The round trip through the bisection inverse is exact to better than 10-14. The from_data initialisation, with equal weights and scales set from quantile spacing, gives a narrower output that the KS test clearly rejects, which is why it is a starting point for training rather than a fit.

3. Choosing the knob: bandwidth and components

Each estimator has one capacity knob, and choosing it is the usual trade-off between bias and variance.

The KDE bandwidth hh. Too small and p^h\hat{p}_h is a spiky overfit with high variance, whose CDF has phantom wiggles and whose derivative is noisy. Too large and it smears the two modes into one, with high bias. Plug-in rules such as Scott’s and Silverman’s Silverman, 1986 give a sensible default.

The number of components MM. The Bayesian information criterion Schwarz, 1978 trades the log-likelihood of the fitted mixture against its 3M−13M - 1 free parameters,

BIC(M)=(3M−1)log⁡N−2∑n=1Nlog⁡px(xn)\mathrm{BIC}(M) = (3M - 1) \log N - 2 \sum_{n=1}^{N} \log p_x(x_n)

where pxp_x is the MM-component mixture (2) fitted by EM, and the smallest value wins. We start with three bandwidths: one too small, Scott’s, and one too large.

too small h = 0.093, std of z = 0.993
Scott     h = 0.409, std of z = 0.915
too large h = 1.121, std of z = 0.765

Then BIC (3) for mixtures of one to seven components, each fitted by EM.

BIC by M: {1: 8191, 2: 6597, 3: 6627, 4: 6642, 5: 6667, 6: 6685, 7: 6708}
BIC is smallest at M = 2
<Figure size 1200x450 with 2 Axes>

Figure 3:Left: KDEs of the same sample at three bandwidths; the smallest is spiky and the largest erases the valley between the modes. Right: BIC against the number of mixture components, smallest at the true number of modes.

The bandwidth sweep shows the two failures, and the printed lines show the bias of section 1 in numbers: the standard deviation of the Gaussianized sample falls from 0.993 to 0.765 as hh grows. A small bandwidth gives an output close to unit variance but a rough CDF, a large one a smooth CDF and a narrow output. BIC (3) is smallest at M=2M = 2, the true number of modes; more components buy too little likelihood to pay for their parameters.

4. The log-determinant of a mixture CDF

A flow needs the per-coordinate log-determinant of its marginal map, which (5) gives as the log-density of the data minus the standard-normal log-density at zz. For the mixture the density is (2), a smooth function of xx, so the log-determinant is smooth too. The histogram CDF of notebook 00 had a piecewise-constant density instead. MixtureGaussianCDF.transform_and_log_det returns this log-determinant, and we check it against the derivative that autodiff computes through the forward map.

log-determinant against autodiff: max abs difference = 9.4e-15
density of the layer against scikit-learn: max abs difference = 2.2e-16
<Figure size 700x450 with 1 Axes>

Figure 4:The log-determinant of the EM-loaded mixture layer. It dips in the low-density valley between the modes and rises into the tails, where the standard-normal density at zz shrinks faster than the data density.

The log-determinant of the layer matches autodiff to machine precision, and the density it implies matches scikit-learn’s. The low-density valley between the modes is the dip in the middle, and the curve rises into the tails, because there the standard-normal density at zz falls faster than the mixture density at xx. This smooth, exact term is what a flow sums in log_prob.

Recap

EstimatorCDFKnobHow to choose it
KDE (1)(2) with M=NM = Nbandwidth hhScott, Silverman, cross-validation
Gaussian mixture (2)(2)components MMBIC (3), held-out likelihood
ToolRole
rbig.KDEGaussianizernonparametric smooth marginal
rbig.GMMGaussianizerparametric marginal, fitted by EM
MixtureGaussianCDFparametric marginal bijection with a log-determinant, the flow layer

Next. Mixtures are smooth and compact, but their inverse needs a root find. Notebook 02 builds marginals from monotone splines, including the rational-quadratic spline whose inverse is closed form.

References
  1. Silverman, B. W. (1986). Density Estimation for Statistics and Data Analysis. Chapman.
  2. Schwarz, G. (1978). Estimating the Dimension of a Model. The Annals of Statistics, 6(2), 461–464. 10.1214/aos/1176344136