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.

Gaussianity diagnostics

Did we reach a standard Gaussian? QQ-plots, moments, negentropy and a joint energy test

06 · Gaussianity diagnostics

Open In Colab

We can build a Gaussianization and trust its arithmetic. The last foundational question is how we know it worked: that the latent z=f(x)\vect{z} = f(\vect{x}) really follows N(0,I)\mathcal{N}(\vect{0}, \mat{I}), the base density of the log-density (4). Gaussianity has two levels and each needs its own diagnostic. Coordinate by coordinate, is every marginal a standard normal? Jointly, is the whole vector a standard normal, which also requires the coordinates to be independent?

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

numpy     : 2.4.3
scipy     : 1.17.1
matplotlib: 3.11.2
rbig      : 0.1.16

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

1. Three states to diagnose

We take N=2000N = 2000 points of the two-moons dataset in Dx=2D_x = 2 dimensions, standardised per coordinate, and fit an RBIG model of 30 layers, each a marginal Gaussianization followed by a random rotation. We keep three states to compare: the data x\vect{x}, the data after one marginal Gaussianization only, m\vect{m}, and the fully Gaussianized latent z\vect{z}. We also keep every intermediate state xk\vect{x}_k of (2), for section 5.

30 RBIG layers fitted

The latent is the last state, and the marginal-only state applies one marginal Gaussianization to the data with no rotation.

2. Marginal diagnostics: QQ-plots and moments

The cheapest check is per coordinate. Write zd∈Rz_d \in \mathbb{R} for coordinate d∈{1,…,Dx}d \in \{1, \ldots, D_x\} of z\vect{z}, with density pzdp_{z_d}, mean μd\mu_d and standard deviation σd\sigma_d. A QQ-plot sorts the NN samples of zdz_d and plots the ii-th smallest against the standard-normal quantile Φ−1((i−0.5)/N)\Phi^{-1}((i - 0.5) / N), where Φ\Phi is the standard-normal CDF. If zd∼N(0,1)z_d \sim \mathcal{N}(0, 1), the points fall on the identity line. Two scalar summaries put a number on what the plot shows, the skewness (asymmetry) and the excess kurtosis (tail weight),

γ1,d=Epzd[(zd−μd)3]/σd3\gamma_{1,d} = \mathbb{E}_{p_{z_d}}[(z_d - \mu_d)^3] / \sigma_d^3
γ2,d=Epzd[(zd−μd)4]/σd4−3\gamma_{2,d} = \mathbb{E}_{p_{z_d}}[(z_d - \mu_d)^4] / \sigma_d^4 - 3

both zero for a Gaussian. Figure 1 shows the first coordinate of the three states.

<Figure size 1500x500 with 3 Axes>

Figure 1:QQ-plots of the first coordinate of the data (left), the data after one marginal Gaussianization (centre) and the Gaussianized latent (right), against standard-normal quantiles. The dashed line is where a standard normal falls.

                              skewness       excess kurtosis
data x                 [-0.003  0.003]       [-0.883 -1.251]
marginal only m                [0. 0.]       [-0.016 -0.016]
Gaussianized z         [-0.     0.002]         [0.027 0.017]

The data’s QQ-plot is S-shaped: the two-moons marginals have light tails, with excess kurtosis -0.88 and -1.25. Their skewness is already near zero, because the two moons are symmetric, so for this dataset skewness alone would miss the problem. After Gaussianization the points sit on the line and both moments are within 0.03 of zero. The middle panel matters most: one marginal Gaussianization, with no rotation at all, already passes both marginal checks.

3. Negentropy and total correlation

Skewness and kurtosis only probe the third and fourth moments. The complete measure of how far one marginal is from a Gaussian is its negentropy, the entropy it lacks compared with the Gaussian of the same mean and variance.

Notebook 03 defined both measures. Negentropy (3), applied to one coordinate, compares pzdp_{z_d} with the Gaussian N(μd,σd2)\mathcal{N}(\mu_d, \sigma_d^2) of the same mean and variance, and total correlation (5) is the sum of the marginal entropies minus the joint one. Both are non-negative: Neg[pzd]=0\mathrm{Neg}[p_{z_d}] = 0 exactly when coordinate dd is Gaussian, and TC[pz]=0\mathrm{TC}[p_z] = 0 exactly when the coordinates are independent.

Together they account for the whole distance to the standard normal.

Driving (3) to zero is what RBIG does: marginal Gaussianization removes the negentropy term, and the rotations turn the remaining total correlation into new marginal non-Gaussianity for the next layer to remove. We estimate the negentropy of each coordinate with rbig.negentropy, which estimates H[pzd]\mathbb{H}[p_{z_d}] with a kernel density estimate. Once a flow is trained, gauss_flows estimates the same quantities from the flow itself, with negentropy and total_correlation.

negentropy of data x          : [0.1199 0.1703] nats
negentropy of marginal only m : [0. 0.] nats
negentropy of Gaussianized z  : [0.0014 0.0009] nats

Figure 2 compares the three states coordinate by coordinate.

