Skip to main content

gam_math/
probability.rs

1use libm::{erf, erfc};
2use statrs::function::{
3    beta::{beta_reg, inv_beta_reg, ln_beta},
4    gamma::gamma_ur,
5};
6
7const INV_SQRT_PI: f64 = 0.564_189_583_547_756_3;
8const SQRT_2_OVER_PI: f64 = 0.797_884_560_802_865_4;
9
10/// Quantile (inverse CDF) of a Beta distribution with shape parameters `a > 0`
11/// and `b > 0` at probability `p`: the value `x in [0, 1]` with
12/// `I_x(a, b) = p`, where `I` is the regularized incomplete beta.
13///
14/// `p <= 0` maps to the support floor and `p >= 1` to the support ceiling. A
15/// non-finite or non-positive shape yields `NaN`.
16pub fn beta_quantile(p: f64, a: f64, b: f64) -> f64 {
17    if !(a.is_finite() && a > 0.0 && b.is_finite() && b > 0.0) {
18        return f64::NAN;
19    }
20    if !p.is_finite() || p <= 0.0 {
21        return 0.0;
22    }
23    if p >= 1.0 {
24        return 1.0;
25    }
26    match lower_tail_beta_quantile(p, a, b) {
27        Some(x) => x,
28        None => inv_beta_reg(a, b, p),
29    }
30}
31
32/// `Beta⁻¹(p; a, b)` on the branch where the answer is small enough for the
33/// ascending series to be exact, or `None` when it is not.
34///
35/// `inv_beta_reg` converges on an ABSOLUTE tolerance in `x`, so it cannot
36/// resolve a quantile below about `1e-16`: it stalls and returns a number in
37/// the `1e-17..1e-19` band unrelated to the answer. That band is not exotic —
38/// it is the ordinary lower tail of a beta-regression predictive interval
39/// whenever the mean is small. For `Beta(0.04, 3.96)` at `p = 0.025`, the
40/// shapes a mean of `0.01` with a fifth of the Bernoulli variance produces, it
41/// returned `6.7e-18` where the truth is `1.5e-41` (#2528).
42///
43/// The lower tail has a convergent ascending series,
44///
45/// ```text
46/// I_x(a,b) = x^a / B(a,b) · S(x),   S(x) = Σ_{k≥0} c_k x^k,
47/// c_k = (1−b)_k / (k!·(a+k)),       c_0 = 1/a
48/// ```
49///
50/// whose leading term inverts in closed form to
51/// `x₀ = exp([ln p + ln a + ln B(a,b)] / a)`. Refining it in `y = ln x` rather
52/// than in `x` is what removes the floor: the answer's own variable becomes the
53/// iteration variable, so an absolute step tolerance in `y` is a RELATIVE
54/// tolerance in `x` and there is nothing to stall against. The iteration is
55/// also better conditioned than the one it replaces —
56/// `G(y) = ln I_{e^y}(a,b) − ln p` has `G′(y) = a + x·S′(x)/S(x) → a`, a
57/// constant, where the `x`-space derivative `∂I/∂x` spans hundreds of orders
58/// over the same range.
59///
60/// Underflow then becomes something the function can state rather than paper
61/// over: a true quantile below `f64::MIN_POSITIVE` reaches `exp(y) = 0`, which
62/// is the correctly rounded answer, instead of a spurious positive floor a
63/// caller cannot distinguish from a resolved bound.
64///
65/// The branch condition is `x·max(1, b) ≤ ½`, which is derived rather than
66/// tuned. The term ratio is `|x·(k+1−b)/(k+1)|·(a+k)/(a+k+1)`, and
67/// `|k+1−b| ≤ (k+1)·max(1, b)` for every `k ≥ 0`, so the condition bounds every
68/// ratio by `½` and the series reaches `f64` resolution in at most
69/// [`BETA_SERIES_MAX_TERMS`] terms. It is the same boundary, for the same
70/// reason, that `crates/gam-terms/src/basis/polylog.rs` uses for its own
71/// ascending series.
72fn lower_tail_beta_quantile(p: f64, a: f64, b: f64) -> Option<f64> {
73    let ln_b = ln_beta(a, b);
74    if !ln_b.is_finite() {
75        return None;
76    }
77    // Leading-order inverse: `I_x ≈ x^a / (a·B(a,b))` as `x → 0`.
78    let mut y = (p.ln() + a.ln() + ln_b) / a;
79    if !y.is_finite() {
80        return None;
81    }
82    // Reject before iterating if the seed is outside the series branch. The
83    // seed underestimates `x` for `b < 1` and overestimates it for `b > 1`, by
84    // a factor that is itself `1 + O(x)`, so a seed comfortably inside the
85    // branch keeps every iterate inside it.
86    let ratio_bound = (0.5_f64).ln() - b.max(1.0).ln();
87    if !(y <= ratio_bound) {
88        return None;
89    }
90    let ln_p = p.ln();
91    for _ in 0..BETA_NEWTON_MAX_STEPS {
92        let x = y.exp();
93        if x * b.max(1.0) > 0.5 {
94            return None;
95        }
96        let (sum, derivative_sum) = beta_ascending_series(x, a, b)?;
97        if !(sum.is_finite() && sum > 0.0 && derivative_sum.is_finite()) {
98            return None;
99        }
100        // `G(y) = a·y − ln B(a,b) + ln S(e^y) − ln p`.
101        let g = a * y - ln_b + sum.ln() - ln_p;
102        let g_prime = a + x * derivative_sum / sum;
103        if !(g.is_finite() && g_prime.is_finite() && g_prime > 0.0) {
104            return None;
105        }
106        let step = g / g_prime;
107        if !step.is_finite() {
108            return None;
109        }
110        y -= step;
111        // Absolute in `y` is relative in `x`, which is the whole point.
112        if step.abs() <= f64::EPSILON * y.abs().max(1.0) {
113            break;
114        }
115    }
116    let x = y.exp();
117    if x.is_finite() && (0.0..=1.0).contains(&x) {
118        Some(x)
119    } else {
120        None
121    }
122}
123
124/// `(S(x), S′(x))` for `S(x) = Σ_{k≥0} (1−b)_k · x^k / (k!·(a+k))`.
125///
126/// Accumulated by the ratio `t_{k+1} = t_k·(k+1−b)/(k+1)` on the Pochhammer
127/// factor, so no factorial or gamma is formed. `None` if the guard term count
128/// is exhausted, which the caller's branch condition makes unreachable.
129fn beta_ascending_series(x: f64, a: f64, b: f64) -> Option<(f64, f64)> {
130    let mut pochhammer_over_factorial = 1.0_f64;
131    let mut power = 1.0_f64;
132    let mut sum = 1.0 / a;
133    let mut derivative_sum = 0.0_f64;
134    for k in 1..=BETA_SERIES_MAX_TERMS {
135        let kf = k as f64;
136        pochhammer_over_factorial *= (kf - b) / kf;
137        let coefficient = pochhammer_over_factorial / (a + kf);
138        // `power` holds `x^{k-1}` here, which is what `S′` wants.
139        derivative_sum += kf * coefficient * power;
140        power *= x;
141        let term = coefficient * power;
142        sum += term;
143        if term.abs() <= f64::EPSILON * sum.abs() {
144            return Some((sum, derivative_sum));
145        }
146    }
147    None
148}
149
150/// `I_x(a,b)` from `ln(x)`, retaining a representable result when `x` itself
151/// underflows.
152///
153/// The ordinary `beta_reg(a,b,x)` interface necessarily loses every result
154/// whose beta argument is below the smallest subnormal, even when the
155/// regularized integral is much larger because `a < 1`. On the derived
156/// ascending-series branch, `x` appears only in the well-scaled correction
157/// `S(x)` while its leading power stays in log space:
158///
159/// `ln I_x(a,b) = a·ln(x) − ln B(a,b) + ln S(x)`.
160///
161/// The same term-ratio proof used by [`lower_tail_beta_quantile`] supplies the
162/// branch boundary. Outside that boundary, the ordinary regularized-beta
163/// implementation receives a representable argument and remains the canonical
164/// general evaluator.
165fn regularized_beta_lower_from_log_x(log_x: f64, a: f64, b: f64) -> f64 {
166    if !(a.is_finite() && a > 0.0 && b.is_finite() && b > 0.0)
167        || log_x.is_nan()
168        || log_x > 0.0
169    {
170        return f64::NAN;
171    }
172    if log_x == 0.0 {
173        return 1.0;
174    }
175    if log_x == f64::NEG_INFINITY {
176        return 0.0;
177    }
178
179    let series_limit = (0.5_f64).ln() - b.max(1.0).ln();
180    if log_x <= series_limit {
181        let x = log_x.exp();
182        let Some((sum, _)) = beta_ascending_series(x, a, b) else {
183            return f64::NAN;
184        };
185        let log_beta = ln_beta(a, b);
186        if !(sum.is_finite() && sum > 0.0 && log_beta.is_finite()) {
187            return f64::NAN;
188        }
189        return (a * log_x - log_beta + sum.ln()).exp();
190    }
191
192    beta_reg(a, b, log_x.exp())
193}
194
195/// `ln(1 / (1 + exp(log_ratio)))` without overflowing or rounding a
196/// representable small unit fraction to zero.
197#[inline]
198fn log_reciprocal_one_plus_exp(log_ratio: f64) -> f64 {
199    if log_ratio <= 0.0 {
200        -log_ratio.exp().ln_1p()
201    } else {
202        -log_ratio - (-log_ratio).exp().ln_1p()
203    }
204}
205
206/// Guard term count for [`beta_ascending_series`]. The caller's `x·max(1,b) ≤ ½`
207/// branch bounds every term ratio by `½`, so the series reaches one ulp of an
208/// `O(1/a)` partial sum in at most `53` terms; this is the non-convergence
209/// guard, not the expected count.
210const BETA_SERIES_MAX_TERMS: usize = 128;
211
212/// Guard step count for the log-space Newton. From a seed whose relative error
213/// is `O(x)` the iteration is quadratic, so it converges in two or three steps
214/// over the whole branch; this is the non-convergence guard.
215const BETA_NEWTON_MAX_STEPS: usize = 32;
216
217/// The part of `x·x` that `f64` cannot hold: `x² = x*x + square_residual(x)`,
218/// exactly, for every `x` whose square neither overflows nor goes subnormal.
219///
220/// This exists because of what `exp` does to a squared argument. Rounding
221/// `x*x` perturbs it by at most `ulp(x²)/2` — a RELATIVE perturbation of
222/// `ε/2`, which is unremarkable on its own. But `exp` converts a relative
223/// perturbation `δ` of its ARGUMENT into a relative perturbation `x²·δ` of
224/// its RESULT, so `exp(x*x)` carries `x²·ε/2` relative error: `3.7e-14` at
225/// `x = 26`, and `7.7e-14` at the `x ≈ 37` where `φ(x)` finally underflows.
226/// That is two orders worse than the `exp` evaluation's own rounding, and it
227/// is the error `erfcx` and `normal_pdf` were both actually delivering.
228///
229/// The residual is the whole of that discarded term and is itself exactly
230/// representable (Dekker's two-product theorem, in its one-FMA form), so
231/// `exp(x²) = exp(x*x)·exp(residual)` and `exp(residual) = 1 + residual` to
232/// `O(residual²)` — below `1e-27` over the entire domain either caller uses.
233/// One multiply by `1 + residual` therefore buys back every digit, and the
234/// callers below apply it fused so the correction itself costs one more
235/// rounding and nothing else.
236///
237/// `mul_add` is a single instruction wherever FMA is in the baseline ISA
238/// (aarch64, and x86-64 built with `+fma`); on a baseline x86-64 build it is
239/// a `glibc` call, measured at ~2.5 ns. Against `erfcx`'s 38 ns that is 9%;
240/// against `normal_pdf`'s 6.2 ns it is 40% of a function that is nowhere the
241/// bottleneck of a row loop that also assembles a design row and a Hessian
242/// block. Both callers guard the pathological arguments BEFORE calling this,
243/// so it never has to defend `±∞` (whose residual would be `NaN`).
244#[inline]
245fn square_residual(x: f64, rounded_square: f64) -> f64 {
246    x.mul_add(x, -rounded_square)
247}
248
249/// Standard normal PDF phi(x).
250///
251/// The squared argument is carried exactly (see `square_residual`); without
252/// that, `exp(-½·fl(x*x))` degrades like `x²·ε/2` and reaches `5.7e-14`
253/// relative before `φ` underflows, against the `3.3e-16` it holds with.
254#[inline]
255pub fn normal_pdf(x: f64) -> f64 {
256    const INV_SQRT_2PI: f64 = 0.398_942_280_401_432_7;
257    let rounded_square = x * x;
258    let head = INV_SQRT_2PI * (-0.5 * rounded_square).exp();
259    if head == 0.0 || head.is_nan() {
260        // The pdf underflowed or `x` was `±∞` (head `0`), or `x` was `NaN`.
261        // Neither admits a relative correction, and `±∞` would feed the
262        // residual an `∞ − ∞`; return the limit the plain form gives.
263        return head;
264    }
265    let residual = square_residual(x, rounded_square);
266    head.mul_add(-0.5 * residual, head)
267}
268
269/// Standard normal CDF Phi(x) evaluated via the exact special-function identity
270///
271///   Phi(x) = 0.5 * erfc(-x / sqrt(2)).
272///
273/// This is the exact Gaussian CDF semantics used throughout the codebase. The
274/// numerical `erfc` implementation may use internal approximations, but the
275/// returned function is the standard normal CDF itself rather than a separate
276/// polynomial surrogate surface.
277#[inline]
278pub fn normal_cdf(x: f64) -> f64 {
279    0.5 * erfc(-x / std::f64::consts::SQRT_2)
280}
281
282/// Standard normal survival probability `P(Z > x)`.
283///
284/// This is evaluated as `½·erfc(x/√2)`, not as `1 − Φ(x)`. The latter loses
285/// relative accuracy as soon as `Φ(x)` approaches one and becomes identically
286/// zero for every representable `x` above roughly `8.3`, while the direct
287/// complementary form retains the full representable tail.
288#[inline]
289pub fn normal_sf(x: f64) -> f64 {
290    0.5 * erfc(x / std::f64::consts::SQRT_2)
291}
292
293/// Two-sided standard-normal probability `P(|Z| ≥ |z|)`.
294///
295/// The exact symmetric identity is `erfc(|z|/√2)`. Evaluating that identity
296/// directly avoids both the cancellation in `2·(1 − Φ(|z|))` and an
297/// unnecessary rounding from multiplying a one-sided tail by two.
298#[inline]
299pub fn normal_two_sided_probability(z: f64) -> f64 {
300    erfc(z.abs() / std::f64::consts::SQRT_2)
301}
302
303/// Two-sided Student-t probability `P(|T_ν| ≥ |t|)`.
304///
305/// For finite `ν > 0`,
306///
307/// `P(|T_ν| ≥ |t|) = I_x(ν/2, 1/2)`, `x = ν / (ν + t²)`.
308///
309/// Neither `t²` nor `x` is formed directly. Their ratio is carried as
310/// `ln(t²/ν)`, and the regularized beta receives `ln(x)`. This matters beyond
311/// avoiding overflow: for `ν = 1` and `t = f64::MAX`, `x` underflows to zero
312/// although the Cauchy tail is still a representable subnormal. The log-beta
313/// series preserves that probability. Invalid degrees of freedom produce
314/// `NaN`; infinite statistics map to the exact limiting probability zero.
315pub fn student_t_two_sided_probability(t: f64, degrees_of_freedom: f64) -> f64 {
316    let half_df = 0.5 * degrees_of_freedom;
317    if t.is_nan()
318        || !(degrees_of_freedom.is_finite()
319            && degrees_of_freedom > 0.0
320            && half_df > 0.0)
321    {
322        return f64::NAN;
323    }
324    if t.is_infinite() {
325        return 0.0;
326    }
327
328    let log_t_squared_over_df = 2.0 * t.abs().ln() - degrees_of_freedom.ln();
329    let log_x = log_reciprocal_one_plus_exp(log_t_squared_over_df);
330    regularized_beta_lower_from_log_x(log_x, half_df, 0.5)
331}
332
333/// Student-t survival probability `P(T_ν > t)`.
334///
335/// The small tail is always obtained from
336/// [`student_t_two_sided_probability`]. For negative `t`, subtracting its
337/// half-tail from one constructs the large probability, where subtraction is
338/// well conditioned.
339pub fn student_t_sf(t: f64, degrees_of_freedom: f64) -> f64 {
340    let two_sided = student_t_two_sided_probability(t, degrees_of_freedom);
341    if t < 0.0 {
342        1.0 - 0.5 * two_sided
343    } else {
344        0.5 * two_sided
345    }
346}
347
348/// Chi-squared survival probability `P(X_ν > statistic)`.
349///
350/// Uses the regularized upper incomplete gamma directly instead of
351/// reconstructing a small tail as `1 − P(ν/2, statistic/2)`.
352pub fn chi_square_sf(statistic: f64, degrees_of_freedom: f64) -> f64 {
353    let half_df = 0.5 * degrees_of_freedom;
354    if statistic.is_nan()
355        || statistic < 0.0
356        || !(degrees_of_freedom.is_finite()
357            && degrees_of_freedom > 0.0
358            && half_df > 0.0)
359    {
360        return f64::NAN;
361    }
362    if statistic == 0.0 {
363        return 1.0;
364    }
365    if statistic == f64::INFINITY {
366        return 0.0;
367    }
368    gamma_ur(half_df, 0.5 * statistic)
369}
370
371/// Survival probability `P(Σ_j w_j Z_j² > statistic)` for independent standard
372/// normals `Z_j` and non-negative weights `w`.
373///
374/// This is the exact null law of every quadratic form `u'Au` in a standard
375/// normal vector — `w` being the eigenvalues of the symmetric part of `A` — and
376/// it is the reference distribution a *penalized* likelihood-ratio statistic is
377/// actually drawn from. Only the degenerate all-weights-equal case reduces to a
378/// (scaled) χ²; matching a χ² to the mean `Σ w_j` alone leaves the reference
379/// over-dispersed whenever the weights differ, because `Var = 2Σ w_j²` while the
380/// mean-matched χ² carries `2 Σ w_j`, and `Σ w_j² ≤ (max_j w_j)·Σ w_j`.
381///
382/// # Method
383///
384/// Imhof's (1961) exact inversion of the characteristic function, in the central
385/// one-degree-of-freedom-per-weight form:
386///
387/// ```text
388/// P(Q > x) = 1/2 + (1/π) ∫_0^∞ sin θ(u) / (u ρ(u)) du,
389/// θ(u) = ½ Σ_j arctan(w_j u) − ½ x u,
390/// ρ(u) = Π_j (1 + w_j² u²)^{1/4}.
391/// ```
392///
393/// The integrand is bounded (`sin θ(u)/u → (Σ w_j − x)/2` as `u → 0`) and is
394/// integrated on panels of one full oscillation of the `−xu/2` phase with a
395/// fixed 16-node Gauss–Legendre rule, which is exact for the amplitude to well
396/// past the resolution of the phase.
397///
398/// # Truncation, and why the bound is the oscillatory one
399///
400/// The naive tail bound `∫_U^∞ du/(u ρ(u))` decays only like `U^{-m/2}` in the
401/// number `m` of weights that are *active* at `U` (i.e. `w_j U ≳ 1`), which is
402/// useless when one weight dominates. The integrand is an oscillation, though:
403/// once `φ'(u) = ½ Σ_j w_j/(1 + w_j²u²)` has fallen below `x/4`, the phase
404/// `θ` is strictly decreasing with `|θ'| ≥ x/4`, so substituting the phase as
405/// the integration variable turns the tail into `∫ G(t) sin(θ(U) − t) dt` with
406/// `G` positive and decreasing from `G(0) ≤ 4/(x U ρ(U))`. The alternating
407/// half-period sum of such an integral is bounded by `4 G(0)`, giving
408///
409/// ```text
410/// |tail(U)| ≤ 16 / (x · U · ρ(U)),
411/// ```
412///
413/// which is the stopping rule. This is a bound on the answer, not a guess about
414/// it: the loop runs until the bound is under [`WEIGHTED_CHI_SQUARE_TOLERANCE`],
415/// and `ρ` is non-decreasing so it always terminates.
416///
417/// # Exact special cases
418///
419/// * no positive weight — `Q ≡ 0`;
420/// * all positive weights bit-identical — `Q = w χ²_q` exactly, so the
421///   incomplete-gamma path is both faster and more accurate than any quadrature
422///   (this also covers the single-weight and the classical unpenalized
423///   `w ≡ 1 ⇒ χ²_q` cases).
424///
425/// Returns `NaN` if any weight is negative or non-finite, or if `statistic` is
426/// `NaN`.
427pub fn weighted_chi_square_sf(weights: &[f64], statistic: f64) -> f64 {
428    weighted_chi_square_sf_with_bound(weights, statistic).0
429}
430
431/// [`weighted_chi_square_sf`] together with the certified absolute bound on its
432/// own truncation error, so a consumer (or a test) can see the accuracy rather
433/// than trust it.
434///
435/// The bound is `0.0` on the exact closed-form branches. On the Imhof branch it
436/// is `16/(x·U·ρ(U))` at the truncation point `U` actually reached, which is at
437/// or below [`WEIGHTED_CHI_SQUARE_TOLERANCE`] unless the panel backstop
438/// [`IMHOF_MAX_PANELS`] bound first.
439pub fn weighted_chi_square_sf_with_bound(weights: &[f64], statistic: f64) -> (f64, f64) {
440    weighted_chi_square_sf_to_tolerance(weights, statistic, WEIGHTED_CHI_SQUARE_TOLERANCE)
441}
442
443/// [`weighted_chi_square_sf_with_bound`] at a caller-chosen absolute accuracy.
444///
445/// # Why this is a parameter and not a constant
446///
447/// The truncation point `U` needed for a bound `ε` grows like `ε^{-2/(2+m)}` in
448/// the number `m` of weights *active* there, and the panel count like `U·x/4π`.
449/// With one dominant weight over a tail of small ones — which is the shape of a
450/// shrunk penalized smooth, not a corner case — `m = 1` over the whole useful
451/// range and the cost is `ε^{-2/3}`. Measured on `w = 1 − p²` for
452/// `p = (0, 10⁻³, 10⁻⁵, 10⁻⁷, 10⁻⁹)` at `x = 3Σw`:
453///
454/// ```text
455/// ε = 1e-11    467,919 panels   1.73 s
456/// ε = 1e-9      74,433 panels   237 ms
457/// ε = 1e-7      10,519 panels    46 ms
458/// ```
459///
460/// with the three answers agreeing to `1.0e-9` absolute — i.e. the certified
461/// bound is two orders pessimistic, and the top row buys nothing but time. A
462/// caller that knows what its answer is *for* can say so, and one that does not
463/// still gets [`WEIGHTED_CHI_SQUARE_TOLERANCE`] through the entry point above.
464///
465/// A non-positive or non-finite `absolute_tolerance` is treated as
466/// [`WEIGHTED_CHI_SQUARE_TOLERANCE`]: the contract is "at least this accurate",
467/// and a caller that asks for nonsense gets the strictest answer rather than the
468/// loosest. The returned bound is always the one actually achieved, which may be
469/// tighter than requested (the sweep stops at a panel boundary) or looser (the
470/// [`IMHOF_MAX_PANELS`] backstop bound first).
471pub fn weighted_chi_square_sf_to_tolerance(
472    weights: &[f64],
473    statistic: f64,
474    absolute_tolerance: f64,
475) -> (f64, f64) {
476    let mut terms = Vec::with_capacity(weights.len());
477    for &weight in weights {
478        if !weight.is_finite() || weight < 0.0 {
479            return (f64::NAN, f64::NAN);
480        }
481        terms.push(WeightedChiSquareTerm {
482            weight,
483            degrees_of_freedom: 1.0,
484        });
485    }
486    signed_weighted_chi_square_sf_to_tolerance(&terms, statistic, absolute_tolerance)
487}
488
489/// One `λ_j · χ²_{h_j}` term of a linear combination of independent
490/// chi-squares, with the weight's SIGN and the term's degrees of freedom both
491/// carried explicitly.
492///
493/// Two things separate this from the `&[f64]` weight list
494/// [`weighted_chi_square_sf`] takes, and each of them is a distribution the
495/// one-degree-of-freedom non-negative form cannot express:
496///
497/// * **A negative weight makes a RATIO a tail.** `P(A/B > t)` for independent
498///   non-negative `A`, `B` is `P(A − tB > 0)`, so every F-shaped reference —
499///   any statistic whose scale was estimated from the same data — is a
500///   *signed* combination evaluated at zero. The classical `F_{a,b}` is the
501///   two-term case `λ = (1, −t·a/b)`, `h = (a, b)`.
502/// * **A multiplicity is not `h` copies of a weight.** It is, mathematically,
503///   but the Imhof integrand costs one `atan` and one `ln` per TERM, and a
504///   residual sum of squares carries `n − p` unit weights. Folding them into
505///   one term with `h = n − p` is what makes an `n`-sized reference cost the
506///   same as a `p`-sized one.
507#[derive(Clone, Copy, Debug, PartialEq)]
508pub struct WeightedChiSquareTerm {
509    /// `λ_j`, of either sign. A zero weight contributes nothing and is dropped.
510    pub weight: f64,
511    /// `h_j > 0`. Real rather than integral: a two-moment summary of a spectrum
512    /// is a chi-square with a fractional shape, and this type is what carries it.
513    pub degrees_of_freedom: f64,
514}
515
516/// Survival probability `P(Σ_j λ_j χ²_{h_j} > statistic)` for independent
517/// central chi-squares, with weights of EITHER SIGN, at
518/// [`WEIGHTED_CHI_SQUARE_TOLERANCE`].
519///
520/// See [`WeightedChiSquareTerm`] for why the sign and the multiplicity are
521/// worth carrying, and [`signed_weighted_chi_square_sf_to_tolerance`] for the
522/// accuracy contract.
523pub fn signed_weighted_chi_square_sf(terms: &[WeightedChiSquareTerm], statistic: f64) -> f64 {
524    signed_weighted_chi_square_sf_to_tolerance(terms, statistic, WEIGHTED_CHI_SQUARE_TOLERANCE).0
525}
526
527/// [`signed_weighted_chi_square_sf`] at a caller-chosen absolute accuracy,
528/// returning the bound actually achieved alongside the value.
529///
530/// # Method
531///
532/// Imhof's (1961) inversion in its general central form, of which the
533/// non-negative unit-`h` case documented on [`weighted_chi_square_sf`] is the
534/// specialization:
535///
536/// ```text
537/// P(Q > x) = 1/2 + (1/π) ∫_0^∞ sin θ(u) / (u ρ(u)) du,
538/// θ(u) = ½ Σ_j h_j arctan(λ_j u) − ½ x u,
539/// ρ(u) = Π_j (1 + λ_j² u²)^{h_j/4}.
540/// ```
541///
542/// Nothing in the derivation asks `λ_j > 0` — `arctan` is odd and `λ²` is even,
543/// so a negative weight simply turns its part of the phase the other way.
544///
545/// # Two truncation bounds, because one of them stops working at `x = 0`
546///
547/// The oscillatory bound `16/(x·U·ρ(U))` documented on
548/// [`weighted_chi_square_sf`] divides by `x`, and the ratio references this
549/// signed form exists for are evaluated at exactly `x = 0`, where the phase
550/// stops turning at all: `θ(u) → (π/4)·Σ_j h_j·sgn(λ_j)`, a constant. There is
551/// no oscillation left to cancel, so the alternating-series argument yields
552/// nothing.
553///
554/// What replaces it is the AMPLITUDE, which the same `x = 0` makes strong
555/// rather than weak. For `u ≥ U` and `t = u/U ≥ 1`,
556/// `(1 + λ²u²)/(1 + λ²U²) ≥ (1 + t²)/2 ≥ t` on every term ACTIVE at `U`
557/// (`|λ_j|·U ≥ 1`) and `≥ 1` on the rest, so `ρ(u) ≥ ρ(U)·t^{H/4}` with
558/// `H = Σ_{active} h_j` and
559///
560/// ```text
561/// |tail(U)| ≤ ∫_U^∞ du/(u ρ(u)) ≤ 4 / (H · ρ(U)).
562/// ```
563///
564/// This is a bound on the answer, not a guess about it, and it is the CHEAP
565/// one exactly where the oscillatory bound is unavailable: a ratio reference
566/// carries the residual `χ²_{n−p}`, so `H` is of order `n` and `ρ` grows like
567/// `U^{n/2}` — a handful of panels. Both bounds are evaluated and the smaller
568/// is taken, which also strictly improves the non-negative case at small `x`,
569/// where `16/(x·U·ρ)` is what used to make the sweep long.
570///
571/// # Phase monotonicity, generalized
572///
573/// The oscillatory bound is valid only past the point where `|θ′| ≥ x/4`. With
574/// mixed signs `φ′(u) = ½ Σ_j h_j λ_j/(1 + λ_j²u²)` is no longer monotone in
575/// `u`, so the test is applied to `½ Σ_j h_j |λ_j|/(1 + λ_j²u²)` — an upper
576/// bound on `|φ′|` that IS decreasing, hence a condition at `U` that holds for
577/// every `u ≥ U`. On non-negative weights the two expressions coincide.
578///
579/// # Exact special cases
580///
581/// * no nonzero weight — `Q ≡ 0`;
582/// * all weights positive and `x ≤ 0`, or all negative and `x ≥ 0` — the
583///   inequality is decided by the support;
584/// * all weights bit-identical — `Q = λ·χ²_{Σh}` exactly, on either sign.
585///
586/// Returns `NaN` if any weight is non-finite, if any degrees-of-freedom is not
587/// finite and positive, or if `statistic` is `NaN`.
588pub fn signed_weighted_chi_square_sf_to_tolerance(
589    terms: &[WeightedChiSquareTerm],
590    statistic: f64,
591    absolute_tolerance: f64,
592) -> (f64, f64) {
593    let tolerance = if absolute_tolerance.is_finite() && absolute_tolerance > 0.0 {
594        absolute_tolerance
595    } else {
596        WEIGHTED_CHI_SQUARE_TOLERANCE
597    };
598    if statistic.is_nan() {
599        return (f64::NAN, f64::NAN);
600    }
601    let mut active = Vec::with_capacity(terms.len());
602    for term in terms {
603        if !term.weight.is_finite()
604            || !(term.degrees_of_freedom.is_finite() && term.degrees_of_freedom > 0.0)
605        {
606            return (f64::NAN, f64::NAN);
607        }
608        if term.weight != 0.0 {
609            active.push(*term);
610        }
611    }
612    if active.is_empty() {
613        // `Q` is identically zero: it exceeds a negative threshold with
614        // certainty and a non-negative one never.
615        return (if statistic < 0.0 { 1.0 } else { 0.0 }, 0.0);
616    }
617    let all_positive = active.iter().all(|term| term.weight > 0.0);
618    let all_negative = active.iter().all(|term| term.weight < 0.0);
619    if all_positive && statistic <= 0.0 {
620        // `Q > 0` almost surely once one weight is positive.
621        return (1.0, 0.0);
622    }
623    if all_negative && statistic >= 0.0 {
624        // `Q < 0` almost surely once every weight is negative.
625        return (0.0, 0.0);
626    }
627    let first = active[0].weight;
628    if active.iter().all(|term| term.weight == first) {
629        let total_df: f64 = active.iter().map(|term| term.degrees_of_freedom).sum();
630        // `P(λ·χ² > x)` is the χ² upper tail at `x/λ` for `λ > 0` and the LOWER
631        // tail there for `λ < 0`, because dividing by a negative number turns
632        // the inequality around.
633        let scaled = statistic / first;
634        let tail = if first > 0.0 {
635            chi_square_sf(scaled, total_df)
636        } else {
637            1.0 - chi_square_sf(scaled, total_df)
638        };
639        return (tail, 0.0);
640    }
641    imhof_survival(&active, statistic, tolerance)
642}
643
644/// Default absolute accuracy [`weighted_chi_square_sf`] certifies on its Imhof
645/// truncation. It is four orders below the smallest probability any consumer
646/// of a survival function resolves in practice and eleven below one, so the
647/// truncation is never the term that limits a reported tail.
648pub const WEIGHTED_CHI_SQUARE_TOLERANCE: f64 = 1e-11;
649
650/// Gauss-Legendre nodes and weights on `[-1, 1]`, 16 points. A 16-node rule is
651/// exact through degree 31, which is far beyond the smooth amplitude
652/// `1/(u ρ(u))` over one phase period; the panel width, not the node count, is
653/// what resolves the oscillation.
654const GAUSS_LEGENDRE_16: [(f64, f64); 8] = [
655    (0.095_012_509_837_637_44, 0.189_450_610_455_068_64),
656    (0.281_603_550_779_258_9, 0.182_603_415_044_923_64),
657    (0.458_016_777_657_227_37, 0.169_156_519_395_002_65),
658    (0.617_876_244_402_643_8, 0.149_595_988_816_576_7),
659    (0.755_404_408_355_003, 0.124_628_971_255_534_07),
660    (0.865_631_202_387_831_8, 0.095_158_511_682_492_6),
661    (0.944_575_023_073_232_6, 0.062_253_523_938_647_456),
662    (0.989_400_934_991_649_9, 0.027_152_459_411_754_176),
663];
664
665/// Imhof's integrand `sin θ(u) / (u ρ(u))` with the `u → 0` limit folded in.
666#[inline]
667fn imhof_integrand(terms: &[WeightedChiSquareTerm], statistic: f64, u: f64) -> f64 {
668    if u == 0.0 {
669        let mean: f64 = terms
670            .iter()
671            .map(|term| term.weight * term.degrees_of_freedom)
672            .sum();
673        return 0.5 * (mean - statistic);
674    }
675    let mut phase = -0.5 * statistic * u;
676    let mut log_rho = 0.0;
677    for term in terms {
678        let wu = term.weight * u;
679        phase += 0.5 * term.degrees_of_freedom * wu.atan();
680        log_rho += 0.25 * term.degrees_of_freedom * wu.mul_add(wu, 1.0).ln();
681    }
682    phase.sin() / (u * log_rho.exp())
683}
684
685/// `ln ρ(u)`, the Imhof amplitude exponent.
686#[inline]
687fn imhof_log_rho(terms: &[WeightedChiSquareTerm], u: f64) -> f64 {
688    terms
689        .iter()
690        .map(|term| {
691            let wu = term.weight * u;
692            0.25 * term.degrees_of_freedom * wu.mul_add(wu, 1.0).ln()
693        })
694        .sum()
695}
696
697/// `½ Σ_j h_j|w_j|/(1 + w_j²u²)`, a DECREASING upper bound on the magnitude of
698/// the non-linear part of the phase's own derivative.
699///
700/// The oscillatory truncation bound is valid only past the point where the
701/// phase is monotone with `|θ'| ≥ x/4`, which needs `|φ'(u)| ≤ x/4` for every
702/// `u` past the truncation point rather than at it. With mixed-sign weights
703/// `φ'` is not monotone, so the test is applied to this bound instead; on
704/// non-negative weights the two are the same expression.
705#[inline]
706fn imhof_phase_slack(terms: &[WeightedChiSquareTerm], u: f64) -> f64 {
707    terms
708        .iter()
709        .map(|term| {
710            let wu = term.weight * u;
711            0.5 * term.degrees_of_freedom * term.weight.abs() / wu.mul_add(wu, 1.0)
712        })
713        .sum()
714}
715
716/// `4/(H·ρ(U))`, the AMPLITUDE truncation bound, with
717/// `H = Σ_{|w_j|U ≥ 1} h_j` the degrees of freedom already active at `U`.
718///
719/// Valid unconditionally — it bounds `∫_U^∞ du/(u ρ(u))` and never looks at the
720/// phase — and it is the only bound available at `statistic = 0`, where the
721/// oscillatory one divides by zero. `None` when nothing is active yet, since
722/// `ρ` is then still flat and there is no decay to integrate against.
723/// Panel width that resolves the integrand's AMPLITUDE, as opposed to its
724/// phase.
725///
726/// The phase rule below sizes a panel so it sweeps at most one oscillation.
727/// That is necessary and it is not sufficient: `1/(u ρ(u))` has structure of
728/// its own, on the scale `1/|λ|` where `(1 + λ²u²)^{h/4}` turns over, and a
729/// panel far wider than that scale is a 16-node rule aliasing a factor it never
730/// sampled. The two rules coincide only when the phase happens to turn at the
731/// same rate the amplitude does — which is exactly what fails when the phase
732/// rate is small: a ratio reference is evaluated at `statistic = 0`, and a
733/// two-term `F`-shaped combination can have `Σ h_j|λ_j|` of order one while
734/// `max_j|λ_j|` is also of order one, so `4π/Σ h|λ| ≈ 12` against an amplitude
735/// scale of `1`. Measured on `F_{1,5}` at `f = 0.05`: the phase-only panel
736/// returned `0.8319119` against the exact `0.8319122`, an error of `3.4e-7`
737/// certified at `1e-11`.
738///
739/// The scale is not a guess. As a function of complex `u` the integrand's
740/// nearest singularities are the branch points of `(1 + λ_j²u²)^{h_j/4}` at
741/// `u = ±i/|λ_j|`; the closest is `d = 1/max_j|λ_j|`, and the `−xu/2` phase and
742/// the `1/u` are entire and removable respectively. Gauss–Legendre with `N`
743/// nodes on a panel of half-width `a` converges like `ϱ^{-2N}` in the Bernstein
744/// parameter of the largest ellipse the integrand is analytic in, and an
745/// ellipse with semi-minor axis `d` has `ϱ` solving `(ϱ − 1/ϱ)/2 = d/a`. So
746/// asking `ϱ^{-2N} ≤ tolerance` fixes the half-width:
747///
748/// ```text
749/// ϱ = tolerance^{-1/2N},   a = d / [(ϱ − 1/ϱ)/2].
750/// ```
751///
752/// This is a RATE, not a certificate: the Bernstein bound also carries the
753/// integrand's maximum modulus on that ellipse, which the ellipse touching the
754/// branch point does not bound. The node count is what carries the margin, and
755/// the margin is MEASURED rather than asserted —
756/// `the_quadrature_resolves_the_amplitude_not_only_the_phase` compares against
757/// a reference at a far finer panel and reads the achieved error off it.
758///
759/// A looser request buys a wider panel here, which is the right direction: the
760/// consumer that derives its tolerance from the resolution of the statistic it
761/// is scoring pays for what it asked for.
762#[inline]
763fn imhof_amplitude_panel(max_abs_weight: f64, tolerance: f64) -> f64 {
764    let node_count = 2.0 * GAUSS_LEGENDRE_16.len() as f64;
765    let bernstein = tolerance.recip().powf(0.5 / node_count);
766    let semi_minor_ratio = 0.5 * (bernstein - bernstein.recip());
767    if !(semi_minor_ratio > 0.0 && max_abs_weight > 0.0) {
768        return f64::INFINITY;
769    }
770    2.0 / (max_abs_weight * semi_minor_ratio)
771}
772
773#[inline]
774fn imhof_amplitude_bound(terms: &[WeightedChiSquareTerm], u: f64) -> Option<f64> {
775    let active_df: f64 = terms
776        .iter()
777        .filter(|term| term.weight.abs() * u >= 1.0)
778        .map(|term| term.degrees_of_freedom)
779        .sum();
780    (active_df > 0.0).then(|| 4.0 / (active_df * imhof_log_rho(terms, u).exp()))
781}
782
783/// Cost backstop on the Imhof panel sweep.
784///
785/// The truncation point `U` needed for a given bound scales as
786/// `(16/(x·tol·C))^{2/(2+m)}` in the number `m` of weights that are *active*
787/// (`w_j U ≳ 1`) there, and the panel count as `U·x/4π`. With three or more
788/// comparable weights that count stays in the thousands for any statistic a
789/// likelihood-ratio consumer produces, so this backstop is unreachable — it
790/// exists for the one degenerate corner where it is not: two weights spread
791/// over several orders of magnitude, with a large statistic, where the sweep
792/// would otherwise run for tens of millions of panels to buy digits far below
793/// the modelling error of any statistic being referenced against it. The
794/// achieved bound is returned rather than discarded, so a caller that lands in
795/// that corner can see it instead of inferring it.
796///
797/// The panel width is the smaller of the phase rule and
798/// `imhof_amplitude_panel`, so the count above is a LOWER bound on what the
799/// sweep costs. It moves the corner slightly closer without changing which
800/// corner it is: the amplitude panel is `2/(|λ|_max·s(tol))`, independent of
801/// the statistic, so it binds where the phase rate is small — and a small phase
802/// rate is a small truncation point, which is the cheap end.
803pub const IMHOF_MAX_PANELS: usize = 1 << 21;
804
805fn imhof_survival(
806    terms: &[WeightedChiSquareTerm],
807    statistic: f64,
808    tolerance: f64,
809) -> (f64, f64) {
810    // A panel has to resolve the WHOLE phase, not just the `−xu/2` half. The
811    // total phase rate is bounded by `|θ'(u)| = |φ'(u) − x/2| ≤ (Σ h_j|w_j| +
812    // |x|)/2` — `|φ'|` is largest at the origin, where it is `½ Σ h_j|w_j|` —
813    // so a panel of `4π/(|x| + Σ h_j|w_j|)` sweeps at most one full oscillation
814    // anywhere on the half-line. Sizing on `4π/x` alone is correct only in the
815    // tail: at a small statistic that panel is enormous while the arctan part
816    // of the phase still turns over on the scale `1/w_j`, and the 16-node rule
817    // then aliases it (measured: a monotonicity violation of ~1e-5 at
818    // `x ≈ 4e-4`). At `x = 0` — the ratio references — the arctan part is the
819    // ONLY phase there is, and sizing on it is what keeps the rule honest.
820    let rate: f64 = terms
821        .iter()
822        .map(|term| term.degrees_of_freedom * term.weight.abs())
823        .sum();
824    let phase_panel = 4.0 * std::f64::consts::PI / (statistic.abs() + rate);
825    // ...and it has to resolve the AMPLITUDE as well; see
826    // `imhof_amplitude_panel` for why the phase rule alone is not enough and
827    // where the second scale comes from.
828    let max_abs_weight = terms
829        .iter()
830        .map(|term| term.weight.abs())
831        .fold(0.0_f64, f64::max);
832    let panel = phase_panel.min(imhof_amplitude_panel(max_abs_weight, tolerance));
833    let mut integral = 0.0_f64;
834    let mut lower = 0.0_f64;
835    let mut bound = f64::INFINITY;
836    for _ in 0..IMHOF_MAX_PANELS {
837        let upper = lower + panel;
838        let half = 0.5 * (upper - lower);
839        let mid = 0.5 * (upper + lower);
840        let mut panel_value = 0.0;
841        for &(node, weight) in &GAUSS_LEGENDRE_16 {
842            let offset = half * node;
843            panel_value += weight
844                * (imhof_integrand(terms, statistic, mid + offset)
845                    + imhof_integrand(terms, statistic, mid - offset));
846        }
847        integral += half * panel_value;
848        lower = upper;
849        // The amplitude bound holds unconditionally; the oscillatory one only
850        // once the phase is monotone, and only for a positive statistic.
851        // Whichever is available and smaller is the certified accuracy.
852        bound = imhof_amplitude_bound(terms, lower).unwrap_or(f64::INFINITY);
853        if statistic > 0.0 && imhof_phase_slack(terms, lower) <= 0.25 * statistic {
854            let oscillatory =
855                16.0 / (statistic * lower * imhof_log_rho(terms, lower).exp());
856            bound = bound.min(oscillatory);
857        }
858        if bound <= tolerance {
859            break;
860        }
861    }
862    (
863        (0.5 + integral / std::f64::consts::PI).clamp(0.0, 1.0),
864        bound,
865    )
866}
867
868/// Fisher-Snedecor survival probability `P(F_{d1,d2} > statistic)`.
869///
870/// The complementary regularized-beta identity is evaluated directly:
871///
872/// `I_x(d2/2, d1/2)`, `x = d2 / (d2 + d1·statistic)`.
873///
874/// The beta argument is derived in log space, so neither `d1·statistic` nor
875/// the denominator can overflow before a representable tail is recovered.
876pub fn fisher_snedecor_sf(
877    statistic: f64,
878    numerator_degrees_of_freedom: f64,
879    denominator_degrees_of_freedom: f64,
880) -> f64 {
881    let beta_a = 0.5 * denominator_degrees_of_freedom;
882    let beta_b = 0.5 * numerator_degrees_of_freedom;
883    if statistic.is_nan()
884        || statistic < 0.0
885        || !(numerator_degrees_of_freedom.is_finite()
886            && numerator_degrees_of_freedom > 0.0
887            && denominator_degrees_of_freedom.is_finite()
888            && denominator_degrees_of_freedom > 0.0
889            && beta_a > 0.0
890            && beta_b > 0.0)
891    {
892        return f64::NAN;
893    }
894    if statistic == 0.0 {
895        return 1.0;
896    }
897    if statistic == f64::INFINITY {
898        return 0.0;
899    }
900
901    let log_ratio = numerator_degrees_of_freedom.ln() + statistic.ln()
902        - denominator_degrees_of_freedom.ln();
903    let log_x = log_reciprocal_one_plus_exp(log_ratio);
904    regularized_beta_lower_from_log_x(log_x, beta_a, beta_b)
905}
906
907/// Scaled complementary error function `erfcx(x) = exp(x²) · erfc(x)`,
908/// specialized to the closed domain `x ∈ [0, +∞]`.
909///
910/// `+∞` maps to the exact limiting value `0`; `NaN` and negative inputs map to
911/// `NaN` because they violate this restricted kernel's domain. For
912/// `0 ≤ x < 26` the direct `exp(x²)·erfc(x)` form is finite. Beyond that point
913/// a six-correction asymptotic expansion avoids overflow while retaining the
914/// representable subnormal tail. At the switch, the first omitted term is
915/// below `2e-17` relative to the leading term.
916///
917/// The direct branch carries `x²` exactly (see `square_residual`). Without
918/// that correction the branch degraded like `x²·ε/2` — `1.4e-14` at `x = 10`,
919/// `5.7e-14` by the top of its range — while the asymptotic branch that takes
920/// over at `26` was already delivering `3e-16`. The seam was therefore a
921/// 190-fold step DOWN in error at the point where the code switches to what
922/// reads like the fallback, and the whole `[0, 26)` interval, where every
923/// probit / Mills / log-CDF consumer actually lives, was the inaccurate side.
924/// Both branches now hold `< 5e-16`, so the crossover is invisible.
925#[inline]
926pub fn erfcx_nonnegative(x: f64) -> f64 {
927    if x.is_nan() || x < 0.0 {
928        return f64::NAN;
929    }
930    if x == f64::INFINITY {
931        return 0.0;
932    }
933    if x < 26.0 {
934        // `x` is finite and in `[0, 26)`, so the square is exact-splittable and
935        // `head` is finite and strictly positive (`erfc(26⁻) ≈ 1e-295`).
936        let rounded_square = x * x;
937        let head = rounded_square.exp() * erfc(x);
938        head.mul_add(square_residual(x, rounded_square), head)
939    } else {
940        let inv = 1.0 / x;
941        let inv2 = inv * inv;
942        // erfcx(x) ~ 1/(sqrt(pi)x) * sum_n (-1)^n (2n-1)!!/(2x^2)^n.
943        // Horner form keeps the correction well scaled when `inv2` is tiny.
944        let poly = 1.0
945            + inv2
946                * (-0.5
947                    + inv2
948                        * (0.75
949                            + inv2
950                                * (-1.875
951                                    + inv2 * (6.5625 + inv2 * (-29.53125 + inv2 * 162.421875)))));
952        inv * poly * INV_SQRT_PI
953    }
954}
955
956/// Computes `log(1 - exp(-a))` for `a >= 0` without cancellation.
957#[inline]
958pub fn log1mexp_positive(a: f64) -> f64 {
959    assert!(a >= 0.0, "log1mexp_positive requires a >= 0: a={a}");
960    if a > core::f64::consts::LN_2 {
961        (-(-a).exp()).ln_1p()
962    } else if a > 0.0 {
963        (-(-a).exp_m1()).ln()
964    } else {
965        f64::NEG_INFINITY
966    }
967}
968
969// A finite binary64 is an integer multiple of 2^-1074. Its largest possible
970// significand occupies bits 2045..=2097 on that lattice. Thirty-three limbs
971// leave 14 carry bits, enough to sum at most 2^14-1 finite inputs exactly.
972const EXACT_BINARY64_SUM_WORDS: usize = 33;
973const EXACT_BINARY64_SUM_MAX_TERMS: usize = (1 << 14) - 1;
974const _: () = assert!(EXACT_BINARY64_SUM_WORDS * 64 == 2112);
975
976/// Why [`exact_binary64_sum_sign`] could not classify its finite exact sum.
977#[derive(Clone, Copy, Debug, Eq, PartialEq)]
978pub enum ExactBinary64SumSignError {
979    /// One input was not a finite binary64.
980    NonFiniteTerm { index: usize },
981    /// The fixed exact accumulator's structural term bound was exceeded.
982    TermCapacityExceeded { maximum: usize },
983}
984
985impl std::fmt::Display for ExactBinary64SumSignError {
986    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
987        match self {
988            Self::NonFiniteTerm { index } => {
989                write!(formatter, "exact binary64 sum term {index} is not finite")
990            }
991            Self::TermCapacityExceeded { maximum } => write!(
992                formatter,
993                "exact binary64 sum exceeds its structural {maximum}-term capacity"
994            ),
995        }
996    }
997}
998
999impl std::error::Error for ExactBinary64SumSignError {}
1000
1001/// Exact sign of a finite binary64 sum, independent of order and cancellation.
1002///
1003/// Every input is decoded as an integer significand on the common `2^-1074`
1004/// lattice. Positive and negative magnitudes accumulate into separate fixed
1005/// 2,112-bit unsigned integers; comparing those integers returns the sign of
1006/// the exact real sum, with no floating-point reduction and no tolerance.
1007///
1008/// At most 16,383 terms are admitted, the largest count whose worst-case carry
1009/// is structurally contained by the fixed accumulator.
1010pub fn exact_binary64_sum_sign(
1011    values: impl IntoIterator<Item = f64>,
1012) -> Result<std::cmp::Ordering, ExactBinary64SumSignError> {
1013    fn add_magnitude(
1014        accumulator: &mut [u64; EXACT_BINARY64_SUM_WORDS],
1015        value: f64,
1016    ) -> Result<(), ExactBinary64SumSignError> {
1017        let magnitude_bits = value.to_bits() & !(1_u64 << 63);
1018        let exponent_bits = ((magnitude_bits >> 52) & 0x7ff) as usize;
1019        let fraction = magnitude_bits & ((1_u64 << 52) - 1);
1020        let (significand, shift) = if exponent_bits == 0 {
1021            (fraction, 0usize)
1022        } else {
1023            ((1_u64 << 52) | fraction, exponent_bits - 1)
1024        };
1025        if significand == 0 {
1026            return Ok(());
1027        }
1028
1029        let mut word = shift / 64;
1030        let offset = shift % 64;
1031        let (low_sum, low_carry) =
1032            accumulator[word].overflowing_add(significand << offset);
1033        accumulator[word] = low_sum;
1034        word += 1;
1035
1036        let high = if offset == 0 {
1037            0
1038        } else {
1039            significand >> (64 - offset)
1040        };
1041        let (high_sum, high_carry) = accumulator[word].overflowing_add(high);
1042        let (high_sum, carry_carry) = high_sum.overflowing_add(u64::from(low_carry));
1043        accumulator[word] = high_sum;
1044        let mut carry = high_carry || carry_carry;
1045        word += 1;
1046        while carry {
1047            if word == EXACT_BINARY64_SUM_WORDS {
1048                return Err(ExactBinary64SumSignError::TermCapacityExceeded {
1049                    maximum: EXACT_BINARY64_SUM_MAX_TERMS,
1050                });
1051            }
1052            let (sum, next_carry) = accumulator[word].overflowing_add(1);
1053            accumulator[word] = sum;
1054            carry = next_carry;
1055            word += 1;
1056        }
1057        Ok(())
1058    }
1059
1060    let mut positive = [0_u64; EXACT_BINARY64_SUM_WORDS];
1061    let mut negative = [0_u64; EXACT_BINARY64_SUM_WORDS];
1062    for (index, value) in values.into_iter().enumerate() {
1063        if index == EXACT_BINARY64_SUM_MAX_TERMS {
1064            return Err(ExactBinary64SumSignError::TermCapacityExceeded {
1065                maximum: EXACT_BINARY64_SUM_MAX_TERMS,
1066            });
1067        }
1068        if !value.is_finite() {
1069            return Err(ExactBinary64SumSignError::NonFiniteTerm { index });
1070        }
1071        let target = if value.is_sign_negative() {
1072            &mut negative
1073        } else {
1074            &mut positive
1075        };
1076        add_magnitude(target, value)?;
1077    }
1078    for index in (0..EXACT_BINARY64_SUM_WORDS).rev() {
1079        match positive[index].cmp(&negative[index]) {
1080            std::cmp::Ordering::Less => return Ok(std::cmp::Ordering::Less),
1081            std::cmp::Ordering::Greater => return Ok(std::cmp::Ordering::Greater),
1082            std::cmp::Ordering::Equal => {}
1083        }
1084    }
1085    Ok(std::cmp::Ordering::Equal)
1086}
1087
1088/// Numerically stable signed log-sum-exp.  Given pairs
1089/// `(log|aⱼ|, sign(aⱼ))` (with `signs[j] ∈ {−1, 0, +1}`), returns
1090/// `(log|S|, sign(S))` for `S = Σⱼ signs[j]·exp(log_mags[j])`.  Positive
1091/// and negative magnitudes are first reduced together, after one common
1092/// log-space rescaling, with a twofold compensated sum. This avoids rounding
1093/// each same-sign subtotal through `ln` and `exp` before subtracting them — an
1094/// avoidable loss that is amplified in cancellation-conditioned derivative
1095/// cumulants. If the compensated residual lies inside its forward-error bound,
1096/// the function instead uses the two-subtotal log-domain difference
1097/// `log(|p − n|) = max(log p, log n) +
1098/// log1mexp(|log p − log n|)`. That branch retains differences between two input
1099/// logs even when their exponentials round to the same `f64`. When all signs are
1100/// zero or all magnitudes are `−∞`, returns `(NEG_INFINITY, 0.0)`.
1101///
1102/// A `+∞` log-magnitude denotes an infinite-magnitude term (`exp(+∞) = +∞`)
1103/// and dominates the sum: if it appears only with positive sign the result
1104/// is `(+∞, +1)`; only with negative sign, `(+∞, −1)` (a log-magnitude of
1105/// `+∞` with sign `−1` encodes the value `−∞`); with both signs the sum is
1106/// the indeterminate `+∞ − ∞`, returned as `(NaN, 0.0)`.  A `−∞`
1107/// log-magnitude is `exp(−∞) = 0` and is correctly dropped.
1108pub fn signed_log_sum_exp(log_mags: &[f64], signs: &[f64]) -> (f64, f64) {
1109    // Infinite-magnitude terms dominate any finite contribution, so resolve
1110    // them before the finite log-sum-exp reduction below. `−∞` log-magnitudes
1111    // are `exp(−∞) = 0` and need no special handling.
1112    let mut has_pos_inf = false;
1113    let mut has_neg_inf = false;
1114    for (idx, &lm) in log_mags.iter().enumerate() {
1115        if lm == f64::INFINITY {
1116            if signs[idx] > 0.0 {
1117                has_pos_inf = true;
1118            } else if signs[idx] < 0.0 {
1119                has_neg_inf = true;
1120            }
1121        }
1122    }
1123    match (has_pos_inf, has_neg_inf) {
1124        // P = +∞, N = +∞ ⇒ indeterminate +∞ − ∞.
1125        (true, true) => return (f64::NAN, 0.0),
1126        // P = +∞, N < ∞ ⇒ S = +∞.
1127        (true, false) => return (f64::INFINITY, 1.0),
1128        // N = +∞, P < ∞ ⇒ S = −∞, encoded as log-magnitude +∞ with sign −1.
1129        (false, true) => return (f64::INFINITY, -1.0),
1130        (false, false) => {}
1131    }
1132
1133    let mut pos_max = f64::NEG_INFINITY;
1134    let mut neg_max = f64::NEG_INFINITY;
1135    for (idx, &lm) in log_mags.iter().enumerate() {
1136        if signs[idx] > 0.0 {
1137            pos_max = pos_max.max(lm);
1138        } else if signs[idx] < 0.0 {
1139            neg_max = neg_max.max(lm);
1140        }
1141    }
1142
1143    if pos_max == f64::NEG_INFINITY && neg_max == f64::NEG_INFINITY {
1144        // Both partial sums are empty: no terms at all, all signs zero, or every
1145        // magnitude `−∞` (each `exp(−∞) = 0`). The signed sum is exactly `0`.
1146        return (f64::NEG_INFINITY, 0.0);
1147    }
1148
1149    // First reduce the signed terms directly after one common scaling. `head`
1150    // plus `tail` is a twofold sum: TwoSum recovers every addition's exact
1151    // residual, so cancellation does not discard the low part of either
1152    // same-sign subtotal before the final subtraction.
1153    let common_max = pos_max.max(neg_max);
1154    let mut signed_head = 0.0_f64;
1155    let mut signed_tail = 0.0_f64;
1156    let mut absolute_scaled_sum = 0.0_f64;
1157    let mut finite_term_count = 0usize;
1158    for (idx, &lm) in log_mags.iter().enumerate() {
1159        if !lm.is_finite() || !(signs[idx] > 0.0 || signs[idx] < 0.0) {
1160            continue;
1161        }
1162        let magnitude = (lm - common_max).exp();
1163        let term = if signs[idx] > 0.0 {
1164            magnitude
1165        } else {
1166            -magnitude
1167        };
1168        let combined = signed_head + term;
1169        let shifted = combined - signed_head;
1170        let residual = (signed_head - (combined - shifted)) + (term - shifted);
1171        signed_head = combined;
1172        signed_tail += residual;
1173        absolute_scaled_sum += magnitude;
1174        finite_term_count += 1;
1175    }
1176    let signed_scaled_sum = signed_head + signed_tail;
1177
1178    // Each scaled exponential and each accumulated residual contributes at most
1179    // one working-precision rounding. This conservative Wilkinson-style bound
1180    // decides from the operation count, rather than from a fitted threshold,
1181    // whether the linear-domain residual has a trustworthy sign and magnitude.
1182    // Below the bound, retain the input-log separation in the log-domain branch.
1183    let direct_error_bound =
1184        (finite_term_count as f64 + 2.0) * f64::EPSILON * absolute_scaled_sum;
1185    if signed_scaled_sum.abs() > direct_error_bound {
1186        return (
1187            common_max + signed_scaled_sum.abs().ln(),
1188            signed_scaled_sum.signum(),
1189        );
1190    }
1191
1192    // When exponentiation itself cannot resolve the signed residual, reduce
1193    // positive and negative groups separately in log space. Their internal sums
1194    // are still twofold-compensated before taking the logarithm.
1195    let mut pos_sum = 0.0_f64;
1196    let mut pos_tail = 0.0_f64;
1197    let mut neg_sum = 0.0_f64;
1198    let mut neg_tail = 0.0_f64;
1199    for (idx, &lm) in log_mags.iter().enumerate() {
1200        if !lm.is_finite() {
1201            continue;
1202        }
1203        if signs[idx] > 0.0 {
1204            let term = (lm - pos_max).exp();
1205            let combined = pos_sum + term;
1206            let shifted = combined - pos_sum;
1207            pos_tail += (pos_sum - (combined - shifted)) + (term - shifted);
1208            pos_sum = combined;
1209        } else if signs[idx] < 0.0 {
1210            let term = (lm - neg_max).exp();
1211            let combined = neg_sum + term;
1212            let shifted = combined - neg_sum;
1213            neg_tail += (neg_sum - (combined - shifted)) + (term - shifted);
1214            neg_sum = combined;
1215        }
1216    }
1217    pos_sum += pos_tail;
1218    neg_sum += neg_tail;
1219
1220    let log_pos = if pos_sum > 0.0 {
1221        pos_max + pos_sum.ln()
1222    } else {
1223        f64::NEG_INFINITY
1224    };
1225    let log_neg = if neg_sum > 0.0 {
1226        neg_max + neg_sum.ln()
1227    } else {
1228        f64::NEG_INFINITY
1229    };
1230
1231    if log_neg == f64::NEG_INFINITY {
1232        return (log_pos, 1.0);
1233    }
1234    if log_pos == f64::NEG_INFINITY {
1235        return (log_neg, -1.0);
1236    }
1237    if log_pos > log_neg {
1238        let gap = log_pos - log_neg;
1239        (log_pos + log1mexp_positive(gap), 1.0)
1240    } else if log_neg > log_pos {
1241        let gap = log_neg - log_pos;
1242        (log_neg + log1mexp_positive(gap), -1.0)
1243    } else {
1244        (f64::NEG_INFINITY, 0.0)
1245    }
1246}
1247
1248/// Numerically stable `ln Φ(x)` for the standard normal CDF. For `x ≥ 0`,
1249/// evaluates `ln(1 - 0.5 erfc(x/sqrt(2)))` with `ln_1p`, retaining the small
1250/// negative result after `Φ(x)` itself rounds to one. For `x < 0`, rewrites
1251/// `ln Φ(x) = −u² + ln(½·erfcx(u))`, `u = −x/√2`,
1252/// which preserves digits throughout the representable left tail without a
1253/// probability floor. Returns the corresponding IEEE limit at infinities and
1254/// propagates `NaN`.
1255#[inline]
1256pub fn normal_logcdf(x: f64) -> f64 {
1257    if x == f64::INFINITY {
1258        return 0.0;
1259    }
1260    if x == f64::NEG_INFINITY {
1261        return f64::NEG_INFINITY;
1262    }
1263    if x.is_nan() {
1264        return f64::NAN;
1265    }
1266    if x < 0.0 {
1267        let (u, scaled_tail) = negative_normal_tail_components(x);
1268        negative_normal_logcdf_from_scaled_tail(u, scaled_tail)
1269    } else {
1270        let upper_tail = 0.5 * erfc(x / std::f64::consts::SQRT_2);
1271        (-upper_tail).ln_1p()
1272    }
1273}
1274
1275/// Numerically stable `ln(1 − Φ(x)) = ln Φ(−x)` for the standard normal
1276/// survival function.  Delegates to `normal_logcdf(-x)` so the deep-right
1277/// tail benefits from the same `erfcx`-based representation.
1278#[inline]
1279pub fn normal_logsf(x: f64) -> f64 {
1280    normal_logcdf(-x)
1281}
1282
1283/// Joint evaluation of `ln Φ(x)` and the Mills-ratio analogue
1284/// `φ(x) / Φ(x)`, signed for the symmetric branch.  Used by the latent
1285/// probit families where the inverse-link gradient needs the ratio and
1286/// the likelihood needs the log-CDF on the same `x`; computing both in
1287/// one call shares the `erfcx` evaluation that dominates the cost in the
1288/// deep tail.
1289#[inline]
1290pub fn signed_probit_logcdf_and_mills_ratio(x: f64) -> (f64, f64) {
1291    if x == f64::INFINITY {
1292        return (0.0, 0.0);
1293    }
1294    if x == f64::NEG_INFINITY {
1295        return (f64::NEG_INFINITY, f64::INFINITY);
1296    }
1297    if x.is_nan() {
1298        return (f64::NAN, f64::NAN);
1299    }
1300    if x < 0.0 {
1301        let (u, scaled_tail) = negative_normal_tail_components(x);
1302        (
1303            negative_normal_logcdf_from_scaled_tail(u, scaled_tail),
1304            SQRT_2_OVER_PI / scaled_tail,
1305        )
1306    } else {
1307        let upper_tail = 0.5 * erfc(x / std::f64::consts::SQRT_2);
1308        let cdf = 1.0 - upper_tail;
1309        let lambda = normal_pdf(x) / cdf;
1310        ((-upper_tail).ln_1p(), lambda)
1311    }
1312}
1313
1314#[inline]
1315fn negative_normal_tail_components(x: f64) -> (f64, f64) {
1316    assert!(x.is_finite() && x < 0.0);
1317    let u = -x / std::f64::consts::SQRT_2;
1318    (u, erfcx_nonnegative(u))
1319}
1320
1321#[inline]
1322fn negative_normal_logcdf_from_scaled_tail(u: f64, scaled_tail: f64) -> f64 {
1323    -u * u + scaled_tail.ln() - std::f64::consts::LN_2
1324}
1325
1326/// Stable value and first four derivatives of `ln Φ(x)`.
1327///
1328/// The moderate regime uses the exact Mills-ratio recurrence, with the brackets
1329/// collected in `q = λ + x` once `x < 0` so that they do not cancel as `λ`
1330/// closes on `−x`. In the deep left tail, differentiating the Laplace continued
1331/// fraction
1332///
1333/// `φ(t)/Φ(-t) = t + 1/(t + 2/(t + 3/(...)))`, `t = -x`,
1334///
1335/// carries the small correction to `t` independently, so `f'' -> -1` and the
1336/// higher derivatives approach zero without subtracting nearly equal `f64`s.
1337/// In the right tail, signed log-magnitude sums preserve polynomially weighted
1338/// derivatives even when `φ(x)/Φ(x)` itself has rounded to zero.
1339#[inline]
1340pub fn normal_logcdf_derivatives(x: f64) -> [f64; 5] {
1341    if x.is_nan() {
1342        return [f64::NAN; 5];
1343    }
1344    if x == f64::INFINITY {
1345        return [0.0; 5];
1346    }
1347    if x == f64::NEG_INFINITY {
1348        return [f64::NEG_INFINITY, f64::INFINITY, -1.0, 0.0, 0.0];
1349    }
1350
1351    const RIGHT_LOG_MAGNITUDE_SWITCH: f64 = 8.0;
1352    if x <= LEFT_CONTINUED_FRACTION_SWITCH {
1353        return normal_logcdf_derivatives_left_tail(x);
1354    }
1355    if x >= RIGHT_LOG_MAGNITUDE_SWITCH {
1356        return normal_logcdf_derivatives_right_tail(x);
1357    }
1358
1359    let (log_cdf, lambda) = signed_probit_logcdf_and_mills_ratio(x);
1360    let x2 = x * x;
1361    if x < 0.0 {
1362        // Left of the origin the brackets below are collected in the SAME Mills
1363        // correction `q = λ + x` the continued-fraction branch carries, because
1364        // written in `λ` they cancel catastrophically long before the branch
1365        // ends. `λ(x) → −x` as `x → −∞`, so every term of, say,
1366        // `(x³−3x) + (7x²−4)λ + 12xλ² + 6λ³` grows like `|x|³` while their sum
1367        // decays: at `x = −4` they are `−52`, `456`, `−857`, `453` and add to
1368        // `−0.0023`, a cancellation of 380000 that costs eleven digits. In `q`
1369        // the same bracket is `−6q³ + 6xq² + (4−x²)q − x`, whose terms are
1370        // `−0.069`, `−1.22`, `−2.71`, `4` — a cancellation of 1847, three
1371        // orders milder. The reformulation is exact (`λ = q − x` substituted and
1372        // re-collected), costs the same flops, and buys 16–34x across the whole
1373        // branch: worst over `x ∈ [−4, 0]` falls from `4.5e−11` to `2.8e−12`.
1374        //
1375        // `q` itself is safe to form here: `λ/2 ≤ |x| ≤ 2λ` holds over most of
1376        // the range, so `λ + x` is EXACT by Sterbenz, and where it is not (`x`
1377        // near 0) `q` is the same size as `λ` and nothing cancels. That is the
1378        // whole reason the rewrite works — it moves the cancellation out of the
1379        // brackets and into a subtraction that has none.
1380        //
1381        // Past the origin `q → x` is no longer small, the `λ` form has nothing
1382        // to cancel (`λ → 0` and `x² − 1` dominates), and it is the more
1383        // accurate of the two — hence the sign test rather than a blanket swap.
1384        let q = lambda + x;
1385        let q2 = q * q;
1386        return [
1387            log_cdf,
1388            lambda,
1389            -lambda * q,
1390            lambda * (2.0 * q2 - x * q - 1.0),
1391            lambda * (-6.0 * q2 * q + 6.0 * x * q2 + (4.0 - x2) * q - x),
1392        ];
1393    }
1394    let lambda2 = lambda * lambda;
1395    let lambda3 = lambda2 * lambda;
1396    [
1397        log_cdf,
1398        lambda,
1399        -lambda * (x + lambda),
1400        lambda * (x2 - 1.0 + 3.0 * x * lambda + 2.0 * lambda2),
1401        -lambda
1402            * ((x * x2 - 3.0 * x) + (7.0 * x2 - 4.0) * lambda + 12.0 * x * lambda2 + 6.0 * lambda3),
1403    ]
1404}
1405
1406#[derive(Clone, Copy)]
1407struct MillsCorrectionDerivatives {
1408    value: f64,
1409    first: f64,
1410    second: f64,
1411    third: f64,
1412}
1413
1414/// `x` at or below which the left-tail Mills ratio is taken from the Laplace
1415/// continued fraction rather than from `erfcx`. Equivalently `t = −x ≥ 4`.
1416const LEFT_CONTINUED_FRACTION_SWITCH: f64 = -4.0;
1417
1418/// The Laplace continued-fraction **correction** to the left-tail Mills ratio,
1419///
1420/// `q(t) = λ(−t) − t = 1/(t + 2/(t + 3/(...)))`,   `λ(x) = φ(x)/Φ(x)`,
1421///
1422/// together with its first three derivatives in `t`. Requires `t ≥ 4`.
1423///
1424/// `q` is the whole content of the left tail that is NOT the leading `t`: it
1425/// decays like `1/t − 2/t³ + 10/t⁵ − ...`, and every operation building it is
1426/// a division or an addition of positive quantities, so it carries full
1427/// relative precision no matter how small it gets. That is the property its
1428/// two consumers need, and it is why the correction is returned separately
1429/// instead of pre-added to `t`:
1430///
1431/// * [`normal_logcdf_derivatives_left_tail`] needs `f'' = −(1 + q')` and the
1432///   higher derivatives, which tend to `−1` and `0` and would be destroyed by
1433///   differencing nearly equal `f64`s.
1434/// * [`cone_boundary_log_factor_and_derivatives`] needs `∂corr/∂a = b − q(t)`,
1435///   which is the same statement one substitution away (#2306 §4).
1436///
1437/// Recovering `q` from a separately computed `λ` — `q = λ − t` — is exactly the
1438/// cancellation this exists to avoid, and it is not a small effect: at `t = 1e8`
1439/// it costs every significant digit, and past `t ≈ 2e8` it returns the wrong
1440/// SIGN. The reference itself has to be carried at ~120 decimal digits before it
1441/// reproduces what this recursion gives in binary64.
1442#[inline]
1443fn mills_correction_continued_fraction(t: f64) -> MillsCorrectionDerivatives {
1444    assert!(t.is_finite() && t >= 4.0);
1445    let mut q = MillsCorrectionDerivatives {
1446        value: 0.0,
1447        first: 0.0,
1448        second: 0.0,
1449        third: 0.0,
1450    };
1451    // The truncation error is damped by a product of the continued-fraction
1452    // sensitivities `n/(t + q)^2`, so the depth must be sized at `t = 4` — the
1453    // LEAST converged point of the domain, and the one the log-CDF branch sits
1454    // exactly on. Each successive derivative converges roughly 15x slower than
1455    // the last, because differentiating the recursion multiplies each level's
1456    // contribution by another factor of that same sensitivity. Measured against
1457    // a 60-digit reference at `t = 4`:
1458    //
1459    // ```text
1460    //            q         q'        q''       q'''
1461    //   32   1.9e-15    7.0e-14    1.4e-12    2.1e-11
1462    //   64   2.3e-23    1.4e-21    4.4e-20    1.0e-18
1463    // ```
1464    //
1465    // 32 levels is enough for the VALUE and nothing else: it leaves `q'''` — the
1466    // fourth log-CDF derivative — wrong in its eleventh digit. The depths that
1467    // first reach `1e-17` at `t = 4` are 41, 47, 53 and 60 for the four
1468    // channels, so 64 covers the worst of them with ~200x of margin, and the
1469    // requirement falls off fast enough (33 levels at `t = 6`, 24 at `t = 8`,
1470    // 12 at `t = 20`) that one constant sized for the edge is safe everywhere
1471    // above it. The extra levels are pure convergence — every step divides
1472    // positive quantities — so they cannot destabilise a large `t`.
1473    for n in (1..=64).rev() {
1474        let denominator = t + q.value;
1475        let inv_denominator = denominator.recip();
1476        let value = f64::from(n) / denominator;
1477        let denominator_first = 1.0 + q.first;
1478        let a = denominator_first * inv_denominator;
1479        let b = q.second * inv_denominator;
1480        let c = q.third * inv_denominator;
1481        q = MillsCorrectionDerivatives {
1482            value,
1483            first: -value * denominator_first / denominator,
1484            second: value * (2.0 * a * a - b),
1485            third: value * (-6.0 * a * a * a + 6.0 * a * b - c),
1486        };
1487    }
1488    q
1489}
1490
1491#[inline]
1492fn normal_logcdf_derivatives_left_tail(x: f64) -> [f64; 5] {
1493    assert!(x.is_finite() && x <= LEFT_CONTINUED_FRACTION_SWITCH);
1494    let t = -x;
1495    let q = mills_correction_continued_fraction(t);
1496    [
1497        normal_logcdf(x),
1498        t + q.value,
1499        -(1.0 + q.first),
1500        q.second,
1501        -q.third,
1502    ]
1503}
1504
1505#[inline]
1506fn normal_logcdf_derivatives_right_tail(x: f64) -> [f64; 5] {
1507    assert!(x.is_finite() && x >= 8.0);
1508    const LOG_SQRT_2PI: f64 = 0.918_938_533_204_672_7;
1509    let log_cdf = normal_logcdf(x);
1510    let u = x / std::f64::consts::SQRT_2;
1511    let log_lambda = -u * u - LOG_SQRT_2PI - log_cdf;
1512    let log_x = x.ln();
1513    let inv_x2 = x.recip() * x.recip();
1514
1515    let first = log_lambda.exp();
1516    let second = signed_exp_sum(&[log_x + log_lambda, 2.0 * log_lambda], &[-1.0, -1.0]);
1517    let third = signed_exp_sum(
1518        &[
1519            2.0 * log_x + (-inv_x2).ln_1p() + log_lambda,
1520            3.0_f64.ln() + log_x + 2.0 * log_lambda,
1521            2.0_f64.ln() + 3.0 * log_lambda,
1522        ],
1523        &[1.0, 1.0, 1.0],
1524    );
1525    let fourth = signed_exp_sum(
1526        &[
1527            3.0 * log_x + (-3.0 * inv_x2).ln_1p() + log_lambda,
1528            7.0_f64.ln() + 2.0 * log_x + (-(4.0 / 7.0) * inv_x2).ln_1p() + 2.0 * log_lambda,
1529            12.0_f64.ln() + log_x + 3.0 * log_lambda,
1530            6.0_f64.ln() + 4.0 * log_lambda,
1531        ],
1532        &[-1.0, -1.0, -1.0, -1.0],
1533    );
1534    [log_cdf, first, second, third, fourth]
1535}
1536
1537#[inline]
1538fn signed_exp_sum(log_magnitudes: &[f64], signs: &[f64]) -> f64 {
1539    let (log_magnitude, sign) = signed_log_sum_exp(log_magnitudes, signs);
1540    if sign == 0.0 {
1541        0.0
1542    } else {
1543        sign * log_magnitude.exp()
1544    }
1545}
1546
1547#[inline]
1548fn acklam_lower_tail_quantile_from_log_probability(log_p: f64) -> f64 {
1549    const C: [f64; 6] = [
1550        -7.784_894_002_430_293e-3,
1551        -3.223_964_580_411_365e-1,
1552        -2.400_758_277_161_838,
1553        -2.549_732_539_343_734,
1554        4.374_664_141_464_968,
1555        2.938_163_982_698_783,
1556    ];
1557    const D: [f64; 4] = [
1558        7.784_695_709_041_462e-3,
1559        3.224_671_290_700_398e-1,
1560        2.445_134_137_142_996,
1561        3.754_408_661_907_416,
1562    ];
1563    let q = (-2.0 * log_p).sqrt();
1564    (((((C[0] * q + C[1]) * q + C[2]) * q + C[3]) * q + C[4]) * q + C[5])
1565        / ((((D[0] * q + D[1]) * q + D[2]) * q + D[3]) * q + 1.0)
1566}
1567
1568/// Standard normal quantile Φ⁻¹(p) using Acklam's rational approximation.
1569#[inline]
1570pub fn standard_normal_quantile(p: f64) -> Result<f64, String> {
1571    if !(p.is_finite() && p > 0.0 && p < 1.0) {
1572        return Err(format!("normal quantile requires p in (0,1), got {p}"));
1573    }
1574
1575    const A: [f64; 6] = [
1576        -3.969_683_028_665_376e1,
1577        2.209_460_984_245_205e2,
1578        -2.759_285_104_469_687e2,
1579        1.383_577_518_672_69e2,
1580        -3.066_479_806_614_716e1,
1581        2.506_628_277_459_239,
1582    ];
1583    const B: [f64; 5] = [
1584        -5.447_609_879_822_406e1,
1585        1.615_858_368_580_409e2,
1586        -1.556_989_798_598_866e2,
1587        6.680_131_188_771_972e1,
1588        -1.328_068_155_288_572e1,
1589    ];
1590    const P_LOW: f64 = 0.02425;
1591    const P_HIGH: f64 = 1.0 - P_LOW;
1592
1593    let mut x = if p < P_LOW {
1594        acklam_lower_tail_quantile_from_log_probability(p.ln())
1595    } else if p <= P_HIGH {
1596        let q = p - 0.5;
1597        let r = q * q;
1598        (((((A[0] * r + A[1]) * r + A[2]) * r + A[3]) * r + A[4]) * r + A[5]) * q
1599            / (((((B[0] * r + B[1]) * r + B[2]) * r + B[3]) * r + B[4]) * r + 1.0)
1600    } else {
1601        -acklam_lower_tail_quantile_from_log_probability((1.0 - p).ln())
1602    };
1603    for _ in 0..2 {
1604        let density = normal_pdf(x);
1605        if !(density.is_finite() && density > 0.0) {
1606            break;
1607        }
1608        // Residual F(x) − p, formed without catastrophic cancellation in
1609        // either tail. For an upper-tail iterate `x > 0`, `normal_cdf(x)`
1610        // saturates to ~1, so the direct `normal_cdf(x) − p` annihilates the
1611        // tiny residual the polish must act on; instead use the upper-tail
1612        // complement `F(x) − p = (1 − p) − 0.5·erfc(x/√2)`, where both terms
1613        // are the small upper-tail quantities (`1 − p` is exact by Sterbenz
1614        // for `p ∈ [½,1)`). For `x ≤ 0`, `normal_cdf(x) = 0.5·erfc(|x|/√2)` is
1615        // itself the faithfully carried small lower-tail value, so the direct
1616        // form is already cancellation-free.
1617        let residual = if (0.25..=0.75).contains(&p) {
1618            // Central band. Both tail forms below subtract two quantities of
1619            // size ~½, so their difference carries an absolute error of one ulp
1620            // of ½ (1.1e-16) NO MATTER how small the true residual is. Since
1621            // `Δx ≈ residual_error / φ(x)`, the returned quantile then carries a
1622            // FIXED absolute error ~1.2e-16 and a relative error ~1.2e-16/|x|
1623            // that diverges as `p → ½`: measured 4.1e-14 at `p = 0.50125` and
1624            // 1.2e-03 at `p = ½ + 2.75e-14`, against ~2e-16 everywhere else in
1625            // this module. The polish cannot repair the seed there — the
1626            // residual it is handed is quantized to multiples of one ulp of ½
1627            // and is usually exactly 0, so the answer that ships is the raw
1628            // Acklam seed at its own 1.15e-9.
1629            //
1630            // Subtracting the ½ ANALYTICALLY removes it: `F(x) − p` is
1631            // `(F(x) − ½) − (p − ½)` = `½·erf(x/√2) − δ`, and both terms are now
1632            // of size |δ| with full RELATIVE accuracy — `erf` near 0 is `z·R(z²)`,
1633            // no cancellation — so the residual error is `ε·|δ|` and the relative
1634            // error in `x` is `ε` uniformly, including in the limit `x → 0`.
1635            //
1636            // The band is the exactness domain of `δ`, not a tuning choice:
1637            // Sterbenz's lemma makes `p − ½` exact for `p ∈ [¼, 1]`, and the
1638            // reflection `p ↦ 1 − p` maps that onto `[0, ¾]`, so `[¼, ¾]` is
1639            // where δ is exact on both sides. It is also where the centered form
1640            // is the better one: outside it `|x| > 0.6745` and the tail forms
1641            // carry relative accuracy in their own small quantity, which is what
1642            // the deep tails need. At the shared boundary the two agree to
1643            // within a factor of two, so nothing steps across the seam.
1644            0.5 * erf(x / std::f64::consts::SQRT_2) - (p - 0.5)
1645        } else if x > 0.0 {
1646            (1.0 - p) - 0.5 * erfc(x / std::f64::consts::SQRT_2)
1647        } else {
1648            normal_cdf(x) - p
1649        };
1650        let correction = residual / density;
1651        let denominator = 1.0 + 0.5 * x * correction;
1652        if !(correction.is_finite() && denominator.is_finite() && denominator != 0.0) {
1653            break;
1654        }
1655        let step = correction / denominator;
1656        if !step.is_finite() {
1657            break;
1658        }
1659        x -= step;
1660        if step.abs() <= 2.0 * f64::EPSILON * x.abs().max(1.0) {
1661            break;
1662        }
1663    }
1664    Ok(x)
1665}
1666
1667/// Standard normal quantile from `log_p = ln Φ(x)`.
1668///
1669/// Unlike [`standard_normal_quantile`], this remains defined when `Φ(x)` is
1670/// smaller than the least positive `f64`, and when `Φ(x)` is so close to one
1671/// that exponentiating `log_p` rounds to exactly one. Acklam's lower-tail
1672/// approximation supplies the initial point; Newton polishing solves
1673/// `ln Φ(x) = log_p` with the stable log-CDF and Mills ratio, so neither tail
1674/// forms a probability-space subtraction.
1675#[inline]
1676pub fn standard_normal_quantile_from_log_cdf(log_p: f64) -> Result<f64, String> {
1677    if !(log_p.is_finite() && log_p < 0.0) {
1678        return Err(format!(
1679            "normal log-quantile requires finite log_p < 0, got {log_p}"
1680        ));
1681    }
1682
1683    if log_p > -std::f64::consts::LN_2 {
1684        // Reflect through the upper tail without forming `1 - exp(log_p)`.
1685        let log_q = (-log_p.exp_m1()).ln();
1686        return standard_normal_quantile_from_log_cdf(log_q).map(|x| -x);
1687    }
1688
1689    let p = log_p.exp();
1690    let mut x = if p > 0.0 {
1691        standard_normal_quantile(p)?
1692    } else {
1693        acklam_lower_tail_quantile_from_log_probability(log_p)
1694    };
1695    for _ in 0..4 {
1696        let (current_log_p, mills_ratio) = signed_probit_logcdf_and_mills_ratio(x);
1697        if !(current_log_p.is_finite() && mills_ratio.is_finite() && mills_ratio > 0.0) {
1698            break;
1699        }
1700        let step = (current_log_p - log_p) / mills_ratio;
1701        if !step.is_finite() {
1702            break;
1703        }
1704        x -= step;
1705        if step.abs() <= 2.0 * f64::EPSILON * x.abs().max(1.0) {
1706            break;
1707        }
1708    }
1709    Ok(x)
1710}
1711
1712/// Log of the standardized one-sided truncated-Gaussian boundary factor for
1713/// the constrained-LAML cone correction (gam#2306 §4).
1714///
1715/// For a constraint coordinate with Lagrange multiplier `μ ≥ 0`, normal
1716/// curvature `h > 0`, and signed interior slack `s ≥ 0`, the exact 1-D
1717/// boundary integral is
1718///
1719/// ```text
1720///   ∫_{−s}^{∞} exp(−μ·u − ½·h·u²) du
1721///     = √(2π/h) · exp(μ²/(2h)) · Φ(s·√h − μ/√h),
1722/// ```
1723///
1724/// and the correction of the Laplace criterion RELATIVE to the unrestricted
1725/// Gaussian factor `√(2π/h)` is, in the standardized arguments
1726/// `a = μ/√h ≥ 0`, `b = s·√h ≥ 0`:
1727///
1728/// ```text
1729///   corr(a, b) = a²/2 + ln Φ(b − a).
1730/// ```
1731///
1732/// Key limits (the #2306 derivation's continuity contract): an activating
1733/// row (`a = 0`, `b = 0`) contributes exactly `ln ½` (the half-Gaussian); a
1734/// deep interior row (`b − a → ∞`) contributes `→ 0`, reducing byte-exactly
1735/// to the unrestricted LAML; a hard-pushed active row (`a → ∞`, `b = 0`)
1736/// follows the exact linear-decay limit `corr → −ln(a·√(2π))`.
1737///
1738/// Evaluated FUSED: computing `a²/2` and `ln Φ(b−a)` as two separate f64
1739/// terms cancels catastrophically once `a ≳ 10⁴` (both grow like `±a²/2`).
1740/// On the `b < a` branch the sum collapses analytically to
1741/// `a·b − b²/2 + ln(erfcx((a−b)/√2)/2)`, which is cancellation-free (for an
1742/// active row, `b = 0`, it is a single `erfcx` evaluation).
1743#[must_use]
1744pub fn cone_boundary_log_factor(mu_over_sqrt_h: f64, slack_times_sqrt_h: f64) -> f64 {
1745    let a = mu_over_sqrt_h;
1746    let b = slack_times_sqrt_h;
1747    if !(a.is_finite() && b.is_finite()) || a < 0.0 || b < 0.0 {
1748        return f64::NAN;
1749    }
1750    let xi = b - a;
1751    if xi >= 0.0 {
1752        // Interior-dominant: ln Φ(ξ) is a small negative number and a²/2 is
1753        // exact; no cancellation between them (a ≤ b here, so a²/2 ≤ ab −
1754        // b²/2 + O(1) stays modest whenever the factor itself is modest).
1755        0.5 * a * a + normal_logcdf(xi)
1756    } else {
1757        // Active-dominant: fused analytic collapse of a²/2 + ln Φ(−(a−b)).
1758        let u = (a - b) / std::f64::consts::SQRT_2;
1759        a * b - 0.5 * b * b + (0.5 * erfcx_nonnegative(u)).ln()
1760    }
1761}
1762
1763/// [`cone_boundary_log_factor`] together with its exact partial derivatives
1764/// in the standardized arguments — the pieces the outer ρ-gradient chains
1765/// through `(μ̃, h̃, s)(ρ)` (gam#2306 §4 "the g-factors differentiate in
1766/// closed form"). With `ξ = b − a` and the Mills ratio `λ(ξ) = φ(ξ)/Φ(ξ)`:
1767///
1768/// ```text
1769///   ∂corr/∂a = a − λ(ξ),      ∂corr/∂b = λ(ξ).
1770/// ```
1771///
1772/// `∂corr/∂b = λ(ξ)` is a single `erfcx` evaluation and needs nothing further.
1773///
1774/// `∂corr/∂a` does. Written literally as `a − λ(ξ)` it is a subtraction of two
1775/// quantities that both grow like `a`, because `λ(−t) = t + q(t)` with
1776/// `q(t) ~ 1/t`: the answer is the SMALL correction `q`, and forming it by
1777/// subtraction destroys `log₁₀(a²·ε)` digits of it. The value
1778/// [`cone_boundary_log_factor`] is fused precisely to dodge the twin of this
1779/// cancellation, and the gradient has to be fused the same way rather than
1780/// re-derived from a `λ` that has already lost the digits.
1781///
1782/// So on the active branch the correction is taken directly from the Laplace
1783/// continued fraction (`mills_correction_continued_fraction`), the same one
1784/// the left-tail log-CDF derivatives use, under the substitution
1785///
1786/// ```text
1787///   ξ = b − a,  t = −ξ = a − b  ⇒  ∂corr/∂a = a − λ(ξ) = a − (t + q(t)) = b − q(t),
1788/// ```
1789///
1790/// which is cancellation-free for every `a`: `b ≥ 0` and `q(t) ∈ (0, ¼]`. The
1791/// deep-active limit `∂corr/∂a → −1/a` then holds to full relative precision
1792/// instead of to none, and the sign is right (the factor is strictly decreasing
1793/// in `a`, so `∂corr/∂a < 0` whenever `b = 0`).
1794///
1795/// Measured on `a ∈ [10⁻², 10¹⁴] × b ∈ {0, …, 10³}` against a 250-digit
1796/// reference: the subtractive form reaches `6.1e6` relative error and turns
1797/// positive past `a ≈ 2e8`; this form is within `7.5e-16` — about 3 ulp — of
1798/// the truth, measured against the magnitudes entering the subtraction rather
1799/// than against the result. That is the right denominator because `∂corr/∂a`
1800/// genuinely passes through ZERO along the curve `b = q(a − b)` (the factor is
1801/// increasing in `a` for slack rows and decreasing for active ones), and no
1802/// representation carries relative precision across its own root; near it the
1803/// error is bounded in absolute terms by `ε·b`, which is what a gradient
1804/// consumer needs.
1805#[must_use]
1806pub fn cone_boundary_log_factor_and_derivatives(
1807    mu_over_sqrt_h: f64,
1808    slack_times_sqrt_h: f64,
1809) -> (f64, f64, f64) {
1810    let a = mu_over_sqrt_h;
1811    let b = slack_times_sqrt_h;
1812    let value = cone_boundary_log_factor(a, b);
1813    if value.is_nan() {
1814        // The value's domain guard (finite, non-negative `a` and `b`) is the
1815        // function's domain; a gradient off it is not defined either, and
1816        // returning a finite one next to a NaN value would read as usable.
1817        return (value, f64::NAN, f64::NAN);
1818    }
1819    let xi = b - a;
1820    let (_, mills) = signed_probit_logcdf_and_mills_ratio(xi);
1821    let d_a = if xi <= LEFT_CONTINUED_FRACTION_SWITCH {
1822        b - mills_correction_continued_fraction(-xi).value
1823    } else {
1824        // `|ξ| < 4`, so `λ(ξ) < λ(−4) ≈ 4.26` and `a = b − ξ` is bounded by it:
1825        // the subtraction is between two `O(1)` quantities and loses nothing
1826        // that matters.
1827        a - mills
1828    };
1829    (value, d_a, mills)
1830}
1831
1832#[cfg(test)]
1833mod cone_boundary_factor_tests {
1834    use super::*;
1835
1836    /// Adaptive-free Simpson quadrature of the exact 1-D boundary integral
1837    /// `∫_{−s}^{U} exp(−μu − ½hu²) du` on a truncation `U` chosen so the
1838    /// discarded tail is below 1e-18 of the mass.
1839    fn quadrature_log_relative_factor(mu: f64, h: f64, s: f64) -> f64 {
1840        let upper = ((-mu / h) + 12.0 / h.sqrt()).max(-s + 12.0 / h.sqrt());
1841        let lower = -s;
1842        let n = 40_000usize;
1843        let step = (upper - lower) / n as f64;
1844        let f = |u: f64| (-mu * u - 0.5 * h * u * u).exp();
1845        let mut acc = f(lower) + f(upper);
1846        for i in 1..n {
1847            let u = lower + step * i as f64;
1848            acc += if i % 2 == 1 { 4.0 } else { 2.0 } * f(u);
1849        }
1850        let integral = acc * step / 3.0;
1851        (integral / (2.0 * std::f64::consts::PI / h).sqrt()).ln()
1852    }
1853
1854    /// The closed form must match direct quadrature of the defining integral
1855    /// across active (s=0), interior (μ=0), and mixed regimes (gam#2306 §4).
1856    #[test]
1857    fn boundary_factor_matches_quadrature_across_regimes() {
1858        let cases: [(f64, f64, f64); 8] = [
1859            (0.0, 1.0, 0.0),  // activating row: exactly ln ½
1860            (0.0, 4.0, 0.0),  // curvature does not move the standardized value
1861            (2.5, 1.0, 0.0),  // active with a real multiplier
1862            (30.0, 9.0, 0.0), // deep linear-decay limit
1863            (0.0, 1.0, 0.7),  // interior near-boundary
1864            (0.0, 2.0, 4.0),  // interior far: → 0
1865            (1.5, 0.5, 2.0),  // mixed multiplier + slack
1866            (4.0, 2.0, 1.0),  // active-dominant mixed
1867        ];
1868        for &(mu, h, s) in &cases {
1869            let a = mu / h.sqrt();
1870            let b = s * h.sqrt();
1871            let closed = cone_boundary_log_factor(a, b);
1872            let quad = quadrature_log_relative_factor(mu, h, s);
1873            assert!(
1874                (closed - quad).abs() <= 1e-9 * (1.0 + quad.abs()),
1875                "(μ={mu}, h={h}, s={s}): closed {closed} vs quadrature {quad}"
1876            );
1877        }
1878        assert!(
1879            (cone_boundary_log_factor(0.0, 0.0) - 0.5_f64.ln()).abs() < 1e-15,
1880            "an activating row must contribute exactly the half-Gaussian ln ½"
1881        );
1882    }
1883
1884    /// The deep-active limit is the exact linear decay `corr → −ln(a·√(2π))`,
1885    /// and the deep-interior limit vanishes — the two continuity anchors that
1886    /// make the constrained criterion reduce to the unrestricted LAML away
1887    /// from the boundary.
1888    #[test]
1889    fn boundary_factor_limits_are_exact() {
1890        let a = 1.0e6;
1891        let expected = -(a * (2.0 * std::f64::consts::PI).sqrt()).ln();
1892        let got = cone_boundary_log_factor(a, 0.0);
1893        assert!(
1894            (got - expected).abs() <= 1e-9 * expected.abs(),
1895            "deep-active: got {got}, expected {expected}"
1896        );
1897        let interior = cone_boundary_log_factor(0.0, 40.0);
1898        assert!(
1899            interior.abs() < 1e-300 || interior > -1e-12,
1900            "deep-interior must vanish; got {interior}"
1901        );
1902    }
1903
1904    /// The deep-active GRADIENT has to survive as far as the deep-active VALUE
1905    /// does. `∂corr/∂a = a − λ(−a)` is the small residual left by two terms
1906    /// that both grow like `a`, so writing it as that subtraction loses
1907    /// `log₁₀(a²·ε)` digits: at `a = 1e6` it was already 4 digits down, at
1908    /// `a = 2e8` it came back POSITIVE, and past `a = 5e8` it was flat zero
1909    /// while the true value is `−2e-9`. The value alongside it was correct to
1910    /// 15 digits the whole way, which is what made the defect quiet.
1911    ///
1912    /// The reference here is the asymptotic series of the Mills correction,
1913    /// `λ(−a) = a + 1/a − 2/a³ + 10/a⁵ − 74/a⁷ + …` (so `∂corr/∂a = −1/a +
1914    /// 2/a³ − …`), which is the cheapest exact statement of the limit and is
1915    /// good to well past f64 from `a = 100` up. Finite differences cannot gate
1916    /// this: the quantity under test is smaller than any usable FD step's own
1917    /// truncation error.
1918    #[test]
1919    fn boundary_factor_active_gradient_holds_to_the_representable_limit() {
1920        let mut a = 100.0_f64;
1921        while a <= 1.0e14 {
1922            let (_, d_a, _) = cone_boundary_log_factor_and_derivatives(a, 0.0);
1923            let inv = 1.0 / a;
1924            let expected = -inv + 2.0 * inv.powi(3) - 10.0 * inv.powi(5) + 74.0 * inv.powi(7);
1925            assert!(
1926                d_a < 0.0,
1927                "corr is strictly decreasing in a at b=0, so ∂a must stay negative; \
1928                 got {d_a} at a={a}"
1929            );
1930            assert!(
1931                (d_a - expected).abs() <= 1.0e-13 * expected.abs(),
1932                "deep-active ∂a at a={a}: got {d_a}, expected {expected} \
1933                 (rel {:.3e})",
1934                (d_a - expected).abs() / expected.abs()
1935            );
1936            a *= 10.0;
1937        }
1938    }
1939
1940    /// `∂corr/∂a + ∂corr/∂b = a` identically, since the two partials are
1941    /// `a − λ(ξ)` and `λ(ξ)` for the same `ξ`. The two are now computed by
1942    /// different routes in the active branch — a continued fraction and an
1943    /// `erfcx` — so this is the gate that they still describe one function.
1944    #[test]
1945    fn boundary_factor_partials_sum_to_a() {
1946        for &a in &[0.0_f64, 0.5, 3.0, 4.0, 12.0, 1.0e3, 1.0e7, 1.0e12] {
1947            for &b in &[0.0_f64, 1.0e-3, 0.9, 5.0, 1.0e3] {
1948                let (_, d_a, d_b) = cone_boundary_log_factor_and_derivatives(a, b);
1949                assert!(
1950                    (d_a + d_b - a).abs() <= 1.0e-14 * a.max(d_b).max(1.0),
1951                    "(a={a}, b={b}): ∂a {d_a} + ∂b {d_b} = {} ≠ a",
1952                    d_a + d_b
1953                );
1954            }
1955        }
1956    }
1957
1958    /// The continued-fraction branch and the direct `a − λ` form must agree
1959    /// just inside the `ξ ≤ −4` switch, where the subtraction still has most of
1960    /// its digits. Without this, the branch could be precise and WRONG — the
1961    /// accuracy gate above pins a limit the continued fraction could hit while
1962    /// disagreeing with the function it is supposed to be differentiating.
1963    ///
1964    /// The band is set by the instrument being compared against, not by taste.
1965    /// `direct` is `a − λ` with `λ` from the `erfcx` route, whose measured
1966    /// relative accuracy is `~5e-14` (libm `erfc` plus the `exp(x²)` multiply);
1967    /// its absolute error is therefore `~5e-14·λ`, and the subtraction cannot
1968    /// remove it. Note how little room that leaves already: the amplification
1969    /// `λ/|a−λ|` is 18x at `a = 4` and 403x at `a = 20`, so at the top of this
1970    /// range the direct form is down to ~11 correct digits — five short — while
1971    /// the continued fraction still matches a 250-digit reference to 16. This
1972    /// test is deliberately capped at `a = 20` for that reason; it is the last
1973    /// place the two CAN be compared.
1974    #[test]
1975    fn boundary_factor_active_branch_agrees_with_the_direct_form_where_both_are_valid() {
1976        const LAMBDA_REL_ACCURACY: f64 = 5.0e-14;
1977        for &a in &[4.0_f64, 4.5, 6.0, 9.0, 20.0] {
1978            for &b in &[0.0_f64, 0.25, 1.5] {
1979                if b - a > LEFT_CONTINUED_FRACTION_SWITCH {
1980                    continue; // not on the continued-fraction branch
1981                }
1982                let (_, d_a, _) = cone_boundary_log_factor_and_derivatives(a, b);
1983                let (_, mills) = signed_probit_logcdf_and_mills_ratio(b - a);
1984                let direct = a - mills;
1985                assert!(
1986                    (d_a - direct).abs() <= LAMBDA_REL_ACCURACY * mills,
1987                    "(a={a}, b={b}): continued fraction {d_a} vs direct {direct} \
1988                     (gap {:.3e}, budget {:.3e})",
1989                    (d_a - direct).abs(),
1990                    LAMBDA_REL_ACCURACY * mills
1991                );
1992            }
1993        }
1994    }
1995
1996    /// A gradient off the domain must not read as usable next to a NaN value.
1997    #[test]
1998    fn boundary_factor_derivatives_are_nan_off_the_domain() {
1999        for &(a, b) in &[
2000            (-1.0_f64, 0.0_f64),
2001            (1.0, -1.0),
2002            (f64::NAN, 1.0),
2003            (f64::INFINITY, 0.0),
2004        ] {
2005            let (v, d_a, d_b) = cone_boundary_log_factor_and_derivatives(a, b);
2006            assert!(
2007                v.is_nan() && d_a.is_nan() && d_b.is_nan(),
2008                "(a={a}, b={b}) is off-domain: got value {v}, ∂a {d_a}, ∂b {d_b}"
2009            );
2010        }
2011    }
2012
2013    /// Closed-form partials against finite differences of the value
2014    /// (test-only FD; the production gradient consumes the analytic form).
2015    /// The domain is `a, b ≥ 0`, so a coordinate sitting exactly on the
2016    /// boundary uses a one-sided forward difference instead of stepping
2017    /// outside the domain (where the factor is deliberately NaN).
2018    #[test]
2019    fn boundary_factor_derivatives_match_finite_differences() {
2020        let cases: [(f64, f64); 5] = [(0.3, 0.0), (2.0, 0.5), (0.0, 1.2), (5.0, 0.2), (0.7, 3.0)];
2021        let step = 1e-6;
2022        let fd = |lo: f64, mid: f64, hi: f64, coord: f64| -> f64 {
2023            if coord >= step {
2024                (hi - lo) / (2.0 * step)
2025            } else {
2026                (hi - mid) / step
2027            }
2028        };
2029        for &(a, b) in &cases {
2030            let (_, d_a, d_b) = cone_boundary_log_factor_and_derivatives(a, b);
2031            let fd_a = fd(
2032                cone_boundary_log_factor((a - step).max(0.0), b),
2033                cone_boundary_log_factor(a, b),
2034                cone_boundary_log_factor(a + step, b),
2035                a,
2036            );
2037            let fd_b = fd(
2038                cone_boundary_log_factor(a, (b - step).max(0.0)),
2039                cone_boundary_log_factor(a, b),
2040                cone_boundary_log_factor(a, b + step),
2041                b,
2042            );
2043            // One-sided differences on boundary coordinates carry O(step)
2044            // truncation error, so the band is a few multiples of step.
2045            assert!(
2046                (d_a - fd_a).abs() <= 5e-6 * (1.0 + fd_a.abs()),
2047                "(a={a}, b={b}): ∂a analytic {d_a} vs FD {fd_a}"
2048            );
2049            assert!(
2050                (d_b - fd_b).abs() <= 5e-6 * (1.0 + fd_b.abs()),
2051                "(a={a}, b={b}): ∂b analytic {d_b} vs FD {fd_b}"
2052            );
2053        }
2054    }
2055}
2056
2057#[cfg(test)]
2058mod tests {
2059    use super::*;
2060
2061    const TOL: f64 = 1e-12;
2062
2063    fn rel_err(got: f64, expected: f64) -> f64 {
2064        (got - expected).abs() / expected.abs().max(1e-300)
2065    }
2066
2067    #[test]
2068    fn student_t_primitives_keep_the_tail_that_one_minus_the_cdf_destroys() {
2069        // References are correctly rounded doubles from a 60-dps regularized
2070        // incomplete beta. The `nu = 10000, t = 10` row is here because a
2071        // plausible-looking hand-extrapolated literal for it (1.60e-23) is 20%
2072        // from the truth: this table has to come from the reference, not from
2073        // pattern-matching the rows above it.
2074        const ROWS: [(f64, f64, f64); 10] = [
2075            (5.0, 20.0, 2.887758186612086e-6),
2076            (5.0, 40.0, 9.205981085886477e-8),
2077            (30.0, 10.0, 2.2876257041148065e-11),
2078            (30.0, 20.0, 3.3745418328856434e-19),
2079            (30.0, 40.0, 6.863022597203202e-28),
2080            (500.0, 8.0, 4.3648313969400955e-15),
2081            (500.0, 10.0, 6.930246799119958e-22),
2082            (500.0, 20.0, 4.056001518093838e-66),
2083            (500.0, 40.0, 3.14532145912912e-158),
2084            (10000.0, 10.0, 9.816403714331914e-24),
2085        ];
2086        // Bar: 1e-11, a measured envelope rather than a derivation. Everything
2087        // below the incomplete beta is derivable -- the identity is exact and
2088        // forms no difference -- but `beta_reg` is statrs's continued fraction
2089        // and its error is a property of that implementation, so the honest
2090        // thing is to measure it and say so. Worst over this table by shape
2091        // parameter `a = nu/2`:
2092        //
2093        //     a = 2.5     2e-15
2094        //     a = 15      1.6e-13
2095        //     a = 250     2.3e-13
2096        //     a = 5000    2.0e-12
2097        //
2098        // It grows slowly with nu, which is what a continued fraction needing
2099        // more terms looks like, and it does *not* grow with tail depth -- the
2100        // nu = 500 rows sit at 2e-13 whether the answer is 1e-15 or 1e-158.
2101        // That is the distinction that matters: a fixed relative cost, not a
2102        // cancellation. Bar is 5x the worst measured.
2103        let bar = 1.0e-11;
2104        for (nu, t, want) in ROWS {
2105            let got = student_t_sf(t, nu);
2106            let rel = ((got - want) / want).abs();
2107            assert!(
2108                rel <= bar,
2109                "student_t_sf({t}, {nu}) = {got:e}, want {want:e}, relative {rel:e} > {bar:e}"
2110            );
2111            let got_two_sided = student_t_two_sided_probability(t, nu);
2112            let two_sided_rel = ((got_two_sided - 2.0 * want) / (2.0 * want)).abs();
2113            assert!(
2114                two_sided_rel <= bar,
2115                "student_t_two_sided_probability({t}, {nu}) = {got_two_sided:e}, \
2116                 want {:e}, relative {two_sided_rel:e} > {bar:e}",
2117                2.0 * want
2118            );
2119            // The reflection. `1 - want` is O(1), so its own absolute error of
2120            // one ulp is a relative error of one ulp -- which is exactly why
2121            // reflecting is safe here and reconstructing the small tail is not.
2122            let lower = student_t_sf(-t, nu);
2123            assert!(
2124                (lower - (1.0 - want)).abs() <= 2.0 * f64::EPSILON,
2125                "student_t_sf({}, {nu}) = {lower}, want {}",
2126                -t,
2127                1.0 - want
2128            );
2129        }
2130        // Symmetry at the median, and the degenerate arguments.
2131        for nu in [1.0_f64, 5.0, 1e4] {
2132            assert!(
2133                (student_t_sf(0.0, nu) - 0.5).abs() <= f64::EPSILON,
2134                "median at nu = {nu}"
2135            );
2136        }
2137        assert!(student_t_sf(1.0, 0.0).is_nan(), "nu = 0 is not a t");
2138        assert!(
2139            student_t_sf(1.0, f64::INFINITY).is_nan(),
2140            "nu = inf is not a t"
2141        );
2142        assert_eq!(student_t_sf(f64::INFINITY, 5.0), 0.0, "tail beyond +inf");
2143        assert_eq!(
2144            student_t_sf(f64::NEG_INFINITY, 5.0),
2145            1.0,
2146            "tail beyond -inf"
2147        );
2148    }
2149
2150    #[test]
2151    fn normal_sf_keeps_the_upper_tail_that_one_minus_the_cdf_destroys() {
2152        // `Φ(x)` rounds to exactly 1.0 once its upper tail drops below half an
2153        // ulp of one, so `1 - normal_cdf(x)` returns exactly zero from x ~ 8.3 up
2154        // and is already 7% high at x = 8. `normal_sf` computes the tail rather
2155        // than reconstructing it. References are correctly rounded doubles from a
2156        // 60-dps `erfc(x/√2)/2`.
2157        //
2158        // Bar: `x * x * eps`, which is derived rather than chosen. Forming the
2159        // argument `u = x / √2` rounds it, a relative eps, i.e. an absolute
2160        // `u * eps`. The relative condition number of `erfc` at `u` is
2161        // `u * |erfc'(u)| / erfc(u)`, and since `erfc(u) ~ exp(-u^2) / (u√π)` for
2162        // large `u` that tends to `2u^2 = x^2`. So the returned tail inherits
2163        // `x^2 * eps` from the argument alone, before `erfc`'s own couple of ulp
2164        // -- 36 ulp at x = 6, 1370 ulp at x = 37. That is intrinsic to taking a
2165        // z score as the input: the tail is exponentially steep in `x`, so the
2166        // last bit of `x` is worth `x^2` bits of the tail. It is also
2167        // irrelevant next to what it replaces, which is a relative error of 1.
2168        const ROWS: [(f64, f64); 13] = [
2169            (0.5, 0.3085375387259869),
2170            (2.0, 0.02275013194817921),
2171            (4.0, 3.1671241833119924e-5),
2172            (5.0, 2.866515718791933e-7),
2173            (6.0, 9.86587645037698e-10),
2174            (7.0, 1.279812543885835e-12),
2175            (8.0, 6.220960574271784e-16),
2176            (8.3, 5.205569744890254e-17),
2177            (9.0, 1.1285884059538405e-19),
2178            (12.0, 1.776482112077679e-33),
2179            (20.0, 2.7536241186062337e-89),
2180            (30.0, 4.906713927148187e-198),
2181            (37.0, 5.725571222524577e-300),
2182        ];
2183        for (x, want) in ROWS {
2184            let bar = (x * x + 2.0) * f64::EPSILON;
2185            let got = normal_sf(x);
2186            let rel = ((got - want) / want).abs();
2187            assert!(
2188                rel <= bar,
2189                "normal_sf({x}) = {got:e}, want {want:e}, relative {rel:e} > {bar:e}"
2190            );
2191            let got_two_sided = normal_two_sided_probability(x);
2192            let two_sided_rel = ((got_two_sided - 2.0 * want) / (2.0 * want)).abs();
2193            assert!(
2194                two_sided_rel <= bar,
2195                "normal_two_sided_probability({x}) = {got_two_sided:e}, \
2196                 want {:e}, relative {two_sided_rel:e} > {bar:e}",
2197                2.0 * want
2198            );
2199            // The value this replaces. Above the saturation point it is not a
2200            // less accurate answer, it is no answer.
2201            if x >= 8.3 {
2202                assert_eq!(
2203                    1.0 - normal_cdf(x),
2204                    0.0,
2205                    "1 - normal_cdf({x}) is expected to have saturated"
2206                );
2207            }
2208        }
2209        // Complementarity holds wherever the sum is representable, and the
2210        // symmetry that makes a two-sided p-value a single call.
2211        for x in [-3.0_f64, -0.25, 0.0, 0.25, 3.0] {
2212            let sum = normal_sf(x) + normal_cdf(x);
2213            assert!((sum - 1.0).abs() <= 2.0 * f64::EPSILON, "sf + cdf = {sum}");
2214            assert_eq!(normal_sf(-x), normal_cdf(x), "sf(-x) != cdf(x) at {x}");
2215        }
2216    }
2217
2218    /// The final representable normal two-sided tail is subnormal. This is a
2219    /// separate absolute/ULP assertion because a conventional relative-error
2220    /// helper with a normal-number floor would make the edge vacuous.
2221    #[test]
2222    fn normal_two_sided_tail_retains_subnormal_edge() {
2223        const EXPECTED_AT_38: f64 = 5.770_856_702_007_929e-316;
2224        let got = normal_two_sided_probability(38.0);
2225        let ulps = got.to_bits().abs_diff(EXPECTED_AT_38.to_bits());
2226        assert!(
2227            got.is_subnormal() && ulps <= 128,
2228            "two-sided normal tail at z=38: got {got:.17e}, \
2229             expected {EXPECTED_AT_38:.17e}, ulps {ulps}"
2230        );
2231        assert_eq!(normal_two_sided_probability(40.0), 0.0);
2232        assert_eq!(normal_two_sided_probability(f64::INFINITY), 0.0);
2233        assert!(normal_two_sided_probability(f64::NAN).is_nan());
2234    }
2235
2236    /// `t²` and then `ν/(ν+t²)` both underflow at this edge, but the Cauchy
2237    /// tail itself is still representable. The analytic Cauchy survival law is
2238    /// an independent oracle for the log-beta implementation.
2239    #[test]
2240    fn student_t_two_sided_tail_retains_subnormal_cauchy_edge() {
2241        const EXPECTED: f64 = 3.541_315_033_259_774_5e-309;
2242        let got = student_t_two_sided_probability(f64::MAX, 1.0);
2243        let analytic = 2.0 * (1.0 / f64::MAX).atan() / std::f64::consts::PI;
2244        let pinned_ulps = got.to_bits().abs_diff(EXPECTED.to_bits());
2245        let analytic_ulps = got.to_bits().abs_diff(analytic.to_bits());
2246        assert!(
2247            got.is_subnormal() && pinned_ulps <= 512 && analytic_ulps <= 512,
2248            "Cauchy tail at f64::MAX: got {got:.17e}, pinned {EXPECTED:.17e}, \
2249             analytic {analytic:.17e}, pinned ulps {pinned_ulps}, \
2250             analytic ulps {analytic_ulps}"
2251        );
2252    }
2253
2254    #[test]
2255    fn distribution_survival_primitives_define_boundaries_and_identities() {
2256        assert_eq!(normal_sf(f64::INFINITY), 0.0);
2257        assert_eq!(normal_sf(f64::NEG_INFINITY), 1.0);
2258        assert!(normal_sf(f64::NAN).is_nan());
2259
2260        assert_eq!(student_t_two_sided_probability(0.0, 7.0), 1.0);
2261        assert_eq!(student_t_sf(0.0, 7.0), 0.5);
2262        assert!(student_t_sf(f64::NAN, 7.0).is_nan());
2263
2264        assert_eq!(chi_square_sf(0.0, 3.0), 1.0);
2265        assert_eq!(chi_square_sf(f64::INFINITY, 3.0), 0.0);
2266        assert!(chi_square_sf(-1.0, 3.0).is_nan());
2267        assert!(chi_square_sf(1.0, 0.0).is_nan());
2268
2269        assert_eq!(fisher_snedecor_sf(0.0, 3.0, 20.0), 1.0);
2270        assert_eq!(
2271            fisher_snedecor_sf(f64::INFINITY, 3.0, 20.0),
2272            0.0
2273        );
2274        assert!(fisher_snedecor_sf(-1.0, 3.0, 20.0).is_nan());
2275        assert!(fisher_snedecor_sf(1.0, 0.0, 20.0).is_nan());
2276        assert!(fisher_snedecor_sf(1.0, 3.0, 0.0).is_nan());
2277
2278        // χ²₁ is the square of a standard normal; F₁,₁ is the square of a
2279        // Cauchy. These identities independently anchor both direct survival
2280        // implementations in a small-tail regime.
2281        let statistic = 160.0_f64;
2282        let chi_expected = normal_two_sided_probability(statistic.sqrt());
2283        let chi_got = chi_square_sf(statistic, 1.0);
2284        assert!(rel_err(chi_got, chi_expected) <= 2.0e-13);
2285
2286        let f_expected = student_t_two_sided_probability(statistic.sqrt(), 1.0);
2287        let f_got = fisher_snedecor_sf(statistic, 1.0, 1.0);
2288        assert!(rel_err(f_got, f_expected) <= 2.0e-13);
2289    }
2290
2291    #[test]
2292    /// The lower tail of a beta quantile, where `inv_beta_reg`'s absolute
2293    /// convergence tolerance in `x` used to stall (#2528).
2294    ///
2295    /// Shapes are the ones `gam_inference::probability` derives from a mean and
2296    /// a variance (`precision = mu(1-mu)/total_var - 1`), so every row is the
2297    /// lower endpoint of a 95% predictive interval a caller can actually ask
2298    /// for. References are an 80-digit bisection in `ln x` on
2299    /// `I_x(a,b) = p`; the `Beta(0.1, 0.1)` row is additionally checkable in
2300    /// closed form, since `I_x -> x^a/(a B(a,b))` gives
2301    /// `x = (p a B(a,b))^(1/a)` there.
2302    ///
2303    /// What shipped before, against the same references: `6.7e-18` for the
2304    /// first row (true `1.5e-41`, relative error 4.6e+23), `5.8e-18` for the
2305    /// second (true `6.3e-161`), and `9.6e-19` for the underflow row, whose
2306    /// true quantile is `7.7e-688` and whose only correct `f64` answer is `0`.
2307    /// The failure was not a loss of digits but a floor: every one of those
2308    /// returns is the solver's own resolution limit rather than a quantile.
2309    fn beta_quantile_resolves_the_lower_tail_below_the_solver_floor() {
2310        const CASES: [(f64, f64, f64, f64); 8] = [
2311            (0.04, 3.96, 0.025, 1.4749755854885786e-41),
2312            (0.01, 0.99, 0.025, 6.326229749489128e-161),
2313            (
2314                0.046666666666666666,
2315                2.2866666666666666,
2316                0.025,
2317                1.488779171021457e-35,
2318            ),
2319            (0.05, 0.95, 0.025, 9.875267916846768e-33),
2320            (0.1, 0.9, 0.025, 1.12479965068234e-16),
2321            (0.3, 0.7, 0.025, 7.6005358168401896e-6),
2322            (0.5, 0.5, 0.025, 1.5413331334360133e-3),
2323            (0.1, 0.1, 1.0e-4, 8.869280655550463e-38),
2324        ];
2325        let mut worst = 0.0_f64;
2326        for (a, b, p, want) in CASES {
2327            let got = beta_quantile(p, a, b);
2328            let relative = ((got - want) / want).abs();
2329            assert!(
2330                relative <= 16.0 * f64::EPSILON,
2331                "beta_quantile({p}, {a}, {b}) = {got:e}, want {want:e}, relative {relative:e}"
2332            );
2333            worst = worst.max(relative);
2334        }
2335        println!("worst relative error over the lower-tail table: {worst:e}");
2336
2337        // The true quantile here is 7.7e-688. It is not representable, so the
2338        // correctly rounded answer is zero, and a caller reading a positive
2339        // lower bound could not tell that it had underflowed.
2340        let underflowed = beta_quantile(0.025, 0.0023333333333333335, 2.3310000000000004);
2341        assert!(
2342            underflowed == 0.0,
2343            "a quantile below MIN_POSITIVE must round to zero, got {underflowed:e}"
2344        );
2345
2346        // The upper tail of the same shape is not on the series branch and is
2347        // still `inv_beta_reg`'s answer, at `inv_beta_reg`'s own accuracy. It is
2348        // asserted here so that widening the branch cannot silently move it.
2349        const UPPER: f64 = 0.12274676682071068;
2350        let upper = beta_quantile(0.975, 0.04, 3.96);
2351        assert!(
2352            ((upper - UPPER) / UPPER).abs() <= 1.0e-11,
2353            "upper tail moved: {upper:e}, want {UPPER:e}"
2354        );
2355    }
2356
2357    #[test]
2358    fn beta_quantile_matches_known_reference_values() {
2359        let cases: [(f64, f64, f64, f64); 8] = [
2360            (0.025, 2.0, 2.0, 0.094_299_3),
2361            (0.975, 2.0, 2.0, 0.905_700_7),
2362            (0.5, 2.0, 2.0, 0.5),
2363            (0.025, 0.8, 4.0, 0.002_339_1),
2364            (0.975, 0.8, 4.0, 0.564_717_3),
2365            (0.025, 5.0, 1.5, 0.408_549_1),
2366            (0.5, 20.0, 80.0, 0.197_994_8),
2367            (0.975, 20.0, 80.0, 0.283_367_6),
2368        ];
2369        for (p, a, b, expected) in cases {
2370            let got = beta_quantile(p, a, b);
2371            let abs = (got - expected).abs();
2372            assert!(
2373                abs < 1e-5,
2374                "beta_quantile(p={p}, a={a}, b={b}) = {got}, expected ≈ {expected} (abs err {abs})"
2375            );
2376        }
2377    }
2378
2379    #[test]
2380    fn beta_quantile_boundaries_and_degeneracy() {
2381        assert_eq!(beta_quantile(0.0, 2.0, 3.0), 0.0);
2382        assert_eq!(beta_quantile(-0.5, 2.0, 3.0), 0.0);
2383        assert_eq!(beta_quantile(1.0, 2.0, 3.0), 1.0);
2384        assert_eq!(beta_quantile(1.5, 2.0, 3.0), 1.0);
2385        assert!(beta_quantile(0.5, -1.0, 3.0).is_nan());
2386        assert!(beta_quantile(0.5, 2.0, 0.0).is_nan());
2387        assert!(beta_quantile(0.5, f64::NAN, 3.0).is_nan());
2388        let mut prev = 0.0;
2389        for i in 1..100 {
2390            let p = i as f64 / 100.0;
2391            let q = beta_quantile(p, 3.0, 5.0);
2392            assert!(q > prev, "beta quantile not increasing at p={p}");
2393            prev = q;
2394        }
2395    }
2396
2397    // ── normal_pdf ────────────────────────────────────────────────────────────
2398
2399    #[test]
2400    fn normal_pdf_at_zero() {
2401        let expected = 1.0 / (2.0 * std::f64::consts::PI).sqrt();
2402        assert!((normal_pdf(0.0) - expected).abs() < TOL);
2403    }
2404
2405    #[test]
2406    fn normal_pdf_symmetry() {
2407        for &x in &[0.5, 1.0, 2.0, 3.0, 5.0] {
2408            assert_eq!(normal_pdf(x), normal_pdf(-x), "symmetry failed at x={x}");
2409        }
2410    }
2411
2412    /// `x*x` is exact-splittable and the split is what `exp` needs.
2413    ///
2414    /// Two independent statements, because the correction is only worth what
2415    /// its residual is worth. First, `x*x + residual` is `x²` EXACTLY: checked
2416    /// against a Veltkamp/Dekker split, which reaches the same residual through
2417    /// pure multiplies and adds and shares no code path with the `mul_add`
2418    /// route. Second, the residual is not decorative — for these arguments it
2419    /// is a relative perturbation of `x²` big enough that `exp` amplifies it
2420    /// past a single ulp of the result.
2421    #[test]
2422    fn square_residual_completes_the_rounded_square_exactly() {
2423        // 2^27 + 1: Veltkamp's splitting factor, exact for any `x` whose
2424        // scaled form does not overflow.
2425        const SPLIT: f64 = 134_217_729.0;
2426        let mut saw_amplified = false;
2427        for &x in &[
2428            0.1, 0.7, 1.3, 2.9, 6.1, 10.5, 14.3, 19.7, 23.9, 25.9999, 34.7,
2429        ] {
2430            let rounded = x * x;
2431            let residual = square_residual(x, rounded);
2432
2433            let c = x * SPLIT;
2434            let head = c - (c - x);
2435            let tail = x - head;
2436            let dekker = ((head * head - rounded) + 2.0 * head * tail) + tail * tail;
2437            assert_eq!(
2438                residual, dekker,
2439                "x={x}: mul_add residual {residual:e} != Dekker residual {dekker:e}"
2440            );
2441
2442            // `exp` multiplies a relative argument perturbation by the argument.
2443            let amplified = (residual / rounded).abs() * rounded;
2444            if amplified > f64::EPSILON {
2445                saw_amplified = true;
2446            }
2447        }
2448        assert!(
2449            saw_amplified,
2450            "no test argument had a residual `exp` could amplify past one ulp; \
2451             the correction under test would be untested"
2452        );
2453    }
2454
2455    /// `φ(x)` against an EXTERNAL high-precision reference (mpmath, dps=60).
2456    ///
2457    /// Every argument here has an INEXACT square, which is the whole point.
2458    /// `exp(−½·fl(x*x))` misplaces the argument by `x²·ε/2` RELATIVE, and `exp`
2459    /// hands that straight back as relative error in the result: `1.4e-14` at
2460    /// `x ≈ 17`, `5.7e-14` by `x ≈ 35`, where `φ` is still a normal `f64`. Only
2461    /// the top of the range makes that visible, so the table has to reach it —
2462    /// a `φ` table that stops at `x = 5` cannot tell the two forms apart.
2463    ///
2464    /// `1.5e-15` (≈7 ulp) is the portability allowance: `f64::exp` is the
2465    /// platform libm and the only part of this that is not fixed by the crate
2466    /// graph, and it is worth ~1 ulp on the implementations in use. That still
2467    /// leaves 38x of margin against the defect at the top of the table.
2468    #[test]
2469    fn normal_pdf_matches_high_precision_reference() {
2470        const TOLERANCE: f64 = 1.5e-15;
2471        let refs: &[(f64, f64)] = &[
2472            (0.5, 0.35206532676429947),
2473            (1.0, 0.24197072451914334),
2474            (2.5, 0.017528300493568537),
2475            (4.0, 0.00013383022576488534),
2476            (7.3, 1.0693837871541648e-12),
2477            (11.9, 7.090702668428078e-32),
2478            (17.4, 7.201308152719057e-67),
2479            (23.6, 4.555989824112156e-122),
2480            (29.1, 5.229437243665329e-185),
2481            (34.7, 1.368008224488383e-262),
2482        ];
2483        for &(x, reference) in refs {
2484            // The small arguments anchor the ordinary range; the large ones are
2485            // where the defect lives, and every one of THOSE has to have a
2486            // square `f64` cannot hold or it exercises nothing.
2487            assert!(
2488                x <= 5.0 || square_residual(x, x * x) != 0.0,
2489                "x={x} squares exactly, so it cannot exercise the correction"
2490            );
2491            let rel = rel_err(normal_pdf(x), reference);
2492            assert!(
2493                rel < TOLERANCE,
2494                "normal_pdf({x}) = {:.17e}, reference {reference:.17e}, rel {rel:.3e}",
2495                normal_pdf(x)
2496            );
2497        }
2498    }
2499
2500    /// `φ` off the ordinary domain, where the square has no usable residual:
2501    /// `±∞` squares to `∞` and would hand the correction an `∞ − ∞`.
2502    #[test]
2503    fn normal_pdf_nonfinite_and_underflowed_arguments() {
2504        assert_eq!(normal_pdf(f64::INFINITY), 0.0);
2505        assert_eq!(normal_pdf(f64::NEG_INFINITY), 0.0);
2506        assert!(normal_pdf(f64::NAN).is_nan());
2507        // Past ~38.6 the pdf underflows; it must reach zero, not NaN.
2508        assert_eq!(normal_pdf(40.0), 0.0);
2509        assert_eq!(normal_pdf(-40.0), 0.0);
2510        assert_eq!(normal_pdf(f64::MAX), 0.0);
2511        // Just inside the underflow edge the result is subnormal but positive.
2512        let edge = normal_pdf(38.0);
2513        assert!(edge > 0.0 && edge.is_subnormal(), "phi(38) = {edge:e}");
2514    }
2515
2516    #[test]
2517    fn normal_pdf_positive() {
2518        for &x in &[-5.0, -1.0, 0.0, 1.0, 5.0] {
2519            assert!(normal_pdf(x) > 0.0, "pdf should be positive at x={x}");
2520        }
2521    }
2522
2523    // ── normal_cdf ────────────────────────────────────────────────────────────
2524
2525    #[test]
2526    fn normal_cdf_at_zero_is_half() {
2527        assert!((normal_cdf(0.0) - 0.5).abs() < TOL);
2528    }
2529
2530    #[test]
2531    fn normal_cdf_symmetry() {
2532        for &x in &[0.5, 1.0, 2.0, 3.0] {
2533            let sum = normal_cdf(x) + normal_cdf(-x);
2534            assert!(
2535                (sum - 1.0).abs() < TOL,
2536                "cdf symmetry failed at x={x}: sum={sum}"
2537            );
2538        }
2539    }
2540
2541    #[test]
2542    fn normal_cdf_bounds() {
2543        assert!(normal_cdf(10.0) > 0.9999);
2544        assert!(normal_cdf(-10.0) < 1e-22);
2545        assert!(normal_cdf(0.0) > 0.0);
2546        assert!(normal_cdf(0.0) < 1.0);
2547    }
2548
2549    #[test]
2550    fn normal_cdf_at_1_96_near_0975() {
2551        // Phi(1.96) ≈ 0.975 — canonical two-sided 5% critical value.
2552        let p = normal_cdf(1.959_963_985);
2553        assert!((p - 0.975).abs() < 1e-8, "p={p}");
2554    }
2555
2556    // ── erfcx_nonnegative ─────────────────────────────────────────────────────
2557
2558    #[test]
2559    fn erfcx_zero_is_one_and_negative_domain_is_rejected() {
2560        assert_eq!(erfcx_nonnegative(0.0), 1.0);
2561        assert!(erfcx_nonnegative(-f64::MIN_POSITIVE).is_nan());
2562        assert!(erfcx_nonnegative(-1.0).is_nan());
2563        assert!(erfcx_nonnegative(f64::NEG_INFINITY).is_nan());
2564    }
2565
2566    #[test]
2567    fn erfcx_positive_inf_returns_zero() {
2568        assert_eq!(erfcx_nonnegative(f64::INFINITY), 0.0);
2569    }
2570
2571    #[test]
2572    fn erfcx_nan_propagates() {
2573        assert!(erfcx_nonnegative(f64::NAN).is_nan());
2574    }
2575
2576    #[test]
2577    fn erfcx_small_positive_matches_direct() {
2578        use libm::erfc;
2579        for &x in &[0.1_f64, 0.5, 1.0, 5.0, 10.0, 25.0] {
2580            let got = erfcx_nonnegative(x);
2581            let expected = (x * x).exp() * erfc(x);
2582            let err = rel_err(got, expected);
2583            assert!(
2584                err < 1e-10,
2585                "x={x}: got={got} expected={expected} rel={err}"
2586            );
2587        }
2588    }
2589
2590    #[test]
2591    fn erfcx_large_x_positive_and_finite() {
2592        // For x >= 26 the asymptotic branch must remain positive and finite.
2593        let got = erfcx_nonnegative(50.0);
2594        assert!(got.is_finite() && got > 0.0, "erfcx(50)={got}");
2595        // Leading asymptotic term: 1/(x*sqrt(pi)).
2596        let asymptotic = 1.0 / (50.0 * std::f64::consts::PI.sqrt());
2597        assert!(
2598            rel_err(got, asymptotic) < 1e-3,
2599            "got={got} asymptotic={asymptotic}"
2600        );
2601    }
2602
2603    /// The two branches must describe one function across `x = 26`.
2604    ///
2605    /// Note WHY the plain `exp(x*x)·erfc(x)` below is a legitimate oracle at
2606    /// this particular argument and nowhere else: `26² = 676` is exactly
2607    /// representable, so the rounded square carries no residual and the direct
2608    /// form is momentarily as good as the corrected one. That is also exactly
2609    /// why this check was blind to the `x²·ε/2` defect it looks like it should
2610    /// have caught — at `25.9` the same comparison would have failed by
2611    /// `5.7e-14`, but the seam was only ever probed at the one point in the
2612    /// neighbourhood where the defect vanishes. The bit-adjacent step below
2613    /// cannot substitute for it either: `d(ln erfcx)/dx ≈ −2x` at the switch,
2614    /// so one ulp of `x` moves the true value by `1.8e-13`, three times the
2615    /// defect. It takes a reference at a DISTANCE from the seam — the table in
2616    /// `erfcx_matches_high_precision_reference` — to see the defect at all.
2617    #[test]
2618    fn erfcx_asymptotic_switch_matches_finite_direct_identity() {
2619        let switch = 26.0_f64;
2620        assert_eq!(
2621            square_residual(switch, switch * switch),
2622            0.0,
2623            "676 must be exact for the direct form below to be an oracle"
2624        );
2625        let direct = (switch * switch).exp() * erfc(switch);
2626        let asymptotic = erfcx_nonnegative(switch);
2627        assert!(
2628            rel_err(asymptotic, direct) < 1.0e-15,
2629            "switch mismatch: asymptotic={asymptotic:.17e}, direct={direct:.17e}"
2630        );
2631
2632        // Continuity across the branch cut, up to how fast the function itself
2633        // moves over one ulp of `x` (`|d ln erfcx/dx| ≈ 2x` ⇒ ~1.9e-13 here).
2634        let immediately_below = f64::from_bits(switch.to_bits() - 1);
2635        let below = erfcx_nonnegative(immediately_below);
2636        let step = 2.0 * switch * (switch - immediately_below);
2637        assert!(
2638            rel_err(asymptotic, below) < 2.0 * step,
2639            "discontinuous switch: below={below:.17e}, at={asymptotic:.17e}, \
2640             one-ulp travel {step:.3e}"
2641        );
2642    }
2643
2644    #[test]
2645    fn erfcx_preserves_representable_subnormal_tail() {
2646        let tail = erfcx_nonnegative(f64::MAX);
2647        assert!(tail > 0.0 && tail.is_subnormal(), "erfcx(MAX)={tail:e}");
2648    }
2649
2650    /// Absolute-accuracy pin against an EXTERNAL high-precision reference
2651    /// (mpmath, dps=60) spanning the direct branch `[0.1, 26)`. This is the
2652    /// root-cause guard: the previous `exp(x²)·erfc(x)` direct form was built on
2653    /// `statrs::erfc`, whose ~1e-10 relative accuracy silently poisoned every
2654    /// downstream probit / Mills / log-CDF derivative.
2655    ///
2656    /// The table had a SECOND job it was not doing. Of its twelve arguments,
2657    /// eleven — `0.5`, `2`, `3.5`, `6`, `9`, `13`, `18`, `22`, `25.5`, and the
2658    /// two whose squares are far too small to matter — square EXACTLY in `f64`,
2659    /// so `fl(x*x) = x²` and the `x²·ε/2` error the rounded square feeds `exp`
2660    /// was identically zero at every one of them. The twelfth, `25.9999`, does
2661    /// not square exactly; it was the one point in the table where the defect
2662    /// was live, and its literal had been recorded WITH the defect in it —
2663    /// `0.021683668126370212` against a true `0.021683668126369115`, off by
2664    /// `5.1e-14`. Three independent high-precision routes (`exp(x²)·erfc(x)`,
2665    /// the 12-term asymptotic series, and a 400-level Laplace continued
2666    /// fraction) and `scipy.special.erfcx` all agree on the corrected value.
2667    /// A `1e-13` tolerance then accepted a reference that was itself wrong by
2668    /// half the tolerance, which is how a 190x accuracy defect sat under a
2669    /// test named for high precision.
2670    ///
2671    /// So the table now RUNS ON arguments with inexact squares (`10.5`,
2672    /// `14.3`, `19.7`, `23.9` alongside the original grid) and the tolerance is
2673    /// `1.5e-15` — 38x below the defect at the top of the range, and still ~7
2674    /// ulp of headroom for the platform `f64::exp` (the only part of this path
2675    /// not pinned by the crate graph; `erfc` comes from the `libm` crate and is
2676    /// identical everywhere).
2677    #[test]
2678    fn erfcx_matches_high_precision_reference() {
2679        const TOLERANCE: f64 = 1.5e-15;
2680        // (x, mpmath exp(x²)·erfc(x) at dps=60, rounded to f64).
2681        let refs: &[(f64, f64)] = &[
2682            (0.1, 0.8964569799691267),
2683            (0.5, 0.6156903441929259),
2684            (1.0, 0.427583576155807),
2685            (2.0, 0.25539567631050575),
2686            (3.5, 0.1552936556088943),
2687            (6.0, 0.09277656780053835),
2688            (9.0, 0.06230772403777468),
2689            (10.5, 0.05349189974656412),
2690            (13.0, 0.043271921864609694),
2691            (14.3, 0.0393580473372741),
2692            (18.0, 0.03129571781590521),
2693            (19.7, 0.028602309402825203),
2694            (22.0, 0.025618570005879453),
2695            (23.9, 0.023585649371803793),
2696            (25.5, 0.022108108052519827),
2697            (25.9999, 0.021683668126369115),
2698        ];
2699        for &(x, reference) in refs {
2700            let got = erfcx_nonnegative(x);
2701            let rel = rel_err(got, reference);
2702            assert!(
2703                rel < TOLERANCE,
2704                "erfcx({x}) = {got:.17e}, reference {reference:.17e}, rel {rel:.3e}"
2705            );
2706        }
2707        // The point of the added arguments: at least four of them must have a
2708        // square `f64` cannot hold, or the table is back to testing nothing.
2709        let inexact = refs
2710            .iter()
2711            .filter(|&&(x, _)| square_residual(x, x * x) != 0.0)
2712            .count();
2713        assert!(
2714            inexact >= 4,
2715            "only {inexact} of {} reference arguments have an inexact square",
2716            refs.len()
2717        );
2718    }
2719
2720    // ── log1mexp_positive ─────────────────────────────────────────────────────
2721
2722    #[test]
2723    fn log1mexp_at_zero_is_neg_inf() {
2724        assert_eq!(log1mexp_positive(0.0), f64::NEG_INFINITY);
2725    }
2726
2727    #[test]
2728    fn log1mexp_recovers_log_one_minus_exp() {
2729        // Verify exp(log1mexp(a)) + exp(-a) ≈ 1 for several a > 0. This
2730        // roundtrip avoids computing `(1 - exp(-a)).ln()` directly, which
2731        // suffers catastrophic cancellation for large a (e.g. a=20 where
2732        // `1.0 - exp(-20)` loses 9 decimal digits from the subtraction).
2733        for &a in &[0.001_f64, 0.5, std::f64::consts::LN_2, 1.0, 5.0, 20.0] {
2734            let lm = log1mexp_positive(a);
2735            let roundtrip = lm.exp() + (-a).exp();
2736            assert!(
2737                (roundtrip - 1.0).abs() < 1e-14,
2738                "a={a}: exp(log1mexp(a)) + exp(-a) = {roundtrip}, expected 1.0"
2739            );
2740        }
2741    }
2742
2743    #[test]
2744    fn log1mexp_at_ln2_is_neg_ln2() {
2745        let ln2 = std::f64::consts::LN_2;
2746        let got = log1mexp_positive(ln2);
2747        assert!((got - (-ln2)).abs() < TOL, "got={got}");
2748    }
2749
2750    // ── signed_log_sum_exp ────────────────────────────────────────────────────
2751
2752    #[test]
2753    fn slse_all_positive_single() {
2754        let (lm, sg) = signed_log_sum_exp(&[2.0], &[1.0]);
2755        assert!((lm - 2.0).abs() < TOL);
2756        assert!((sg - 1.0).abs() < TOL);
2757    }
2758
2759    #[test]
2760    fn slse_difference_recovers_log2() {
2761        // 3 - 1 = 2 → log|2| = ln(2), sign = +1.
2762        let log3 = 3.0_f64.ln();
2763        let log1 = 0.0_f64; // ln(1)
2764        let (lm, sg) = signed_log_sum_exp(&[log3, log1], &[1.0, -1.0]);
2765        assert!((lm - 2.0_f64.ln()).abs() < TOL, "lm={lm}");
2766        assert!((sg - 1.0).abs() < TOL, "sg={sg}");
2767    }
2768
2769    #[test]
2770    fn slse_cancellation_gives_neg_inf() {
2771        // a - a = 0 → log|0| = -∞.
2772        let ln2 = 2.0_f64.ln();
2773        let (lm, sg) = signed_log_sum_exp(&[ln2, ln2], &[1.0, -1.0]);
2774        assert_eq!(lm, f64::NEG_INFINITY);
2775        assert_eq!(sg, 0.0);
2776    }
2777
2778    #[test]
2779    fn slse_compensated_signed_reduction_preserves_conditioned_residual() {
2780        // High-precision truth for these exact f64 log inputs is
2781        // -7.141194316117315021451...e-13. Reducing the positive and negative
2782        // groups through separate logarithms first returned
2783        // -7.141196119493781e-13: two otherwise harmless log roundings were
2784        // amplified by the nearly cancelling subtraction.
2785        let log_magnitudes = [
2786            -8.752777116220523,
2787            -8.741767521635955,
2788            -8.77021076826994,
2789            -8.75153786858979,
2790            -8.754172660745834,
2791            -8.768217028174623,
2792            -8.756625396724502,
2793            -8.737312647396818,
2794        ];
2795        let signs = [1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0];
2796        let (log_magnitude, sign) = signed_log_sum_exp(&log_magnitudes, &signs);
2797        let got = sign * log_magnitude.exp();
2798        let truth = -7.141194316117315e-13;
2799        let legacy = -7.141196119493781e-13;
2800        assert_eq!(sign, -1.0);
2801        assert!(
2802            (got - truth).abs() < (legacy - truth).abs(),
2803            "compensated signed reduction did not improve the conditioned residual: \
2804             got={got:.17e}, truth={truth:.17e}, legacy={legacy:.17e}"
2805        );
2806    }
2807
2808    #[test]
2809    fn slse_log_domain_branch_retains_sub_ulp_two_term_gap() {
2810        // exp(-gap) rounds to 1.0 at this gap, so a purely linear-domain signed
2811        // reduction sees 1 - 1. The forward-error gate must route to the
2812        // log-domain difference, where the distinct input logs retain the gap.
2813        let gap = f64::EPSILON * 0.25;
2814        let (log_magnitude, sign) = signed_log_sum_exp(&[0.0, -gap], &[1.0, -1.0]);
2815        assert_eq!(sign, 1.0);
2816        assert_eq!(log_magnitude, log1mexp_positive(gap));
2817    }
2818
2819    #[test]
2820    fn exact_binary64_sum_sign_resolves_midpoint_and_both_adjacent_sides() {
2821        let half_upper_ulp_at_one = 2.0_f64.powi(-53);
2822        let least_subnormal = f64::from_bits(1);
2823        assert_eq!(
2824            exact_binary64_sum_sign([
2825                1.0,
2826                half_upper_ulp_at_one,
2827                -1.0,
2828                -half_upper_ulp_at_one,
2829            ]),
2830            Ok(std::cmp::Ordering::Equal),
2831            "an exact rounding midpoint must compare equal"
2832        );
2833        assert_eq!(
2834            exact_binary64_sum_sign([
2835                1.0,
2836                half_upper_ulp_at_one,
2837                least_subnormal,
2838                -1.0,
2839                -half_upper_ulp_at_one,
2840            ]),
2841            Ok(std::cmp::Ordering::Greater),
2842            "one binary lattice quantum above the midpoint must compare positive"
2843        );
2844        assert_eq!(
2845            exact_binary64_sum_sign([
2846                1.0,
2847                half_upper_ulp_at_one,
2848                -least_subnormal,
2849                -1.0,
2850                -half_upper_ulp_at_one,
2851            ]),
2852            Ok(std::cmp::Ordering::Less),
2853            "one binary lattice quantum below the midpoint must compare negative"
2854        );
2855    }
2856
2857    #[test]
2858    fn exact_binary64_sum_sign_enforces_its_finite_structural_contract() {
2859        assert_eq!(
2860            exact_binary64_sum_sign([f64::MAX, -f64::MAX, f64::from_bits(1)]),
2861            Ok(std::cmp::Ordering::Greater),
2862        );
2863        assert_eq!(
2864            exact_binary64_sum_sign([0.0, f64::NAN]),
2865            Err(ExactBinary64SumSignError::NonFiniteTerm { index: 1 }),
2866        );
2867        assert_eq!(
2868            exact_binary64_sum_sign(
2869                std::iter::repeat_n(1.0, EXACT_BINARY64_SUM_MAX_TERMS + 1)
2870            ),
2871            Err(ExactBinary64SumSignError::TermCapacityExceeded {
2872                maximum: EXACT_BINARY64_SUM_MAX_TERMS,
2873            }),
2874        );
2875    }
2876
2877    #[test]
2878    fn slse_empty_returns_neg_inf_with_zero_sign() {
2879        // With no terms the sum is exactly 0, so the docstring contract is
2880        // `(−∞, 0.0)`. (This test previously encoded the buggy `+1.0` positive-sum
2881        // convention, which contradicted both the docstring and the cancellation
2882        // test below; rewritten to the correct zero sign.)
2883        let (lm, sg) = signed_log_sum_exp(&[], &[]);
2884        assert_eq!(lm, f64::NEG_INFINITY);
2885        assert_eq!(sg, 0.0);
2886    }
2887
2888    #[test]
2889    fn slse_all_zero_signs_return_zero_sign() {
2890        // A single term whose sign is 0 contributes nothing; S = 0 ⇒ (−∞, 0.0).
2891        let (lm, sg) = signed_log_sum_exp(&[0.0], &[0.0]);
2892        assert_eq!(lm, f64::NEG_INFINITY);
2893        assert_eq!(sg, 0.0);
2894    }
2895
2896    #[test]
2897    fn slse_all_neg_inf_magnitudes_return_zero_sign() {
2898        // Every magnitude is exp(−∞) = 0 regardless of sign, so the sum is 0 and
2899        // the reported sign must be 0.0, not +1.0.
2900        let (lm, sg) = signed_log_sum_exp(&[f64::NEG_INFINITY, f64::NEG_INFINITY], &[1.0, -1.0]);
2901        assert_eq!(lm, f64::NEG_INFINITY);
2902        assert_eq!(sg, 0.0);
2903    }
2904
2905    #[test]
2906    fn slse_pos_inf_dominates() {
2907        let (lm, sg) = signed_log_sum_exp(&[f64::INFINITY, 1.0], &[1.0, -1.0]);
2908        assert_eq!(lm, f64::INFINITY);
2909        assert_eq!(sg, 1.0);
2910    }
2911
2912    #[test]
2913    fn slse_neg_inf_dominates() {
2914        let (lm, sg) = signed_log_sum_exp(&[f64::INFINITY, 1.0], &[-1.0, 1.0]);
2915        assert_eq!(lm, f64::INFINITY);
2916        assert_eq!(sg, -1.0);
2917    }
2918
2919    #[test]
2920    fn slse_both_inf_signs_gives_nan() {
2921        let (lm, sg) = signed_log_sum_exp(&[f64::INFINITY, f64::INFINITY], &[1.0, -1.0]);
2922        assert!(lm.is_nan());
2923        assert_eq!(sg, 0.0);
2924    }
2925
2926    // ── normal_logcdf ─────────────────────────────────────────────────────────
2927
2928    #[test]
2929    fn logcdf_at_zero_is_log_half() {
2930        let got = normal_logcdf(0.0);
2931        let expected = 0.5_f64.ln();
2932        assert!((got - expected).abs() < TOL, "got={got}");
2933    }
2934
2935    #[test]
2936    fn logcdf_pos_inf_is_zero() {
2937        assert_eq!(normal_logcdf(f64::INFINITY), 0.0);
2938    }
2939
2940    #[test]
2941    fn logcdf_neg_inf_is_neg_inf() {
2942        assert_eq!(normal_logcdf(f64::NEG_INFINITY), f64::NEG_INFINITY);
2943    }
2944
2945    #[test]
2946    fn logcdf_nan_is_nan() {
2947        assert!(normal_logcdf(f64::NAN).is_nan());
2948    }
2949
2950    #[test]
2951    fn logcdf_matches_log_cdf_for_moderate_x() {
2952        for &x in &[-2.0_f64, -1.0, 0.0, 1.0, 2.0, 3.0] {
2953            let got = normal_logcdf(x);
2954            let expected = normal_cdf(x).ln();
2955            assert!(
2956                (got - expected).abs() < 1e-10,
2957                "x={x}: got={got} expected={expected}"
2958            );
2959        }
2960    }
2961
2962    #[test]
2963    fn logcdf_deep_left_tail_stays_finite() {
2964        // For very negative x, normal_cdf(x) underflows to 0, but logcdf should
2965        // remain finite and large-negative.
2966        let got = normal_logcdf(-20.0);
2967        assert!(got.is_finite() && got < -100.0, "logcdf(-20)={got}");
2968    }
2969
2970    #[test]
2971    fn logcdf_positive_tail_does_not_round_through_unit_cdf() {
2972        let x = 10.0_f64;
2973        let got = normal_logcdf(x);
2974        let expected = (-0.5 * erfc(x / std::f64::consts::SQRT_2)).ln_1p();
2975        assert!(
2976            got < 0.0,
2977            "logcdf(10) must retain its negative tail: {got:e}"
2978        );
2979        assert_eq!(got.to_bits(), expected.to_bits());
2980    }
2981
2982    #[test]
2983    fn log_cdf_quantile_round_trips_both_unrepresentable_tails() {
2984        for x in [-1.0e6, -40.0, -10.0, -2.0, 0.0, 2.0, 10.0] {
2985            let log_p = normal_logcdf(x);
2986            let recovered = standard_normal_quantile_from_log_cdf(log_p)
2987                .expect("finite strict log-CDF has a quantile");
2988            assert!(
2989                (recovered - x).abs() <= 2.0e-12 * x.abs().max(1.0),
2990                "log-quantile round trip at x={x}: log_p={log_p}, recovered={recovered}"
2991            );
2992        }
2993    }
2994
2995    // ── normal_logsf ─────────────────────────────────────────────────────────
2996
2997    #[test]
2998    fn logsf_at_zero_is_log_half() {
2999        let got = normal_logsf(0.0);
3000        let expected = 0.5_f64.ln();
3001        assert!((got - expected).abs() < TOL, "got={got}");
3002    }
3003
3004    #[test]
3005    fn logsf_mirrors_logcdf() {
3006        // logsf(x) = logcdf(-x) by definition.
3007        for &x in &[-3.0_f64, -1.0, 0.0, 1.0, 3.0] {
3008            assert_eq!(normal_logsf(x), normal_logcdf(-x));
3009        }
3010    }
3011
3012    // ── signed_probit_logcdf_and_mills_ratio ──────────────────────────────────
3013
3014    #[test]
3015    fn probit_at_pos_inf() {
3016        let (lc, mr) = signed_probit_logcdf_and_mills_ratio(f64::INFINITY);
3017        assert_eq!(lc, 0.0);
3018        assert_eq!(mr, 0.0);
3019    }
3020
3021    #[test]
3022    fn probit_at_neg_inf() {
3023        let (lc, mr) = signed_probit_logcdf_and_mills_ratio(f64::NEG_INFINITY);
3024        assert_eq!(lc, f64::NEG_INFINITY);
3025        assert_eq!(mr, f64::INFINITY);
3026    }
3027
3028    #[test]
3029    fn probit_nan_propagates() {
3030        let (lc, mr) = signed_probit_logcdf_and_mills_ratio(f64::NAN);
3031        assert!(lc.is_nan() && mr.is_nan());
3032    }
3033
3034    #[test]
3035    fn probit_at_zero_logcdf_and_mills() {
3036        let (lc, mr) = signed_probit_logcdf_and_mills_ratio(0.0);
3037        assert!((lc - 0.5_f64.ln()).abs() < TOL, "lc={lc}");
3038        // phi(0)/Phi(0) = 0.3989.../0.5 ≈ 0.7979.
3039        assert!((mr - 0.797_884_560_802_865).abs() < 1e-10, "mr={mr}");
3040    }
3041
3042    #[test]
3043    fn probit_positive_branch_matches_logcdf() {
3044        for &x in &[0.5_f64, 1.0, 2.0, 3.0] {
3045            let (lc, mr) = signed_probit_logcdf_and_mills_ratio(x);
3046            let lc_ref = normal_logcdf(x);
3047            let mr_ref = normal_pdf(x) / normal_cdf(x);
3048            assert!(
3049                (lc - lc_ref).abs() < 1e-10,
3050                "x={x}: lc={lc} lc_ref={lc_ref}"
3051            );
3052            assert!(
3053                (mr - mr_ref).abs() < 1e-10,
3054                "x={x}: mr={mr} mr_ref={mr_ref}"
3055            );
3056        }
3057    }
3058
3059    #[test]
3060    fn probit_negative_branch_matches_logcdf() {
3061        for &x in &[-0.5_f64, -1.0, -2.0, -5.0] {
3062            let (lc, mr) = signed_probit_logcdf_and_mills_ratio(x);
3063            let lc_ref = normal_logcdf(x);
3064            assert!(
3065                (lc - lc_ref).abs() < 1e-10,
3066                "x={x}: lc={lc} lc_ref={lc_ref}"
3067            );
3068            assert!(mr.is_finite() && mr > 0.0, "x={x}: mr={mr}");
3069        }
3070    }
3071
3072    #[test]
3073    fn probit_mills_ratio_has_no_deep_tail_floor() {
3074        let x = -1.0e305_f64;
3075        let (log_cdf, mills_ratio) = signed_probit_logcdf_and_mills_ratio(x);
3076        assert_eq!(log_cdf, f64::NEG_INFINITY);
3077        assert!(mills_ratio.is_finite());
3078        assert!(
3079            ((mills_ratio / -x) - 1.0).abs() < 5.0e-15,
3080            "mills({x:e})={mills_ratio:e}"
3081        );
3082    }
3083
3084    #[test]
3085    fn normal_logcdf_derivative_stack_has_honest_infinite_limits() {
3086        assert_eq!(normal_logcdf_derivatives(f64::INFINITY), [0.0; 5]);
3087        assert_eq!(
3088            normal_logcdf_derivatives(f64::NEG_INFINITY),
3089            [f64::NEG_INFINITY, f64::INFINITY, -1.0, 0.0, 0.0]
3090        );
3091        assert!(
3092            normal_logcdf_derivatives(f64::NAN)
3093                .into_iter()
3094                .all(f64::is_nan)
3095        );
3096
3097        for x in [-1.0e200_f64, 1.0e200_f64] {
3098            let derivatives = normal_logcdf_derivatives(x);
3099            assert!(
3100                derivatives.into_iter().all(|value| !value.is_nan()),
3101                "NaN derivative at x={x:e}: {derivatives:?}"
3102            );
3103        }
3104    }
3105
3106    #[test]
3107    fn normal_logcdf_left_tail_derivatives_do_not_cancel() {
3108        let x = -1.0e100_f64;
3109        let derivatives = normal_logcdf_derivatives(x);
3110        assert_eq!(derivatives[2], -1.0);
3111        assert!(derivatives[3] > 0.0 && derivatives[3].is_finite());
3112        assert!(
3113            (derivatives[3] / 2.0e-300 - 1.0).abs() < 2.0e-14,
3114            "third derivative={:e}",
3115            derivatives[3]
3116        );
3117        assert_eq!(derivatives[4], 0.0);
3118    }
3119
3120    #[test]
3121    fn normal_logcdf_right_tail_preserves_weighted_subnormal_derivatives() {
3122        let derivatives = normal_logcdf_derivatives(38.6);
3123        assert_eq!(derivatives[1], 0.0);
3124        assert!(derivatives[2] < 0.0 && derivatives[2].is_subnormal());
3125        assert!(derivatives[3] > 0.0 && derivatives[3].is_subnormal());
3126        assert!(derivatives[4] < 0.0 && derivatives[4].is_subnormal());
3127    }
3128
3129    #[test]
3130    fn normal_logcdf_tail_stack_is_finite_difference_consistent() {
3131        let h = 1.0e-4_f64;
3132        for x in [-8.0_f64, -4.0, 8.0, 20.0] {
3133            let center = normal_logcdf_derivatives(x);
3134            let left = normal_logcdf_derivatives(x - h);
3135            let right = normal_logcdf_derivatives(x + h);
3136            for order in 1..=3 {
3137                let finite_difference = (right[order] - left[order]) / (2.0 * h);
3138                let expected = center[order + 1];
3139                let relative = (finite_difference - expected).abs() / expected.abs().max(1.0e-300);
3140                assert!(
3141                    relative < 2.0e-5,
3142                    "x={x}, order={order}: fd={finite_difference:e}, expected={expected:e}, rel={relative:e}"
3143                );
3144            }
3145        }
3146    }
3147
3148    /// Absolute-accuracy pin of the full `ln Φ(x)` derivative tower against an
3149    /// EXTERNAL high-precision reference (mpmath, dps=60), covering all three
3150    /// branches (continued-fraction left tail at x=−4, the moderate Mills
3151    /// recurrence for x∈(−4, 8), and both signs). Before the `erfc` root-cause
3152    /// fix the moderate branch's `λ = φ/Φ` inherited `statrs::erfc`'s ~1e-10
3153    /// error, so `f''` was wrong by ~1e-9 near the −4 seam; this pins every
3154    /// entry to `2e-11` relative, catching that regression head-on rather than
3155    /// through a seam-straddling finite difference.
3156    #[test]
3157    fn normal_logcdf_derivative_tower_matches_high_precision_reference() {
3158        // (x, [value, f', f'', f''', f''''] from mpmath at dps=60).
3159        let refs: &[(f64, [f64; 5])] = &[
3160            (
3161                -4.0,
3162                [
3163                    -10.360101486527291,
3164                    4.2256071444894711,
3165                    -0.95332716160257737,
3166                    0.017856339307658426,
3167                    0.0095065764315958691,
3168                ],
3169            ),
3170            // Two points well inside the continued-fraction branch, where the
3171            // truncation the depth controls is the ONLY error source: at -4 the
3172            // branch is at its least converged, and these confirm it stays put.
3173            (
3174                -10.0,
3175                [
3176                    -53.231285150512471,
3177                    10.098093233962512,
3178                    -0.99055462217434374,
3179                    0.0017864003921165069,
3180                    0.00049785382237944016,
3181                ],
3182            ),
3183            (
3184                -6.0,
3185                [
3186                    -20.736768949974706,
3187                    6.1584826045445989,
3188                    -0.97601236321083323,
3189                    0.0069535374991643118,
3190                    0.0028992056785575027,
3191                ],
3192            ),
3193            (
3194                -2.0,
3195                [
3196                    -3.7831843336820319,
3197                    2.3732155328228409,
3198                    -0.88572089958591874,
3199                    0.059355861291565813,
3200                    0.039421993865946813,
3201                ],
3202            ),
3203            (
3204                -1.0,
3205                [
3206                    -1.8410216450092635,
3207                    1.5251352761609812,
3208                    -0.80090233442965121,
3209                    0.11693119540604883,
3210                    0.07917498368074563,
3211                ],
3212            ),
3213            (
3214                -0.3,
3215                [
3216                    -0.96210281816885066,
3217                    0.99816596885848332,
3218                    -0.69688551072964971,
3219                    0.18398317992442132,
3220                    0.11037564722092704,
3221                ],
3222            ),
3223            (
3224                0.5,
3225                [
3226                    -0.36894641528865639,
3227                    0.50916043383703349,
3228                    -0.5138245643036329,
3229                    0.27099012446870783,
3230                    0.088167801929197554,
3231                ],
3232            ),
3233            (
3234                2.0,
3235                [
3236                    -0.023012909328963488,
3237                    0.055247862678989959,
3238                    -0.11354805168857645,
3239                    0.18439481503247759,
3240                    -0.18785468561160969,
3241                ],
3242            ),
3243        ];
3244        // The moderate-branch statrs regression produced ~1e-9 errors in f''.
3245        // The bound used to sit at 1e-10 to respect what was called the
3246        // continued-fraction branch's "inherent" ~2e-11 in f''''; that was not
3247        // inherent but a depth, and at 64 levels the branch reproduces this
3248        // 60-digit reference EXACTLY at x = -4, -6 and -10. What remains is the
3249        // moderate branch, where the brackets are already collected in `q` and
3250        // the floor is `λ`'s own relative error amplified by `λ/q` (18.7 at the
3251        // switch): 1.8e-13 at x = -2, the worst point here. 1e-11 keeps 55x of
3252        // headroom over that while still failing the 32-level truncation head-on.
3253        for &(x, reference) in refs {
3254            let got = normal_logcdf_derivatives(x);
3255            for (order, (&g, &r)) in got.iter().zip(reference.iter()).enumerate() {
3256                let rel = (g - r).abs() / r.abs().max(1.0e-3);
3257                assert!(
3258                    rel < 1.0e-11,
3259                    "normal_logcdf_derivatives({x})[{order}] = {g:.17e}, reference {r:.17e}, \
3260                     rel {rel:.3e} >= 1e-11"
3261                );
3262            }
3263        }
3264    }
3265
3266    // ── standard_normal_quantile ──────────────────────────────────────────────
3267
3268    #[test]
3269    fn quantile_rejects_out_of_range() {
3270        assert!(standard_normal_quantile(0.0).is_err());
3271        assert!(standard_normal_quantile(1.0).is_err());
3272        assert!(standard_normal_quantile(-0.1).is_err());
3273        assert!(standard_normal_quantile(1.1).is_err());
3274        assert!(standard_normal_quantile(f64::NAN).is_err());
3275    }
3276
3277    #[test]
3278    fn quantile_at_half_is_near_zero() {
3279        let q = standard_normal_quantile(0.5).unwrap();
3280        assert!(q.abs() < 1e-10, "quantile(0.5)={q}");
3281    }
3282
3283    #[test]
3284    fn quantile_at_0975_is_near_196() {
3285        let q = standard_normal_quantile(0.975).unwrap();
3286        assert!((q - 1.959_963_984_540_054).abs() < 1e-14, "q={q}");
3287    }
3288
3289    /// `standard_normal_quantile` and its log-CDF sibling, against a 120-digit
3290    /// root of `Φ(x) = p` (respectively `ln Φ(x) = log_p`).
3291    ///
3292    /// The seed is Acklam's rational approximation, whose accuracy is `1.15e-9`
3293    /// relative; the two Halley steps after it are what make the result
3294    /// ulp-accurate. Deleting the polish loop entirely leaves EVERY other
3295    /// quantile test in this module green except `quantile_roundtrip_cdf`, and
3296    /// that one only by a factor of 1.9 — so the polish had no real gate. This
3297    /// table is that gate: it fails by six orders if the seed ships unpolished.
3298    ///
3299    /// The grid straddles Acklam's own `P_LOW = 0.02425` branch on both sides,
3300    /// runs out to `p = 1e-300` where the seed is far from the root, and covers
3301    /// the reflected upper tail where the residual must be formed from
3302    /// `(1 − p) − ½erfc(x/√2)` rather than `Φ(x) − p`.
3303    /// The CENTRAL band, where the residual `F(x) − p` must never be formed
3304    /// against `½`.
3305    ///
3306    /// The sibling table above straddles Acklam's `P_LOW` branch and runs into
3307    /// both tails, but its tightest central point is `p = 0.5000000001`. That
3308    /// is not where the old residual failed. Forming `F(x) − p` as
3309    /// `(1 − p) − ½erfc(x/√2)` (or `F(x) − p` directly) subtracts two numbers
3310    /// of size ~½, so the residual carries a FIXED absolute error of one ulp of
3311    /// ½ however small the true residual is; `Δx ≈ residual_error / φ(x)` then
3312    /// pins the quantile's ABSOLUTE error at ~1.2e-16 and lets its RELATIVE
3313    /// error grow like `1.2e-16 / |x|` without bound as `p → ½`.
3314    ///
3315    /// Measured against a 50-digit `erfinv` reference at the exact `f64`
3316    /// abscissae below, before the centered residual and after:
3317    ///
3318    /// | `p`             | before   | after   |
3319    /// |-----------------|----------|---------|
3320    /// | `½ + 2⁻⁴⁵`      | 1.13e-09 | 2.3e-16 |
3321    /// | `0.5012506…`    | 7.31e-15 | 2.3e-16 |
3322    /// | `0.4987493…`    | 7.33e-15 | 2.3e-16 |
3323    ///
3324    /// The `1.13e-09` is not a coincidence: it is `|A[5] − √(2π)| / √(2π)`,
3325    /// Acklam's own advertised accuracy. As `p → ½` the seed reduces to
3326    /// `A[5]·(p − ½)` and the polish is handed a residual quantized to
3327    /// multiples of one ulp of ½ — usually exactly `0` — so the raw seed is
3328    /// what shipped.
3329    ///
3330    /// The bar is `4·f64::EPSILON` relative: half an ulp for the correctly
3331    /// rounded reference literal, the rest for the evaluator. Worst measured
3332    /// margin over this table is 1.0 ulp.
3333    #[test]
3334    fn normal_quantile_is_ulp_accurate_through_the_median() {
3335        // `[p, Φ⁻¹(p)]`, the second entry correctly rounded from a 50-digit
3336        // `sqrt(2)·erfinv(2p − 1)` evaluated at the EXACT binary `p`.
3337        const CENTRAL_REFERENCE: [[f64; 2]; 19] = [
3338            [0.5000000000000284, 7.124266047159724e-14],
3339            [0.4999999999999716, -7.124266047159724e-14],
3340            [0.5000000009313226, 2.3344794983332983e-09],
3341            [0.4999999990686774, -2.3344794983332983e-09],
3342            [0.5000009536743164, 2.390507006295574e-06],
3343            [0.500000001, 2.5066282037387115e-09],
3344            [0.4999999999, -2.506628482030354e-10],
3345            [0.5001, 0.00025066283008800747],
3346            [0.4999, -0.00025066283008800747],
3347            [0.51, 0.025068908258711057],
3348            [0.49, -0.025068908258711057],
3349            [0.55, 0.12566134685507416],
3350            [0.45, -0.12566134685507402],
3351            [0.6, 0.2533471031357997],
3352            [0.4, -0.2533471031357997],
3353            [0.7, 0.5244005127080407],
3354            [0.3, -0.5244005127080408],
3355            [0.75, 0.6744897501960817],
3356            [0.25, -0.6744897501960817],
3357        ];
3358        let bar = 4.0 * f64::EPSILON;
3359        let mut worst = 0.0_f64;
3360        let mut worst_at = f64::NAN;
3361        for [p, expected] in CENTRAL_REFERENCE {
3362            let got = standard_normal_quantile(p).expect("central p is in (0,1)");
3363            let relative = ((got - expected) / expected).abs();
3364            if relative > worst {
3365                worst = relative;
3366                worst_at = p;
3367            }
3368            assert!(
3369                relative <= bar,
3370                "Phi^-1({p}) = {got}, expected {expected}, relative {relative:e} > {bar:e}"
3371            );
3372        }
3373        println!("central quantile worst relative {worst:e} at p = {worst_at}");
3374    }
3375
3376    #[test]
3377    fn normal_quantiles_match_independent_high_precision_reference() {
3378        const QUANTILE_REFERENCE: [[f64; 2]; 22] = [
3379            [1e-300, -37.0470962993612],
3380            [1e-100, -21.273453560965326],
3381            [1e-20, -9.262340089798407],
3382            [1e-08, -5.612001244174789],
3383            [0.001, -3.0902323061678136],
3384            [0.02424, -1.9731366119445441],
3385            [0.02425, -1.972961051311885],
3386            [0.02426, -1.9727855514678605],
3387            [0.05, -1.6448536269514726],
3388            [0.1, -1.2815515655446004],
3389            [0.25, -0.6744897501960817],
3390            [0.4, -0.2533471031357997],
3391            [0.5, 0.0],
3392            [0.6, 0.2533471031357997],
3393            [0.75, 0.6744897501960817],
3394            [0.9, 1.2815515655446006],
3395            [0.95, 1.6448536269514722],
3396            [0.975, 1.9599639845400538],
3397            [0.99, 2.3263478740408408],
3398            [0.999, 3.090232306167813],
3399            [0.99999999, 5.612001243305505],
3400            [0.9999999999999999, 8.209536151601387],
3401        ];
3402        for [p, want] in QUANTILE_REFERENCE {
3403            let got = standard_normal_quantile(p).expect("p in (0,1) has a quantile");
3404            let error = (got - want).abs();
3405            // `Φ⁻¹(½) = 0` exactly, so it is the one absolute comparison.
3406            let budget = if want == 0.0 {
3407                1e-16
3408            } else {
3409                4e-15 * want.abs()
3410            };
3411            assert!(
3412                error <= budget,
3413                "Φ⁻¹({p}): got {got:.17e}, want {want:.17e} (error {error:.3e} > {budget:.3e})"
3414            );
3415        }
3416
3417        const LOG_CDF_QUANTILE_REFERENCE: [[f64; 2]; 9] = [
3418            [-0.7, -0.008559478582480282],
3419            [-2.0, -1.1015196284987503],
3420            [-10.0, -3.913946240531893],
3421            [-50.0, -9.674825283612357],
3422            [-200.0, -19.803669380301212],
3423            [-1000.0, -44.6157477319694],
3424            [-10000.0, -141.37983987312717],
3425            [-100000.0, -447.1978936785251],
3426            [-1000000.0, -1414.2077829910174],
3427        ];
3428        for [log_p, want] in LOG_CDF_QUANTILE_REFERENCE {
3429            let got =
3430                standard_normal_quantile_from_log_cdf(log_p).expect("finite log_p < 0 has a root");
3431            let error = (got - want).abs();
3432            // Rounding `log_p` itself to `f64` already moves the root by
3433            // `ulp(log_p)·dx/d(log_p)`, and `dx/d(log_p) = Φ/φ = 1/λ` — about
3434            // `1.25` near `p = ½` and `≈ 1/|x|` in the deep tail. That input
3435            // conditioning, not the solver, is what limits `log_p = −0.7`,
3436            // where the root sits at `−0.00856` and one ulp of `0.7` is already
3437            // `1.4e-16` of it.
3438            let conditioning = 8.0 * f64::EPSILON * log_p.abs() / want.abs().max(0.8);
3439            let budget = 4e-15 * want.abs() + conditioning;
3440            assert!(
3441                error <= budget,
3442                "Φ⁻¹(exp({log_p})): got {got:.17e}, want {want:.17e} \
3443                 (error {error:.3e} > {budget:.3e})"
3444            );
3445        }
3446    }
3447
3448    #[test]
3449    fn quantile_antisymmetry() {
3450        let q_lo = standard_normal_quantile(0.1).unwrap();
3451        let q_hi = standard_normal_quantile(0.9).unwrap();
3452        assert!((q_lo + q_hi).abs() < 1e-10, "q_lo={q_lo} q_hi={q_hi}");
3453    }
3454
3455    #[test]
3456    fn quantile_roundtrip_cdf() {
3457        for &p in &[
3458            0.001, 0.01, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.99, 0.999,
3459        ] {
3460            let q = standard_normal_quantile(p).unwrap();
3461            let p_back = normal_cdf(q);
3462            // RELATIVE, and sized by what the round trip can cost: a few ulp of
3463            // `q` propagated through `φ(q)`, plus a couple of ulp from `erfc`
3464            // itself. The former absolute `1e-10` bar was two orders looser than
3465            // an unpolished Acklam seed at its worst point.
3466            assert!(
3467                (p_back - p).abs() <= 1e-14 * p,
3468                "roundtrip failed at p={p}: q={q} p_back={p_back}"
3469            );
3470        }
3471    }
3472}
3473
3474#[cfg(test)]
3475mod weighted_chi_square_tests {
3476    use super::*;
3477
3478    /// The all-equal branch is an identity, not an approximation: `Σ w Z_j²`
3479    /// IS `w·χ²_q`. Pinning it here keeps the fast path honest — a future
3480    /// "optimization" that routes equal weights through the quadrature would
3481    /// change the answer in the last digits and this catches it.
3482    #[test]
3483    fn equal_weights_are_the_scaled_chi_square_exactly() {
3484        for &q in &[1usize, 2, 5, 13] {
3485            for &w in &[0.25_f64, 1.0, 7.5] {
3486                let weights = vec![w; q];
3487                for &x in &[1e-3_f64, 0.5, 3.84, 25.0, 400.0] {
3488                    let (got, bound) = weighted_chi_square_sf_with_bound(&weights, x);
3489                    let want = chi_square_sf(x / w, q as f64);
3490                    assert_eq!(got, want, "q={q} w={w} x={x}");
3491                    assert_eq!(bound, 0.0, "the closed form has no truncation");
3492                }
3493            }
3494        }
3495    }
3496
3497    /// Unequal weights against the same quantity computed a completely
3498    /// different way: the exact convolution of two scaled `χ²_1` densities,
3499    /// evaluated by high-order quadrature on the *density* rather than by
3500    /// inverting the characteristic function.
3501    ///
3502    /// `P(w₁Z₁² + w₂Z₂² > x) = ∫₀^∞ f_{w₁χ²₁}(s) · P(w₂χ²₁ > x − s) ds`,
3503    /// with `f_{wχ²₁}(s) = exp(−s/(2w)) / sqrt(2π w s)`. The `1/√s` endpoint
3504    /// singularity is removed by substituting `s = t²`.
3505    #[test]
3506    fn two_unequal_weights_match_an_independent_convolution() {
3507        fn convolution(w1: f64, w2: f64, x: f64) -> f64 {
3508            // Condition on the SMALLER weight's normal and leave the larger
3509            // weight in the tail factor: `S_a(x − b y)` then varies on the
3510            // scale `a/b ≥ 1` in `y`, so the integrand is smooth even when the
3511            // two weights are orders apart. Conditioning the other way puts a
3512            // near-step of width `b` inside the quadrature and is what makes a
3513            // naive convolution disagree with Imhof in the seventh digit.
3514            let a = w1.max(w2);
3515            let b = w1.min(w2);
3516            // y = t² removes the 1/√y endpoint singularity of the χ²₁ density;
3517            // the kink at y = x/b becomes a panel boundary.
3518            let kink = (x / b).sqrt();
3519            let mut total = 0.0;
3520            for (lo, hi) in [(0.0, kink), (kink, kink + 15.0)] {
3521                let panels = 4_000;
3522                let step = (hi - lo) / panels as f64;
3523                for panel in 0..panels {
3524                    let half = 0.5 * step;
3525                    let mid = lo + panel as f64 * step + half;
3526                    for &(node, weight) in &GAUSS_LEGENDRE_16 {
3527                        for signed in [half * node, -half * node] {
3528                            let t = mid + signed;
3529                            let y = t * t;
3530                            let density =
3531                                2.0 * (-0.5 * y).exp() / (2.0 * std::f64::consts::PI).sqrt();
3532                            let remaining = x - b * y;
3533                            let tail = if remaining <= 0.0 {
3534                                1.0
3535                            } else {
3536                                chi_square_sf(remaining / a, 1.0)
3537                            };
3538                            total += weight * half * density * tail;
3539                        }
3540                    }
3541                }
3542            }
3543            total
3544        }
3545        for &(w1, w2) in &[(1.0_f64, 0.25_f64), (2.0, 0.1), (1.0, 0.001)] {
3546            for &x in &[0.05_f64, 0.5, 3.0, 9.0] {
3547                let (got, bound) = weighted_chi_square_sf_with_bound(&[w1, w2], x);
3548                let want = convolution(w1, w2, x);
3549                assert!(
3550                    (got - want).abs() <= 1e-8 + 5e-7 * want,
3551                    "w=({w1},{w2}) x={x}: imhof {got} vs convolution {want} (bound {bound:.3e})"
3552                );
3553            }
3554        }
3555    }
3556
3557    /// The property that makes this the right reference for a penalized LR: the
3558    /// mean-matched χ² is systematically CONSERVATIVE in the upper tail when the
3559    /// weights differ, because it carries variance `2Σw` against the true
3560    /// `2Σw²`. Asserted as a strict inequality at the α the test is used at, on
3561    /// a spectrum shaped like a real smooth block.
3562    #[test]
3563    fn the_mean_matched_chi_square_is_conservative_against_the_exact_law() {
3564        let weights = [0.95_f64, 0.62, 0.31, 0.14, 0.05, 0.01];
3565        let mean: f64 = weights.iter().sum();
3566        // Upper 5% point of the mean-matched reference.
3567        let mut lo = 0.0_f64;
3568        let mut hi = 200.0_f64;
3569        for _ in 0..200 {
3570            let mid = 0.5 * (lo + hi);
3571            if chi_square_sf(mid, mean) > 0.05 {
3572                lo = mid;
3573            } else {
3574                hi = mid;
3575            }
3576        }
3577        let critical = 0.5 * (lo + hi);
3578        let exact_size = weighted_chi_square_sf(&weights, critical);
3579        assert!(
3580            exact_size < 0.05,
3581            "the mean-matched χ²_{mean} critical value {critical} carries exact tail \
3582             mass {exact_size}, which must be strictly under the nominal 0.05"
3583        );
3584        // And the gap is material rather than a rounding artifact.
3585        assert!(
3586            exact_size < 0.045,
3587            "exact tail mass at the mean-matched critical value is {exact_size}"
3588        );
3589    }
3590
3591    /// A survival function has to be a survival function: monotone
3592    /// non-increasing, in `[0, 1]`, one at the origin.
3593    #[test]
3594    fn the_survival_function_is_monotone_and_bounded() {
3595        let weights = [1.0_f64, 0.4, 0.4, 0.05, 0.002];
3596        assert_eq!(weighted_chi_square_sf(&weights, 0.0), 1.0);
3597        let mut previous = 1.0;
3598        let mut x = 1e-4;
3599        while x < 60.0 {
3600            let value = weighted_chi_square_sf(&weights, x);
3601            assert!((0.0..=1.0).contains(&value), "x={x} value={value}");
3602            assert!(
3603                value <= previous + 1e-10,
3604                "not monotone at x={x}: {value} > {previous}"
3605            );
3606            previous = value;
3607            x *= 1.35;
3608        }
3609    }
3610
3611    /// Zero weights are structural (a direction the statistic cannot see), not
3612    /// numerical noise: they must drop out exactly rather than perturb the law.
3613    #[test]
3614    fn zero_weights_drop_out_and_an_all_zero_spectrum_is_the_point_mass_at_zero() {
3615        let padded = [0.7_f64, 0.0, 0.2, 0.0, 0.0];
3616        let bare = [0.7_f64, 0.2];
3617        for &x in &[0.1_f64, 1.0, 4.0] {
3618            assert_eq!(
3619                weighted_chi_square_sf(&padded, x),
3620                weighted_chi_square_sf(&bare, x)
3621            );
3622        }
3623        assert_eq!(weighted_chi_square_sf(&[0.0, 0.0], 0.0), 0.0);
3624        assert_eq!(weighted_chi_square_sf(&[0.0, 0.0], -1.0), 1.0);
3625    }
3626
3627    /// The scale is carried exactly: `P(Σ c·w Z² > c·x) = P(Σ w Z² > x)`. This
3628    /// is the identity that lets a Bartlett factor be applied either as a
3629    /// rescaling of the statistic or as a rescaling of the whole spectrum, and
3630    /// the LR consumer relies on the two being the same operation.
3631    #[test]
3632    fn the_law_is_exactly_scale_equivariant() {
3633        let weights = [0.9_f64, 0.35, 0.08];
3634        for &c in &[0.37_f64, 1.0, 4.25] {
3635            let scaled: Vec<f64> = weights.iter().map(|w| c * w).collect();
3636            for &x in &[0.2_f64, 2.0, 11.0] {
3637                let a = weighted_chi_square_sf(&scaled, c * x);
3638                let b = weighted_chi_square_sf(&weights, x);
3639                assert!((a - b).abs() <= 1e-9, "c={c} x={x}: {a} vs {b}");
3640            }
3641        }
3642    }
3643
3644    /// The truncation backstop must not bind on any spectrum a smooth block can
3645    /// produce. "Smooth block" here means at least three positive weights in
3646    /// `(0, 1]` — the shape `2F − F²` always has, since a rank-`r` penalty
3647    /// leaves `r` directions to shrink and the basis carries more than two
3648    /// columns. The bound returned is the evidence.
3649    #[test]
3650    fn the_certified_bound_is_met_on_realistic_smooth_spectra() {
3651        let spectra: [&[f64]; 5] = [
3652            &[1.0, 0.5, 0.25, 0.125, 0.0625],
3653            &[0.999, 0.31, 0.02, 1e-3, 1e-4, 1e-5],
3654            &[0.4, 0.4, 0.4, 0.39, 0.01],
3655            &[1.0, 1e-2, 1e-4, 1e-6, 1e-8],
3656            &[0.05, 0.02, 0.01, 5e-3, 1e-3],
3657        ];
3658        for spectrum in spectra {
3659            let mean: f64 = spectrum.iter().sum();
3660            for scale in [0.1_f64, 1.0, 6.0, 30.0] {
3661                let x = scale * mean;
3662                let (_, bound) = weighted_chi_square_sf_with_bound(spectrum, x);
3663                assert!(
3664                    bound <= WEIGHTED_CHI_SQUARE_TOLERANCE,
3665                    "spectrum {spectrum:?} at x={x} truncated at bound {bound:.3e}"
3666                );
3667            }
3668        }
3669    }
3670
3671    /// A caller that asks for less accuracy must get less WORK and still get a
3672    /// number that is inside the bound it was handed — the bound is the contract,
3673    /// not the tolerance.
3674    ///
3675    /// This is the property `gam-models` leans on: it derives an accuracy from
3676    /// the resolution of the statistic being scored, which on a shrunk smooth is
3677    /// four orders looser than [`WEIGHTED_CHI_SQUARE_TOLERANCE`], and the
3678    /// difference is the difference between a millisecond and a second.
3679    #[test]
3680    fn a_looser_tolerance_is_certified_at_what_it_asked_for() {
3681        // One weight of order one over a tail of dust — the shape a REML-shrunk
3682        // penalized smooth actually produces, and the one where Imhof's
3683        // truncation is most expensive.
3684        let spectrum: &[f64] = &[1.0, 1e-3, 1e-5, 1e-7, 1e-9];
3685        let mean: f64 = spectrum.iter().sum();
3686        for scale in [0.5_f64, 3.0, 10.0] {
3687            let x = scale * mean;
3688            let (strict, strict_bound) = weighted_chi_square_sf_with_bound(spectrum, x);
3689            assert!(strict_bound <= WEIGHTED_CHI_SQUARE_TOLERANCE);
3690            for tolerance in [1e-9_f64, 1e-7, 1e-5] {
3691                let (loose, bound) = weighted_chi_square_sf_to_tolerance(spectrum, x, tolerance);
3692                assert!(
3693                    bound <= tolerance,
3694                    "x={x} tol={tolerance:.0e}: achieved bound {bound:.3e} exceeds the request"
3695                );
3696                assert!(
3697                    (loose - strict).abs() <= bound + strict_bound,
3698                    "x={x} tol={tolerance:.0e}: {loose} is outside the strict value {strict} \
3699                     by more than the two certified bounds ({bound:.3e} + {strict_bound:.3e})"
3700                );
3701            }
3702        }
3703    }
3704
3705    /// A tolerance that is not a positive finite number is not an instruction to
3706    /// stop early: the contract is "at least this accurate", so nonsense gets the
3707    /// strict default rather than the first panel.
3708    #[test]
3709    fn a_nonsense_tolerance_gets_the_strict_default() {
3710        let spectrum: &[f64] = &[1.0, 0.3, 0.02];
3711        let x = 3.0;
3712        let (want, _) = weighted_chi_square_sf_with_bound(spectrum, x);
3713        for tolerance in [0.0_f64, -1.0, f64::NAN, f64::INFINITY] {
3714            let (got, bound) = weighted_chi_square_sf_to_tolerance(spectrum, x, tolerance);
3715            assert_eq!(got, want, "tolerance {tolerance} did not fall back to the default");
3716            assert!(bound <= WEIGHTED_CHI_SQUARE_TOLERANCE);
3717        }
3718    }
3719
3720    /// Domain errors are `NaN`, not a silently plausible probability.
3721    #[test]
3722    fn invalid_inputs_are_not_answered() {
3723        assert!(weighted_chi_square_sf(&[1.0, -1e-16], 1.0).is_nan());
3724        assert!(weighted_chi_square_sf(&[1.0, f64::NAN], 1.0).is_nan());
3725        assert!(weighted_chi_square_sf(&[1.0, f64::INFINITY], 1.0).is_nan());
3726        assert!(weighted_chi_square_sf(&[1.0, 0.5], f64::NAN).is_nan());
3727    }
3728}
3729
3730/// The SIGNED, multiplicity-carrying form — the generalization the estimated-
3731/// scale references need (gam#2672).
3732#[cfg(test)]
3733mod signed_weighted_chi_square_tests {
3734    use super::*;
3735
3736    fn term(weight: f64, degrees_of_freedom: f64) -> WeightedChiSquareTerm {
3737        WeightedChiSquareTerm {
3738            weight,
3739            degrees_of_freedom,
3740        }
3741    }
3742
3743    /// THE identity the signed form exists for, against a closed form computed
3744    /// a completely different way (the regularized incomplete beta):
3745    ///
3746    /// ```text
3747    /// P(F_{a,b} > f) = P( (χ²_a/a) / (χ²_b/b) > f ) = P( χ²_a − (f·a/b)·χ²_b > 0 ).
3748    /// ```
3749    ///
3750    /// A ratio's tail IS a signed combination evaluated at zero. Fractional `a`
3751    /// is included because a two-moment summary of a smooth's null spectrum is a
3752    /// chi-square with a non-integral shape, which is exactly what this form is
3753    /// asked for.
3754    #[test]
3755    fn the_f_tail_is_the_two_term_signed_combination_at_zero() {
3756        let mut worst = 0.0_f64;
3757        for &(a, b) in &[
3758            (1.0_f64, 5.0_f64),
3759            (2.0, 17.0),
3760            (3.0, 26.0),
3761            (0.7, 24.0),
3762            (5.4, 191.0),
3763            (11.0, 4.0),
3764        ] {
3765            for &f in &[0.05_f64, 0.5, 1.0, 2.5, 9.0, 40.0] {
3766                let terms = [term(1.0, a), term(-f * a / b, b)];
3767                let (got, bound) = signed_weighted_chi_square_sf_to_tolerance(
3768                    &terms,
3769                    0.0,
3770                    WEIGHTED_CHI_SQUARE_TOLERANCE,
3771                );
3772                let want = fisher_snedecor_sf(f, a, b);
3773                let error = (got - want).abs();
3774                worst = worst.max(error);
3775                assert!(
3776                    error <= 1e-9 + bound,
3777                    "F({a},{b}) at {f}: imhof {got} vs beta {want} \
3778                     (error {error:.3e}, certified bound {bound:.3e})"
3779                );
3780            }
3781        }
3782        println!("worst |imhof − F| over the grid: {worst:.3e}");
3783    }
3784
3785    /// A multiplicity is not a different law from repeating the weight — it is
3786    /// the same law computed in one term instead of `h`. Bit-level agreement is
3787    /// not claimed (the arithmetic is reassociated); the certified bounds are.
3788    #[test]
3789    fn a_multiplicity_is_the_law_of_the_repeated_weight() {
3790        for &(weight, count) in &[(1.0_f64, 5usize), (0.37, 9), (2.5, 2)] {
3791            let repeated = vec![weight; count];
3792            let folded = [term(weight, count as f64)];
3793            for &x in &[1e-3_f64, 0.4, 3.0, 20.0, 150.0] {
3794                let (want, _) = weighted_chi_square_sf_with_bound(&repeated, x);
3795                let (got, _) = signed_weighted_chi_square_sf_to_tolerance(
3796                    &folded,
3797                    x,
3798                    WEIGHTED_CHI_SQUARE_TOLERANCE,
3799                );
3800                assert_eq!(got, want, "w={weight} h={count} x={x}");
3801            }
3802        }
3803        // And in a MIXED spectrum, where neither side takes the closed form.
3804        let repeated = [0.9_f64, 0.2, 0.2, 0.2, 0.2];
3805        let folded = [term(0.9, 1.0), term(0.2, 4.0)];
3806        for &x in &[0.2_f64, 1.7, 6.0] {
3807            let (want, want_bound) = weighted_chi_square_sf_with_bound(&repeated, x);
3808            let (got, got_bound) = signed_weighted_chi_square_sf_to_tolerance(
3809                &folded,
3810                x,
3811                WEIGHTED_CHI_SQUARE_TOLERANCE,
3812            );
3813            assert!(
3814                (got - want).abs() <= want_bound + got_bound + 1e-12,
3815                "x={x}: folded {got} vs repeated {want}"
3816            );
3817        }
3818    }
3819
3820    /// The unit-weight entry point is a special case of this one and must not
3821    /// have moved when it became one. Bit-for-bit, on the branch that goes
3822    /// through the quadrature.
3823    #[test]
3824    fn the_non_negative_entry_point_is_unchanged_bit_for_bit() {
3825        let spectra: [&[f64]; 3] = [
3826            &[1.0, 0.5, 0.25, 0.125],
3827            &[0.999, 0.31, 0.02, 1e-3],
3828            &[0.4, 0.4, 0.39, 0.01],
3829        ];
3830        for spectrum in spectra {
3831            let terms: Vec<WeightedChiSquareTerm> =
3832                spectrum.iter().map(|&w| term(w, 1.0)).collect();
3833            for &x in &[0.05_f64, 0.9, 4.0, 25.0] {
3834                let (direct, direct_bound) = weighted_chi_square_sf_with_bound(spectrum, x);
3835                let (general, general_bound) = signed_weighted_chi_square_sf_to_tolerance(
3836                    &terms,
3837                    x,
3838                    WEIGHTED_CHI_SQUARE_TOLERANCE,
3839                );
3840                assert_eq!(direct.to_bits(), general.to_bits(), "spectrum {spectrum:?} x={x}");
3841                assert_eq!(direct_bound.to_bits(), general_bound.to_bits());
3842            }
3843        }
3844    }
3845
3846    /// A single negative weight is the LOWER tail of a chi-square, because
3847    /// dividing by a negative number turns the inequality around. Exact branch,
3848    /// so exact agreement.
3849    #[test]
3850    fn an_all_negative_spectrum_is_the_reflected_chi_square() {
3851        for &(weight, df) in &[(-1.0_f64, 3.0_f64), (-0.25, 7.5), (-4.0, 1.0)] {
3852            for &x in &[-40.0_f64, -8.0, -0.3, -1e-4] {
3853                let got = signed_weighted_chi_square_sf(&[term(weight, df)], x);
3854                let want = 1.0 - chi_square_sf(x / weight, df);
3855                assert_eq!(got, want, "w={weight} h={df} x={x}");
3856            }
3857            // The support decides everything at or above zero.
3858            assert_eq!(signed_weighted_chi_square_sf(&[term(weight, df)], 0.0), 0.0);
3859            assert_eq!(signed_weighted_chi_square_sf(&[term(weight, df)], 1.0), 0.0);
3860        }
3861    }
3862
3863    /// A survival function over the WHOLE line, which is what a signed
3864    /// combination has: monotone non-increasing, in `[0, 1]`, going to one in
3865    /// the far negative tail and to zero in the far positive one.
3866    #[test]
3867    fn the_signed_survival_function_is_monotone_over_the_whole_line() {
3868        let terms = [term(1.0, 1.0), term(0.4, 2.0), term(-0.3, 6.0), term(-1.5, 1.0)];
3869        let mut previous = 1.0_f64;
3870        let mut x = -60.0_f64;
3871        while x < 60.0 {
3872            let value = signed_weighted_chi_square_sf(&terms, x);
3873            assert!((0.0..=1.0).contains(&value), "x={x} value={value}");
3874            assert!(
3875                value <= previous + 1e-9,
3876                "not monotone at x={x}: {value} > {previous}"
3877            );
3878            previous = value;
3879            x += 0.5;
3880        }
3881        assert!(signed_weighted_chi_square_sf(&terms, -400.0) > 0.999);
3882        assert!(signed_weighted_chi_square_sf(&terms, 400.0) < 1e-3);
3883    }
3884
3885    /// The certified bound at `statistic = 0` — where the oscillatory bound does
3886    /// not exist and the amplitude bound is the whole contract. Checked against
3887    /// a reference computed at a far stricter request, so the assertion is that
3888    /// the RETURNED bound actually bounds the error.
3889    #[test]
3890    fn the_amplitude_bound_certifies_the_zero_statistic_answer() {
3891        let cases: [&[WeightedChiSquareTerm]; 3] = [
3892            &[term(1.0, 1.0), term(-0.05, 26.0)],
3893            &[term(0.9, 1.0), term(0.2, 3.0), term(-0.01, 191.0)],
3894            &[term(1.0, 5.4), term(-2.5, 1.0), term(-0.004, 44.0)],
3895        ];
3896        for terms in cases {
3897            let (reference, reference_bound) =
3898                signed_weighted_chi_square_sf_to_tolerance(terms, 0.0, 1e-14);
3899            for tolerance in [1e-4_f64, 1e-7, 1e-10] {
3900                let (got, bound) =
3901                    signed_weighted_chi_square_sf_to_tolerance(terms, 0.0, tolerance);
3902                assert!(
3903                    bound <= tolerance,
3904                    "asked {tolerance:.0e}, certified {bound:.3e} on {terms:?}"
3905                );
3906                assert!(
3907                    (got - reference).abs() <= bound + reference_bound,
3908                    "{got} vs {reference} exceeds the certified {bound:.3e} + \
3909                     {reference_bound:.3e} on {terms:?}"
3910                );
3911            }
3912        }
3913    }
3914
3915    /// The limit that makes this a *generalization* rather than a replacement:
3916    /// as the denominator's degrees of freedom grow, `Q/(V/ν)` collapses onto
3917    /// `Q`, so the ratio reference converges to the known-scale reference. This
3918    /// is the property that says a fix built on it cannot change large-`n`
3919    /// answers.
3920    #[test]
3921    fn the_ratio_reference_converges_to_the_known_scale_reference() {
3922        let spectrum = [0.95_f64, 0.62, 0.31, 0.14, 0.05];
3923        for &x in &[1.0_f64, 3.0, 8.0] {
3924            let known = weighted_chi_square_sf(&spectrum, x);
3925            let mut previous_gap = f64::INFINITY;
3926            for &nu in &[50.0_f64, 500.0, 5_000.0, 50_000.0] {
3927                // `P(Q > x·V/ν) = P(Q − (x/ν)·χ²_ν > 0)`.
3928                let mut terms: Vec<WeightedChiSquareTerm> =
3929                    spectrum.iter().map(|&w| term(w, 1.0)).collect();
3930                terms.push(term(-x / nu, nu));
3931                let ratio = signed_weighted_chi_square_sf(&terms, 0.0);
3932                let gap = (ratio - known).abs();
3933                assert!(
3934                    gap < previous_gap,
3935                    "x={x} ν={nu}: gap {gap:.3e} did not shrink from {previous_gap:.3e}"
3936                );
3937                previous_gap = gap;
3938            }
3939            assert!(
3940                previous_gap < 1e-4,
3941                "x={x}: the ν = 50000 ratio reference is still {previous_gap:.3e} from the \
3942                 known-scale one"
3943            );
3944        }
3945    }
3946
3947    /// The estimated scale always costs POWER at the level, never buys it: the
3948    /// ratio tail is above the known-scale tail at every threshold in the upper
3949    /// tail, because dividing by an independent mean-one variate can only add
3950    /// spread. This is the sign of the whole correction, asserted as a property
3951    /// rather than read off one measurement.
3952    #[test]
3953    fn the_ratio_reference_is_uniformly_more_conservative_in_the_upper_tail() {
3954        let spectrum = [0.95_f64, 0.62, 0.31, 0.14, 0.05];
3955        for &nu in &[10.0_f64, 26.0, 100.0] {
3956            for &x in &[2.0_f64, 4.0, 8.0, 16.0] {
3957                let known = weighted_chi_square_sf(&spectrum, x);
3958                if known > 0.4 {
3959                    continue; // not the upper tail
3960                }
3961                let mut terms: Vec<WeightedChiSquareTerm> =
3962                    spectrum.iter().map(|&w| term(w, 1.0)).collect();
3963                terms.push(term(-x / nu, nu));
3964                let ratio = signed_weighted_chi_square_sf(&terms, 0.0);
3965                assert!(
3966                    ratio > known,
3967                    "ν={nu} x={x}: ratio tail {ratio} must exceed the known-scale {known}"
3968                );
3969            }
3970        }
3971    }
3972
3973    /// The panel rule has to resolve the integrand's AMPLITUDE, not only its
3974    /// phase, and this is the arm that measures whether it does.
3975    ///
3976    /// The reference is the same quadrature at a panel forced far below either
3977    /// rule (by asking for an accuracy the sizing then honours), so the
3978    /// comparison isolates the discretization from the truncation. The shapes
3979    /// are the ones where the two scales come apart: a small phase rate
3980    /// (`statistic = 0`, weights that nearly cancel) against an amplitude that
3981    /// turns over at `u ≈ 1`.
3982    ///
3983    /// Pre-fix, `F_{1,5}` at `f = 0.05` missed by `3.4e-7` while certifying
3984    /// `1e-11`.
3985    #[test]
3986    fn the_quadrature_resolves_the_amplitude_not_only_the_phase() {
3987        let cases: [&[WeightedChiSquareTerm]; 5] = [
3988            &[term(1.0, 1.0), term(-0.01, 5.0)],
3989            &[term(1.0, 1.0), term(-0.2, 2.0)],
3990            &[term(1.0, 3.0), term(-1.0, 3.0)],
3991            &[term(0.9, 1.0), term(0.2, 4.0), term(-0.05, 26.0)],
3992            &[term(1.0, 0.7), term(-0.006, 24.0)],
3993        ];
3994        let mut worst = 0.0_f64;
3995        for terms in cases {
3996            for &statistic in &[0.0_f64, 0.3, -0.2] {
3997                let (reference, reference_bound) =
3998                    signed_weighted_chi_square_sf_to_tolerance(terms, statistic, 1e-15);
3999                let (got, bound) = signed_weighted_chi_square_sf_to_tolerance(
4000                    terms,
4001                    statistic,
4002                    WEIGHTED_CHI_SQUARE_TOLERANCE,
4003                );
4004                let error = (got - reference).abs();
4005                worst = worst.max(error);
4006                assert!(
4007                    error <= bound + reference_bound,
4008                    "{terms:?} at {statistic}: {got} vs {reference} differs by {error:.3e}, \
4009                     above the certified {bound:.3e} + {reference_bound:.3e}"
4010                );
4011            }
4012        }
4013        println!("worst discretization error against the fine-panel reference: {worst:.3e}");
4014    }
4015
4016    /// Domain errors are `NaN`. A zero or negative degrees-of-freedom is one:
4017    /// `χ²_0` is a point mass the inversion has no phase for, and a caller that
4018    /// produced it has a rank bug, not a distribution.
4019    #[test]
4020    fn signed_domain_errors_are_not_answered() {
4021        assert!(signed_weighted_chi_square_sf(&[term(1.0, 0.0)], 1.0).is_nan());
4022        assert!(signed_weighted_chi_square_sf(&[term(1.0, -2.0)], 1.0).is_nan());
4023        assert!(signed_weighted_chi_square_sf(&[term(1.0, f64::NAN)], 1.0).is_nan());
4024        assert!(signed_weighted_chi_square_sf(&[term(f64::INFINITY, 1.0)], 1.0).is_nan());
4025        assert!(signed_weighted_chi_square_sf(&[term(1.0, 1.0)], f64::NAN).is_nan());
4026        // A zero weight is dropped, not rejected — it is a direction the
4027        // statistic cannot see — and an all-zero spectrum is the point mass.
4028        assert_eq!(
4029            signed_weighted_chi_square_sf(&[term(0.0, 3.0), term(-1.0, 2.0)], -1.0),
4030            signed_weighted_chi_square_sf(&[term(-1.0, 2.0)], -1.0)
4031        );
4032        assert_eq!(signed_weighted_chi_square_sf(&[term(0.0, 3.0)], -1.0), 1.0);
4033        assert_eq!(signed_weighted_chi_square_sf(&[term(0.0, 3.0)], 0.0), 0.0);
4034    }
4035}