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.

Numerical mechanics of bijectors

Saturated CDFs, clamps, log-space probits, precision and the round-trip test

05 · Numerical mechanics of bijectors

Open In Colab

A Gaussianization flow applies the marginal step z=Φ−1(F(x))z = \Phi^{-1}(F(x)) many times Chen & Gopinath, 2000Laparra et al., 2011, where F:R→[0,1]F: \mathbb{R} \to [0, 1] is the CDF of one dimension of the data and Φ−1\Phi^{-1} is the probit, the inverse of the standard-normal CDF Φ\Phi. On paper each step is harmless. In floating point F(x)F(x) rounds to exactly 0 or 1 far enough into the tails, the probit sends those values to ±∞\pm \infty, and a sum of thousands of log-determinants slowly loses its low-order digits. This notebook is the engineering that keeps the maths of the earlier notebooks runnable.

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

jax             : 0.12.1
numpy           : 2.4.3
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

1. Where the round-trip error lives

A bijection should come back exactly: f−1(f(x))=xf^{-1}(f(x)) = x. We compare two marginal maps on N=4000N = 4000 samples of x∼N(0,1.62)x \sim \mathcal{N}(0, 1.6^2). The first is the empirical-CDF marginal of rbig, which interpolates the CDF between the sorted samples. The second is a smooth MixtureGaussianCDF placed on the same data with from_data. Figure 1 plots the round-trip error of each sample against ∣x∣\vert x \vert, its distance into the tail.

The gauss_flows marginal goes through the same round trip, and we average both errors in the bulk and in the tails.

empirical CDF  mean error |x| < 1: 1.3e-04   |x| > 3: 9.8e-03
mixture CDF    mean error |x| < 1: 2.1e-16   |x| > 3: 2.2e-17
<Figure size 1200x450 with 1 Axes>

Figure 1:Round-trip error of each sample against its distance into the tail. The empirical CDF (rbig) loses accuracy as |x| grows; the smooth mixture CDF stays near the float64 floor everywhere.

The empirical CDF’s mean error grows by more than an order of magnitude from the bulk (∣x∣<1\vert x \vert < 1) to the tails (∣x∣>3\vert x \vert > 3), because it has no information between and beyond the sparse tail samples. The mixture CDF is smooth and strictly increasing everywhere, and its round trip stays near the float64 floor. A marginal estimator’s tail behaviour decides whether it can be inverted, which is why parametric flows use smooth CDFs (mixtures, splines) rather than a raw empirical CDF.

2. Two guards against a saturated CDF

Even a smooth CDF rounds to exactly 0 or 1 far enough into the tail, and Φ−1(0)=−∞\Phi^{-1}(0) = -\infty. The classic guard is a clamp: keep the CDF inside [ε,1−ε][\varepsilon, 1 - \varepsilon], with a small ε>0\varepsilon > 0, before the probit,

z=Φ−1(clip(F(x),ε,1−ε))z = \Phi^{-1}(\mathrm{clip}(F(x), \varepsilon, 1 - \varepsilon))

which caps the latent at a ceiling set by ε\varepsilon alone,

∣z∣≤Φ−1(1−ε)\vert z \vert \le \Phi^{-1}(1 - \varepsilon)

The clamp trades a small bias in the tails for latents that are always finite. Figure 2 shows the trade-off on a two-component Gaussian mixture CDF, with weights 0.5, means ±1.5\pm 1.5 and standard deviations 0.5.

We evaluate it at 1,500 points on [−7,7][-7, 7] and clamp it at three values of ε\varepsilon.

unclamped: 153 of 1500 latents are ±inf

The ceiling (2) depends on ε\varepsilon alone, so we trace it for ε\varepsilon from 10-15 to 10-1.

eps = 1e-03: ceiling = 3.09
eps = 1e-06: ceiling = 4.75
eps = 1e-07: ceiling = 5.20
<Figure size 1200x450 with 2 Axes>

Figure 2:Left: the clamped marginal map for three values of the clamp ε; each flattens at its ceiling. Right: the latent ceiling Φ⁻¹(1 − ε) as a function of ε, with the clamp of MixtureLogisticCDF marked.

A smaller ε\varepsilon lets the latent reach further into the tails, and the ceiling grows only slowly: ε=10−3\varepsilon = 10^{-3} caps ∣z∣\vert z \vert at 3.09, ε=10−7\varepsilon = 10^{-7} at 5.20. Without a clamp the latent becomes ±∞\pm \infty wherever the CDF rounds to 0 or 1.

The two mixture marginals of gauss_flows use different guards. MixtureLogisticCDF clamps with ε=10−6\varepsilon = 10^{-6}, so its latent never exceeds 4.75. MixtureGaussianCDF works with log⁡F(x)\log F(x) instead of F(x)F(x). In the lower tail it solves

log⁡Φ(z)=log⁡F(x)\log \Phi(z) = \log F(x)

for zz by Newton’s method on the log-CDF, which stays accurate where F(x)F(x) itself would underflow, and in the upper tail it does the same with the survival function 1−F(x)1 - F(x). We push a few points through both, with their default parameters, and back.

