Monotone inverses¶
gauss_flows_maps.monotone inverts an elementwise increasing function \(f\) that has no closed-form inverse. For a target \(\tau\) and a bracket \([x_{\mathrm{lo}}, x_{\mathrm{hi}}]\) with \(f(x_{\mathrm{lo}}) \le \tau \le f(x_{\mathrm{hi}})\), the function \(x \mapsto f(x) - \tau\) changes sign once on the bracket, so a bracketing root finder needs neither a derivative nor a starting guess. A mixture CDF, the monotone transformer of a normalizing flow and an ensemble CDF are the usual cases.
monotone_inverse returns \(x_\tau = f^{-1}(\tau)\), entry by entry, and is safe under jax.jit and jax.vmap. Its gradients come from the implicit function theorem through optimistix.ImplicitAdjoint: with \(f' = \partial f / \partial x\) (the density \(p\) when \(f\) is a CDF),
By default it uses Chandrupatla, an optimistix.AbstractRootFinder that takes an inverse-quadratic-interpolation step when the interpolant is monotone on the bracket and bisects otherwise, so it keeps the guarantee of bisection and converges superlinearly near a smooth root. Any optimistix root finder that takes options=dict(lower=..., upper=...) can replace it.
Worked example: quantiles of a Gaussian mixture¶
The two-component mixture \(F(y) = \frac{1}{2} \Phi(y + 1) + \frac{1}{2} \Phi((y - 2) / 0.5)\) has no closed-form quantile function. Its 5 %, 50 % and 95 % quantiles, bracketed in \([-10, 10]\):
import jax.numpy as jnp
import jax.scipy.stats as jss
from gauss_flows_maps.monotone import monotone_inverse
def mixture_cdf(y):
return 0.5 * jss.norm.cdf(y + 1.0) + 0.5 * jss.norm.cdf((y - 2.0) / 0.5)
levels = jnp.array([0.05, 0.5, 0.95])
quantiles = monotone_inverse(mixture_cdf, levels, -10.0, 10.0, rtol=1e-6)
print([round(float(v), 4) for v in quantiles])
# [-2.2816, 1.0, 2.6412]
The median is exactly 1, where \(\Phi(2)\) and \(\Phi(-2)\) sum to one. A level outside \([F(y_{\mathrm{lo}}), F(y_{\mathrm{hi}})]\) returns the nearer endpoint, or raises with throw=True.
Reference¶
monotone_inverse
¶
monotone_inverse(fn: Callable[[Float[Array, ...]], Float[Array, ...]], target: Float[ArrayLike, ...], lower: Float[ArrayLike, ...], upper: Float[ArrayLike, ...], *, solver: AbstractRootFinder | None = None, rtol: float = 0.0001, atol: float = 1e-06, max_steps: int = 64, throw: bool = False) -> Float[Array, ...]
Invert an elementwise increasing function by bracketed root finding.
f⁻¹(τ) = x such that f(x) − τ = 0, x ∈ [x_lo, x_hi]
f is increasing, so f(x) − τ changes sign once on any bracket with f(x_lo) ≤ τ ≤ f(x_hi), and a bracketing method needs neither derivatives nor a good initial guess. The first use is a quantile: the CDF of a mixture or an ensemble, F_mix(y) = (1/E) ∑_e F_e(y), has no closed-form inverse even when every F_e does, and its quantile F_mix⁻¹(τ) is the root of F_mix(y) − τ. The monotone transformer of a normalizing flow is another.
Gradients go through optimistix.ImplicitAdjoint. For a root x_τ
of f(x; θ) − τ, with derivative f′ = ∂f/∂x (the density p when f is a
CDF),
∂x_τ/∂θ = −∂_θ f(x_τ; θ) / f′(x_τ; θ), ∂x_τ/∂τ = 1 / f′(x_τ; θ)
evaluated with one Jacobian-vector product (the Jacobian is tagged
diagonal). Parameters θ closed over by fn are differentiated too.
The brackets are not.
Algorithm
- Promote τ and the bracket to one floating dtype and broadcast them to the output shape.
- With
throw, check f(x_lo) − τ ≤ 0 ≤ f(x_hi) − τ and a finite bracket for every entry. - Find the root of x ↦ f(x) − τ with the solver (
Chandrupatlaby default) from the bracket midpoint, throughoptimistix.root_findwith the implicit adjoint and a diagonal Jacobian tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[[Float[Array, ...]], Float[Array, ...]]
|
Elementwise increasing function f, mapping an array to an array of the same shape. It must be strictly increasing where it crosses τ: on a flat segment at level τ the solver returns some point of the segment, not the generalized inverse inf{x : f(x) ≥ τ}. Gaussian-mixture CDFs always qualify. |
required |
target
|
Float[ArrayLike, ...]
|
Target values τ, e.g. quantile levels in [0, 1] when |
required |
lower
|
Float[ArrayLike, ...]
|
Finite lower bracket x_lo, with f(x_lo) ≤ τ. |
required |
upper
|
Float[ArrayLike, ...]
|
Finite upper bracket x_hi, with f(x_hi) ≥ τ. Infinite brackets are rejected; for a Gaussian mixture, means ± 40 standard deviations bracket every level. |
required |
solver
|
AbstractRootFinder | None
|
An optimistix root finder that takes
|
None
|
rtol
|
float
|
Relative tolerance on the root (default solver only). |
0.0001
|
atol
|
float
|
Absolute tolerance on the root (default solver only). |
1e-06
|
max_steps
|
int
|
Maximum number of solver iterations. |
64
|
throw
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Float[Array, ...]
|
The roots f⁻¹(τ), with the broadcast shape of |
Float[Array, ...]
|
and |
Raises:
| Type | Description |
|---|---|
EquinoxRuntimeError
|
With |
Shape
target, lower and upper broadcast together (NumPy rules)
to the output shape, and fn is called on arrays of that shape.
It must act elementwise: entry i of its output may depend
only on entry i of its input, e.g. a scalar CDF, or a per-point
mixture CDF with a trailing level axis. With Q targets of shape
(Q,) and a batch of brackets, give the brackets a trailing axis,
(*batch, 1), to get (*batch, Q).
References
Chandrupatla, T. R. (1997). A new hybrid quadratic/bisection algorithm for finding the zero of a nonlinear function without using derivatives. Advances in Engineering Software 28(3), 145-149.
Examples:
The median and 97.5% quantile of a two-member Gaussian mixture:
>>> import jax.numpy as jnp
>>> from jax.scipy.stats import norm
>>> from gauss_flows_maps.monotone import monotone_inverse
>>> cdf = lambda y: 0.5 * (norm.cdf(y + 1.0) + norm.cdf(y - 1.0))
>>> y = monotone_inverse(cdf, jnp.array([0.5, 0.975]), -10.0, 10.0)
>>> [round(float(v), 3) for v in y]
[0.0, 2.646]
A batch of brackets with a trailing axis gives one row per bracket:
>>> lower = jnp.full((3, 1), -10.0)
>>> monotone_inverse(cdf, jnp.array([0.1, 0.9]), lower, 10.0).shape
(3, 2)
An unbracketed target returns the endpoint closer to τ:
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/monotone/inverse.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
Chandrupatla
¶
Bases: AbstractRootFinder
Chandrupatla's hybrid inverse-quadratic / bisection root finder.
x_t = (1 − t) a + t b
t_IQI = f_a/(f_b − f_a) · f_c/(f_b − f_c)
+ (c − a)/(b − a) · f_a/(f_c − f_a) · f_b/(f_c − f_b)
A bracketing method for f(x) = 0 that keeps the robustness of bisection and the superlinear convergence of inverse quadratic interpolation (Chandrupatla, 1997). The bracket [a, b] with f_a f_b ≤ 0 and the previous point c are updated each step with the new point x_t. The next t is the inverse quadratic interpolant t_IQI through (a, b, c), accepted only when ξ = (a − b)/(c − b) and Φ = (f_a − f_b)/(f_c − f_b) satisfy Φ² < ξ and (1 − Φ)² < 1 − ξ, i.e. when the interpolant is monotone on the bracket; otherwise t = ½ (bisection). t is clipped to [t_lim, 1 − t_lim] with t_lim = ½ tol / |b − c| and tol = rtol |x_m| + atol, where x_m is whichever of a, b has the smaller |f|. The solve stops when t_lim > ½, i.e. |b − c| < tol, which bounds the bracket |b − a| ≤ |b − c| and so puts x_m within tol of the root; or when f(x_m) = 0; or when the bracket has shrunk to adjacent floats and x_t can no longer move. It returns x_m. (Scherer's formulation stops at 2 tol; halving keeps the requested tolerance.)
The solver is elementwise: y may be an array, fn must act
on each entry independently (as a CDF evaluated pointwise does), and
every entry carries its own bracket and stops on its own. The loop
ends when all entries have converged. Pass tags=frozenset(
{lineax.diagonal_tag}) to optimistix.root_find so the implicit
adjoint uses the diagonal Jacobian.
Requires options=dict(lower=..., upper=...) (broadcastable to
y), as optimistix.Bisection does. The bracket and every iterate
keep y's dtype; only the function values take fn's. An entry
whose bracket has no sign change (or a NaN endpoint value) is not
iterated: it returns the endpoint with the smaller |f|, so the caller
decides whether that is an error. With has_aux=True the auxiliary
output is that of fn at the returned root (one extra evaluation).
Algorithm
Per entry, following Scherer (2010), §6.1:
- Start from b = lower, a = c = upper and t = ½; an entry whose bracket has no sign change, or is infinite, is done at once.
- Evaluate f at x_t = (1 − t) a + t b. If f(x_t) has the sign of f_a, set c ← a; otherwise set c ← b and b ← a. Then a ← x_t.
- Take x_m, whichever of a, b has the smaller |f|, and t_lim = (rtol |x_m| + atol) / (2 |b − c|). Stop if f(x_m) = 0, t_lim > ½, or x_t equalled a or b before the update.
- Take t = t_IQI if Φ² < ξ and (1 − Φ)² < 1 − ξ, else t = ½, and clip it to [t_lim, 1 − t_lim]. Repeat from step 2.
Attributes:
| Name | Type | Description |
|---|---|---|
rtol |
float
|
Relative tolerance on the root. |
atol |
float
|
Absolute tolerance on the root. |
References
Chandrupatla, T. R. (1997). A new hybrid quadratic/bisection algorithm for finding the zero of a nonlinear function without using derivatives. Advances in Engineering Software 28(3), 145-149.
Scherer, P. O. J. (2010). Computational Physics: Simulation of Classical and Quantum Systems. Springer, §6.1.
Examples:
The cube root of 2 on the bracket [0, 2]:
>>> import jax.numpy as jnp
>>> import optimistix as optx
>>> from gauss_flows_maps.monotone import Chandrupatla
>>> sol = optx.root_find(
... lambda x, _: x**3 - 2.0,
... Chandrupatla(rtol=1e-10, atol=1e-12),
... jnp.array(1.0),
... options=dict(lower=0.0, upper=2.0),
... )
>>> round(float(sol.value), 6)
1.259921
Every entry carries its own bracket and target:
>>> sol = optx.root_find(
... lambda x, target: x**2 - target,
... Chandrupatla(rtol=1e-6, atol=1e-8),
... jnp.ones(2),
... args=jnp.array([4.0, 9.0]),
... options=dict(lower=0.0, upper=jnp.array([5.0, 10.0])),
... )
>>> [round(float(v), 4) for v in sol.value]
[2.0, 3.0]
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/monotone/chandrupatla.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
init
¶
init(fn: Callable[[Array, Any], tuple[Array, Any]], y: Array, args: PyTree, options: dict[str, Any], f_struct: Any, aux_struct: Any, tags: frozenset[object]) -> _ChandrupatlaState
Set up the bracket b = lower, a = c = upper and t = ½.
The arguments are those of optimistix.AbstractRootFinder.init.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/monotone/chandrupatla.py
step
¶
step(fn: Callable[[Array, Any], tuple[Array, Any]], y: Array, args: PyTree, options: dict[str, Any], state: _ChandrupatlaState, tags: frozenset[object]) -> tuple[Array, _ChandrupatlaState, Any]
One Chandrupatla step on every entry that has not converged.
x_t = (1 − t) a + t b
The arguments are those of optimistix.AbstractRootFinder.step;
it returns the best point x_m, the new state and fn's aux at
x_t.
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/monotone/chandrupatla.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
terminate
¶
terminate(fn: Callable[[Array, Any], tuple[Array, Any]], y: Array, args: PyTree, options: dict[str, Any], state: _ChandrupatlaState, tags: frozenset[object]) -> tuple[Bool[Array, ''], RESULTS]
Stop once every entry has converged.
The arguments are those of optimistix.AbstractRootFinder.terminate.
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/monotone/chandrupatla.py
postprocess
¶
postprocess(fn: Callable[[Array, Any], tuple[Array, Any]], y: Array, aux: Any, args: PyTree, options: dict[str, Any], state: _ChandrupatlaState, tags: frozenset[object], result: RESULTS) -> tuple[Array, Any, dict[str, Any]]
Return x_m with fn's aux evaluated there.
The arguments are those of
optimistix.AbstractRootFinder.postprocess.