Skip to main content

hpr_forensics/
posterior.rs

1//! The Bayesian option's sampler: a posterior's quantiles by importance sampling.
2//!
3//! **What it does.** Given where a posterior peaks and its curvature there (the least-squares
4//! fit's answer and covariance, the Laplace approximation), it draws points around the peak,
5//! weighs each by how much more or less likely the posterior makes it than the draw did, and reads
6//! each unknown's 5%, 50% and 95% points off the weighted draws. Where the posterior is Gaussian
7//! this gives what the covariance already says; where it bends or has a heavy side, as a wind told
8//! by heights alone does, it gives the posterior's own shape.
9//!
10//! **The method.** Self-normalized importance sampling (J. Geweke, "Bayesian inference in
11//! econometric models using Monte Carlo integration", *Econometrica* 57(6), 1989, pp. 1317–1339):
12//! draws `xᵢ` from a proposal `q`, weights `wᵢ = p(xᵢ)/q(xᵢ)` with `p` the posterior known up to a
13//! constant, and each expectation as `Σ wᵢ f(xᵢ) / Σ wᵢ`. The proposal is a multivariate Student-t
14//! with ν = [`DEGREES_OF_FREEDOM`], centered on the peak, its scale the Laplace covariance: its
15//! tails are heavier than a Gaussian posterior's, so the weights stay bounded where the posterior is
16//! near Gaussian (Geweke on a proposal's tails). A draw is
17//!
18//! x = m + L z / √(u/ν),  z ~ N(0, I),  u ~ χ²_ν,  L Lᵀ = Σ,
19//!
20//! and up to a constant `ln q(x) = −(ν + N)/2 · ln(1 + δ²/ν)` with `δ² = |z|²/(u/ν)`.
21//!
22//! **How far to trust it.** The weighted draws count for `(Σ wᵢ)² / Σ wᵢ²` independent ones, Kish's
23//! effective sample size (L. Kish, *Survey Sampling*, Wiley, 1965), a result for a weighted mean
24//! read here for quantiles as an approximation. A posterior far from its Laplace approximation
25//! leaves few effective draws; the quantiles still come back, and [`Posterior::enough`] says
26//! they're too few to read. A draw whose posterior can't be had (a flight that won't fly) gets no
27//! weight and is counted in [`Posterior::failed`]. Tested on posteriors whose quantiles are known
28//! in closed form or by quadrature.
29//!
30//! **In two blocks.** A posterior whose unknowns follow some of them along a curved ridge, as a
31//! fit's drag and clock follow the wind on a log of height alone, leaves one Student-t about its
32//! peak few effective draws. [`sample_in_two`] draws the marked unknowns first, then the others
33//! about where the caller says they center given those, and widens its scales where a pilot's
34//! draws reach further ([ADR-320][adr-320]). [`crate::fit::bayesian`] draws a fit's posterior so,
35//! and `cargo xtask fit` counts its 90% intervals on the synthetic flights.
36//!
37//! [adr-320]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0320-m7-3c2-the-bayesian-option-in-two-blocks.md
38
39use hpr_core::random::SeededRng;
40
41use crate::fit::cholesky;
42
43/// The proposal's degrees of freedom: 4. Any Student-t has tails heavier than a Gaussian
44/// posterior's, which keeps the weights bounded there (Geweke); a small ν widens them further for a
45/// posterior with a heavy side, and ν = 4 still has a finite variance. A judgment, not a
46/// measurement.
47pub const DEGREES_OF_FREEDOM: usize = 4;
48
49/// The fewest effective draws for a quantile to be read: 100. At 100, the 5% point's Monte Carlo
50/// error is about `√(0.05 · 0.95 / 100) / φ(1.645)`, 0.22 posterior standard deviations rounded
51/// up, past which an interval's ends say more of the draws than of the flight. A judgment, not a
52/// measurement.
53pub const MIN_EFFECTIVE_DRAWS: f64 = 100.0;
54
55/// The probabilities [`Posterior::quantiles`] reads: the 90% interval's ends and the median.
56pub const PROBABILITIES: [f64; 3] = [0.05, 0.5, 0.95];
57
58/// What the weighted draws say of each unknown.
59#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
60pub struct Posterior<const N: usize> {
61    /// Each unknown's 5%, 50% and 95% points ([`PROBABILITIES`]), in the unknowns' order.
62    #[serde(with = "rows")]
63    pub quantiles: [[f64; 3]; N],
64    /// Kish's effective sample size, `(Σ wᵢ)² / Σ wᵢ²`.
65    pub effective_draws: f64,
66    /// The draws made, failed ones included.
67    pub draws: usize,
68    /// The draws whose posterior couldn't be had, or whose weight came out infinite or NaN; each
69    /// has no weight. A draw where the posterior is zero (`log_target` gives −∞) has no weight
70    /// either, but isn't a failure.
71    pub failed: usize,
72    /// Whether [`Posterior::effective_draws`] reaches [`MIN_EFFECTIVE_DRAWS`]. It doesn't weigh
73    /// [`Posterior::failed`]: many failed draws cut the posterior off where they fell, and
74    /// `failed / draws` says how many.
75    pub enough: bool,
76}
77
78/// `[[f64; 3]; N]` as a list of rows, since serde's arrays stop at 32 and take no const length.
79mod rows {
80    use serde::{Deserialize, Deserializer, Serialize, Serializer};
81
82    pub fn serialize<S: Serializer, const N: usize>(
83        rows: &[[f64; 3]; N],
84        s: S,
85    ) -> Result<S::Ok, S::Error> {
86        rows.as_slice().serialize(s)
87    }
88
89    pub fn deserialize<'de, D: Deserializer<'de>, const N: usize>(
90        d: D,
91    ) -> Result<[[f64; 3]; N], D::Error> {
92        let rows = Vec::<[f64; 3]>::deserialize(d)?;
93        let count = rows.len();
94        rows.try_into()
95            .map_err(|_| serde::de::Error::invalid_length(count, &"one row for each unknown"))
96    }
97}
98
99/// The posterior whose log density, up to a constant, is `log_target`, by `draws` weighted draws
100/// around `peak` with the Laplace covariance `covariance`.
101///
102/// `log_target` gives `None` where the posterior can't be had; that draw gets no weight. Only the
103/// lower triangle of `covariance` is read. `None` overall if `peak` isn't finite, `covariance`
104/// isn't positive definite, `draws` is 0, or no draw has a finite weight.
105/// The same `seed` and a `log_target` that gives the same answer for the same point give the same
106/// answer, bit for bit, on one platform (the draws go through the platform's `ln`, `ln_1p`,
107/// `sqrt` and `exp`).
108pub fn sample<const N: usize>(
109    peak: &[f64; N],
110    covariance: &[[f64; N]; N],
111    draws: usize,
112    seed: u64,
113    log_target: impl FnMut(&[f64; N]) -> Option<f64>,
114) -> Option<Posterior<N>> {
115    sample_weighted(peak, covariance, draws, seed, log_target, true)
116}
117
118/// [`sample`], with the proposal's density in each weight, or left out of it for the tests' probe
119/// that the weights need it.
120fn sample_weighted<const N: usize>(
121    peak: &[f64; N],
122    covariance: &[[f64; N]; N],
123    draws: usize,
124    seed: u64,
125    mut log_target: impl FnMut(&[f64; N]) -> Option<f64>,
126    proposal_in_weight: bool,
127) -> Option<Posterior<N>> {
128    if !peak.iter().all(|v| v.is_finite()) {
129        return None;
130    }
131    let l = cholesky(covariance)?;
132    let nu = DEGREES_OF_FREEDOM as f64;
133    // Cast: N is a count of unknowns, far below 2⁵³.
134    let dimensions = N as f64;
135    let mut rng = SeededRng::seed_from_u64(seed);
136    // Grown as draws succeed, not reserved up front, so a huge `draws` can't fail an allocation
137    // before the first flight: memory is about `draws · 8 (N + 4)` bytes, the points with their
138    // weights twice over and one column to sort.
139    let mut points: Vec<([f64; N], f64)> = Vec::new();
140    let mut failed = 0;
141    for _ in 0..draws {
142        let z: [f64; N] = std::array::from_fn(|_| rng.standard_normal());
143        let u: f64 = (0..DEGREES_OF_FREEDOM)
144            .map(|_| rng.standard_normal().powi(2))
145            .sum();
146        let stretch = 1.0 / (u / nu).sqrt();
147        let x: [f64; N] = std::array::from_fn(|i| {
148            peak[i] + stretch * (0..=i).map(|k| l[i][k] * z[k]).sum::<f64>()
149        });
150        let distance2 = z.iter().map(|v| v * v).sum::<f64>() * stretch * stretch;
151        let log_proposal = if proposal_in_weight {
152            -0.5 * (nu + dimensions) * (distance2 / nu).ln_1p()
153        } else {
154            0.0
155        };
156        match log_target(&x) {
157            // A posterior of zero there: no weight, and nothing failed.
158            Some(log_p) if log_p == f64::NEG_INFINITY => {}
159            Some(log_p) if (log_p - log_proposal).is_finite() => {
160                points.push((x, log_p - log_proposal))
161            }
162            _ => failed += 1,
163        }
164    }
165    read_out(&points, draws, failed)
166}
167
168/// [`sample`] in two blocks, adapted on a pilot: for a posterior whose other unknowns follow some
169/// of them along a ridge the Laplace approximation can't bend to, and which may reach further than
170/// its curvature says.
171///
172/// **The blocks.** The unknowns `outer` marks are drawn first, from a Student-t about their share
173/// of `peak`; then the others, from a Student-t about `inner_middle` of the point drawn so far, the
174/// others still at `peak`. `inner_middle` gives a point whose unknowns not marked `outer` are where
175/// the others are taken to center given the marked ones, as a step of Gauss–Newton's from `peak`
176/// gives them; its marked unknowns aren't read. It must give the same answer for the same point:
177/// the proposal's density is then known, so the draws weigh to the posterior whatever middle it
178/// gives, a poor one costing effective draws only. `None` from it, or one not finite, makes a
179/// failed draw.
180///
181/// **The pilot.** First `pilot` draws, the marked block's scale its share of `covariance` and the
182/// others' their covariance given it (the Schur complement). Their weighted spread about the
183/// proposal's center, `Ĉ = Σ ωᵢ eᵢ eᵢᵀ`, is set against each block's scale `S`: where, along an
184/// eigenvector of `S^(-1/2) Ĉ S^(-T/2)`, the posterior reaches further than `S` says, that block's
185/// scale is widened to it, up to [`MAX_WIDENING`] in variance; it is never narrowed. Then `draws`
186/// more on the widened scales, and only those are read, so that given the pilot the answer is
187/// plain importance sampling (adaptive importance sampling: M.-S. Oh and J. O. Berger, "Adaptive
188/// importance sampling in Monte Carlo integration", *J. Statist. Comput. Simul.* 41, 1992,
189/// pp. 143–168, who adapt a Student-t to a pilot's weighted moments). [`Posterior::draws`] and
190/// [`Posterior::failed`] count both stages' draws; [`Posterior::effective_draws`] the read ones'.
191///
192/// Each draw's proposal is the product of two Student-t densities of ν = [`DEGREES_OF_FREEDOM`],
193/// up to a constant `ln q = −(ν + K)/2 · ln(1 + δ₁²/ν) − (ν + N − K)/2 · ln(1 + δ₂²/ν)` for `K`
194/// marked unknowns, each `δ²` its block's as in [`sample`]: a marginal times a conditional, not one
195/// joint Student-t. Otherwise as [`sample`], whose `None`s it shares, and also `None` if a block's
196/// scale isn't positive definite. With nothing marked it draws one Student-t about
197/// `inner_middle(peak)`.
198#[expect(
199    clippy::too_many_arguments,
200    reason = "a posterior, its blocks, two stages' draws, a seed and two functions"
201)]
202pub fn sample_in_two<const N: usize>(
203    peak: &[f64; N],
204    covariance: &[[f64; N]; N],
205    outer: &[bool; N],
206    pilot: usize,
207    draws: usize,
208    seed: u64,
209    inner_middle: impl FnMut(&[f64; N]) -> Option<[f64; N]>,
210    log_target: impl FnMut(&[f64; N]) -> Option<f64>,
211) -> Option<Posterior<N>> {
212    let stages = Stages {
213        pilot,
214        draws,
215        adapt: true,
216        proposal_in_weight: true,
217    };
218    sample_in_two_with(
219        peak,
220        covariance,
221        outer,
222        seed,
223        stages,
224        inner_middle,
225        log_target,
226    )
227}
228
229/// The most [`sample_in_two`]'s pilot widens a block's scale along any direction, in variance: 4,
230/// twice the standard deviation, so that a pilot ruled by one far draw can't spread the main
231/// draws past use. A judgment, not a measurement.
232pub const MAX_WIDENING: f64 = 4.0;
233
234/// How [`sample_in_two_with`] draws: its pilot's draws, its main draws, whether the pilot widens
235/// the scales, and whether the proposal's density is in each weight; the last two off only in the
236/// tests' probes.
237#[derive(Debug, Clone, Copy)]
238struct Stages {
239    pilot: usize,
240    draws: usize,
241    adapt: bool,
242    proposal_in_weight: bool,
243}
244
245/// One draw of [`sample_in_two`]: its point, its proposal's center for the second block, and its
246/// log weight.
247struct Drawn<const N: usize> {
248    x: [f64; N],
249    middle: [f64; N],
250    log_weight: f64,
251}
252
253/// [`sample_in_two`], drawn as `stages` says.
254fn sample_in_two_with<const N: usize>(
255    peak: &[f64; N],
256    covariance: &[[f64; N]; N],
257    outer: &[bool; N],
258    seed: u64,
259    stages: Stages,
260    mut inner_middle: impl FnMut(&[f64; N]) -> Option<[f64; N]>,
261    mut log_target: impl FnMut(&[f64; N]) -> Option<f64>,
262) -> Option<Posterior<N>> {
263    if !peak.iter().all(|v| v.is_finite()) {
264        return None;
265    }
266    let first: Vec<usize> = (0..N).filter(|&i| outer[i]).collect();
267    let second: Vec<usize> = (0..N).filter(|&i| !outer[i]).collect();
268    let block = |rows: &[usize], columns: &[usize]| -> Vec<Vec<f64>> {
269        rows.iter()
270            .map(|&i| {
271                columns
272                    .iter()
273                    .map(|&j| covariance[i.max(j)][i.min(j)])
274                    .collect()
275            })
276            .collect()
277    };
278    let l_first = cholesky_of(&block(&first, &first))?;
279    // The second block's covariance given the first: Σ₂₂ − Aᵀ A, with L₁ A = Σ₁₂.
280    let across = block(&first, &second);
281    let a: Vec<Vec<f64>> = (0..second.len())
282        .map(|c| {
283            let column: Vec<f64> = across.iter().map(|row| row[c]).collect();
284            forward_solve(&l_first, &column)
285        })
286        .collect();
287    let mut given = block(&second, &second);
288    for (i, row) in given.iter_mut().enumerate() {
289        for (j, value) in row.iter_mut().enumerate() {
290            *value -= a[i].iter().zip(&a[j]).map(|(p, q)| p * q).sum::<f64>();
291        }
292    }
293    let l_second = cholesky_of(&given)?;
294    let mut rng = SeededRng::seed_from_u64(seed);
295    let mut stage = |l_first: &[Vec<f64>], l_second: &[Vec<f64>], count: usize| {
296        let mut made: Vec<Drawn<N>> = Vec::new();
297        let mut failed = 0;
298        for _ in 0..count {
299            match draw_in_two(
300                peak,
301                (&first, l_first),
302                (&second, l_second),
303                &mut rng,
304                &mut inner_middle,
305                &mut log_target,
306                stages.proposal_in_weight,
307            ) {
308                Some(Some(drawn)) => made.push(drawn),
309                Some(None) => {}
310                None => failed += 1,
311            }
312        }
313        (made, failed)
314    };
315    let (piloted, pilot_failed) = stage(&l_first, &l_second, stages.pilot);
316    let (l_first, l_second) = if stages.adapt {
317        let top = piloted
318            .iter()
319            .map(|d| d.log_weight)
320            .fold(f64::NEG_INFINITY, f64::max);
321        let weights: Vec<f64> = piloted.iter().map(|d| (d.log_weight - top).exp()).collect();
322        let total: f64 = weights.iter().sum();
323        let spread = |indices: &[usize], about: &dyn Fn(&Drawn<N>) -> [f64; N]| {
324            let mut c = vec![vec![0.0; indices.len()]; indices.len()];
325            if top.is_finite() && total > 0.0 {
326                for (d, w) in piloted.iter().zip(&weights) {
327                    let center = about(d);
328                    for (p, &i) in indices.iter().enumerate() {
329                        for (q, &j) in indices.iter().enumerate() {
330                            c[p][q] += w / total * (d.x[i] - center[i]) * (d.x[j] - center[j]);
331                        }
332                    }
333                }
334            }
335            c
336        };
337        let spread_first = spread(&first, &|_| *peak);
338        let spread_second = spread(&second, &|d| d.middle);
339        (
340            widened(&l_first, &spread_first)?,
341            widened(&l_second, &spread_second)?,
342        )
343    } else {
344        (l_first, l_second)
345    };
346    let (points, failed) = stage(&l_first, &l_second, stages.draws);
347    let points: Vec<([f64; N], f64)> = points.into_iter().map(|d| (d.x, d.log_weight)).collect();
348    read_out(&points, stages.pilot + stages.draws, pilot_failed + failed)
349}
350
351/// One draw in two blocks, each block its indices and its scale's lower triangle: `None` if it
352/// failed, `Some(None)` where the posterior is zero, which has no weight and isn't a failure.
353/// Every number a draw takes is drawn before any is used, so a failed draw moves no other.
354fn draw_in_two<const N: usize>(
355    peak: &[f64; N],
356    (first, l_first): (&[usize], &[Vec<f64>]),
357    (second, l_second): (&[usize], &[Vec<f64>]),
358    rng: &mut SeededRng,
359    inner_middle: &mut impl FnMut(&[f64; N]) -> Option<[f64; N]>,
360    log_target: &mut impl FnMut(&[f64; N]) -> Option<f64>,
361    proposal_in_weight: bool,
362) -> Option<Option<Drawn<N>>> {
363    let nu = DEGREES_OF_FREEDOM as f64;
364    let z1: Vec<f64> = first.iter().map(|_| rng.standard_normal()).collect();
365    let u1: f64 = (0..DEGREES_OF_FREEDOM)
366        .map(|_| rng.standard_normal().powi(2))
367        .sum();
368    let z2: Vec<f64> = second.iter().map(|_| rng.standard_normal()).collect();
369    let u2: f64 = (0..DEGREES_OF_FREEDOM)
370        .map(|_| rng.standard_normal().powi(2))
371        .sum();
372    let (stretch1, stretch2) = (1.0 / (u1 / nu).sqrt(), 1.0 / (u2 / nu).sqrt());
373    let mut x = *peak;
374    for (row, &i) in first.iter().enumerate() {
375        x[i] = peak[i] + stretch1 * (0..=row).map(|k| l_first[row][k] * z1[k]).sum::<f64>();
376    }
377    let middle = inner_middle(&x).filter(|m| second.iter().all(|&i| m[i].is_finite()))?;
378    for (row, &i) in second.iter().enumerate() {
379        x[i] = middle[i] + stretch2 * (0..=row).map(|k| l_second[row][k] * z2[k]).sum::<f64>();
380    }
381    // Casts: counts of unknowns, far below 2⁵³.
382    let (k1, k2) = (first.len() as f64, second.len() as f64);
383    let distance1 = z1.iter().map(|v| v * v).sum::<f64>() * stretch1 * stretch1;
384    let distance2 = z2.iter().map(|v| v * v).sum::<f64>() * stretch2 * stretch2;
385    let log_proposal = if proposal_in_weight {
386        -0.5 * (nu + k1) * (distance1 / nu).ln_1p() - 0.5 * (nu + k2) * (distance2 / nu).ln_1p()
387    } else {
388        0.0
389    };
390    match log_target(&x) {
391        Some(log_p) if log_p == f64::NEG_INFINITY => Some(None),
392        Some(log_p) if (log_p - log_proposal).is_finite() => Some(Some(Drawn {
393            x,
394            middle,
395            log_weight: log_p - log_proposal,
396        })),
397        _ => None,
398    }
399}
400
401/// The lower triangle of the scale `L Lᵀ` widened to the spread `spread` where it reaches
402/// further: with `A = L⁻¹ spread L⁻ᵀ = V Λ Vᵀ`, the scale `L V max(Λ, 1) Vᵀ Lᵀ`, each eigenvalue
403/// held to [`MAX_WIDENING`]. `None` if that isn't positive definite.
404fn widened(l: &[Vec<f64>], spread: &[Vec<f64>]) -> Option<Vec<Vec<f64>>> {
405    let n = l.len();
406    if n == 0 {
407        return Some(Vec::new());
408    }
409    // A = L⁻¹ C L⁻ᵀ: solve L B = C by columns, then L Aᵀ = Bᵀ.
410    let b: Vec<Vec<f64>> = (0..n)
411        .map(|c| forward_solve(l, &spread.iter().map(|row| row[c]).collect::<Vec<_>>()))
412        .collect();
413    // b[c] is column c of L⁻¹ C; A's column r is L⁻¹ (row r of L⁻¹ C).
414    let rows: Vec<Vec<f64>> = (0..n).map(|r| (0..n).map(|c| b[c][r]).collect()).collect();
415    let raw: Vec<Vec<f64>> = rows.iter().map(|row| forward_solve(l, row)).collect();
416    // Symmetric to rounding: made so.
417    let a: Vec<Vec<f64>> = (0..n)
418        .map(|i| (0..n).map(|j| 0.5 * (raw[i][j] + raw[j][i])).collect())
419        .collect();
420    let (values, vectors) = jacobi_eigen(a);
421    // B = V max(Λ, 1) Vᵀ, then L B Lᵀ.
422    let held: Vec<f64> = values
423        .iter()
424        .map(|v| {
425            if v.is_finite() {
426                v.clamp(1.0, MAX_WIDENING)
427            } else {
428                1.0
429            }
430        })
431        .collect();
432    let mut m = vec![vec![0.0; n]; n];
433    for i in 0..n {
434        for j in 0..n {
435            m[i][j] = (0..n)
436                .map(|k| vectors[i][k] * held[k] * vectors[j][k])
437                .sum();
438        }
439    }
440    let mut scale = vec![vec![0.0; n]; n];
441    for i in 0..n {
442        for j in 0..n {
443            scale[i][j] = (0..=i)
444                .map(|p| (0..=j).map(|q| l[i][p] * m[p][q] * l[j][q]).sum::<f64>())
445                .sum();
446        }
447    }
448    cholesky_of(&scale)
449}
450
451/// The eigenvalues of the symmetric `a` and its eigenvectors as `V`'s columns, `a = V Λ Vᵀ`, by
452/// Jacobi's rotations (W. H. Press et al., *Numerical Recipes*, 3rd ed., 2007, §11.1), swept until
453/// what is off the diagonal is gone to rounding, or for 100 sweeps.
454fn jacobi_eigen(mut a: Vec<Vec<f64>>) -> (Vec<f64>, Vec<Vec<f64>>) {
455    let n = a.len();
456    let mut v: Vec<Vec<f64>> = (0..n)
457        .map(|i| (0..n).map(|j| if i == j { 1.0 } else { 0.0 }).collect())
458        .collect();
459    for _ in 0..100 {
460        let off: f64 = (0..n)
461            .flat_map(|i| (0..i).map(move |j| (i, j)))
462            .map(|(i, j)| a[i][j] * a[i][j])
463            .sum();
464        let whole: f64 = (0..n).map(|i| a[i][i] * a[i][i]).sum::<f64>() + 2.0 * off;
465        if off <= 1e-30 * whole || !off.is_finite() {
466            break;
467        }
468        for p in 0..n {
469            for q in p + 1..n {
470                if a[p][q] == 0.0 {
471                    continue;
472                }
473                let theta = 0.5 * (a[q][q] - a[p][p]) / a[p][q];
474                let t = theta.signum() / (theta.abs() + theta.hypot(1.0));
475                let t = if theta == 0.0 { 1.0 } else { t };
476                let c = 1.0 / t.hypot(1.0);
477                let s = t * c;
478                for row in &mut a {
479                    let (akp, akq) = (row[p], row[q]);
480                    row[p] = c * akp - s * akq;
481                    row[q] = s * akp + c * akq;
482                }
483                // Rows p and q, p < q.
484                let (before, from_q) = a.split_at_mut(q);
485                for (apk, aqk) in before[p].iter_mut().zip(from_q[0].iter_mut()) {
486                    let (x, y) = (*apk, *aqk);
487                    *apk = c * x - s * y;
488                    *aqk = s * x + c * y;
489                }
490                for row in &mut v {
491                    let (vkp, vkq) = (row[p], row[q]);
492                    row[p] = c * vkp - s * vkq;
493                    row[q] = s * vkp + c * vkq;
494                }
495            }
496        }
497    }
498    ((0..n).map(|i| a[i][i]).collect(), v)
499}
500
501/// The lower triangle `L` of `a = L Lᵀ`, `a` square; `None` if it isn't positive definite. An
502/// empty `a` gives an empty `L`.
503fn cholesky_of(a: &[Vec<f64>]) -> Option<Vec<Vec<f64>>> {
504    let n = a.len();
505    let mut l = vec![vec![0.0; n]; n];
506    for i in 0..n {
507        for j in 0..=i {
508            let sum: f64 = (0..j).map(|k| l[i][k] * l[j][k]).sum();
509            if i == j {
510                let d = a[i][i] - sum;
511                if d <= 0.0 || !d.is_finite() {
512                    return None;
513                }
514                l[i][i] = d.sqrt();
515            } else {
516                l[i][j] = (a[i][j] - sum) / l[j][j];
517            }
518        }
519    }
520    Some(l)
521}
522
523/// `y` with `L y = b`, `L` lower triangular.
524fn forward_solve(l: &[Vec<f64>], b: &[f64]) -> Vec<f64> {
525    let mut y = b.to_vec();
526    for i in 0..y.len() {
527        let sum: f64 = (0..i).map(|k| l[i][k] * y[k]).sum();
528        y[i] = (y[i] - sum) / l[i][i];
529    }
530    y
531}
532
533/// The posterior the weighted `points` give, each a point and its log weight, of `draws` draws of
534/// which `failed` failed.
535fn read_out<const N: usize>(
536    points: &[([f64; N], f64)],
537    draws: usize,
538    failed: usize,
539) -> Option<Posterior<N>> {
540    let top = points
541        .iter()
542        .map(|(_, w)| *w)
543        .fold(f64::NEG_INFINITY, f64::max);
544    if !top.is_finite() {
545        return None;
546    }
547    let weights: Vec<f64> = points.iter().map(|(_, w)| (w - top).exp()).collect();
548    let total: f64 = weights.iter().sum();
549    let squares: f64 = weights.iter().map(|w| w * w).sum();
550    let effective_draws = total * total / squares;
551    let quantiles = std::array::from_fn(|j| {
552        let mut column: Vec<(f64, f64)> = points
553            .iter()
554            .zip(&weights)
555            .map(|((x, _), w)| (x[j], *w))
556            .collect();
557        column.sort_by(|a, b| a.0.total_cmp(&b.0));
558        PROBABILITIES.map(|p| weighted_quantile(&column, total, p))
559    });
560    Some(Posterior {
561        quantiles,
562        effective_draws,
563        draws,
564        failed,
565        enough: effective_draws >= MIN_EFFECTIVE_DRAWS,
566    })
567}
568
569/// The least value of `sorted` (value, weight) pairs whose cumulative weight reaches `p` of `total`:
570/// the weighted empirical distribution's lower `p` quantile.
571fn weighted_quantile(sorted: &[(f64, f64)], total: f64, p: f64) -> f64 {
572    let goal = p * total;
573    let mut running = 0.0;
574    for &(value, weight) in sorted {
575        running += weight;
576        if running >= goal {
577            return value;
578        }
579    }
580    // Rounding can leave the running sum a hair under the total; the largest value is the answer.
581    sorted.last().map_or(f64::NAN, |&(value, _)| value)
582}
583
584#[cfg(test)]
585mod tests {
586    use super::*;
587    use hpr_flightdata::readings::interval::Z_90;
588
589    /// A Gaussian posterior with mean `mean` and standard deviations `sd`, uncorrelated.
590    fn gaussian<const N: usize>(
591        mean: [f64; N],
592        sd: [f64; N],
593    ) -> impl FnMut(&[f64; N]) -> Option<f64> {
594        move |x| {
595            Some(
596                -0.5 * (0..N)
597                    .map(|i| ((x[i] - mean[i]) / sd[i]).powi(2))
598                    .sum::<f64>(),
599            )
600        }
601    }
602
603    const GAUSSIAN_MEAN: [f64; 2] = [1.0, -2.0];
604    const GAUSSIAN_SD: [f64; 2] = [0.5, 2.0];
605    /// The Gaussian case's correlation, a strong one: a draw that lost it (`Lᵀ z` for `L z`)
606    /// misses the posterior's shape, which a diagonal case can't see.
607    const GAUSSIAN_CORRELATION: f64 = 0.9;
608
609    /// A correlated Gaussian posterior drawn from a proposal off center by 0.3 sd in each unknown
610    /// and 1.5 times too wide, as a Laplace approximation that missed would be.
611    fn gaussian_case(proposal_in_weight: bool) -> Posterior<2> {
612        let ([m0, m1], [s0, s1], r) = (GAUSSIAN_MEAN, GAUSSIAN_SD, GAUSSIAN_CORRELATION);
613        let target = move |x: &[f64; 2]| {
614            let (a, b) = ((x[0] - m0) / s0, (x[1] - m1) / s1);
615            Some(-0.5 * (a * a - 2.0 * r * a * b + b * b) / (1.0 - r * r))
616        };
617        let cross = 2.25 * r * s0 * s1;
618        sample_weighted(
619            &[1.15, -1.4],
620            &[[2.25 * s0 * s0, cross], [cross, 2.25 * s1 * s1]],
621            20_000,
622            7,
623            target,
624            proposal_in_weight,
625        )
626        .unwrap()
627    }
628
629    /// The farthest of the Gaussian case's points from its closed form, in standard deviations.
630    fn gaussian_worst(posterior: &Posterior<2>) -> f64 {
631        (0..2)
632            .flat_map(|i| {
633                let (mean, sd) = (GAUSSIAN_MEAN[i], GAUSSIAN_SD[i]);
634                let expected = [mean - Z_90 * sd, mean, mean + Z_90 * sd];
635                posterior.quantiles[i]
636                    .into_iter()
637                    .zip(expected)
638                    .map(move |(got, want)| ((got - want) / sd).abs())
639            })
640            .fold(0.0, f64::max)
641    }
642
643    #[test]
644    fn a_gaussian_posterior_comes_back_at_its_closed_form() {
645        // The 5% point's Monte Carlo error at n effective draws is about √(0.05·0.95/n)/φ(1.645)
646        // sd: 0.024 sd at n = 8,000. That formula, a weighted mean's, is optimistic for a
647        // quantile; over seeds 100 to 299 no case misses 0.06 sd, the worst 0.052 sd.
648        let posterior = gaussian_case(true);
649        assert!(
650            posterior.enough && posterior.effective_draws > 8_000.0,
651            "{}",
652            posterior.effective_draws
653        );
654        assert_eq!(posterior.failed, 0);
655        let worst = gaussian_worst(&posterior);
656        assert!(worst < 0.06, "{worst} sd");
657    }
658
659    /// The curved case's b: y = b x² + e.
660    const B: f64 = 0.5;
661
662    /// y = b x² + e, x and e standard normal: y's distribution is skewed, which a Laplace
663    /// approximation at the peak (0, 0), identity covariance, can't show.
664    fn curved_case(proposal_in_weight: bool) -> Posterior<2> {
665        let target =
666            move |v: &[f64; 2]| Some(-0.5 * v[0] * v[0] - 0.5 * (v[1] - B * v[0] * v[0]).powi(2));
667        sample_weighted(
668            &[0.0, 0.0],
669            &[[1.0, 0.0], [0.0, 1.0]],
670            160_000,
671            11,
672            target,
673            proposal_in_weight,
674        )
675        .unwrap()
676    }
677
678    /// The curved case's y at [`PROBABILITIES`], by quadrature, worked out once for every test:
679    /// its CDF is P(Y ≤ t) = ∫ φ(x) Φ(t − b x²) dx, by Simpson's rule on [−8, 8], bisected.
680    fn curved_truth() -> [f64; 3] {
681        static TRUTH: std::sync::OnceLock<[f64; 3]> = std::sync::OnceLock::new();
682        *TRUTH.get_or_init(|| {
683            let cdf = |t: f64| {
684                let n = 4_000;
685                let h = 16.0 / n as f64;
686                (0..=n)
687                    .map(|k| {
688                        let x = -8.0 + h * k as f64;
689                        let weight = if k == 0 || k == n {
690                            1.0
691                        } else if k % 2 == 1 {
692                            4.0
693                        } else {
694                            2.0
695                        };
696                        weight * (-0.5 * x * x).exp() / (2.0 * std::f64::consts::PI).sqrt()
697                            * normal_cdf(t - B * x * x)
698                    })
699                    .sum::<f64>()
700                    * h
701                    / 3.0
702            };
703            PROBABILITIES.map(|p| {
704                let (mut lo, mut hi) = (-10.0, 30.0);
705                for _ in 0..80 {
706                    let mid = 0.5 * (lo + hi);
707                    if cdf(mid) < p { lo = mid } else { hi = mid }
708                }
709                0.5 * (lo + hi)
710            })
711        })
712    }
713
714    /// The farthest of the curved case's points for y from its quadrature, in y's units.
715    fn curved_worst(posterior: &Posterior<2>) -> f64 {
716        posterior.quantiles[1]
717            .into_iter()
718            .zip(curved_truth())
719            .map(|(got, want)| (got - want).abs())
720            .fold(0.0, f64::max)
721    }
722
723    #[test]
724    fn a_curved_posterior_comes_back_at_its_quadrature() {
725        let posterior = curved_case(true);
726        // y's standard deviation is √(1 + 2b²) ≈ 1.2247, so 0.08 is 0.065 of it. y's 95% point
727        // sits in its long upper tail, where the density is low and the point's error is largest:
728        // over seeds 100 to 199 its error has a standard deviation of 0.018 at these draws, so 0.08
729        // is over four of those, and no seed takes any of the three points past it.
730        assert!(
731            posterior.enough && posterior.effective_draws > 80_000.0,
732            "{}",
733            posterior.effective_draws
734        );
735        let worst = curved_worst(&posterior);
736        assert!(
737            worst < 0.08,
738            "{:?} vs {:?}",
739            posterior.quantiles[1],
740            curved_truth()
741        );
742        // What a Laplace approximation can't show: the median stands above the peak's 0, and the
743        // 95% point farther above it than the 5% point is below.
744        let [low, mid, high] = posterior.quantiles[1];
745        assert!(
746            mid > 0.3 && high - mid > 1.1 * (mid - low),
747            "{low} {mid} {high}"
748        );
749        // The fit's page quotes these three, to two places ("a median of 0.43 and an interval of
750        // −1.35 to 2.57"); a change here needs the page changed too.
751        let rounded = [low, mid, high].map(|v| (v * 100.0).round() / 100.0);
752        assert_eq!(rounded, [-1.35, 0.43, 2.57]);
753    }
754
755    #[test]
756    fn both_closed_form_tests_fail_with_the_proposal_left_out_of_the_weights() {
757        // Weighing by the posterior alone reads the product of proposal and posterior, not the
758        // posterior: the probe that the two tests above can see a wrong weight. (The probe that
759        // they see a draw that lost its correlation, `Lᵀ z` for `L z`, was run by hand: 0.93 sd
760        // off with 69 effective draws, so the Gaussian test fails it twice over.)
761        let gaussian = gaussian_worst(&gaussian_case(false));
762        assert!(gaussian >= 0.06, "{gaussian} sd");
763        let curved = curved_worst(&curved_case(false));
764        assert!(curved >= 0.08, "{curved}");
765    }
766
767    /// Φ by Simpson's rule on φ from −9, where Φ is below 10⁻¹⁸.
768    fn normal_cdf(t: f64) -> f64 {
769        if t < -9.0 {
770            return 0.0;
771        }
772        let n = 2_000;
773        let h = (t + 9.0) / n as f64;
774        (0..=n)
775            .map(|k| {
776                let x = -9.0 + h * k as f64;
777                let weight = if k == 0 || k == n {
778                    1.0
779                } else if k % 2 == 1 {
780                    4.0
781                } else {
782                    2.0
783                };
784                weight * (-0.5 * x * x).exp()
785            })
786            .sum::<f64>()
787            * h
788            / 3.0
789            / (2.0 * std::f64::consts::PI).sqrt()
790    }
791
792    #[test]
793    fn a_draw_that_cant_be_had_has_no_weight_and_is_counted() {
794        // Where the first unknown passes 1, the posterior can't be had, as a flight that won't fly.
795        let mut inner = gaussian([0.0, 0.0], [1.0, 1.0]);
796        let posterior = sample(&[0.0, 0.0], &[[1.0, 0.0], [0.0, 1.0]], 5_000, 3, |x| {
797            if x[0] > 1.0 { None } else { inner(x) }
798        })
799        .unwrap();
800        assert!(posterior.failed > 500, "{}", posterior.failed);
801        assert_eq!(posterior.draws, 5_000);
802        // The first unknown is then a normal cut at 1: its 95% point is Φ⁻¹(0.95 Φ(1)) ≈ 0.839.
803        // Over seeds 100 to 299 this point's error has a standard deviation of 0.011 and is at most
804        // 0.031, so 0.04 is over three and a half standard deviations, and no seed misses it.
805        let (mut lo, mut hi) = (-5.0, 1.0);
806        for _ in 0..60 {
807            let mid = 0.5 * (lo + hi);
808            if normal_cdf(mid) < 0.95 * normal_cdf(1.0) {
809                lo = mid
810            } else {
811                hi = mid
812            }
813        }
814        let truth = 0.5 * (lo + hi);
815        assert!(
816            (posterior.quantiles[0][2] - truth).abs() < 0.04,
817            "{} vs {truth}",
818            posterior.quantiles[0][2]
819        );
820    }
821
822    #[test]
823    fn a_draw_where_the_posterior_is_zero_has_no_weight_and_isnt_a_failure() {
824        let mut inner = gaussian([0.0, 0.0], [1.0, 1.0]);
825        let posterior = sample(&[0.0, 0.0], &[[1.0, 0.0], [0.0, 1.0]], 5_000, 3, |x| {
826            if x[0] > 1.0 {
827                Some(f64::NEG_INFINITY)
828            } else {
829                inner(x)
830            }
831        })
832        .unwrap();
833        assert_eq!(posterior.failed, 0);
834        assert!(posterior.quantiles[0][2] <= 1.0);
835    }
836
837    #[test]
838    fn a_posterior_reads_back_as_written_and_refuses_the_wrong_count() {
839        let posterior = sample(
840            &[0.0, 1.0],
841            &[[1.0, 0.3], [0.3, 2.0]],
842            500,
843            1,
844            gaussian([0.1, 0.9], [0.8, 1.2]),
845        )
846        .unwrap();
847        let json = serde_json::to_string(&posterior).unwrap();
848        assert_eq!(
849            serde_json::from_str::<Posterior<2>>(&json).unwrap(),
850            posterior
851        );
852        assert!(serde_json::from_str::<Posterior<3>>(&json).is_err());
853    }
854
855    #[test]
856    fn a_proposal_far_from_the_posterior_says_its_draws_are_too_few() {
857        // The posterior sits 6 proposal sd away and 20 times narrower: few draws land on it.
858        let posterior = sample(&[0.0], &[[1.0]], 2_000, 5, gaussian([6.0], [0.05])).unwrap();
859        assert!(!posterior.enough, "{}", posterior.effective_draws);
860    }
861
862    /// The curved case in two blocks: `v₀` first, then `v₁` about `middle(v₀)`.
863    fn curved_in_two(middle: fn(f64) -> f64, stages: Stages) -> Posterior<2> {
864        let target =
865            move |v: &[f64; 2]| Some(-0.5 * v[0] * v[0] - 0.5 * (v[1] - B * v[0] * v[0]).powi(2));
866        sample_in_two_with(
867            &[0.0, 0.0],
868            &[[1.0, 0.0], [0.0, 1.0]],
869            &[true, false],
870            13,
871            stages,
872            |v| Some([v[0], middle(v[0])]),
873            target,
874        )
875        .unwrap()
876    }
877
878    const IN_TWO: Stages = Stages {
879        pilot: 2_000,
880        draws: 40_000,
881        adapt: true,
882        proposal_in_weight: true,
883    };
884
885    #[test]
886    fn in_two_blocks_a_curved_posterior_comes_back_at_its_quadrature() {
887        // With the conditional's own middle, `B v₀²`, the draws follow the curve: far more of them
888        // count than the one Student-t's about the peak keeps on the same posterior.
889        let exact = curved_in_two(|v0| B * v0 * v0, IN_TWO);
890        // Over seeds 0 to 199 (measured in development) the effective draws were at least 35,121
891        // of 40,000, and the worst point's error had a standard deviation of 0.0068 and was at most
892        // 0.039: 0.08 is over ten of those.
893        assert!(
894            exact.effective_draws > 0.75 * 40_000.0,
895            "{}",
896            exact.effective_draws
897        );
898        assert_eq!(exact.draws, 42_000);
899        assert_eq!(exact.failed, 0);
900        assert!(curved_worst(&exact) < 0.08, "{:?}", exact.quantiles[1]);
901        // A middle that ignores the curve still weighs to the posterior: it costs draws only. Over
902        // the same seeds it kept at most 0.70 of the exact middle's effective draws, and its worst
903        // error had a standard deviation of 0.020 and was at most 0.13.
904        let poor = curved_in_two(|_| 0.0, IN_TWO);
905        assert!(poor.effective_draws < 0.75 * exact.effective_draws);
906        assert!(curved_worst(&poor) < 0.15, "{:?}", poor.quantiles[1]);
907    }
908
909    #[test]
910    fn in_two_blocks_the_proposal_must_be_in_the_weights() {
911        let probe = curved_in_two(
912            |v0| B * v0 * v0,
913            Stages {
914                proposal_in_weight: false,
915                ..IN_TWO
916            },
917        );
918        // Over seeds 0 to 199 its worst error was at least 0.93, past the poor middle's 0.15.
919        assert!(curved_worst(&probe) > 0.5, "{:?}", probe.quantiles[1]);
920    }
921
922    /// A Gaussian of standard deviations `sd`, uncorrelated, drawn in two blocks (the first
923    /// unknown, then the other two) from a unit scale, so that each unknown is `sd` times as wide
924    /// as the scale says.
925    fn gaussian_in_two(sd: [f64; 3], adapt: bool, seed: u64) -> Posterior<3> {
926        sample_in_two_with(
927            &[0.0; 3],
928            &[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
929            &[true, false, false],
930            seed,
931            Stages {
932                pilot: 1_000,
933                draws: 4_000,
934                adapt,
935                proposal_in_weight: true,
936            },
937            |v| Some([v[0], 0.0, 0.0]),
938            gaussian([0.0; 3], sd),
939        )
940        .unwrap()
941    }
942
943    /// The farthest of a centered Gaussian's points from its closed form, in its standard
944    /// deviations.
945    fn centered_worst<const N: usize>(posterior: &Posterior<N>, sd: [f64; N]) -> f64 {
946        (0..N)
947            .flat_map(|i| {
948                let expected = [-Z_90 * sd[i], 0.0, Z_90 * sd[i]];
949                posterior.quantiles[i]
950                    .into_iter()
951                    .zip(expected)
952                    .map(move |(got, want)| ((got - want) / sd[i]).abs())
953            })
954            .fold(0.0, f64::max)
955    }
956
957    #[test]
958    fn the_pilot_widens_a_scale_where_the_posterior_reaches_further() {
959        // The second unknown is 1.8 times as wide as the scale says, the others as it says.
960        let sd = [1.0, 1.8, 1.0];
961        let adapted = gaussian_in_two(sd, true, 21);
962        let fixed = gaussian_in_two(sd, false, 21);
963        // Widened, the draws reach it: over seeds 0 to 199, at least 1.36 times as many effective
964        // draws (measured in development).
965        assert!(
966            adapted.effective_draws > 1.3 * fixed.effective_draws,
967            "{} against {}",
968            adapted.effective_draws,
969            fixed.effective_draws
970        );
971        // Over the same seeds the worst point's error had a standard deviation of 0.016 and was at
972        // most 0.10, in the 1.8-wide unknown's tails.
973        assert!(
974            centered_worst(&adapted, sd) < 0.12,
975            "{:?}",
976            adapted.quantiles
977        );
978        assert_eq!(adapted.draws, 5_000);
979    }
980
981    #[test]
982    fn the_pilot_never_narrows_a_scale_and_holds_its_widening() {
983        // A scale twice too wide stays as it is: the posterior's points still come back.
984        let narrow = [0.5, 0.5, 0.5];
985        let adapted = gaussian_in_two(narrow, true, 22);
986        let fixed = gaussian_in_two(narrow, false, 22);
987        assert_eq!(
988            adapted.effective_draws.to_bits(),
989            fixed.effective_draws.to_bits()
990        );
991        // The widening is held to MAX_WIDENING in variance, so a spread of 10² in one direction
992        // gives a scale of 2 there, not 10.
993        let l = widened(
994            &[vec![1.0, 0.0], vec![0.0, 1.0]],
995            &[vec![100.0, 0.0], vec![0.0, 0.25]],
996        )
997        .unwrap();
998        assert!(
999            (l[0][0] - 2.0).abs() < 1e-12 && (l[1][1] - 1.0).abs() < 1e-12,
1000            "{l:?}"
1001        );
1002        assert!(l[1][0].abs() < 1e-12);
1003    }
1004
1005    #[test]
1006    fn jacobi_gives_each_eigenvalue_and_its_vector() {
1007        let a = vec![
1008            vec![4.0, 1.0, -2.0, 0.5],
1009            vec![1.0, 3.0, 0.0, 1.5],
1010            vec![-2.0, 0.0, 5.0, -1.0],
1011            vec![0.5, 1.5, -1.0, 2.0],
1012        ];
1013        let (values, v) = jacobi_eigen(a.clone());
1014        for i in 0..4 {
1015            for j in 0..4 {
1016                let back: f64 = (0..4).map(|k| v[i][k] * values[k] * v[j][k]).sum();
1017                assert!((back - a[i][j]).abs() < 1e-12, "{i} {j}: {back}");
1018                let dot: f64 = (0..4).map(|k| v[k][i] * v[k][j]).sum();
1019                let unit = if i == j { 1.0 } else { 0.0 };
1020                assert!((dot - unit).abs() < 1e-12);
1021            }
1022        }
1023    }
1024
1025    #[test]
1026    fn in_two_blocks_a_failed_middle_is_counted_and_the_seed_holds() {
1027        let run = |seed| {
1028            sample_in_two(
1029                &[0.0, 0.0],
1030                &[[1.0, 0.0], [0.0, 1.0]],
1031                &[true, false],
1032                200,
1033                800,
1034                seed,
1035                |v| (v[0] < 1.0).then_some([v[0], 0.0]),
1036                gaussian([0.0, 0.0], [1.0, 1.0]),
1037            )
1038        };
1039        let posterior = run(4).unwrap();
1040        assert_eq!(posterior.draws, 1_000);
1041        // About a Student-t's share past 1 of its scale, 18.7% of both stages' draws: from 159 to
1042        // 215 over seeds 0 to 199 (measured in development).
1043        assert!(
1044            (120..260).contains(&posterior.failed),
1045            "{}",
1046            posterior.failed
1047        );
1048        let bits = |p: Option<Posterior<2>>| {
1049            p.map(|p| {
1050                (
1051                    p.quantiles.map(|row| row.map(f64::to_bits)),
1052                    p.effective_draws.to_bits(),
1053                    p.failed,
1054                )
1055            })
1056        };
1057        assert_eq!(bits(run(4)), bits(run(4)));
1058        assert_ne!(bits(run(4)), bits(run(5)));
1059        // A scale that isn't positive definite, in either block, gives none.
1060        let given = |c: [[f64; 2]; 2]| {
1061            sample_in_two(
1062                &[0.0, 0.0],
1063                &c,
1064                &[true, false],
1065                10,
1066                10,
1067                1,
1068                |v| Some(*v),
1069                gaussian([0.0, 0.0], [1.0, 1.0]),
1070            )
1071        };
1072        assert!(given([[-1.0, 0.0], [0.0, 1.0]]).is_none());
1073        assert!(given([[1.0, 0.0], [1.0, 1.0]]).is_none());
1074        assert!(given([[1.0, 0.0], [0.0, 1.0]]).is_some());
1075    }
1076
1077    #[test]
1078    fn the_same_seed_gives_the_same_answer_and_nothing_gives_none() {
1079        let run = |seed| {
1080            sample(
1081                &[0.0, 1.0],
1082                &[[1.0, 0.3], [0.3, 2.0]],
1083                500,
1084                seed,
1085                gaussian([0.1, 0.9], [0.8, 1.2]),
1086            )
1087        };
1088        // Every field, the floats by their bits.
1089        let bits = |p: Option<Posterior<2>>| {
1090            p.map(|p| {
1091                (
1092                    p.quantiles.map(|row| row.map(f64::to_bits)),
1093                    p.effective_draws.to_bits(),
1094                    p.draws,
1095                    p.failed,
1096                    p.enough,
1097                )
1098            })
1099        };
1100        assert_eq!(bits(run(1)), bits(run(1)));
1101        assert_ne!(bits(run(1)), bits(run(2)));
1102        assert!(sample(&[0.0], &[[-1.0]], 10, 1, gaussian([0.0], [1.0])).is_none());
1103        assert!(sample(&[f64::NAN], &[[1.0]], 10, 1, gaussian([0.0], [1.0])).is_none());
1104        assert!(sample(&[0.0], &[[1.0]], 0, 1, gaussian([0.0], [1.0])).is_none());
1105        assert!(sample(&[0.0], &[[1.0]], 10, 1, |_: &[f64; 1]| None).is_none());
1106    }
1107}