<Figure size 1200x450 with 1 Axes>

Figure 2:Estimated negentropy of each coordinate of the three states. One marginal Gaussianization removes it as completely as the full RBIG model does.

The data carries 0.12 and 0.17 nats of negentropy, and both the marginal-only state and the Gaussianized latent are at or below 0.002 nats.

<Figure size 1500x500 with 3 Axes>

Figure 3:Samples of the data (left), the data after one marginal Gaussianization (centre) and the Gaussianized latent (right). The centre panel has standard-normal marginals but keeps the structure of the two moons.

4. A joint test: the energy distance

For the joint verdict we compare pzp_z with N(0,I)\mathcal{N}(\vect{0}, \mat{I}) through the energy distance of Székely and Rizzo, a two-sample statistic built from Euclidean distances alone,

ED[pz]=2E∥z−g∥2−E∥z−z′∥2−E∥g−g′∥2\mathrm{ED}[p_z] = 2 \mathbb{E} \Vert \vect{z} - \vect{g} \Vert_2 - \mathbb{E} \Vert \vect{z} - \vect{z}' \Vert_2 - \mathbb{E} \Vert \vect{g} - \vect{g}' \Vert_2

where z,z′∈RDx\vect{z}, \vect{z}' \in \mathbb{R}^{D_x} are independent draws of pzp_z and g,g′∈RDx\vect{g}, \vect{g}' \in \mathbb{R}^{D_x} independent draws of N(0,I)\mathcal{N}(\vect{0}, \mat{I}).

We estimate (4) by replacing each expectation with an average over n=1000n = 1000 samples of pzp_z and nn fresh standard-normal samples. To turn the estimate into a test we need its spread when pzp_z really is N(0,I)\mathcal{N}(\vect{0}, \mat{I}): we compute the same statistic between 200 pairs of standard-normal samples, and the p-value is the fraction of those null statistics at least as large as the observed one. The classical alternative is the Henze–Zirkler test of multivariate normality; the energy test needs no tables and takes a few lines.

To compare a state with the standard normal, we pick nn of its rows and draw nn fresh standard-normal rows.

The null distribution repeats the statistic between two genuine standard-normal samples of the same size, and the p-value counts the null statistics that reach the observed one.

                     energy distance   p-value
data x                        0.0406     0.005
marginal only m               0.0312     0.005
Gaussianized z                0.0031     0.547

The test separates what the marginal checks could not. The data and the marginal-only state both get p=0.005p = 0.005, the smallest value 200 null draws can give (1/2011/201), so normality is rejected for both, while the Gaussianized latent gets p=0.55p = 0.55 and is consistent with N(0,I)\mathcal{N}(\vect{0}, \mat{I}).

5. Watching RBIG converge

The same statistic, computed once on every intermediate state xk\vect{x}_k, shows how many layers the joint Gaussianization needs. The grey band is the central 95 % of the null distribution: a state inside it cannot be told apart from N(0,I)\mathcal{N}(\vect{0}, \mat{I}) with n=1000n = 1000 samples.

layer 0: 0.0570, layer 30: 0.0054
null band: [0.0014, 0.0096]

We then find the first layer whose state enters the band, and count the later states that leave it again.

first state inside the null band: layer 3
states from there on above the band: 0 of 28

Figure 4 plots the statistic against the number of layers applied, with the null band behind it.

<Figure size 1200x450 with 1 Axes>

Figure 4:Energy distance from each RBIG state to the standard normal, against the number of layers applied. The band is the central 95 % of the statistic between two standard-normal samples.

The energy distance falls from 0.057 on the data into the null band by layer 3 and stays inside it for every later layer, so the remaining layers change nothing a test with n=1000n = 1000 samples can detect. This is a practical stopping rule: keep adding layers until the joint statistic enters the null band, a decision that the noisy negentropy estimates of nearly Gaussian states cannot make reliably.

Recap

LevelDiagnosticValue for a standard normalTool
marginal shapeQQ-plotpoints on the identity linescipy.stats.norm.ppf
marginal momentsskewness (1), excess kurtosis (2)0 and 0scipy.stats.skew, scipy.stats.kurtosis
marginal informationnegentropy (3)0rbig.negentropy
dependencetotal correlation (5)0flow-based total_correlation
jointenergy distance (4), with a p-valueinside the null bandscipy.spatial.distance.cdist

By (3), the marginal checks only see the second term of the distance to N(0,I)\mathcal{N}(\vect{0}, \mat{I}), so a Gaussianization is certified by a joint check.

End of Part 0. We now have the foundations: the change-of-variables formula and its log-determinant, how log-determinants add under composition, why the target is a standard Gaussian, what a density destructor is, how to keep one numerically stable, and how to check that it reached a standard Gaussian. Part 1 builds the first real component, the one-dimensional marginal transforms that every method in this curriculum stacks.

References
  1. Laparra, V., Camps-Valls, G., & Malo, J. (2011). Iterative Gaussianization: From ICA to Random Rotations. IEEE Transactions on Neural Networks, 22(4), 537–549. 10.1109/TNN.2011.2106511