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.

The canonical RBIG loop

Alternate a marginal Gaussianization and a rotation until the data are a standard Gaussian, then run the stack backwards to generate

00 · The canonical RBIG loop

Open In Colab

Part 1 built the marginal map that makes each coordinate standard normal, and Part 2 the rotation that mixes the coordinates so the next marginal map has something left to do. Rotation-Based Iterative Gaussianization (RBIG) Laparra et al., 2011 alternates the two: each layer is the density-destructor layer fk(x)=Qkgk(x)f_k(\vect{x}) = \mat{Q}_k g_k(\vect{x}) of (3), and the layers are composed as in (2). What makes it RBIG is how the layers are fitted: greedily, one at a time, each to the data as the previous layers left them, and never revisited.

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_models: 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=4000N = 4000 points of the two-moons dataset, two interleaved half circles with Gaussian noise of standard deviation 0.07, standardised per coordinate. Both coordinates are far from Gaussian, and the dependence between them is curved, so no single rotation can remove it.

1. One RBIG layer

The marginal step of layer kk is the probability integral transform of Part 1 with an empirical CDF fitted to the current state. rbig.MarginalGaussianize uses the midpoint empirical CDF, which puts the nn-th smallest value x(n)x_{(n)} of a coordinate at the level

F^N(x(n))=n−1/2N\hat{F}_N(x_{(n)}) = \frac{n - 1/2}{N}

so that no level is exactly 0 or 1, and the marginal map of coordinate dd is

gk(x)d=Φ−1(F^k,d(xd))g_k(\vect{x})_d = \Phi^{-1}(\hat{F}_{k,d}(x_d))

where F^k,d\hat{F}_{k,d} is the midpoint empirical CDF of coordinate dd of the state xk−1\vect{x}_{k-1}. The rotation Qk\mat{Q}_k is then the PCA rotation (3) of gk(xk−1)g_k(\vect{x}_{k-1}).

We compare the standard deviations and the correlation of the two coordinates before and after each step.

data        std [1. 1.], correlation -0.445
g_1(x)      std [1. 1.], correlation -0.374
Q_1 g_1(x)  std [1.172 0.791], correlation -0.000
<Figure size 1350x430 with 3 Axes>

Figure 1:One RBIG layer on two moons. The marginal step makes both coordinates standard normal but keeps the two crescents, bent, because a map that acts on each coordinate separately cannot undo a curved dependence. The rotation removes the correlation and turns the cloud to a new frame, where the next marginal step has structure left to remove.

The data are already standardised, so the marginal step keeps both standard deviations at 1; it changes the correlation from -0.45 to -0.37 and bends the crescents. The rotation removes the correlation and spreads the variance unequally along its new axes. Neither step on its own makes the data Gaussian; the loop is meant to.

2. The loop

We wrap one layer in a function that takes the rotation to fit, and apply it again and again, each time to the state the previous layers produced. To tell whether the result is Gaussian we need more than its mean and covariance, which a PCA layer sets to 0\vect{0} and I\mat{I} after the first pass. A joint test that is cheap in two dimensions projects the points onto JJ unit directions ej\vect{e}_j: every projection of N(0,I)\mathcal{N}(\vect{0}, \mat{I}) is N(0,1)\mathcal{N}(0, 1), and a distribution whose every projection is N(0,1)\mathcal{N}(0, 1) is N(0,I)\mathcal{N}(\vect{0}, \mat{I}) (the Cramér–Wold theorem). The statistic is the largest Kolmogorov–Smirnov distance over the directions,

KSmax⁡=max⁡jsup⁡t∣F^N,j(t)−Φ(t)∣\mathrm{KS}_{\max} = \max_{j} \sup_{t} \vert \hat{F}_{N,j}(t) - \Phi(t) \vert

where F^N,j\hat{F}_{N,j} is the empirical CDF of the projections ej⊤zn\vect{e}_j^\top \vect{z}_n. We compare it with its value on standard-normal samples of the same size.

Fifty standard-normal samples give the null distribution of the statistic.

KS_max of standard-normal samples, 95% quantile: 0.0282

One layer, for any rotation, is the function below.

We run thirty layers with PCA rotations, and thirty with a fresh random rotation from the Haar measure in each layer, keeping every fitted layer and every state.

Both latents have identity covariance to within 0.001, so we compare them by KSmax⁡\mathrm{KS}_{\max} along the loop.

PCA    covariance [[1.0, 0.0], [0.0, 1.0]], KS_max after 1, 2, 4, 8, 12, 30 layers: ['0.145', '0.090', '0.050', '0.051', '0.051', '0.052']
random covariance [[0.999, -0.001], [-0.001, 1.001]], KS_max after 1, 2, 4, 8, 12, 30 layers: ['0.146', '0.087', '0.056', '0.040', '0.032', '0.009']
<Figure size 1250x520 with 10 Axes>

Figure 2:The state of two moons after 1, 2, 4, 12 and 30 RBIG layers, with PCA rotations (top) and random rotations (bottom), every second point shown. With PCA the loop settles by layer 4 on a four-armed shape that it never leaves; with random rotations the arms keep blurring into a round cloud.

With PCA rotations the loop stalls: KSmax⁡\mathrm{KS}_{\max} falls to about 0.05 by layer 4 and stays there, almost twice the null quantile, while the four-armed shape in the figure stays put. Once each coordinate is Gaussian and the two are uncorrelated, PCA has no correlation left to rotate away and the marginal step has almost nothing to fix, so later layers change little. We measure how far the last PCA marginal step moves any point.

