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.

Monotone-spline CDFs

Monotone cubic CDFs, and the rational-quadratic spline with a closed-form inverse

02 · Monotone-spline CDFs

Open In Colab

The mixture CDF of notebook 01 is smooth and compact, but its inverse is a root find, (4). Splines keep the smoothness and the compactness and buy something better: a parameterisation that is monotone by construction and, for the right spline, invertible in closed form. That is why splines are the marginal and coupling transform of choice in modern flows.

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
functax         : 0.0.15
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. Monotonicity is the whole game

A CDF FF is non-decreasing, and its slope F′=pxF' = p_x is the density, so a spline used as a CDF must be monotone. If it is not, F′F' is negative somewhere, FF is not a CDF, and the marginal map (3) is not invertible. A natural cubic spline, the smoothest C2C^2 interpolant, ignores this: between knots it can overshoot, rising past the next knot value and then falling back. The monotone cubic Hermite interpolant of Fritsch and Carlson Fritsch & Carlson, 1980, known as PCHIP, limits the slope at each knot so that every cubic piece stays between its two knot values. We interpolate the same monotone CDF knots, flat then steep then flat, with both, using functax’s CubicSpline and Pchip.

The interpolation plans map knot values to the fine grid, so applying them to the knot values F(xl)F(x_l) gives each spline’s CDF, and its finite-difference slope gives the implied density.

We locate the most negative slope of the natural cubic and its lowest value between the second and third knots, and check that PCHIP never decreases.

natural cubic: smallest slope -0.0447 at x = 4.81
natural cubic: falls to 0.0027
  between the knots F(1) = 0.02 and F(2) = 0.05
PCHIP: smallest slope 0.0151
PCHIP: monotone = True
<Figure size 1200x440 with 2 Axes>

Figure 1:The same monotone CDF knots, interpolated two ways. Left: the natural cubic overshoots between knots, so it is not monotone; PCHIP stays between each pair of knot values. Right: the implied density F′F' of the natural cubic goes negative (shaded), which no density can do.

For these knots the natural cubic never leaves [0,1][0, 1], so a range check would pass it. It still fails as a CDF: between x=1x = 1 and x=2x = 2 it falls from 0.02 to 0.0027, below its own left knot, and its slope reaches -0.045, a negative density. PCHIP honours the same knots with a non-negative slope everywhere. Monotonicity is a property of the slopes between knots, not of the values at them.

2. A spline on the knots of an estimated CDF

rbig’s SplineGaussianizer builds a marginal map from data with exactly this guarantee. It estimates the CDF F^\hat{F} and density p^x\hat{p}_x of each coordinate with a kernel density estimate, places L+1=200L + 1 = 200 knots xlx_l on the data, sets the latent knot values zl=Φ−1(F^(xl))z_l = \Phi^{-1}(\hat{F}(x_l)) by (3), and sets the knot slopes to the derivative of the marginal map, p^x(xl)/N(zl;0,1)\hat{p}_x(x_l) / \mathcal{N}(z_l; 0, 1), the exponential of (5). Those slopes are positive wherever the density estimate is (the library clips them at 10-3), and between knots it interpolates with the rational-quadratic spline of section 3, so the map is monotone and its inverse is closed form. We fit it to N=2000N = 2000 draws of a skewed gamma distribution.

knots: 200, smallest knot slope: 0.001
latent mean 0.020, standard deviation 0.940
round trip: max |x - T^-1(T(x))| = 8.88e-16
<Figure size 1200x420 with 2 Axes>

Figure 2:rbig’s spline Gaussianizer on gamma data. Left: the skewed input. Right: the latent, against the standard-normal density.

The latent is close to standard normal and the round trip is exact to machine precision. Its standard deviation is 0.94 rather than 1: the kernel density estimate smooths the CDF, which pulls the latent knots toward the centre. The map is fitted from the data in one pass, with no training; notebook 03 learns a marginal by maximum likelihood instead.

3. The rational-quadratic spline

