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.

Boundaries and support

The empirical marginal step knows the data only between its smallest and largest points, which shows at the edges of the range, in generation and in discrete data

03 · Boundaries and support

Open In Colab

The marginal step of every RBIG layer is the midpoint empirical CDF (1) followed by the probit. That CDF is defined by the sorted training points, so it knows nothing below the smallest or above the largest, and nothing about values that several points share. This notebook looks at what rbig.MarginalGaussianize does in those places, which options it offers, and which of them matter for Gaussianizing data and which for generating it. One marginal is enough to see all of it, so we work in one dimension.

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. A skewed sample

We draw 5,000 points from a gamma distribution with shape 4, whose long right tail makes the boundary effects easy to see. rbig takes a two-dimensional array, so the sample is one column.

training range [0.17, 18.28]

2. Beyond the range

For a new point the midpoint empirical CDF counts the training points below it, so a point above the largest one gets the level (N+1/2)/N(N + 1/2) / N, which is above 1. With bound_correct=False the probit of that level is NaN, and with the default bound_correct=True the level is clipped as in (1), so the point maps to Φ−1(1−ε)\Phi^{-1}(1 - \varepsilon) with ε=10−6\varepsilon = 10^{-6}. We map two points beyond the range, one close to it and one far.

bound_correct=False z = [nan nan]
bound_correct=True  z = [4.75342431 4.75342431]

Without the clip both points are lost; with it both map to 4.753, however far they are, so the marginal map is flat beyond the range and cannot be inverted there. The clip never touches the training points themselves: the largest of them sits at the level 1−1/(2N)1 - 1/(2N), well inside 1−ε1 - \varepsilon.

largest |z| 3.719, clip value 4.753
largest change from removing the clip 0.0e+00

3. The round trip in the tails