largest move of the 30th PCA marginal step: 0.0155

The thirtieth marginal step moves no point by more than 0.02. Random rotations do not stall, because every new rotation shows the next marginal step directions in which the projections are not yet Gaussian, and after thirty layers KSmax⁡\mathrm{KS}_{\max} is inside the null band. The stopping rule and the choice of rotation are the subjects of the next notebooks; from here on we use the random-rotation stack.

3. Running the stack backwards

Every layer is invertible, so the stack is too. The inverse of layer kk undoes the rotation and then the marginal map,

fk−1(z)=gk−1(Qk⊤z)f_k^{-1}(\vect{z}) = g_k^{-1}(\mat{Q}_k^\top \vect{z})

and the inverse of the stack applies these from layer KK down to layer 1. We check it with a round trip on the training data.

We measure the largest error of each point.

round trip: median 6.6e-03, max 0.23

The round trip is not exact: points come back up to about 0.2 away, in standardised units where the data span about 4. The cause is one marginal map. rbig.MarginalGaussianize places the sorted data at the midpoint levels (1) on the way forward, but its inverse_transform interpolates the sorted data at the levels (n−1)/(N−1)(n - 1) / (N - 1), so a point comes back shifted towards a neighbour by up to half the gap between them. The shift is largest in the tails, where the gaps are widest, and the layers above compound it. Interpolating at the midpoint levels instead makes each marginal map invert its own forward map. This is a bug in rbig (rbig#161); until it is fixed we use our own inverse.

The stack’s inverse runs it in every layer.

We run the round trip again with it.

round trip: median 3.3e-16, max 9.2e-12

With matching levels the stack inverts itself to rounding error.

4. Generation

Running the stack backwards from fresh standard-normal draws, as in (4), generates new data. How good the samples are depends on the latent being Gaussian, and on the marginal maps. The empirical CDF knows nothing between and beyond its training points, so its inverse interpolates linearly between them; the smooth Gaussian-mixture CDFs of Part 1 fill in between with a density. gauss_flows_models.inference.fit_rbig builds such a stack in closed form, each layer a PCA rotation followed by a per-coordinate Gaussian-mixture marginal fitted by EM to the current state. It offers no other rotation.

We score each set of samples by its distance to the noise-free moons, the share of points more than 0.3 from the curves, and compare it with a fresh draw of the dataset.

fresh data share farther than 0.3 from the curves: 0.8%
PCA        share farther than 0.3 from the curves: 17.7%
random     share farther than 0.3 from the curves: 3.5%
smooth     share farther than 0.3 from the curves: 5.2%

The stalled PCA stack puts about one sample in six off the crescents: its latent is not Gaussian, so fresh Gaussian draws land in regions that no training point mapped to. The random-rotation stack does better than the smooth one, about one in thirty against one in twenty, because the smooth stack inherits PCA rotations. Its latent is close to Gaussian but not inside the null band.

KS_max of the smooth latent 0.032, null 95% quantile 0.028

5. The density

The smooth stack also has a density, from (5) with every log-determinant exact. On standard-normal data it should recover the standard-normal log-density, which we can compute directly.

mean log-density: smooth RBIG -2.8372, exact -2.8323

The two agree to about 0.005. On two moons we evaluate the density on a grid over the square [−2.5,2.5]2[-2.5, 2.5]^2.

We count the grid points where it is finite.

grid points with a finite log-density: 68%
lowest finite log-density: -3.01e+05

About a third of the grid has a log-density of −∞-\infty, and some of the rest has absurdly low values. A point beyond the data is pushed further from the origin by every mixture marginal, whose tails are lighter than a standard normal’s. Once it is about a thousand component widths from every component, the log-space probit inside MixtureGaussianCDF returns NaN, which log_prob reports as −∞-\infty; that is a bug in gauss_flows (gauss_flows#248), not an underflow, and with it fixed almost every grid point gets a finite, if extremely low, log-density. Near the data the density is finite and higher than in the gaps between the crescents, but it is far from smooth: straight ridges cross the square, and some of the smooth samples follow them. Notebook 03 of this part deals with tails and support. The figure leaves the points with a log-density of −∞-\infty white and floors the colour scale at -8.

<Figure size 1650x370 with 5 Axes>

Figure 3:From left to right: the data, samples generated by the empirical RBIG stacks with PCA and with random rotations (both with the midpoint inverse), samples from the smooth RBIG of gauss_flows, and the smooth stack’s log-density on the grid. The stalled PCA stack scatters points between and around the crescents. White grid cells are where the log-density is minus infinity.

Recap

PieceEquationIn code
the midpoint empirical CDF(1)rbig.MarginalGaussianize
the marginal step of layer kk(2)MarginalGaussianize().fit(x_{k-1})
the rotation of layer kk(3)rbig.PCARotation(whiten=False), or rbig.RandomRotation
one layer and the stack(3), (2)fit_layer, thirty times
a joint test of Gaussianity(3)projection_ks
the inverse(4)inverse_transform, or midpoint_inverse for an exact round trip
generation(4)invert_midpoint on draws, smooth_rbig.sample
the density(5)smooth_rbig.log_prob, finite on the data

Next. We fitted a fixed thirty layers. The next notebook asks how many a dataset needs, and how to tell from the data when to stop.

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