A monotone cubic is invertible, but only by solving a cubic in each bin. The rational-quadratic spline of Durkan et al. Durkan et al., 2019 replaces each piece by a ratio of two quadratics chosen so that the inverse needs only the quadratic formula. On the interval [−B,B][-B, B] it has LL bins, with increasing input knots x0=−B<x1<…<xL=Bx_0 = -B < x_1 < \ldots < x_L = B, increasing output knots z0=−B<…<zL=Bz_0 = -B < \ldots < z_L = B and positive knot slopes ηl>0\eta_l > 0. On bin ll, the input x∈[xl,xl+1]x \in [x_l, x_{l+1}] becomes the fraction

ξ=(x−xl)/(xl+1−xl)\xi = (x - x_l) / (x_{l+1} - x_l)

and, with the bin slope sl=(zl+1−zl)/(xl+1−xl)s_l = (z_{l+1} - z_l) / (x_{l+1} - x_l) and ωl=ηl+1+ηl−2sl\omega_l = \eta_{l+1} + \eta_l - 2 s_l, the map is

T(x)=zl+(zl+1−zl)slξ2+ηlξ(1−ξ)sl+ωlξ(1−ξ)T(x) = z_l + (z_{l+1} - z_l) \frac{s_l \xi^2 + \eta_l \xi (1 - \xi)}{s_l + \omega_l \xi (1 - \xi)}

It passes through (xl,zl)(x_l, z_l) and (xl+1,zl+1)(x_{l+1}, z_{l+1}) with slopes ηl\eta_l and ηl+1\eta_{l+1}, so neighbouring bins join with a continuous slope, and it is increasing whenever every ηl>0\eta_l > 0. Outside [−B,B][-B, B] the map is the identity. Inverting it means finding ξ\xi from an output z∈[zl,zl+1]z \in [z_l, z_{l+1}]. Clearing the denominator of (2) leaves a quadratic in ξ\xi,

q2ξ2+q1ξ+q0=0q_2 \xi^2 + q_1 \xi + q_0 = 0

with q2=(zl+1−zl)(sl−ηl)+(z−zl)ωlq_2 = (z_{l+1} - z_l)(s_l - \eta_l) + (z - z_l) \omega_l, q1=(zl+1−zl)ηl−(z−zl)ωlq_1 = (z_{l+1} - z_l) \eta_l - (z - z_l) \omega_l and q0=−sl(z−zl)q_0 = -s_l (z - z_l). Its root in [0,1][0, 1] is

ξ=2q0−q1−q12−4q2q0\xi = \frac{2 q_0}{-q_1 - \sqrt{q_1^2 - 4 q_2 q_0}}

written in this form, rather than the textbook one, so that it does not lose precision when q2q_2 is small. Then x=xl+ξ(xl+1−xl)x = x_l + \xi (x_{l+1} - x_l), in one step and with no iteration, against the nn halvings of bisection (5).

This is gauss_flows’ RQSplineMarginal, one spline per coordinate, built on FlowJax’s RationalQuadraticSpline. Its n_bins argument is passed to FlowJax as the number of interior knots, so n_bins=8 gives L=9L = 9 bins. It starts at the identity, the right default for a flow layer, so to see a non-trivial spline we perturb its unconstrained parameters; the softmax and softplus reparameterisations keep any perturbation monotone. We then read its knots and compare (2) and (4), written out in a few lines of jax.numpy, against the library.

Reading the spline’s reparameterised knots gives the input knots xlx_l, the output knots zlz_l and the knot slopes ηl\eta_l of the one coordinate.

bins L = 9, knot slopes in [0.271, 1.878]

Two helpers find the bin ll that holds a point and the per-bin quantities of (2): the width xl+1−xlx_{l+1} - x_l, the height zl+1−zlz_{l+1} - z_l, the bin slope sls_l and ωl\omega_l.

The bin ll that holds a point comes from a search over the knots.

The forward map evaluates (1) and then (2) in the bin that holds xx.

The inverse finds the bin whose outputs hold zz, builds the coefficients q2,q1,q0q_2, q_1, q_0 of (3) and takes the stable root (4).

We evaluate both formulas on a grid just inside [−4,4][-4, 4] and compare them with RQSplineMarginal.