Each point goes to its latent through both marginals and comes back through their inverses.

     x  | logistic: z      back      | Gaussian: z       back
    3.0 |   2.214      3.0000     |   4.297      3.0000
    6.0 |   3.560      6.0000     |   8.594      6.0000
   10.0 |   4.753      9.6453     |  14.324     10.0000
   15.0 |   4.753      9.6453     |  21.485     15.0000
   25.0 |   4.753      9.6453     |  35.809     25.0000

The tail inverse of MixtureGaussianCDF was not always this robust. A round-trip test like the one in section 4 caught its bisection bracket being too narrow for tail samples (gauss_flows#108), and the fix is what lets the table above return x=25x = 25.

3. Precision of a deep log-determinant

A deep flow sums one log-determinant per layer, (4), and FlowJax’s Chain and Scan add them one at a time. Every addition rounds the running total to the working precision, with a relative error of about 6×10−86 \times 10^{-8} in float32 and 1×10−161 \times 10^{-16} in float64, so the error of the sum grows with the number of layers KK. We sum KK per-layer terms in three ways and compare each with the exactly rounded sum from math.fsum. The terms are random stand-ins of order one, N(−0.5,0.52)\mathcal{N}(-0.5, 0.5^2), since only their size matters here.

We run the compiled running sum in float32 and in float64, and pick 25 depths KK from 1 to 105 at which to compare it with the exact sum.

At each depth we measure the three errors against the exactly rounded sum from math.fsum.

K =    100  float32, running: 2.3e-06  float32, pairwise: 2.3e-06  float64, running: 2.1e-14
K =   1000  float32, running: 2.1e-04  float32, pairwise: 3.0e-05  float64, running: 5.7e-14
K =  10000  float32, running: 3.8e-03  float32, pairwise: 1.1e-04  float64, running: 2.7e-12
K = 100000  float32, running: 5.2e-01  float32, pairwise: 2.0e-04  float64, running: 2.6e-10
<Figure size 1200x450 with 1 Axes>

Figure 3:Error of a sum of K per-layer log-determinants against its exactly rounded value. A float32 running sum, as a flow’s chain accumulates it, drifts fastest; a float32 pairwise sum (jnp.sum) drifts less; a float64 running sum stays near its floor.

The float32 running sum is off by about 2×10−62 \times 10^{-6} nats after a hundred layers, 4×10−34 \times 10^{-3} after 104 and half a nat after 105. The pairwise float32 sum of jnp.sum grows far more slowly, to 2×10−42 \times 10^{-4} at 105, and float64 stays below 10-9 throughout. For the tens of layers of a typical flow, float32 is accurate enough, which is why the library defaults to it. The drift matters for very deep stacks, such as an iterative RBIG fit with many more layers, and when two log-likelihoods that differ by little are compared; that is when to switch the accumulation to float64.

4. The round-trip test

The defining contract of a bijection is f−1(f(x))=xf^{-1}(f(\vect{x})) = \vect{x}. In code it becomes a test with a tolerance δ>0\delta > 0 over a batch of samples xn∈RDx\vect{x}_n \in \mathbb{R}^{D_x}, n=1,…,Nn = 1, \ldots, N:

It is the cheapest guard against a broken inverse, a missing clamp or a precision bug, and every layer in gauss_flows has one in its test suite. We run it on four bijections, with N=512N = 512 samples in Dx=3D_x = 3 dimensions and δ=10−8\delta = 10^{-8} in float64, and include the empirical-CDF marginal of rbig for contrast. The rotation gets random reflections, since the default ones cancel to the identity.

We run each bijection forward and back on the same samples and keep its worst error.

The empirical-CDF marginal of rbig goes through the same test, at the tolerance δ=10−8\delta = 10^{-8}.

round trip at tolerance 1e-08:
  pass  MixtureGaussianCDF   5.8e-15
  pass  MixtureLogisticCDF   1.2e-14
  pass  RQSplineMarginal     4.4e-16
  pass  HouseholderRotation  2.7e-15
  FAIL  rbig empirical CDF   5.0e-01
<Figure size 1200x400 with 1 Axes>

Figure 4:Round-trip test at tolerance δ = 1e-8 (dashed). The four gauss_flows bijections pass near the float64 floor; the empirical-CDF marginal of rbig fails.

The four gauss_flows bijections pass with errors near the float64 floor. The empirical-CDF marginal fails, and that failure is not a bug to fix: an empirical CDF has no information between and beyond the samples, as section 1 showed. A round-trip test that runs in CI on random inputs is what catches a real inverse bug, such as the bracket of gauss_flows#108, and what lets us stack a hundred layers and trust the composition.

Recap

HazardSymptomGuard
the CDF rounds to 0 or 1latents of ±∞\pm \infty, no inversea smooth CDF with the clamp (1) or the log-space probit (3)
a clamp that is too tighttails flattened at the ceiling (2)place the marginal on the data, or use the log-space probit
a float32 sum of log-determinants (4)drift that grows with depthfloat64 accumulation for deep stacks
a broken inversesilently wrong samples and densitiesthe round-trip test (4) in CI

Next. We can now build a Gaussianizing map and trust it. The next notebook asks how to measure whether its output actually reached N(0,I)\mathcal{N}(\vect{0}, \mat{I}): QQ plots, moments, negentropy and multivariate normality tests.

References
  1. Chen, S. S., & Gopinath, R. A. (2000). Gaussianization. Advances in Neural Information Processing Systems (NeurIPS).
  2. 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