rbig’s inverse loses accuracy in the tails, and the clip is often blamed for it. It cannot be the cause, because no training point is clipped. The cause is the one notebook 00 found: the forward map puts the sorted data at the midpoint levels, and inverse_transform interpolates them at the levels (n−1)/(N−1)(n - 1) / (N - 1), a bug in rbig (rbig#161). The gap between neighbouring points is widest in the tails, so that is where the mismatch shows. We compare rbig’s inverse with the midpoint inverse of notebook 00.

The two round trips, point by point:

rbig's inverse: median 1.8e-04, max 1.318 at |z| = 3.72
midpoint inverse: max 8.9e-15
gap between the two largest points 2.636

The largest error of rbig’s inverse, 1.318, is at the largest point and is exactly half the gap of 2.636 between it and its neighbour: the inverse sends the largest level halfway back to the next point down. The midpoint inverse removes it to rounding error.

4. What pdf_extension changes

rbig.MarginalUniformize, the uniformizing half of the marginal step, has an option pdf_extension, the percentage of the data range added on each side of the support. The option does more than widen the support: any value above zero switches the fit from the empirical CDF to a histogram CDF with about N\sqrt{N} bins, interpolated on a fine grid that both directions share. We measure the round trip and how Gaussian the training points come out, through the probit, for a few extensions, including a vanishing one.

pdf_extension      0%: round trip 1.3e+00, KS of z 0.0001
pdf_extension  1e-06%: round trip 7.1e-05, KS of z 0.0049
pdf_extension      5%: round trip 1.1e-13, KS of z 0.0049
pdf_extension     10%: round trip 6.9e-14, KS of z 0.0049

A vanishing extension of 10-6% already cuts the round-trip error from 1.3 to below 10-4, and 5% takes it to rounding error: the gain comes mostly from the switch to a CDF that both directions interpolate the same way, and the extension adds only the last few digits. The switch has a cost on the way forward. The histogram CDF puts the training points about fifty times further from Gaussian than the empirical CDF does (a KS distance of 0.005 against 0.0001), though both are far inside the sampling noise of 5,000 points. Its gain is that points just past the range get a graded value instead of the clip.

pdf_extension 5%: z = [3.7106877  4.75342431]

The near point, inside the 5% extension, maps to 3.71 instead of 4.75; the far point, beyond it, is still clipped.

5. Generating beyond the range

Gaussianizing data needs the forward map only where the data are. Generating needs the inverse everywhere, because draws from N(0,1)\mathcal{N}(0, 1) land at any level. The empirical inverse interpolates between the sorted training points and holds the end values beyond them, so it can never generate a value above the largest training point, however many draws we take. rbig.MarginalGaussianize offers a parametric tail beyond the quantile usu_{\mathrm{s}} (q_tail, 0.95 by default): with tail="pareto" the CDF above the seam xs=F^−1(us)x_{\mathrm{s}} = \hat{F}^{-1}(u_{\mathrm{s}}) is

F^(x)=us+(1−us)G(x−xs)\hat{F}(x) = u_{\mathrm{s}} + (1 - u_{\mathrm{s}}) G(x - x_{\mathrm{s}})

where GG is the CDF of a generalised Pareto distribution fitted to the exceedances x−xsx - x_{\mathrm{s}}, the limit law of exceedances over a high threshold Pickands, 1975, and mirrored below 1−us1 - u_{\mathrm{s}}. With tail="gaussian" a normal matched to two quantiles takes its place. We generate a million values with each, and compare the share above 14, where the training data are already sparse, and the number above the largest training point with the gamma’s.

empirical share above 14 3.2e-04, values above the training maximum 0
gaussian  share above 14 1.6e-04, values above the training maximum 0
pareto    share above 14 5.5e-04, values above the training maximum 29
gamma     share above 14 4.7e-04, values above the training maximum 13.9 expected
seam 7.82, free shape -0.034, rbig's tail {'kind': 'pareto', 'c': 0.0, 'scale': 1.3710149517688734}

The empirical inverse never goes past the training maximum, 18.3, and already falls short at 14, where only a few training points are left. The Gaussian tail is too light for the gamma and falls short too. The Pareto tail generates beyond the training maximum, but about twice as often as the gamma would (29 values against 13.9 expected), and is a little heavy at 14 as well. The maximum-likelihood shape of the exceedances is slightly negative, which rbig clamps to zero, so its tail is an exponential whose scale is the mean exceedance, 1.37. The gamma’s tail decays like an exponential of scale 1 far out, so this one is too heavy there; the Pareto tail is the right family, but its fit to the 250 points above the seam carries this much error.

<Figure size 750x420 with 1 Axes>

Figure 1:The share of a million generated values above each level, for the empirical inverse and for the Gaussian and Pareto tails of rbig, against the gamma distribution the data came from; the dotted line is the largest of the 5,000 training points. The empirical inverse stops at it, the Gaussian tail falls away too fast, and the Pareto tail continues past it, somewhat heavier than the gamma.

rbig.MarginalKDEGaussianize is the smooth alternative from Part 1: a kernel density estimate whose CDF runs a little past the data. It grades the near point, still clips the far one, and inverts itself to rounding error.

z beyond the range [4.14603782 4.75342431], round trip 6.3e-13
KS of z 0.011

The kernel smooths the training points too: their KS distance to N(0,1)\mathcal{N}(0, 1), 0.011, is a hundred times the empirical CDF’s.

6. Discrete data

Counts break the empirical CDF in a different way: many points share a value, so the sorted data have long runs of ties. We draw 5,000 counts from a Poisson distribution with mean 3.

11 distinct counts give 11 distinct z
mean of z -0.375, standard deviation 1.221
zeros: 5.4% of the sample, z = -3.72
probit of the middle of their block -1.92

Each count maps to one value of zz, so the eleven counts become eleven atoms. Worse, the atoms are biased low: their mean is -0.37, not 0. rbig gives a new point the level (r+1/2)/N(r + 1/2) / N, where rr counts the training points strictly below it, so every copy of a tied value gets the lowest level of its block. All the zeros, 5.4% of the sample, map to -3.72, the bottom of the whole range, where the middle of their block would give -1.92. This is a defect in rbig (rbig#164): the comment above the count says it counts the points at or below xx, and counting half of the ties (the midrank) would put each atom in the middle of its block.

Two remedies are on offer. dither=True breaks the ties with a small seeded jitter (1% of the smallest gap between values), which spreads each block over its levels. Uniform dequantisation adds u∼U(0,1)u \sim \mathcal{U}(0, 1) to each count, which turns the counts into a continuous variable whose density pxp_x bounds their probabilities pcp_c from below Theis et al., 2016,

log⁡pc(c)≥Eu∼U(0,1)[log⁡px(c+u)]\log p_c(c) \ge \mathbb{E}_{u \sim \mathcal{U}(0, 1)}[\log p_x(c + u)]

so a density fitted to the dequantised counts is a model of the counts.

We compare the three latents, and the mean log-density of the dequantised counts with the best a model of the counts can do, minus the entropy of the Poisson distribution.

ties        distinct z    11, KS of z 0.223
dither      distinct z  5000, KS of z 0.000
dequantised distinct z  5000, KS of z 0.000
mean log-density of the dequantised counts -1.945
minus the Poisson entropy -1.931
<Figure size 1100x380 with 2 Axes>

Figure 2:Histograms of the marginal step’s output for 5,000 Poisson counts, with the standard normal density. Left: the ties map to twelve atoms, each at the bottom of its block. Right: after uniform dequantisation the output is standard normal.

Dithering and dequantisation both give an exactly standard normal output on the training counts. They differ in what the fitted map means: the dither is a device to break ties, while dequantisation makes the marginal step a density model of the counts. Its mean log-density, -1.945, is within 0.02 of minus the Poisson entropy, -1.931, the expected log-probability of the counts under the distribution they came from. Part 8 treats dequantisation as a surjection.

Recap

BoundaryWhat rbig doesWhat to use
a point beyond the rangeNaN, or the clip (1) for any distancea tail, or pdf_extension just past the range
the inverse in the tailswrong levels (rbig#161)the midpoint inverse
pdf_extensiona histogram CDF in both directionsa consistent round trip
generation beyond the rangenever past the largest pointtail="pareto" (1)
tied valuesatoms at the bottom of each blockdequantisation (2)

The clip is a guard for new points, not a cause of error on the training data, and for Gaussianizing data the empirical CDF needs nothing else. Generation is where the boundary matters: the inverse must know what lies beyond the data, which only a parametric tail tells it.

Next. Part 4 trains the layers of a Gaussianization flow end to end by maximum likelihood, starting from the RBIG stacks of this part (RBIG as a warm start).

References
  1. Pickands, J. (1975). Statistical Inference Using Extreme Order Statistics. The Annals of Statistics, 3(1), 119–131.
  2. Theis, L., van den Oord, A., & Bethge, M. (2016). A Note on the Evaluation of Generative Models. International Conference on Learning Representations.