The formulas, on the same points, against the library.

eq-spline-rqs vs RQSplineMarginal.transform:     max |diff| = 4.4e-16
eq-spline-inverse vs RQSplineMarginal.inverse:   max |diff| = 0.0e+00

The library’s round trip is checked on random points inside and outside the interval, and the forward map on a plot grid over [−5,5][-5, 5].

round trip: max |x - T^-1(T(x))| = 1.3e-15
forward map increasing: True
<Figure size 1200x420 with 2 Axes>

Figure 3:Left: the perturbed rational-quadratic spline with its knots, and the identity outside [−4,4][-4, 4]. Right: the round-trip error of the closed-form inverse, at machine precision with no iteration.

Both formulas reproduce the library to rounding error, and the library round-trips at machine precision in one shot. The mixture CDF of notebook 01 reaches the same accuracy only after a bisection or Newton loop; notebook 04 compares those loops. The spline pays for its free inverse with a forward map that needs a bin search and more parameters per coordinate, which is a good trade whenever a flow must both score and sample.

4. The log-determinant in closed form

A flow also needs each layer’s log-determinant, (4). For a marginal map it is the sum over coordinates of log⁡T′(xd)\log T'(x_d), and the general form (5) asks for a density. The spline needs none: differentiating (2) gives

T′(x)=sl2(ηl+1ξ2+2slξ(1−ξ)+ηl(1−ξ)2)(sl+ωlξ(1−ξ))2T'(x) = \frac{s_l^2 (\eta_{l+1} \xi^2 + 2 s_l \xi (1 - \xi) + \eta_l (1 - \xi)^2)}{(s_l + \omega_l \xi (1 - \xi))^2}

which is positive whenever every ηl>0\eta_l > 0, because each term of the numerator is. We compare it with transform_and_log_det and with an autodiff derivative.

Autodiff gives an independent slope to check both the formula and the library’s log-determinant against.

On the plot grid we compare the library’s log-determinant with the log of the autodiff slope, and the formula with autodiff inside the interval, where NinN_{\mathrm{in}} of the plot points lie.

transform_and_log_det vs log of autodiff slope: max |diff| = 1.3e-15
eq-spline-deriv vs autodiff slope:               max |diff| = 2.7e-15
slope at the left edge: inside 0.603, outside 1.000
<Figure size 1200x420 with 1 Axes>

Figure 4:The slope T′(x)T'(x) of the rational-quadratic spline: continuous across the interior knots (dots), and 1 in the identity tails. Its end slopes are learned parameters, so it jumps at x=±4x = \pm 4.

The formula, the library and autodiff agree to rounding error, so the log-determinant costs one evaluation of (5) per coordinate, with no autodiff and no quadrature. The slope is continuous across the interior knots, as section 3 promised. It is not continuous at the ends of the interval: FlowJax learns the end slopes η0\eta_0 and ηL\eta_L while the tails have slope 1, so here T′T' jumps from 0.60 to 1 at x=−4x = -4. Durkan et al. fix the end slopes at 1 to make the whole map C1C^1; with learned end slopes the density of the flow has a step at ±B\pm B, which is harmless for training but visible in a plot of pxp_x.

Recap

SplineMonotoneInverseLog-determinantTool
Natural cubicno, overshootsnonenonenot a CDF
Monotone cubic (PCHIP)yescubic root per binslope of the cubicfunctax.interp.Pchip
Rational-quadraticyes, by constructionclosed form, (4)closed form, (5)RQSplineMarginal, rbig.SplineGaussianizer

Next. So far every marginal has been fitted from quantiles or by EM. Notebook 03 trains the parameters of a mixture CDF end to end by maximum likelihood, as one layer of a flow.

References
  1. Fritsch, F. N., & Carlson, R. E. (1980). Monotone Piecewise Cubic Interpolation. SIAM Journal on Numerical Analysis, 17(2), 238–246. 10.1137/0717021
  2. Durkan, C., Bekasov, A., Murray, I., & Papamakarios, G. (2019). Neural Spline Flows. Advances in Neural Information Processing Systems (NeurIPS).