Skip to main content

gam_geometry/
response_geometry.rs

1//! User-selectable response geometries beyond Sphere and Simplex.
2//!
3//! The fit DSL exposes `response_geometry="..."`: one scalar Gaussian GAM is
4//! fitted per tangent coordinate at a fixed base point (the intrinsic Fréchet
5//! mean when none is supplied), and predictions are mapped back to the manifold
6//! by the exponential map. Sphere and Simplex have bespoke batched wrappers in
7//! their own modules; this module supplies the same `(values 2-D, base 1-D) →
8//! tangent 2-D` / `(tangent 2-D, base 1-D) → values 2-D` contract for the
9//! curved matrix manifolds whose per-point math is already wired in
10//! [`crate::geometry`] but which were never reachable as a *fittable* response
11//! geometry: the SPD cone `Sym⁺(n)`, the Grassmannian `Gr(k, n)`, the Stiefel
12//! manifold `St(k, n)`, and the Poincaré ball `B^d_κ`.
13//!
14//! Every primitive here delegates to the canonical landed math
15//! ([`RiemannianManifold::exp_map`]/[`log_map`](RiemannianManifold::log_map) and
16//! the Poincaré [`exp_map`](crate::manifolds::poincare::exp_map)/[`log_map`](crate::manifolds::poincare::log_map));
17//! the only new code is the batched row loop, the base-point dimension wiring,
18//! and a generic Riemannian Karcher (Fréchet) mean shared by all four. There is
19//! no separate per-manifold mean: the SPD safeguarded Karcher iteration is
20//! generalised once, over the metric supplied by
21//! [`RiemannianManifold::metric_tensor`], so adding a curved response geometry
22//! is a single resolver arm.
23
24use ndarray::{Array1, Array2, ArrayView1, ArrayView2};
25use opt::{BacktrackConfig, armijo_roundoff_cushion, backtracking_line_search, constants};
26use std::{convert::Infallible, fmt};
27
28use crate::manifold::{
29    GEOMETRY_EPS, RiemannianManifold, flatten, from_flat, jacobi_symmetric, spectral_map_symmetric,
30    sym,
31};
32use crate::manifolds::constant_curvature::{ConstantCurvature, cs_stacks3, distance_kappa_jet};
33use crate::{GeometryError, GeometryResult, GrassmannManifold, SpdManifold, StiefelManifold};
34
35/// Split a parenthesised `key=value, key=value` parameter list into trimmed,
36/// lower-cased `(key, value)` pairs. An empty list is valid (`spd()`).
37fn parse_kv(inner: &str) -> Result<Vec<(String, String)>, String> {
38    let trimmed = inner.trim();
39    if trimmed.is_empty() {
40        return Ok(Vec::new());
41    }
42    let mut out = Vec::new();
43    for piece in trimmed.split(',') {
44        let piece = piece.trim();
45        if piece.is_empty() {
46            continue;
47        }
48        let (k, v) = piece
49            .split_once('=')
50            .ok_or_else(|| format!("response_geometry parameter {piece:?} must be key=value"))?;
51        out.push((k.trim().to_ascii_lowercase(), v.trim().to_string()));
52    }
53    Ok(out)
54}
55
56/// A fittable curved response geometry. Each variant carries the shape the user
57/// requested; the embedding/ambient flat dimension is fixed by that shape and
58/// is the column count of the `values` matrix the caller supplies.
59#[derive(Debug, Clone, Copy, PartialEq)]
60pub enum ResponseManifold {
61    /// Symmetric positive-definite `n×n` matrices, flattened row-major to `n²`
62    /// ambient coordinates (the layout [`SpdManifold`] uses).
63    Spd { n: usize },
64    /// `k`-dimensional subspaces of `ℝⁿ`, represented by an orthonormal `n×k`
65    /// frame flattened to `n·k` ambient coordinates.
66    Grassmann { k: usize, n: usize },
67    /// Orthonormal `k`-frames in `ℝⁿ`, flattened to `n·k` ambient coordinates.
68    Stiefel { k: usize, n: usize },
69    /// The Poincaré ball of dimension `d` with curvature `κ < 0`.
70    Poincare { dim: usize, curvature: f64 },
71    /// Constant-curvature manifold `M_κ` of dimension `d` with curvature `κ`
72    /// (any finite real value). `κ > 0` → spherical, `κ = 0` → flat (Euclidean
73    /// up to scale), `κ < 0` → hyperbolic (Poincaré ball). Unlike `Poincare`,
74    /// which fixes `κ < 0`, this variant accepts any curvature including zero
75    /// and positive values, and is the target for curvature-as-estimand fits
76    /// where `κ̂` is optimized over all of ℝ (#1104).
77    ConstantCurvature { dim: usize, kappa: f64 },
78}
79
80impl ResponseManifold {
81    /// Resolve a lower-cased geometry label and its shape parameters into a
82    /// response manifold. Shape parameters are passed positionally exactly as
83    /// the FFI marshals them; absent/zero values are rejected here so the error
84    /// surfaces at selection time rather than mid-fit.
85    ///
86    /// - `"spd"` needs `n` (matrix side).
87    /// - `"grassmann"` / `"stiefel"` need `k` and `n` with `1 ≤ k ≤ n`.
88    /// - `"poincare"` needs `dim` and a strictly negative `curvature`.
89    pub fn resolve(
90        kind: &str,
91        n: Option<usize>,
92        k: Option<usize>,
93        dim: Option<usize>,
94        curvature: Option<f64>,
95    ) -> Result<Self, String> {
96        match kind {
97            "spd" => {
98                let n = n.ok_or_else(|| "response_geometry='spd' requires n".to_string())?;
99                if n == 0 {
100                    return Err("response_geometry='spd' requires n >= 1".to_string());
101                }
102                Ok(Self::Spd { n })
103            }
104            "grassmann" => {
105                let k = k.ok_or_else(|| "response_geometry='grassmann' requires k".to_string())?;
106                let n = n.ok_or_else(|| "response_geometry='grassmann' requires n".to_string())?;
107                if k == 0 || n == 0 || k > n {
108                    return Err("response_geometry='grassmann' requires 1 <= k <= n".to_string());
109                }
110                Ok(Self::Grassmann { k, n })
111            }
112            "stiefel" => {
113                let k = k.ok_or_else(|| "response_geometry='stiefel' requires k".to_string())?;
114                let n = n.ok_or_else(|| "response_geometry='stiefel' requires n".to_string())?;
115                if k == 0 || n == 0 || k > n {
116                    return Err("response_geometry='stiefel' requires 1 <= k <= n".to_string());
117                }
118                Ok(Self::Stiefel { k, n })
119            }
120            "poincare" => {
121                let dim =
122                    dim.ok_or_else(|| "response_geometry='poincare' requires dim".to_string())?;
123                if dim == 0 {
124                    return Err("response_geometry='poincare' requires dim >= 1".to_string());
125                }
126                let curvature = curvature
127                    .ok_or_else(|| "response_geometry='poincare' requires curvature".to_string())?;
128                if !(curvature.is_finite() && curvature < 0.0) {
129                    return Err(
130                        "response_geometry='poincare' requires finite curvature < 0".to_string()
131                    );
132                }
133                Ok(Self::Poincare { dim, curvature })
134            }
135            "constant_curvature" => {
136                let dim = dim.ok_or_else(|| {
137                    "response_geometry='constant_curvature' requires dim".to_string()
138                })?;
139                if dim == 0 {
140                    return Err(
141                        "response_geometry='constant_curvature' requires dim >= 1".to_string()
142                    );
143                }
144                // curvature defaults to 0 (flat) when not supplied — the user can
145                // supply any finite value; the κ-estimand outer loop will optimize it.
146                let kappa = curvature.unwrap_or(0.0);
147                if !kappa.is_finite() {
148                    return Err(
149                        "response_geometry='constant_curvature' requires finite curvature"
150                            .to_string(),
151                    );
152                }
153                Ok(Self::ConstantCurvature { dim, kappa })
154            }
155            other => Err(format!(
156                "response_geometry must be one of 'spd', 'grassmann', 'stiefel', 'poincare', \
157                 'constant_curvature', 'spherical', or 'simplex'; got {other:?}"
158            )),
159        }
160    }
161
162    /// Parse a user-facing `response_geometry` label, magic-by-default: the head
163    /// is the geometry name, an optional parenthesised `key=value` list carries
164    /// shape parameters, and anything not given is inferred from the ambient
165    /// column count `cols` of the response matrix.
166    ///
167    /// Recognised forms (case-insensitive, whitespace tolerant):
168    /// - `"spd"` — `n = √cols` (must be a perfect square).
169    /// - `"grassmann(k=2)"` or `"grassmann(k=2,n=5)"` — `n` defaults to
170    ///   `cols / k`; `k` is required (it cannot be inferred from `n·k`).
171    /// - `"stiefel(k=2)"` / `"stiefel(k=2,n=5)"` — same inference as Grassmann.
172    /// - `"poincare"` or `"poincare(curvature=-0.5)"` — `dim = cols`; curvature
173    ///   defaults to `-1.0`.
174    ///
175    /// This is the single mapping from the formula-DSL string to a constructed
176    /// response manifold; the FFI passes the raw label straight through.
177    pub fn parse(label: &str, cols: usize) -> Result<Self, String> {
178        let lowered = label.trim().to_ascii_lowercase();
179        let (head, params) = match lowered.split_once('(') {
180            Some((h, rest)) => {
181                let rest = rest.trim_end();
182                let inner = rest
183                    .strip_suffix(')')
184                    .ok_or_else(|| format!("response_geometry {label:?}: missing closing ')'"))?;
185                (h.trim().to_string(), parse_kv(inner)?)
186            }
187            None => (lowered.clone(), Vec::new()),
188        };
189        let get_usize = |key: &str| -> Result<Option<usize>, String> {
190            for (k, v) in &params {
191                if k == key {
192                    let parsed: usize = v.parse().map_err(|_| {
193                        format!("response_geometry {label:?}: {key} must be a non-negative integer")
194                    })?;
195                    return Ok(Some(parsed));
196                }
197            }
198            Ok(None)
199        };
200        let get_f64 = |key: &str| -> Result<Option<f64>, String> {
201            for (k, v) in &params {
202                if k == key {
203                    let parsed: f64 = v.parse().map_err(|_| {
204                        format!("response_geometry {label:?}: {key} must be a real number")
205                    })?;
206                    return Ok(Some(parsed));
207                }
208            }
209            Ok(None)
210        };
211
212        match head.as_str() {
213            "spd" => {
214                let n = match get_usize("n")? {
215                    Some(n) => n,
216                    None => {
217                        let r = (cols as f64).sqrt().round() as usize;
218                        if r * r != cols {
219                            return Err(format!(
220                                "response_geometry='spd': {cols} response columns is not a perfect \
221                                 square; pass spd(n=...) explicitly"
222                            ));
223                        }
224                        r
225                    }
226                };
227                Self::resolve("spd", Some(n), None, None, None)
228            }
229            "grassmann" | "stiefel" => {
230                let k = get_usize("k")?.ok_or_else(|| {
231                    format!("response_geometry='{head}' requires k, e.g. {head}(k=2)")
232                })?;
233                let n = match get_usize("n")? {
234                    Some(n) => n,
235                    None => {
236                        if k == 0 || cols % k != 0 {
237                            return Err(format!(
238                                "response_geometry='{head}': {cols} response columns is not \
239                                 divisible by k={k}; pass {head}(k=..,n=..) explicitly"
240                            ));
241                        }
242                        cols / k
243                    }
244                };
245                Self::resolve(&head, Some(n), Some(k), None, None)
246            }
247            "poincare" => {
248                let dim = get_usize("dim")?.unwrap_or(cols);
249                let curvature = get_f64("curvature")?.unwrap_or(-1.0);
250                Self::resolve("poincare", None, None, Some(dim), Some(curvature))
251            }
252            "constant_curvature" => {
253                let dim = get_usize("dim")?.unwrap_or(cols);
254                // κ defaults to 0 (flat initial point for the REML optimizer).
255                let kappa = get_f64("kappa")?
256                    .or_else(|| get_f64("curvature").ok().flatten())
257                    .unwrap_or(0.0);
258                Self::resolve("constant_curvature", None, None, Some(dim), Some(kappa))
259            }
260            other => Err(format!(
261                "response_geometry must be one of 'spd', 'grassmann(k=..)', 'stiefel(k=..)', \
262                 'poincare', 'constant_curvature', 'spherical', or 'simplex'; got {other:?}"
263            )),
264        }
265    }
266
267    /// Canonical, fully-specified label echoed back to the caller (mirrors the
268    /// way the sphere/simplex dispatch reports its resolved coordinate label).
269    pub fn canonical_label(&self) -> String {
270        match self {
271            Self::Spd { n } => format!("spd(n={n})"),
272            Self::Grassmann { k, n } => format!("grassmann(k={k},n={n})"),
273            Self::Stiefel { k, n } => format!("stiefel(k={k},n={n})"),
274            Self::Poincare { dim, curvature } => {
275                format!("poincare(dim={dim},curvature={curvature})")
276            }
277            Self::ConstantCurvature { dim, kappa } => {
278                format!("constant_curvature(dim={dim},kappa={kappa})")
279            }
280        }
281    }
282
283    /// Ambient (flattened) coordinate count: the column width of the `values`
284    /// matrix and the `base` vector.
285    pub fn ambient_dim(&self) -> usize {
286        match self {
287            Self::Spd { n } => n * n,
288            Self::Grassmann { k, n } | Self::Stiefel { k, n } => n * k,
289            Self::Poincare { dim, .. } | Self::ConstantCurvature { dim, .. } => *dim,
290        }
291    }
292
293    /// Radius of a geodesic support ball that certifies a stationary Karcher
294    /// point as the unique global Fréchet mean. `None` denotes a Hadamard
295    /// geometry, where squared distance is globally geodesically convex and no
296    /// finite support-radius gate is needed.
297    ///
298    /// The positive-curvature radii are the conservative strong-convexity bound
299    /// `½ min(inj_lower, π/(2√K_max))`, specialized to each canonical metric:
300    /// `K_max=1` for projective/spherical `k=1`, `K_max=2` for Grassmann,
301    /// `K_max=5/4` for canonical Stiefel, and `K_max=κ` for a spherical
302    /// constant-curvature response. These are geometric invariants, not solver
303    /// tuning knobs.
304    fn frechet_uniqueness_radius(&self) -> Option<f64> {
305        match self {
306            Self::Spd { .. } | Self::Poincare { .. } => None,
307            Self::Grassmann { k: 1, .. } | Self::Stiefel { k: 1, .. } => {
308                Some(std::f64::consts::FRAC_PI_4)
309            }
310            Self::Grassmann { .. } => Some(std::f64::consts::PI / (4.0 * 2.0_f64.sqrt())),
311            Self::Stiefel { .. } => Some(std::f64::consts::PI / (2.0 * 5.0_f64.sqrt())),
312            Self::ConstantCurvature { kappa, .. } if *kappa > 0.0 => {
313                Some(std::f64::consts::PI / (4.0 * kappa.sqrt()))
314            }
315            Self::ConstantCurvature { .. } => None,
316        }
317    }
318
319    /// Build the underlying [`RiemannianManifold`] for the matrix geometries.
320    /// `None` for Poincaré, whose primitives are free functions parameterised
321    /// by curvature rather than a trait object.
322    fn riemannian(&self) -> Option<Box<dyn RiemannianManifold>> {
323        match self {
324            Self::Spd { n } => Some(Box::new(SpdManifold::new(*n))),
325            Self::Grassmann { k, n } => GrassmannManifold::new(*k, *n)
326                .ok()
327                .map(|m| Box::new(m) as _),
328            Self::Stiefel { k, n } => StiefelManifold::new(*k, *n).ok().map(|m| Box::new(m) as _),
329            Self::ConstantCurvature { dim, kappa } => {
330                Some(Box::new(ConstantCurvature::new(*dim, *kappa)))
331            }
332            Self::Poincare { .. } => None,
333        }
334    }
335
336    /// Per-point logarithm `log_base(value)` in flat ambient coordinates.
337    fn log_point(
338        &self,
339        base: ArrayView1<'_, f64>,
340        value: ArrayView1<'_, f64>,
341    ) -> GeometryResult<Array1<f64>> {
342        match self {
343            Self::Poincare { curvature, .. } => {
344                crate::manifolds::poincare::log_map(base, value, *curvature)
345            }
346            // #2351: the constant-curvature response chart identifies its
347            // origin with the base point, so the logarithm evaluates in the
348            // base-centred frame: log_0(value − base). At the origin the
349            // Möbius denominator is identically 1, killing the off-origin
350            // κ>0 antipodal singularity that crashed prediction for
351            // ordinary sphere-patch data. This matches the criterion, which
352            // scores the same centred coordinates.
353            Self::ConstantCurvature { dim, kappa } => {
354                let chart = ConstantCurvature::new(*dim, *kappa);
355                let origin = Array1::<f64>::zeros(*dim);
356                let centred = &value.to_owned() - &base;
357                chart.log_map(origin.view(), centred.view())
358            }
359            Self::Spd { .. } | Self::Grassmann { .. } | Self::Stiefel { .. } => self
360                .riemannian()
361                .expect("riemannian response manifold")
362                .log_map(base, value),
363        }
364    }
365
366    /// Per-point exponential `exp_base(tangent)` in flat ambient coordinates.
367    fn exp_point(
368        &self,
369        base: ArrayView1<'_, f64>,
370        tangent: ArrayView1<'_, f64>,
371    ) -> GeometryResult<Array1<f64>> {
372        match self {
373            Self::Poincare { curvature, .. } => {
374                crate::manifolds::poincare::exp_map(base, tangent, *curvature)
375            }
376            // #2351: exact inverse of the centred logarithm above —
377            // exp_0(tangent) + base. Round-trips exactly with log_point.
378            Self::ConstantCurvature { dim, kappa } => {
379                let chart = ConstantCurvature::new(*dim, *kappa);
380                let origin = Array1::<f64>::zeros(*dim);
381                let centred = chart.exp_map(origin.view(), tangent)?;
382                Ok(centred + &base)
383            }
384            Self::Spd { .. } | Self::Grassmann { .. } | Self::Stiefel { .. } => self
385                .riemannian()
386                .expect("riemannian response manifold")
387                .exp_map(base, tangent),
388        }
389    }
390
391    /// Euclidean / Frobenius distance from an arbitrary ambient row to the
392    /// candidate response geometry, in flat ambient coordinates — the extrinsic
393    /// constraint-violation distance behind [`response_projection_residual`].
394    ///
395    /// Unlike [`log_point`](Self::log_point), which is gatekept to *genuine*
396    /// manifold points on both arguments, this accepts off-manifold `value`. The
397    /// distance is computed in closed form per geometry and is **well-defined for
398    /// every input** — there is no rank-deficiency error path, because the
399    /// distance to a set is defined even where the nearest point is not unique:
400    ///
401    /// * `Gr(k, n)` / `St(k, n)` — distance to the orthonormal-frame set,
402    ///   `√Σ_i (σ_i − 1)²` with `σ_i = √max(λ_i(YᵀY), 0)` the singular values of
403    ///   the `n × k` frame `Y`. Exact for every rank (`σ_i = 0` columns
404    ///   contribute `1` each). Grassmann and Stiefel coincide because this module
405    ///   represents Grassmann points by frames — it is a *representation*
406    ///   distance, not a subspace/principal-angle distance.
407    /// * SPD cone — distance to the *closed* PSD cone,
408    ///   `√(‖skew(A)‖_F² + Σ_{λ_i<0} λ_i²)` with `λ_i` the eigenvalues of the
409    ///   symmetric part `sym(A)`. This is the infimum distance to the open SPD
410    ///   cone; a zero distance means PSD, **not** strictly PD.
411    /// * Poincaré ball — distance to the *manifold* open ball of radius
412    ///   `R = 1/√(−c)`: `max(0, ‖x‖ − R)`. (This uses the true radius `R`, not
413    ///   the slightly smaller numerical safety radius used when projecting points
414    ///   for a fit, so interior points score exactly zero.)
415    /// * `ConstantCurvature` — distance to the chart *domain*: `0` for `κ ≥ 0`
416    ///   (chart is all of `ℝ^d`), else `max(0, ‖x‖ − 1/√(−κ))`. The curvature
417    ///   lives in the metric, not the domain, so this is a domain-admissibility
418    ///   check only and carries little curvature information.
419    fn manifold_residual(&self, value: ArrayView1<'_, f64>) -> GeometryResult<f64> {
420        match self {
421            Self::Poincare { curvature, .. } => ball_domain_residual(value, *curvature),
422            Self::ConstantCurvature { kappa, .. } => {
423                if *kappa >= 0.0 {
424                    Ok(0.0)
425                } else {
426                    ball_domain_residual(value, *kappa)
427                }
428            }
429            Self::Spd { n } => {
430                let mat = from_flat(value, *n, *n)?;
431                let symm = sym(&mat);
432                let psd = spectral_map_symmetric(&symm, |lam| Ok(lam.max(0.0)))?;
433                // Distance to the closed PSD cone, measured against the original
434                // (skew included) input so the skew-symmetric part is counted.
435                Ok(frobenius_distance(value, flatten(&psd).view()))
436            }
437            Self::Grassmann { k, n } | Self::Stiefel { k, n } => {
438                use gam_linalg::faer_ndarray::fast_atb;
439                let frame = from_flat(value, *n, *k)?;
440                let gram = fast_atb(&frame, &frame);
441                let (evals, _) = jacobi_symmetric(&gram)?;
442                let mut sq = 0.0_f64;
443                for &lam in evals.iter() {
444                    let sigma = lam.max(0.0).sqrt();
445                    let d = sigma - 1.0;
446                    sq += d * d;
447                }
448                Ok(sq.sqrt())
449            }
450        }
451    }
452
453    /// Squared metric norm `‖v‖²_base` of a tangent at `base`. Used by the
454    /// Karcher iteration's stationarity test. Poincaré uses the conformal
455    /// factor squared; the matrix manifolds and ConstantCurvature use the trait
456    /// metric tensor.
457    fn sq_metric_norm(
458        &self,
459        base: ArrayView1<'_, f64>,
460        v: ArrayView1<'_, f64>,
461    ) -> GeometryResult<f64> {
462        match self {
463            Self::Poincare { curvature, .. } => {
464                let lam = crate::manifolds::poincare::conformal_factor(base, *curvature)?;
465                Ok(lam * lam * v.iter().map(|x| x * x).sum::<f64>())
466            }
467            Self::ConstantCurvature { .. }
468            | Self::Spd { .. }
469            | Self::Grassmann { .. }
470            | Self::Stiefel { .. } => {
471                let g = self
472                    .riemannian()
473                    .expect("riemannian response manifold")
474                    .metric_tensor(base)?;
475                let gv = g.dot(&v);
476                Ok(v.dot(&gv).max(0.0))
477            }
478        }
479    }
480}
481
482/// Batched response-geometry logarithm: map every manifold-valued response row
483/// to its tangent coordinate at `base`. `values` is `(n_rows, ambient)`, `base`
484/// is `(ambient,)`, and the returned tangent is `(n_rows, ambient)` (the same
485/// flat ambient layout — the tangent of a matrix manifold is itself a flattened
486/// matrix). The scalar Gaussian GAMs the caller fits operate column-wise on
487/// this matrix exactly as they do for the sphere.
488pub fn response_log_map(
489    manifold: ResponseManifold,
490    values: ArrayView2<'_, f64>,
491    base: ArrayView1<'_, f64>,
492) -> Result<Array2<f64>, String> {
493    let ambient = manifold.ambient_dim();
494    let (n_rows, cols) = values.dim();
495    if base.len() != ambient {
496        return Err(format!(
497            "response geometry base point has length {}; expected {ambient}",
498            base.len()
499        ));
500    }
501    if cols != ambient {
502        return Err(format!(
503            "response geometry values have {cols} columns; expected {ambient}"
504        ));
505    }
506    let mut out = Array2::<f64>::zeros((n_rows, ambient));
507    for row in 0..n_rows {
508        let tangent = manifold
509            .log_point(base, values.row(row))
510            .map_err(|e| format!("response geometry log map (row {row}): {e}"))?;
511        out.row_mut(row).assign(&tangent);
512    }
513    Ok(out)
514}
515
516/// Batched response-geometry exponential: map predicted tangent coordinates
517/// back to manifold-valued responses at `base`. Inverse of [`response_log_map`]
518/// with the same shapes.
519pub fn response_exp_map(
520    manifold: ResponseManifold,
521    tangent: ArrayView2<'_, f64>,
522    base: ArrayView1<'_, f64>,
523) -> Result<Array2<f64>, String> {
524    let ambient = manifold.ambient_dim();
525    let (n_rows, cols) = tangent.dim();
526    if base.len() != ambient {
527        return Err(format!(
528            "response geometry base point has length {}; expected {ambient}",
529            base.len()
530        ));
531    }
532    if cols != ambient {
533        return Err(format!(
534            "response geometry tangent has {cols} columns; expected {ambient}"
535        ));
536    }
537    if !tangent.iter().all(|v| v.is_finite()) {
538        return Err("response geometry tangent must contain only finite values".to_string());
539    }
540    let mut out = Array2::<f64>::zeros((n_rows, ambient));
541    for row in 0..n_rows {
542        let value = manifold
543            .exp_point(base, tangent.row(row))
544            .map_err(|e| format!("response geometry exp map (row {row}): {e}"))?;
545        out.row_mut(row).assign(&value);
546    }
547    Ok(out)
548}
549
550/// Numerically-stable Euclidean norm `‖v‖₂`, scaled by the largest-magnitude
551/// entry so the squared sum cannot overflow for large but finite inputs.
552fn scaled_l2_norm(v: ArrayView1<'_, f64>) -> f64 {
553    let mut scale = 0.0_f64;
554    for &x in v.iter() {
555        let a = x.abs();
556        if a > scale {
557            scale = a;
558        }
559    }
560    if scale == 0.0 {
561        return 0.0;
562    }
563    let mut ssq = 0.0_f64;
564    for &x in v.iter() {
565        let t = x / scale;
566        ssq += t * t;
567    }
568    scale * ssq.sqrt()
569}
570
571/// Numerically-stable Frobenius distance `‖a − b‖₂` over equal-length flat
572/// vectors, scaled by the largest entrywise difference to avoid overflow.
573fn frobenius_distance(a: ArrayView1<'_, f64>, b: ArrayView1<'_, f64>) -> f64 {
574    let mut scale = 0.0_f64;
575    for (x, y) in a.iter().zip(b.iter()) {
576        let d = (x - y).abs();
577        if d > scale {
578            scale = d;
579        }
580    }
581    if scale == 0.0 {
582        return 0.0;
583    }
584    let mut ssq = 0.0_f64;
585    for (x, y) in a.iter().zip(b.iter()) {
586        let t = (x - y) / scale;
587        ssq += t * t;
588    }
589    scale * ssq.sqrt()
590}
591
592/// Distance from `value` to the open ball of radius `R = 1/√(−c)` (`c < 0`):
593/// `max(0, ‖value‖ − R)`, the true Euclidean infimum distance to the ball.
594/// Errors if the curvature is not a finite negative number.
595fn ball_domain_residual(value: ArrayView1<'_, f64>, curvature: f64) -> GeometryResult<f64> {
596    if !curvature.is_finite() || curvature >= 0.0 {
597        return Err(GeometryError::InvalidPoint(
598            "ball distance requires a finite negative curvature",
599        ));
600    }
601    let radius = (-curvature).sqrt().recip();
602    Ok((scaled_l2_norm(value) - radius).max(0.0))
603}
604
605/// Per-row extrinsic distance from ambient observations to a *candidate*
606/// response geometry — a coordinate-dependent constraint / closure-distance
607/// diagnostic.
608///
609/// What this is (and is not)
610/// -------------------------
611/// This is a cheap, pre-fit **constraint-violation** measure: given a candidate
612/// response geometry, how far does each raw row sit from that geometry's
613/// extrinsic representation (the unit-norm frame, the PSD cone, the Poincaré
614/// ball)? It is **not** the post-fit on/off-manifold membership signal (which
615/// comes from a fitted geometric smooth's residual and posterior predictive
616/// density), and it is **not** a universal cross-geometry model-selection score:
617/// it measures extrinsic constraint violation *in a chosen coordinate chart*,
618/// not intrinsic topology or curvature. Different candidate geometries have
619/// different chart codimensions (a full-dimensional Poincaré/`κ ≥ 0` chart can
620/// score zero trivially), so residuals are not directly comparable across
621/// candidates without a noise model and per-candidate calibration. Use it as a
622/// fast per-candidate gate, with candidate-specific thresholds.
623///
624/// What it computes
625/// ----------------
626/// For each ambient row `x`, [`manifold_residual`](Self::manifold_residual)
627/// returns the closed-form distance to the candidate geometry (well-defined for
628/// every input and every rank — see that method for the per-geometry formulas),
629/// and this returns:
630///
631/// * `residual[i]` — the absolute distance-to-geometry (zero for genuinely
632///   admissible rows; for the matrix manifolds, exact to machine precision).
633/// * `relative[i] = residual[i] / (‖x‖ + eps)` — the distance normalised by the
634///   row's ambient magnitude. **Note:** this is dimensionless but *not*
635///   scale-invariant for the fixed-radius geometries (Stiefel/Grassmann/ball)
636///   and is *not* bounded by `1` (it diverges as `‖x‖ → 0`); it is scale-free
637///   only for the homogeneous SPD cone. Treat it as `input_norm_relative`, not
638///   an off-manifold fraction.
639///
640/// Unlike [`response_log_map`], **no base point is needed**. `values` is
641/// `(n_rows, ambient)`; both returned arrays are `(n_rows,)`. Every fittable
642/// response geometry — including `ConstantCurvature` — has a closed-form
643/// distance, so no variant errors on a valid, finite input.
644pub fn response_projection_residual(
645    manifold: ResponseManifold,
646    values: ArrayView2<'_, f64>,
647) -> Result<(Array1<f64>, Array1<f64>), String> {
648    let ambient = manifold.ambient_dim();
649    let (n_rows, cols) = values.dim();
650    if cols != ambient {
651        return Err(format!(
652            "response geometry values have {cols} columns; expected {ambient}"
653        ));
654    }
655    if !values.iter().all(|v| v.is_finite()) {
656        return Err("response geometry values must contain only finite values".to_string());
657    }
658
659    let mut residual = Array1::<f64>::zeros(n_rows);
660    let mut relative = Array1::<f64>::zeros(n_rows);
661    for row in 0..n_rows {
662        let value = values.row(row);
663        let dist = manifold
664            .manifold_residual(value)
665            .map_err(|e| format!("response geometry residual (row {row}): {e}"))?;
666        let rel = dist / (scaled_l2_norm(value) + GEOMETRY_EPS);
667        if !dist.is_finite() || !rel.is_finite() {
668            return Err(format!(
669                "response geometry residual (row {row}) is non-finite"
670            ));
671        }
672        residual[row] = dist;
673        relative[row] = rel;
674    }
675    Ok((residual, relative))
676}
677
678/// String-driven response-geometry log map: parse the user `label` (with shape
679/// inference from the response column count), pick the base point (intrinsic
680/// Fréchet mean when `base` is `None`), map every row to its tangent, and report
681/// the canonical resolved label. This is the curved-manifold analogue of the
682/// sphere/simplex dispatch and the single entry the FFI calls for these
683/// geometries.
684///
685/// `weights` are the per-observation prior weights used ONLY to pick the intrinsic
686/// base point (they are ignored when an explicit `base` is supplied). When the
687/// caller supplies observation weights they must reach the linearization point so
688/// the tangent chart is expanded around the *weighted* Fréchet mean — where the
689/// weighted mass lives — matching the weighted tangent regression run there
690/// (#2125). `None` recovers the uniform intrinsic mean.
691pub fn dispatch_log_map(
692    values: ArrayView2<'_, f64>,
693    label: &str,
694    base: Option<ArrayView1<'_, f64>>,
695    weights: Option<ArrayView1<'_, f64>>,
696) -> Result<(Array2<f64>, Array1<f64>, String), String> {
697    let manifold = ResponseManifold::parse(label, values.ncols())?;
698    let base_point = match base {
699        Some(b) => b.to_owned(),
700        // #2351: the constant-curvature chart identifies its origin with the
701        // FLAT centroid — the same κ-independent base the curvature criterion
702        // profiled — so the default base here must be that centroid, not the
703        // Karcher mean (which re-entangles the base with the chart scale and
704        // diverges from the point the fit's κ̂ was estimated around).
705        None => match manifold {
706            ResponseManifold::ConstantCurvature { dim, .. } => {
707                let (n_rows, _) = values.dim();
708                if n_rows == 0 {
709                    return Err(
710                        "constant-curvature log map requires at least one response row".into(),
711                    );
712                }
713                let mut centroid = Array1::<f64>::zeros(dim);
714                match weights {
715                    Some(w) => {
716                        let normalized = crate::normalize_weights(n_rows, Some(w))
717                            .map_err(|_| "constant-curvature log map has invalid weights")?;
718                        for (row, &wi) in values.outer_iter().zip(normalized.iter()) {
719                            centroid.scaled_add(wi, &row);
720                        }
721                    }
722                    None => {
723                        for row in values.outer_iter() {
724                            centroid += &row;
725                        }
726                        centroid.mapv_inplace(|v| v / n_rows as f64);
727                    }
728                }
729                centroid
730            }
731            _ => response_frechet_mean(manifold, values, weights, 1.0e-12, 256)
732                .map_err(|err| err.to_string())?,
733        },
734    };
735    let tangent = response_log_map(manifold, values, base_point.view())?;
736    Ok((tangent, base_point, manifold.canonical_label()))
737}
738
739/// String-driven response-geometry exponential map: inverse of
740/// [`dispatch_log_map`] given an explicit base point.
741pub fn dispatch_exp_map(
742    tangent: ArrayView2<'_, f64>,
743    label: &str,
744    base: ArrayView1<'_, f64>,
745) -> Result<Array2<f64>, String> {
746    let manifold = ResponseManifold::parse(label, tangent.ncols())?;
747    response_exp_map(manifold, tangent, base)
748}
749
750/// Intrinsic (Karcher) Fréchet mean of manifold-valued responses, the default
751/// base point when the user supplies none. `values` is `(n_rows, ambient)`.
752///
753/// This is the SPD safeguarded Karcher iteration generalised over an arbitrary
754/// [`ResponseManifold`]: a Riemannian gradient-descent on the weighted
755/// dispersion `V(P) = Σ_i w_i ‖log_P(X_i)‖²_P` with the descent direction
756/// `ξ = Σ_i w_i log_P(X_i)` (`= −½ grad V`), a unit Karcher step `exp_P(t·ξ)`
757/// with Armijo backtracking plus a round-off cushion, and the metric-norm
758/// stationarity certificate `‖ξ‖_P ≤ tol`. No approximate point is returned on
759/// a stalled line search or exhausted iteration budget. Positively curved
760/// geometries additionally require the weighted support to lie inside their
761/// analytic strong-convexity radius, certifying the stationary point as the
762/// unique global Fréchet mean; diffuse data return a typed error and require an
763/// explicit base instead of selecting a capped multistart basin. The SPD-specific
764/// version in [`crate::manifolds::spd::spd_frechet_mean`] remains for the affine
765/// inverse it caches per step; this generic form pays a metric-tensor solve but
766/// covers all four geometries uniformly.
767pub fn response_frechet_mean(
768    manifold: ResponseManifold,
769    values: ArrayView2<'_, f64>,
770    weights: Option<ArrayView1<'_, f64>>,
771    tol: f64,
772    max_iter: usize,
773) -> GeometryResult<Array1<f64>> {
774    let ambient = manifold.ambient_dim();
775    let (m, cols) = values.dim();
776    if m == 0 || cols != ambient {
777        return Err(GeometryError::InvalidPoint(
778            "response geometry Fréchet mean requires a non-empty value matrix with manifold ambient width",
779        ));
780    }
781    if !(tol.is_finite() && tol > 0.0) {
782        return Err(GeometryError::InvalidPoint(
783            "response geometry Fréchet mean tolerance must be finite and positive",
784        ));
785    }
786    let w = crate::normalize_weights(m, weights).map_err(|_| {
787        GeometryError::InvalidPoint("response geometry Fréchet mean has invalid weights")
788    })?;
789    let samples: Vec<Array1<f64>> = (0..m).map(|i| values.row(i).to_owned()).collect();
790
791    let dispersion = |p: ArrayView1<'_, f64>| -> GeometryResult<f64> {
792        let mut acc = 0.0_f64;
793        for (i, x) in samples.iter().enumerate() {
794            if w[i] == 0.0 {
795                continue;
796            }
797            let lg = manifold.log_point(p, x.view())?;
798            let sq = manifold.sq_metric_norm(p, lg.view())?;
799            acc += w[i] * sq;
800        }
801        Ok(acc)
802    };
803
804    let stationarity = |p: ArrayView1<'_, f64>| -> GeometryResult<(Array1<f64>, f64)> {
805        let mut xi = Array1::<f64>::zeros(ambient);
806        for (i, x) in samples.iter().enumerate() {
807            if w[i] == 0.0 {
808                continue;
809            }
810            let lg = manifold.log_point(p, x.view())?;
811            xi.scaled_add(w[i], &lg);
812        }
813        let residual = manifold.sq_metric_norm(p, xi.view())?.sqrt();
814        Ok((xi, residual))
815    };
816
817    // Safeguarded Riemannian gradient descent from one interior start. The only
818    // success exit is the analytic Karcher certificate `‖Σwᵢlogₚ(xᵢ)‖ₚ≤tol`;
819    // line-search or iteration exhaustion above it is typed non-convergence.
820    let descend = |start: Array1<f64>| -> GeometryResult<(Array1<f64>, f64)> {
821        let mut p = start;
822        let mut f_cur = dispersion(p.view())?;
823        for iteration in 0..max_iter {
824            // Riemannian gradient direction ξ = Σ wᵢ log_p(xᵢ) = −½ grad V.
825            let (xi, grad_norm) = stationarity(p.view())?;
826            if grad_norm <= tol {
827                return Ok((p, grad_norm));
828            }
829
830            // Armijo-backtracked unit Karcher step exp_p(t·ξ). A step that
831            // leaves the manifold's domain (e.g. a Poincaré overshoot past the
832            // ball boundary) or lands where the dispersion is undefined is an
833            // INVALID trial (`Ok(None)`): shrink and retry without consulting
834            // the Armijo test — unlike `spd_frechet_mean`, this generic driver
835            // never aborts the descent on a trial-evaluation error.
836            let pred = grad_norm * grad_norm;
837            let f_tol = armijo_roundoff_cushion(f_cur);
838            let accepted = match backtracking_line_search::<_, Infallible>(
839                BacktrackConfig::default(),
840                |t| {
841                    let step = &xi * t;
842                    let Ok(cand) = manifold.exp_point(p.view(), step.view()) else {
843                        return Ok(None);
844                    };
845                    let Ok(f_cand) = dispersion(cand.view()) else {
846                        return Ok(None);
847                    };
848                    Ok(Some((f_cand, cand)))
849                },
850                |t, f_cand| f_cand <= f_cur - 2.0 * constants::ARMIJO_C1 * t * pred + f_tol,
851            ) {
852                Ok(result) => result,
853                Err(never) => match never {},
854            };
855            let Some(accepted_step) = accepted else {
856                return Err(GeometryError::NonConvergence {
857                    context: "response geometry Fréchet mean",
858                    iterations: iteration + 1,
859                    residual: grad_norm,
860                    tolerance: tol,
861                });
862            };
863            p = accepted_step.payload;
864            f_cur = accepted_step.value;
865        }
866        // The final allowed update can cross the requested threshold.
867        let (_, residual) = stationarity(p.view())?;
868        if residual <= tol {
869            Ok((p, residual))
870        } else {
871            Err(GeometryError::NonConvergence {
872                context: "response geometry Fréchet mean",
873                iterations: max_iter,
874                residual,
875                tolerance: tol,
876            })
877        }
878    };
879
880    // Choose one row-order-invariant positive-mass seed: highest weight, then
881    // lexicographically smallest coordinates. On a Hadamard manifold any seed
882    // reaches the unique global mean. On a positively curved manifold the
883    // support-ball certificate below, rather than an arbitrary number of
884    // restarts, proves that the stationary point is the unique global mean.
885    let mut seed_index: Option<usize> = None;
886    for index in 0..m {
887        if w[index] == 0.0 {
888            continue;
889        }
890        let replace = match seed_index {
891            None => true,
892            Some(current) if w[index] > w[current] => true,
893            Some(current) if w[index] == w[current] => {
894                samples[index]
895                    .iter()
896                    .zip(samples[current].iter())
897                    .find_map(|(&lhs, &rhs)| {
898                        let order = lhs.total_cmp(&rhs);
899                        (order != std::cmp::Ordering::Equal).then_some(order)
900                    })
901                    == Some(std::cmp::Ordering::Less)
902            }
903            Some(_) => false,
904        };
905        if replace {
906            seed_index = Some(index);
907        }
908    }
909    let seed_index = seed_index.ok_or(GeometryError::InvalidPoint(
910        "response geometry Fréchet mean has no positive-weight sample",
911    ))?;
912    let start = manifold.exp_point(
913        samples[seed_index].view(),
914        Array1::<f64>::zeros(ambient).view(),
915    )?;
916    let (mean, stationarity_residual) = descend(start)?;
917
918    if let Some(uniqueness_radius) = manifold.frechet_uniqueness_radius() {
919        let mut support_radius = 0.0_f64;
920        for (index, sample) in samples.iter().enumerate() {
921            if w[index] == 0.0 {
922                continue;
923            }
924            let log = manifold.log_point(mean.view(), sample.view())?;
925            let distance = manifold.sq_metric_norm(mean.view(), log.view())?.sqrt();
926            if !distance.is_finite() {
927                return Err(GeometryError::Singular(
928                    "response geometry Fréchet support radius is non-finite",
929                ));
930            }
931            support_radius = support_radius.max(distance);
932        }
933        if support_radius >= uniqueness_radius {
934            return Err(GeometryError::FrechetMeanNotGloballyCertified {
935                context: "response geometry Fréchet mean",
936                stationarity_residual,
937                tolerance: tol,
938                support_radius,
939                uniqueness_radius,
940            });
941        }
942    }
943
944    Ok(mean)
945}
946
947// ── Curvature as an estimand on the response geometry (#944 stage 4 / #1104) ──
948//
949// `response_geometry="constant_curvature(dim=d)"` does NOT take a fixed κ from
950// the user: κ is ESTIMATED from the manifold-valued responses. At each κ the
951// family `ConstantCurvature{dim, κ}` is laid down and κ is scored by the HONEST
952// change-of-variables likelihood of the observed chart coordinates `yᵢ` w.r.t.
953// ambient Lebesgue measure `dy` — the density that is automatically normalised on
954// the SAME measure in which the data are observed, regardless of how the manifold
955// is parameterised. This is the crux of the #1104 fix.
956//
957// ## Why dispersion alone (and the self-normalising wrapped Gaussian) is degenerate
958//
959// The generative model is the wrapped normal `yᵢ = exp_μ(vᵢ)`, `vᵢ` isotropic at
960// geodesic scale σ. Its density w.r.t. the Riemannian volume `dvol_κ` is
961// `N(sᵢ;0,σ²)/Jᵧ_κ(sᵢ)` with `sᵢ = d_κ(μ,yᵢ)` the geodesic radius and
962// `J_κ(s) = (sn_κ(s)/s)^{d−1}` the exp-map volume Jacobian
963// (`ConstantCurvature::jacobian_radial`). The naive criterion
964// `½nd·ln(Σsᵢ²/nd)` (dispersion only), and even the full `dvol_κ`-density NLL
965// `Σ[sᵢ²/2σ² + (d/2)ln2πσ² + ln J_κ(sᵢ)]`, are SCALE-DEGENERATE: rescaling the
966// manifold radius `R = 1/√|κ|` rescales every `sᵢ` and every volume element, and
967// the σ-profile absorbs the change with no κ information left. That is exactly
968// why a `dvol_κ`-normalised (self-normalising) wrapped Gaussian rails, and why an
969// intrinsic-volume partition function double-counts: the density is already
970// normalised on `dvol_κ`, so re-integrating its volume adds nothing identifying.
971//
972// ## The restoring force is the ambient (chart) volume element at the DATA points
973//
974// Curvature is identified only when the abstract manifold is tied to the CONCRETE
975// observed chart coordinates `yᵢ`. The data are observed as points of `ℝ^d` under
976// Lebesgue `dy`, so the likelihood must be the density w.r.t. `dy`, obtained from
977// the `dvol_κ`-density by the chart volume factor `dvol_κ/dy = λ_{yᵢ}^d`,
978// `λ_y = 2/(1+κ‖y‖²)`:
979//
980// ```text
981//   −ℓ(κ,μ,σ²) = Σᵢ[ sᵢ²/(2σ²) + (d/2)ln(2πσ²) + ln J_κ(sᵢ) − d·ln λ_{yᵢ} ].
982// ```
983//
984// The new term `−d·Σ ln λ_{yᵢ} = d·Σ ln((1+κ‖yᵢ‖²)/2)` is evaluated at every DATA
985// point (not at the mean), so `‖yᵢ‖² > 0` even for mean-centred clouds and it
986// supplies a genuine κ-restoring force: it grows like `+d·κ·Σ‖yᵢ‖²` for small κ
987// and `→ +∞` as κ→+∞ (each `−ln λ_{yᵢ}→+∞`), exactly opposing the dispersion /
988// `ln J_κ` terms which fall as the sphere shrinks. The minimum is therefore
989// INTERIOR at the data-generating curvature. None of `ln J_κ` or `λ` depend on σ,
990// so σ profiles in closed form `σ̂² = D/(nd)`, `D = Σ sᵢ²`.
991//
992// ## Reparameterisation invariance / unit-covariance of κ̂
993//
994// κ carries units of `1/length²`. Under a global rescaling `yᵢ ↦ α·yᵢ` the chart
995// of `M_κ` at scale `α` equals the chart of `M_{κ/α²}` at scale 1 (because
996// `λ` and every geodesic primitive depend on `y` only through `κ‖y‖²`). The whole
997// criterion `V(κ, αy)` therefore equals `V(α²κ, y)`, so its minimiser transforms
998// as `κ̂(αy) = κ̂(y)/α²` — the CORRECT covariance of a curvature with units
999// `1/length²`. The base point μ is held at the κ-independent flat centroid (NOT
1000// re-solved per κ): re-solving the Fréchet mean per κ is precisely what
1001// re-entangles κ with the chart scale and biases the estimate, so it is removed.
1002//
1003// `V_p` is a negative log-evidence (lower is better) so κ̂ = argmin V_p; it is the
1004// full NLL summed over all `n·d` scalar observations, so `2[V_p(0) − V_p(κ̂)]` is
1005// the Wilks LR statistic with a calibrated χ²₁ flatness reference — exactly the
1006// contract `profile_ci_walk` / `flatness_lr_test` in `curvature_estimand.rs`
1007// consume, with no new outer machinery.
1008
1009/// Typed failures from constant-curvature response fitting. In particular,
1010/// optimiser exhaustion carries the exact score/Hessian and the normalized
1011/// box-KKT residual, so a caller never receives a midpoint merely because an
1012/// iteration cap was reached.
1013#[derive(Clone, Debug, PartialEq)]
1014pub enum ResponseGeometryError {
1015    InvalidInput(String),
1016    NumericalGeometry(String),
1017    CurvatureUnidentified {
1018        dispersion: f64,
1019    },
1020    CurvatureNonConvergence {
1021        iterations: usize,
1022        max_iter: usize,
1023        bracket_lo: f64,
1024        bracket_hi: f64,
1025        kappa: f64,
1026        criterion: f64,
1027        score: f64,
1028        curvature: f64,
1029        kkt_residual: f64,
1030        tolerance: f64,
1031    },
1032}
1033
1034impl fmt::Display for ResponseGeometryError {
1035    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1036        match self {
1037            Self::InvalidInput(message) | Self::NumericalGeometry(message) => f.write_str(message),
1038            Self::CurvatureUnidentified { dispersion } => write!(
1039                f,
1040                "response curvature is unidentified: profiled geodesic dispersion is {dispersion:.6e}"
1041            ),
1042            Self::CurvatureNonConvergence {
1043                iterations,
1044                max_iter,
1045                bracket_lo,
1046                bracket_hi,
1047                kappa,
1048                criterion,
1049                score,
1050                curvature,
1051                kkt_residual,
1052                tolerance,
1053            } => write!(
1054                f,
1055                "response curvature did not satisfy its minimizing box-KKT certificate after \
1056                 {iterations}/{max_iter} iterations: bracket=[{bracket_lo:.6e}, \
1057                 {bracket_hi:.6e}], kappa={kappa:.6e}, criterion={criterion:.6e}, \
1058                 score={score:.6e}, normalized KKT residual={kkt_residual:.6e} \
1059                 (required <= {tolerance:.6e}), curvature={curvature:.6e} \
1060                 (required > 0)"
1061            ),
1062        }
1063    }
1064}
1065
1066impl std::error::Error for ResponseGeometryError {}
1067
1068impl From<GeometryError> for ResponseGeometryError {
1069    fn from(error: GeometryError) -> Self {
1070        Self::NumericalGeometry(error.to_string())
1071    }
1072}
1073
1074/// Outcome of fitting curvature as an estimand on a constant-curvature response
1075/// geometry: the optimised κ̂, its tangent base point, the profile-likelihood CI,
1076/// and the interior-point flatness (Wilks) test of κ = 0.
1077#[derive(Clone, Debug)]
1078pub struct ResponseCurvatureFit {
1079    /// The dimension `d` of the constant-curvature response manifold.
1080    pub dim: usize,
1081    /// The REML/evidence-optimal curvature κ̂ (argmin of the profiled criterion).
1082    ///
1083    /// **Units `1/length²`** — κ̂ is therefore *scale-dependent*: rescaling the
1084    /// cloud `y ↦ α·y` rescales `κ̂ ↦ κ̂/α²`. For a scale-free statement of how
1085    /// curved the cloud is, read [`kappa_r2`](Self::kappa_r2) instead. When the
1086    /// cloud is curved BEYOND what its spread can resolve (it fills a large
1087    /// fraction of the sphere `S^d(1/√κ̂)`), the optimiser rails to the
1088    /// chart-resolution cap and [`railed_at_resolution_limit`](Self::railed_at_resolution_limit)
1089    /// is `true`: κ̂ is then a *lower bound on |κ|*, not a point estimate.
1090    pub kappa_hat: f64,
1091    /// The DIMENSIONLESS geometric invariant the cloud actually determines:
1092    /// `κ̂ · r²` with `r` = [`characteristic_radius`](Self::characteristic_radius).
1093    /// This is scale-FREE (`κ̂·r²` is invariant under `y ↦ α·y`, since `κ̂ ↦ κ̂/α²`
1094    /// and `r ↦ α·r`) — the honest answer to "how curved is this cloud relative
1095    /// to its own spread". `|κ̂·r²| ≪ 1` ⇒ nearly flat at this scale; `κ̂·r² ↗ (π/2)²`
1096    /// ⇒ the cloud fills the sphere and curvature is at the chart-resolution limit.
1097    pub kappa_r2: f64,
1098    /// Characteristic geodesic radius `r` of the cloud at κ = 0 (the doubled-gauge
1099    /// chart distance `r = 2·max_i‖y_i − μ‖`): the length scale against which κ̂ is
1100    /// dimensionless. Reported so the caller can convert between scale-dependent κ̂
1101    /// and the scale-free `κ̂·r²` without re-deriving the chart gauge.
1102    pub characteristic_radius: f64,
1103    /// The intrinsic Fréchet-mean base point at κ̂ (the tangent expansion point
1104    /// the scalar GAMs are fitted around).
1105    pub base: Array1<f64>,
1106    /// Profiled criterion value `V_p(κ̂)` (concentrated negative log-evidence).
1107    pub v_p_hat: f64,
1108    /// `true` when the κ̂ search converged ONTO the chart-resolution cap rather
1109    /// than an interior optimum: the data want curvature at or beyond the
1110    /// conjugate radius of their geodesic spread (the cloud fills the sphere).
1111    /// In that case κ̂ / the CI upper end are NOT a resolved point estimate but a
1112    /// HONEST "curvature exceeds chart-resolvable range at this scale" flag — the
1113    /// caller must report it as such and never as a silent `κ̂ = ci_hi`.
1114    pub railed_at_resolution_limit: bool,
1115    /// Twin of [`railed_at_resolution_limit`](Self::railed_at_resolution_limit)
1116    /// for the HYPERBOLIC side (#2351): `true` when the κ̂ search converged ONTO
1117    /// the lower chart-domain bound — the criterion is still improving as κ
1118    /// decreases at the limit where the cloud fills the hyperbolic ball of its
1119    /// own spread (the mean-centred chart-validity edge `1 + κ‖z_max‖² → 0⁺`,
1120    /// where the conformal restoring force diverges linearly and beats the
1121    /// log-log dispersion term, so the criterion genuinely runs away). κ̂ is
1122    /// then an UPPER bound on κ, not a resolved point estimate; the caller must
1123    /// report "curvature exceeds the chart-resolvable hyperbolic range at this
1124    /// scale" and never quote a confident hyperbolic verdict off the rail.
1125    pub railed_at_hyperbolic_resolution_limit: bool,
1126    /// `true` only when the SIGN of κ̂ is statistically resolved — i.e. the
1127    /// profile-likelihood CI excludes 0 (`profile_ci.verdict ≠ Flat`).
1128    ///
1129    /// ## Why a point estimate alone is not enough (the #944/#1059 flat-floor)
1130    ///
1131    /// Curvature is resolvable only through the dimensionless product `κ·r²`
1132    /// (see [`kappa_r2`](Self::kappa_r2)); the per-point Fisher information for κ
1133    /// scales like `σ⁴`. When the cloud is nearly flat at its own scale
1134    /// (`|κ·r²| ≪ 1`), the profiled criterion is so shallow that its single-cloud
1135    /// argmin κ̂ can land on the WRONG SIDE OF ZERO purely by Monte-Carlo
1136    /// fluctuation — empirically a coin-flip below `|κ·r²| ≈ 0.03`, reliable above
1137    /// `≈ 0.09` (the #944 power curve). The estimand itself is UNBIASED (the
1138    /// criterion averaged over clouds minimises exactly at κ⋆), so this is a
1139    /// resolution limit, not a bias.
1140    ///
1141    /// The CI, in contrast, is honest in this regime: at an under-resolved
1142    /// operating point it reports `Flat` (straddles 0) rather than a confident
1143    /// wrong sign — it essentially never claims the wrong-signed geometry. So the
1144    /// SIGN-bearing summary the caller may quote is the CI verdict, not the bare
1145    /// κ̂. This flag exposes that contract on the point-estimate surface: when it
1146    /// is `false`, κ̂'s sign is noise — the caller must report "curvature not
1147    /// resolved at this scale (|κ·r²| too small)" and quote the CI / `kappa_r2`,
1148    /// never a sign-confident κ̂. It is the flat-floor twin of
1149    /// [`railed_at_resolution_limit`](Self::railed_at_resolution_limit) (the
1150    /// spherical-cap rail); together they bracket the two ends of the resolvable
1151    /// `κ·r²` band where κ̂ is a genuine interior point estimate.
1152    pub sign_resolved: bool,
1153    /// Profile-likelihood CI for κ and the geometry verdict from its sign.
1154    pub profile_ci: crate::curvature_estimand::KappaProfileCi,
1155    /// Interior-point χ²₁ likelihood-ratio test of flatness (κ = 0).
1156    pub flatness: crate::curvature_estimand::FlatnessTest,
1157}
1158
1159/// Chart-validity bounds on κ for a constant-curvature response geometry built
1160/// from the supplied responses, plus the characteristic geodesic radius
1161/// `ρ_max = 2·max_i‖y_i − μ‖` against which κ is made dimensionless.
1162///
1163/// Returns `(kappa_min, kappa_max, rho_max)`.
1164///
1165/// * **Lower (hyperbolic) bound.** The κ-stereographic chart requires
1166///   `1 + κ‖x‖² > 0` at every point measured from the chart origin, i.e.
1167///   `κ > −1/R²` with `R² = max_i ‖y_i‖²`. The open boundary is
1168///   approached only to the relative resolution of f64 arithmetic.
1169/// * **Upper (spherical) bound.** Unlike the hyperbolic side this is NOT
1170///   unbounded: on a sphere of curvature κ the geodesic radius cannot exceed the
1171///   conjugate radius `π/√κ`, beyond which the exp-map volume Jacobian
1172///   `J_κ = (sn_κ/·)^{d−1}` changes sign (clamped to 0 here) and `ln J_κ` would
1173///   collapse `V_p` toward `−∞`, railing the optimiser onto a spurious shell.
1174///   The κ = 0 geodesic radius of the farthest point from the centroid is
1175///   `ρ_max = 2·max_i‖y_i − μ‖` (doubled-gauge chart). We cap κ so that radius
1176///   stays strictly inside the first conjugate shell to f64-relative resolution:
1177///   `√κ·ρ_max < π`. This keeps every geodesic radius before the
1178///   antipodal singularity along the whole search/CI walk without an arbitrary
1179///   fractional margin.
1180///
1181/// `κ_max` is the chart-RESOLUTION limit of the cloud: at it the geodesic spread
1182/// fills the conjugate shell to machine resolution, i.e. the cloud nearly fills
1183/// the sphere `S^d(1/√κ_max)`. The DIMENSIONLESS product `κ_max·ρ_max²
1184/// → π²` is fixed and data-scale-free — it is the natural "the cloud is
1185/// maximally curved relative to its spread" sentinel the rail check compares κ̂ to.
1186fn response_kappa_bounds(values: ArrayView2<'_, f64>) -> (f64, f64, f64) {
1187    let (n_rows, dim) = values.dim();
1188    // BOTH rails derive from the centroid-relative spread ‖y_i − μ‖² — the only
1189    // translation-invariant "how spread is this cloud" quantity. The chart
1190    // origin is IDENTIFIED with the cloud's flat centroid (the criterion
1191    // evaluates on the mean-centred coordinates z_i = y_i − μ, #2351), so the
1192    // hyperbolic chart-domain constraint 1 + κ‖z‖² > 0 is governed by the same
1193    // spread as the spherical conjugate-radius cap. The previous ambient-origin
1194    // radius made κ_min collapse to ≈ −1 for any unit-normalised cloud
1195    // regardless of its shape — a pure-translation-sensitive verdict.
1196    let mut centroid = Array1::<f64>::zeros(dim.max(1));
1197    if n_rows > 0 && dim > 0 {
1198        for row in values.outer_iter() {
1199            centroid += &row;
1200        }
1201        centroid.mapv_inplace(|v| v / n_rows as f64);
1202    }
1203    let mut s2_max = 0.0_f64;
1204    if dim > 0 {
1205        for row in values.outer_iter() {
1206            let diff = &row - &centroid;
1207            let r2 = diff.dot(&diff);
1208            if r2 > s2_max {
1209                s2_max = r2;
1210            }
1211        }
1212    }
1213    assert!(
1214        s2_max > 0.0,
1215        "response κ bounds require a non-degenerate cloud: max ‖y−μ‖²={s2_max}"
1216    );
1217    // Stay one square-root-epsilon relative step inside both open singular
1218    // boundaries. This is derived from f64 resolution, not a tuning knob.
1219    let open_boundary = 1.0 - f64::EPSILON.sqrt();
1220    let kappa_min = -open_boundary / s2_max;
1221    // Conjugate-radius cap: ρ_max = 2·max‖y_i − μ‖ is the κ=0 geodesic radius.
1222    let rho_max = 2.0 * s2_max.sqrt();
1223    let edge = open_boundary * std::f64::consts::PI / rho_max;
1224    let kappa_max = edge * edge;
1225    (kappa_min, kappa_max, rho_max)
1226}
1227
1228/// Profiled curvature criterion `V_p(κ)` for the constant-curvature response
1229/// geometry: the σ-profiled HONEST change-of-variables negative log-likelihood of
1230/// the observed chart coordinates `y_i` at curvature `κ`, expressed w.r.t. ambient
1231/// Lebesgue measure `dy`. Lower is better (κ̂ = argmin). Returns `(V_p, base)`;
1232/// the base point is the κ-INDEPENDENT flat centroid (the tangent expansion point
1233/// that the scalar GAMs are fitted around), held fixed across κ so the estimate is
1234/// not re-entangled with the chart scale.
1235///
1236/// The model is the wrapped normal `y_i = exp_{μ,κ}(v_i)` with isotropic geodesic
1237/// scale σ; `s_i = d_κ(μ, y_i)` is the geodesic radius and `J_κ(s)` the exp-map
1238/// volume Jacobian. The density on the Riemannian volume `dvol_κ` is
1239/// `N(s_i;0,σ²)/J_κ(s_i)`; converting to ambient `dy` multiplies by the chart
1240/// volume factor `λ_{y_i}^d`, `λ_y = 2/(1+κ‖y‖²)`. The negative log-likelihood is
1241///
1242/// ```text
1243///   −ℓ(κ,σ²) = Σ_i[ s_i²/(2σ²) + (d/2)ln(2πσ²) + ln J_κ(s_i) − d·ln λ_{y_i} ].
1244/// ```
1245///
1246/// `ln J_κ` and `λ` do not depend on σ, so σ profiles in closed form
1247/// `σ̂² = D/(nd)`, `D = Σ s_i²`. The `−d·Σ ln λ_{y_i}` term — evaluated at the DATA
1248/// points, not the mean — is the κ-restoring force that breaks the scale
1249/// degeneracy of the dispersion / `dvol_κ`-density alone (see the module notes).
1250/// Additive constants independent of κ are kept implicit; they cancel in every
1251/// LR / profile-drop the CI machinery forms. μ is the closed-form flat centroid,
1252/// so the criterion is a pure function of κ with no inner tolerance/iteration
1253/// budget (the outer κ̂ search owns those).
1254pub fn response_curvature_criterion(
1255    values: ArrayView2<'_, f64>,
1256    dim: usize,
1257    kappa: f64,
1258) -> Result<(f64, Array1<f64>), String> {
1259    response_curvature_criterion_jet(values, dim, kappa)
1260        .map(|jet| (jet.value, jet.base))
1261        .map_err(|error| error.to_string())
1262}
1263
1264#[derive(Clone, Debug)]
1265struct CurvatureCriterionJet {
1266    kappa: f64,
1267    value: f64,
1268    score: f64,
1269    curvature: f64,
1270    base: Array1<f64>,
1271}
1272
1273/// Hand-derived value, score, and Hessian of the profiled criterion. Every
1274/// derivative is assembled from the closed-form distance κ-jet and analytic
1275/// chain rules; no production finite difference or autodiff is involved.
1276fn response_curvature_criterion_jet(
1277    values: ArrayView2<'_, f64>,
1278    dim: usize,
1279    kappa: f64,
1280) -> Result<CurvatureCriterionJet, ResponseGeometryError> {
1281    if !kappa.is_finite() {
1282        return Err(ResponseGeometryError::InvalidInput(
1283            "response curvature criterion: kappa must be finite".into(),
1284        ));
1285    }
1286    let (n_rows, cols) = values.dim();
1287    if n_rows == 0 || cols != dim || dim == 0 {
1288        return Err(ResponseGeometryError::InvalidInput(format!(
1289            "response curvature criterion: values must be N×{dim} with N >= 1"
1290        )));
1291    }
1292    // κ-independent base point: the flat (ambient) centroid. Holding μ fixed across
1293    // κ is the de-entangling move — re-solving the Fréchet mean per κ couples the
1294    // base to the chart scale and biases κ̂ (#1104 root cause).
1295    let mut base = Array1::<f64>::zeros(dim);
1296    for row in values.outer_iter() {
1297        base += &row;
1298    }
1299    base.mapv_inplace(|v| v / n_rows as f64);
1300
1301    let chart = ConstantCurvature::new(dim, kappa);
1302    let d = dim as f64;
1303    let mut dispersion = 0.0_f64;
1304    let mut dispersion_d1 = 0.0_f64;
1305    let mut dispersion_d2 = 0.0_f64;
1306    let mut ln_jac = 0.0_f64;
1307    let mut ln_jac_d1 = 0.0_f64;
1308    let mut ln_jac_d2 = 0.0_f64;
1309    let mut chart_volume = 0.0_f64;
1310    let mut chart_volume_d1 = 0.0_f64;
1311    let mut chart_volume_d2 = 0.0_f64;
1312
1313    // #2351: the chart origin is IDENTIFIED with the flat centroid — every
1314    // per-row quantity evaluates on the mean-centred coordinate z_i = y_i − μ.
1315    // This is the translation-invariant model: y ↦ y + t leaves every z_i (and
1316    // hence V_p, κ̂, the verdict, and both rail flags) exactly unchanged, while
1317    // z ↦ dy is unit-Jacobian so the observed-measure likelihood is unaffected.
1318    // (Möbius recentring w = (−μ)⊕_κ y does NOT achieve this: gyro-addition
1319    // does not commute with Euclidean translation, and w is κ-dependent.)
1320    // The centred distance collapses the Möbius denominator to 1, so the
1321    // hyperbolic side has no off-origin antipodal singularity.
1322    let origin = Array1::<f64>::zeros(dim);
1323    for row in values.outer_iter() {
1324        let centred = &row - &base;
1325        let (r, r_d1, r_d2) = distance_kappa_jet(&chart, origin.view(), centred.view())?;
1326        dispersion += r * r;
1327        dispersion_d1 += 2.0 * r * r_d1;
1328        dispersion_d2 += 2.0 * (r_d1 * r_d1 + r * r_d2);
1329
1330        if dim > 1 {
1331            // J_κ(r)=S(u)^(d−1), u=κr². Chain-rule jets of u.
1332            let u = kappa * r * r;
1333            let u_d1 = r * r + 2.0 * kappa * r * r_d1;
1334            let u_d2 = 4.0 * r * r_d1 + 2.0 * kappa * (r_d1 * r_d1 + r * r_d2);
1335            let s = cs_stacks3(u).1;
1336            if !(s[0].is_finite() && s[0] > 0.0) {
1337                return Err(ResponseGeometryError::NumericalGeometry(
1338                    "response curvature criterion reached the conjugate shell".into(),
1339                ));
1340            }
1341            let log_s_d1 = s[1] / s[0];
1342            let log_s_d2 = s[2] / s[0] - log_s_d1 * log_s_d1;
1343            let exponent = (dim - 1) as f64;
1344            ln_jac += exponent * s[0].ln();
1345            ln_jac_d1 += exponent * log_s_d1 * u_d1;
1346            ln_jac_d2 += exponent * (log_s_d2 * u_d1 * u_d1 + log_s_d1 * u_d2);
1347        }
1348
1349        // −d ln λ_z = d[ln(1+κ‖z‖²)−ln 2], evaluated at the CENTRED coordinate
1350        // (#2351): the κ-restoring force reads the cloud's spread, not its
1351        // arbitrary ambient offset.
1352        let q = centred.dot(&centred);
1353        let gauge = 1.0 + kappa * q;
1354        if !(gauge.is_finite() && gauge > 0.0) {
1355            return Err(ResponseGeometryError::NumericalGeometry(
1356                "response curvature criterion reached the chart boundary".into(),
1357            ));
1358        }
1359        chart_volume += d * (gauge.ln() - std::f64::consts::LN_2);
1360        chart_volume_d1 += d * q / gauge;
1361        chart_volume_d2 -= d * q * q / (gauge * gauge);
1362    }
1363    let nobs = (n_rows * dim) as f64;
1364    if !(dispersion.is_finite() && dispersion > 0.0) {
1365        return Err(ResponseGeometryError::CurvatureUnidentified { dispersion });
1366    }
1367
1368    // σ profiles in closed form: σ̂² = D/(nd). Substituting and dropping the
1369    // κ-independent constant (nd/2)(1 + ln 2π):
1370    //   V_p(κ) = (nd/2)·ln(D/(nd)) + Σ ln J_κ(s_i) − d·Σ ln λ_{y_i}.
1371    let value = 0.5 * nobs * (dispersion / nobs).ln() + ln_jac + chart_volume;
1372    let score = 0.5 * nobs * dispersion_d1 / dispersion + ln_jac_d1 + chart_volume_d1;
1373    let curvature = 0.5
1374        * nobs
1375        * (dispersion_d2 / dispersion
1376            - (dispersion_d1 / dispersion) * (dispersion_d1 / dispersion))
1377        + ln_jac_d2
1378        + chart_volume_d2;
1379    if !value.is_finite() || !score.is_finite() || !curvature.is_finite() {
1380        return Err(ResponseGeometryError::NumericalGeometry(
1381            "response curvature criterion jet is non-finite".into(),
1382        ));
1383    }
1384    Ok(CurvatureCriterionJet {
1385        kappa,
1386        value,
1387        score,
1388        curvature,
1389        base,
1390    })
1391}
1392
1393/// Fit curvature as an estimand on a constant-curvature response geometry.
1394///
1395/// κ̂ is the minimiser of the profiled criterion [`response_curvature_criterion`]
1396/// (the σ-profiled honest change-of-variables negative log-evidence of the wrapped
1397/// normal w.r.t. ambient measure), found by a safeguarded root solve of its
1398/// exact analytic score inside the chart-validity bracket. The base point μ is
1399/// the κ-independent flat centroid, so
1400/// every `V_p` evaluation scores the SAME geometry without re-entangling κ with the
1401/// chart scale (the #1104 fix). The exact outer
1402/// curvature `V_p''(κ̂)` is evaluated by the same hand-derived criterion jet
1403/// and handed to [`profile_ci_walk`](crate::profile_ci_walk)
1404/// to size the initial Wald step; the CI itself is the exact χ²₁ profile crossing.
1405/// Flatness is the interior-point χ²₁ LR test
1406/// [`flatness_lr_test`](crate::flatness_lr_test). κ = 0 is an interior
1407/// point of the analytic `S^d ← ℝ^d → H^d` family, so no boundary correction is
1408/// applied. Returns the κ̂, its tangent base point, the profile CI, and the Wilks
1409/// flatness test for the fit summary.
1410///
1411/// ## Scale-awareness and honest railing (#1104)
1412///
1413/// κ has units `1/length²`, so a cloud of characteristic geodesic radius `r`
1414/// resolves only the DIMENSIONLESS product `κ·r²` (every chart primitive depends
1415/// on `y` through `κ‖y‖²`, hence `V(κ, αy) = V(α²κ, y)` and `κ̂ ↦ κ̂/α²` under
1416/// `y ↦ αy`). The fit therefore also returns:
1417/// * `kappa_r2 = κ̂·r²` — the scale-FREE invariant the cloud actually determines
1418///   (how curved relative to its own spread), and `characteristic_radius = r`;
1419/// * `railed_at_resolution_limit` — `true` when the data want curvature at or
1420///   beyond the conjugate radius of their spread (the cloud fills the sphere),
1421///   so the search converges onto the spherical cap. There κ̂ is a LOWER BOUND on
1422///   `|κ|`, not a resolved point estimate, and the caller must report "curvature
1423///   exceeds chart-resolvable range at this scale" rather than silently quoting
1424///   `κ̂ = ci_hi`. This is the #1104 fix: a tightly-concentrated near-spherical
1425///   cloud (e.g. unit-normalised OLMo activations) no longer SILENTLY rails to a
1426///   huge scale-dependent `ci_hi` while claiming a point estimate + CI.
1427pub fn fit_response_curvature(
1428    values: ArrayView2<'_, f64>,
1429    dim: usize,
1430    level: f64,
1431    tol: f64,
1432    max_iter: usize,
1433) -> Result<ResponseCurvatureFit, ResponseGeometryError> {
1434    if dim == 0 {
1435        return Err(ResponseGeometryError::InvalidInput(
1436            "constant-curvature response geometry requires dim >= 1".into(),
1437        ));
1438    }
1439    let (n_rows, cols) = values.dim();
1440    if n_rows == 0 || cols != dim {
1441        return Err(ResponseGeometryError::InvalidInput(format!(
1442            "constant-curvature response geometry: values must be N×{dim} with N >= 1"
1443        )));
1444    }
1445    if !(level > 0.0 && level < 1.0) {
1446        return Err(ResponseGeometryError::InvalidInput(
1447            "response curvature CI level must lie in (0, 1)".into(),
1448        ));
1449    }
1450    if !(tol.is_finite() && tol > 0.0) {
1451        return Err(ResponseGeometryError::InvalidInput(
1452            "response curvature tolerance must be finite and positive".into(),
1453        ));
1454    }
1455
1456    // Establish identifiability at the flat member before constructing bounds;
1457    // a zero-dispersion point cloud carries no curvature scale.
1458    let flat_jet = response_curvature_criterion_jet(values, dim, 0.0)?;
1459    let (kappa_min, kappa_max, rho_max) = response_kappa_bounds(values);
1460    let span = kappa_max - kappa_min;
1461    let nobs = (n_rows * dim) as f64;
1462    if !(span.is_finite() && span > 0.0) {
1463        return Err(ResponseGeometryError::NumericalGeometry(
1464            "response curvature chart bracket is not finite and ordered".into(),
1465        ));
1466    }
1467
1468    // `V_p` as a closure over the criterion; threaded through both the κ̂ search
1469    // and the CI walk. Every evaluation uses the same κ-independent flat-centroid
1470    // base, so the criterion is a clean 1-D function of κ.
1471    let mut v_p = |kappa: f64| -> Result<f64, String> {
1472        response_curvature_criterion(values, dim, kappa).map(|(v, _)| v)
1473    };
1474
1475    // ── κ̂: analytic score root / constrained box-KKT solve. ─────────────
1476    // `(span/nobs)·|V'|` is dimensionless, response-scale invariant, and row-
1477    // replication invariant. At a bound only the outward score component is a
1478    // KKT violation.
1479    let normalized_kkt = |kappa: f64, score: f64| {
1480        let violation = if kappa == kappa_min {
1481            (-score).max(0.0)
1482        } else if kappa == kappa_max {
1483            score.max(0.0)
1484        } else {
1485            score.abs()
1486        };
1487        span * violation / nobs
1488    };
1489
1490    let lower = response_curvature_criterion_jet(values, dim, kappa_min)?;
1491    let upper = response_curvature_criterion_jet(values, dim, kappa_max)?;
1492    let mut a = kappa_min;
1493    let mut b = kappa_max;
1494    let mut iterations = 0_usize;
1495    let (jet, railed_at_resolution_limit, railed_at_hyperbolic_resolution_limit) = if lower.score
1496        >= 0.0
1497    {
1498        // V'(κ_min) ≥ 0: the constrained minimum sits ON the hyperbolic
1499        // chart-domain bound — the criterion is still improving as κ decreases
1500        // past the limit where the cloud fills the hyperbolic ball of its own
1501        // spread. Exactly symmetric to the spherical rail below (#2351): κ̂ is
1502        // an UPPER bound on κ, not a resolved point estimate, and must be
1503        // reported as railed rather than as a confident hyperbolic verdict.
1504        (lower, false, true)
1505    } else if upper.score <= 0.0 {
1506        // V'(κ_max)≤0 means the criterion is still improving at the
1507        // spherical chart-resolution limit.
1508        (upper, true, false)
1509    } else {
1510        let mut current = flat_jet;
1511        while iterations < max_iter {
1512            iterations += 1;
1513            if normalized_kkt(current.kappa, current.score) <= tol && current.curvature > 0.0 {
1514                break;
1515            }
1516            if current.score < 0.0 {
1517                a = current.kappa;
1518            } else {
1519                b = current.kappa;
1520            }
1521
1522            // Newton's score step supplies local quadratic convergence; the
1523            // analytic sign bracket safeguards it globally. An inadmissible
1524            // Newton point is replaced by the strictly contracting midpoint.
1525            let newton = current.kappa - current.score / current.curvature;
1526            let next = if current.curvature > 0.0 && newton.is_finite() && newton > a && newton < b
1527            {
1528                newton
1529            } else {
1530                0.5 * (a + b)
1531            };
1532            current = response_curvature_criterion_jet(values, dim, next)?;
1533        }
1534        let residual = normalized_kkt(current.kappa, current.score);
1535        if residual > tol || current.curvature <= 0.0 {
1536            return Err(ResponseGeometryError::CurvatureNonConvergence {
1537                iterations,
1538                max_iter,
1539                bracket_lo: a,
1540                bracket_hi: b,
1541                kappa: current.kappa,
1542                criterion: current.value,
1543                score: current.score,
1544                curvature: current.curvature,
1545                kkt_residual: residual,
1546                tolerance: tol,
1547            });
1548        }
1549        (current, false, false)
1550    };
1551    let kappa_hat = jet.kappa;
1552    // #2351: the hyperbolic rail flag must also fire on the BOUNDARY-LAYER
1553    // interior optimum. Near the chart-domain edge the conformal restoring
1554    // force diverges and can pin a nominally-interior stationary point a
1555    // fraction of a percent inside κ_min (measured on isotropic unit-vector
1556    // clouds: κ̂/κ_min ≈ 0.997 with p → 0). Dimensionlessly, κ̂ ≤ 0.99·κ_min
1557    // means the fitted curvature says the cloud fills ≥ 99% of the hyperbolic
1558    // ball of its own spread — the estimate is chart-limited, not resolved,
1559    // regardless of whether the KKT condition binds exactly AT the bound.
1560    let railed_at_hyperbolic_resolution_limit =
1561        railed_at_hyperbolic_resolution_limit || kappa_hat <= 0.99 * kappa_min;
1562    let v_p_hat = jet.value;
1563    let base = jet.base.clone();
1564
1565    // The upper rail flag comes only from the exact active-bound KKT condition
1566    // `V'(κ_max) ≤ 0`; proximity to a bound is not treated as convergence.
1567    // Dimensionless scale-free invariant κ̂·r²: the geometric content the cloud
1568    // actually determines (invariant under y ↦ αy). r = ρ_max is the κ=0 doubled-
1569    // gauge characteristic radius; for a degenerate (point) cloud r = 0 and the
1570    // product is 0 (κ unidentified). This is what the caller should report as the
1571    // honest "how curved relative to its spread" number alongside the dimensional κ̂.
1572    let kappa_r2 = kappa_hat * rho_max * rho_max;
1573
1574    let kappa_tol = tol * span;
1575    if !(kappa_tol.is_finite() && kappa_tol > 0.0) {
1576        return Err(ResponseGeometryError::InvalidInput(
1577            "response curvature tolerance underflows in the chart scale".into(),
1578        ));
1579    }
1580    let profile_ci = crate::curvature_estimand::profile_ci_walk(
1581        &mut v_p,
1582        kappa_hat,
1583        jet.curvature,
1584        kappa_min,
1585        kappa_max,
1586        level,
1587        kappa_tol,
1588    )
1589    .map_err(ResponseGeometryError::NumericalGeometry)?;
1590    let flatness = crate::curvature_estimand::flatness_lr_test(&mut v_p, kappa_hat)
1591        .map_err(ResponseGeometryError::NumericalGeometry)?;
1592
1593    // The sign of κ̂ is statistically resolved iff the profile CI excludes 0 — the
1594    // CI is the honest sign-bearing summary (it reports Flat under-resolution rather
1595    // than a confident wrong sign), so we mirror its verdict onto the point-estimate
1596    // surface. Below the resolvable `κ·r²` floor (`|κ·r²| ≪ 1`) the bare κ̂ argmin can
1597    // flip sign on Monte-Carlo noise, so `false` here means "do not quote κ̂'s sign".
1598    let sign_resolved = !matches!(
1599        profile_ci.verdict,
1600        crate::curvature_estimand::CurvatureVerdict::Flat
1601    );
1602
1603    Ok(ResponseCurvatureFit {
1604        dim,
1605        kappa_hat,
1606        kappa_r2,
1607        characteristic_radius: rho_max,
1608        railed_at_resolution_limit,
1609        railed_at_hyperbolic_resolution_limit,
1610        sign_resolved,
1611        base,
1612        v_p_hat,
1613        profile_ci,
1614        flatness,
1615    })
1616}
1617
1618#[cfg(test)]
1619mod tests {
1620    use super::*;
1621    use ndarray::{Array2, array};
1622
1623    fn round_trip(manifold: ResponseManifold, values: Array2<f64>) {
1624        let base =
1625            response_frechet_mean(manifold, values.view(), None, 1e-12, 500).expect("frechet mean");
1626        let tangent = response_log_map(manifold, values.view(), base.view()).expect("log map");
1627        let back = response_exp_map(manifold, tangent.view(), base.view()).expect("exp map");
1628        for row in 0..values.nrows() {
1629            for col in 0..values.ncols() {
1630                assert!(
1631                    (back[[row, col]] - values[[row, col]]).abs() < 1e-6,
1632                    "{manifold:?} exp∘log mismatch at ({row},{col}): {} vs {}",
1633                    back[[row, col]],
1634                    values[[row, col]]
1635                );
1636            }
1637        }
1638    }
1639
1640    #[test]
1641    fn spd_round_trip_and_mean() {
1642        // Three 2×2 SPD matrices, row-major flat.
1643        let values = array![
1644            [2.0, 0.0, 0.0, 1.0],
1645            [1.0, 0.3, 0.3, 2.0],
1646            [3.0, -0.5, -0.5, 1.5],
1647        ];
1648        round_trip(ResponseManifold::Spd { n: 2 }, values);
1649    }
1650
1651    #[test]
1652    fn grassmann_round_trip_and_mean() {
1653        // Gr(1, 3): unit columns (lines through the origin), n·k = 3 flat.
1654        let (c1, s1) = (0.2_f64.cos(), 0.2_f64.sin());
1655        let (c2, s2) = (0.35_f64.cos(), 0.35_f64.sin());
1656        let values = array![[1.0, 0.0, 0.0], [c1, s1, 0.0], [c2, s2, 0.0],];
1657        round_trip(ResponseManifold::Grassmann { k: 1, n: 3 }, values);
1658    }
1659
1660    #[test]
1661    fn stiefel_round_trip_and_mean() {
1662        // St(1, 3): unit 1-frames in ℝ³ (== sphere S²).
1663        let (c1, s1) = (0.2_f64.cos(), 0.2_f64.sin());
1664        let (c2, s2) = (0.3_f64.cos(), 0.3_f64.sin());
1665        let values = array![[1.0, 0.0, 0.0], [c1, s1, 0.0], [c2, 0.0, s2],];
1666        round_trip(ResponseManifold::Stiefel { k: 1, n: 3 }, values);
1667    }
1668
1669    #[test]
1670    fn stiefel_k2_round_trip_and_mean_n_lt_2k() {
1671        // St(3, 2): three orthonormal 2-frames in ℝ³ clustered near [e0, e1],
1672        // exercising the genuine canonical-metric logarithm (k ≥ 2) through the
1673        // full Karcher-mean → log → exp round trip. This is the n < 2k regime
1674        // (n = 3 < 2k = 4) where the economical 2k-block form is rank-deficient.
1675        // Before the k ≥ 2 Stiefel logarithm existed this aborted in
1676        // Fréchet-mean init with a misleading cut-locus error (#1637).
1677        let (c2, s2) = (0.2_f64.cos(), 0.2_f64.sin());
1678        let (c1, s1) = (0.15_f64.cos(), 0.15_f64.sin());
1679        let values = array![
1680            [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
1681            [c2, 0.0, 0.0, 1.0, s2, 0.0],
1682            [1.0, 0.0, 0.0, c1, 0.0, s1],
1683        ];
1684        round_trip(ResponseManifold::Stiefel { k: 2, n: 3 }, values);
1685    }
1686
1687    #[test]
1688    fn stiefel_k2_round_trip_and_mean_n_ge_2k() {
1689        // St(4, 2): the n ≥ 2k regime (n = 4 = 2k), clustered 2-frames in ℝ⁴.
1690        let (c0, s0) = (0.1_f64.cos(), 0.1_f64.sin());
1691        let (c1, s1) = (0.12_f64.cos(), 0.12_f64.sin());
1692        let values = array![
1693            [1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
1694            [c0, 0.0, 0.0, 1.0, s0, 0.0, 0.0, 0.0],
1695            [1.0, 0.0, 0.0, c1, 0.0, 0.0, 0.0, s1],
1696        ];
1697        round_trip(ResponseManifold::Stiefel { k: 2, n: 4 }, values);
1698    }
1699
1700    #[test]
1701    fn poincare_round_trip_and_mean() {
1702        let values = array![[0.1, 0.2], [-0.3, 0.1], [0.2, -0.25],];
1703        round_trip(
1704            ResponseManifold::Poincare {
1705                dim: 2,
1706                curvature: -1.0,
1707            },
1708            values,
1709        );
1710    }
1711
1712    /// Deterministic Fibonacci-lattice cover of S² (== `St(3,1)` == `Gr(1,3)`
1713    /// projectively), spread over the WHOLE sphere. This is the widely spread
1714    /// cloud that makes the Fréchet objective nearly flat, so a single-seed
1715    /// Karcher descent converges only linearly and exhausts a `max_iter=256`
1716    /// budget — the #2140 trigger.
1717    fn fibonacci_sphere(n: usize) -> Array2<f64> {
1718        let mut v = Array2::<f64>::zeros((n, 3));
1719        let golden = std::f64::consts::PI * (1.0 + 5.0_f64.sqrt());
1720        for idx in 0..n {
1721            let i = idx as f64 + 0.5;
1722            let phi = (1.0 - 2.0 * i / n as f64).acos();
1723            let theta = golden * i;
1724            v[[idx, 0]] = theta.cos() * phi.sin();
1725            v[[idx, 1]] = theta.sin() * phi.sin();
1726            v[[idx, 2]] = phi.cos();
1727        }
1728        v
1729    }
1730
1731    /// Analytic Karcher stationarity residual for a uniform-weight cloud.
1732    fn frechet_residual(
1733        manifold: ResponseManifold,
1734        values: ArrayView2<'_, f64>,
1735        p: ArrayView1<'_, f64>,
1736    ) -> f64 {
1737        let mut xi = Array1::<f64>::zeros(values.ncols());
1738        for row in 0..values.nrows() {
1739            let lg = manifold.log_point(p, values.row(row)).expect("log map");
1740            xi.scaled_add(1.0 / values.nrows() as f64, &lg);
1741        }
1742        manifold
1743            .sq_metric_norm(p, xi.view())
1744            .expect("metric norm")
1745            .sqrt()
1746    }
1747
1748    #[test]
1749    fn successful_stiefel_k1_frechet_mean_is_analytically_stationary() {
1750        let inv = 1.0 / 1.01_f64.sqrt();
1751        let values = array![
1752            [1.0, 0.0, 0.0],
1753            [inv, 0.1 * inv, 0.0],
1754            [inv, 0.0, -0.1 * inv],
1755            [inv, -0.1 * inv, 0.0],
1756        ];
1757        let manifold = ResponseManifold::Stiefel { k: 1, n: 3 };
1758        let tol = 1.0e-10;
1759        let mean = response_frechet_mean(manifold, values.view(), None, tol, 256)
1760            .expect("tight sphere cloud must reach the Karcher certificate");
1761
1762        assert_eq!(mean.len(), 3);
1763        let nrm = (mean[0] * mean[0] + mean[1] * mean[1] + mean[2] * mean[2]).sqrt();
1764        assert!(
1765            (nrm - 1.0).abs() < 1e-9,
1766            "mean must be unit-norm, got {nrm}"
1767        );
1768        let residual = frechet_residual(manifold, values.view(), mean.view());
1769        assert!(
1770            residual <= tol,
1771            "successful mean residual {residual:.3e} exceeds tolerance {tol:.3e}"
1772        );
1773    }
1774
1775    #[test]
1776    fn budget_exhausted_generic_frechet_is_typed_non_convergence() {
1777        let values = fibonacci_sphere(60);
1778        for manifold in [
1779            ResponseManifold::Stiefel { k: 1, n: 3 },
1780            ResponseManifold::Grassmann { k: 1, n: 3 },
1781        ] {
1782            match response_frechet_mean(manifold, values.view(), None, 1.0e-30, 0) {
1783                Err(GeometryError::NonConvergence {
1784                    context,
1785                    iterations,
1786                    residual,
1787                    tolerance,
1788                }) => {
1789                    assert_eq!(context, "response geometry Fréchet mean");
1790                    assert_eq!(iterations, 0);
1791                    assert!(residual.is_finite() && residual > tolerance);
1792                }
1793                other => panic!("{manifold:?} expected typed exhaustion, got {other:?}"),
1794            }
1795        }
1796    }
1797
1798    #[test]
1799    fn frechet_global_uniqueness_radii_are_geometry_derived() {
1800        assert_eq!(
1801            ResponseManifold::Spd { n: 2 }.frechet_uniqueness_radius(),
1802            None
1803        );
1804        assert_eq!(
1805            ResponseManifold::Poincare {
1806                dim: 2,
1807                curvature: -1.0
1808            }
1809            .frechet_uniqueness_radius(),
1810            None
1811        );
1812        assert_eq!(
1813            ResponseManifold::Stiefel { k: 1, n: 3 }.frechet_uniqueness_radius(),
1814            Some(std::f64::consts::FRAC_PI_4)
1815        );
1816        assert_eq!(
1817            ResponseManifold::Grassmann { k: 2, n: 4 }.frechet_uniqueness_radius(),
1818            Some(std::f64::consts::PI / (4.0 * 2.0_f64.sqrt()))
1819        );
1820        assert_eq!(
1821            ResponseManifold::ConstantCurvature { dim: 2, kappa: 4.0 }.frechet_uniqueness_radius(),
1822            Some(std::f64::consts::PI / 8.0)
1823        );
1824        assert_eq!(
1825            ResponseManifold::ConstantCurvature {
1826                dim: 2,
1827                kappa: -3.0
1828            }
1829            .frechet_uniqueness_radius(),
1830            None
1831        );
1832
1833        // A tight SPD cluster still converges to the unique Hadamard mean.
1834        let values = array![
1835            [2.0, 0.0, 0.0, 1.0],
1836            [2.1, 0.05, 0.05, 1.02],
1837            [1.95, -0.03, -0.03, 0.98],
1838        ];
1839        let mean = response_frechet_mean(
1840            ResponseManifold::Spd { n: 2 },
1841            values.view(),
1842            None,
1843            1e-12,
1844            500,
1845        )
1846        .expect("SPD cluster must converge");
1847        assert!(mean.iter().all(|c| c.is_finite()));
1848    }
1849
1850    #[test]
1851    fn diffuse_positive_curvature_cloud_has_typed_global_certificate_error() {
1852        let manifold = ResponseManifold::Stiefel { k: 1, n: 2 };
1853        let angle = 0.9_f64;
1854        let values = array![[angle.cos(), -angle.sin()], [angle.cos(), angle.sin()],];
1855        for cloud in [
1856            values.clone(),
1857            values.slice(ndarray::s![..;-1, ..]).to_owned(),
1858        ] {
1859            match response_frechet_mean(manifold, cloud.view(), None, 1.0e-12, 256) {
1860                Err(GeometryError::FrechetMeanNotGloballyCertified {
1861                    stationarity_residual,
1862                    tolerance,
1863                    support_radius,
1864                    uniqueness_radius,
1865                    ..
1866                }) => {
1867                    assert!(stationarity_residual <= tolerance);
1868                    assert!(support_radius >= uniqueness_radius);
1869                    assert_eq!(uniqueness_radius, std::f64::consts::FRAC_PI_4);
1870                }
1871                other => panic!("expected diffuse-cloud certificate error, got {other:?}"),
1872            }
1873        }
1874    }
1875
1876    #[test]
1877    fn tight_positive_curvature_mean_is_permutation_invariant_beyond_eight_rows() {
1878        let manifold = ResponseManifold::Stiefel { k: 1, n: 2 };
1879        let angles = [
1880            -0.20_f64, -0.16, -0.12, -0.08, -0.04, 0.0, 0.03, 0.06, 0.09, 0.12, 0.15, 0.18,
1881        ];
1882        let mut values = Array2::<f64>::zeros((angles.len(), 2));
1883        for (row, angle) in angles.into_iter().enumerate() {
1884            values[[row, 0]] = angle.cos();
1885            values[[row, 1]] = angle.sin();
1886        }
1887        let reversed = values.slice(ndarray::s![..;-1, ..]).to_owned();
1888        let direct = response_frechet_mean(manifold, values.view(), None, 1.0e-12, 256)
1889            .expect("tight cloud has a certified global mean");
1890        let permuted = response_frechet_mean(manifold, reversed.view(), None, 1.0e-12, 256)
1891            .expect("permuted tight cloud has a certified global mean");
1892        assert!(
1893            (&direct - &permuted)
1894                .iter()
1895                .all(|value| value.abs() <= 1.0e-12)
1896        );
1897        assert!(frechet_residual(manifold, values.view(), direct.view()) <= 1.0e-12);
1898    }
1899
1900    #[test]
1901    fn zero_weight_cut_locus_rows_do_not_affect_mean_or_certificate() {
1902        let manifold = ResponseManifold::Stiefel { k: 1, n: 2 };
1903        let values = array![[1.0, 0.0], [-1.0, 0.0]];
1904        let weights = array![1.0, 0.0];
1905        let mean =
1906            response_frechet_mean(manifold, values.view(), Some(weights.view()), 1.0e-12, 32)
1907                .expect("zero-mass cut-locus row must be ignored");
1908        assert!(
1909            (&mean - &values.row(0))
1910                .iter()
1911                .all(|value| value.abs() <= f64::EPSILON)
1912        );
1913    }
1914
1915    #[test]
1916    fn resolver_rejects_bad_shapes() {
1917        assert!(ResponseManifold::resolve("grassmann", Some(2), Some(3), None, None).is_err());
1918        assert!(ResponseManifold::resolve("spd", None, None, None, None).is_err());
1919        assert!(ResponseManifold::resolve("poincare", None, None, Some(2), Some(1.0)).is_err());
1920        assert!(ResponseManifold::resolve("nonsense", None, None, None, None).is_err());
1921        assert_eq!(
1922            ResponseManifold::resolve("spd", Some(3), None, None, None).unwrap(),
1923            ResponseManifold::Spd { n: 3 }
1924        );
1925    }
1926
1927    #[test]
1928    fn parse_infers_shapes_from_columns() {
1929        // SPD: n from the perfect-square column count.
1930        assert_eq!(
1931            ResponseManifold::parse("spd", 9).unwrap(),
1932            ResponseManifold::Spd { n: 3 }
1933        );
1934        assert!(ResponseManifold::parse("spd", 8).is_err());
1935        // Grassmann/Stiefel: n inferred as cols / k.
1936        assert_eq!(
1937            ResponseManifold::parse("grassmann(k=2)", 10).unwrap(),
1938            ResponseManifold::Grassmann { k: 2, n: 5 }
1939        );
1940        assert_eq!(
1941            ResponseManifold::parse("Stiefel( k = 2 , n = 4 )", 8).unwrap(),
1942            ResponseManifold::Stiefel { k: 2, n: 4 }
1943        );
1944        assert!(ResponseManifold::parse("grassmann", 10).is_err());
1945        assert!(ResponseManifold::parse("grassmann(k=3)", 10).is_err());
1946        // Poincaré: dim = cols, default curvature -1.
1947        assert_eq!(
1948            ResponseManifold::parse("poincare", 3).unwrap(),
1949            ResponseManifold::Poincare {
1950                dim: 3,
1951                curvature: -1.0
1952            }
1953        );
1954        assert_eq!(
1955            ResponseManifold::parse("poincare(curvature=-0.5)", 3).unwrap(),
1956            ResponseManifold::Poincare {
1957                dim: 3,
1958                curvature: -0.5
1959            }
1960        );
1961        assert!(ResponseManifold::parse("hyperbolic", 3).is_err());
1962    }
1963
1964    #[test]
1965    fn dispatch_round_trips_through_user_label() {
1966        // Drive the full string-selected user path for each geometry: parse the
1967        // label, build the intrinsic base, log to the tangent, exp back.
1968        let cases: Vec<(&str, Array2<f64>)> = vec![
1969            (
1970                "spd",
1971                array![
1972                    [2.0, 0.0, 0.0, 1.0],
1973                    [1.0, 0.3, 0.3, 2.0],
1974                    [3.0, -0.5, -0.5, 1.5],
1975                ],
1976            ),
1977            (
1978                "grassmann(k=1)",
1979                array![
1980                    [1.0, 0.0, 0.0],
1981                    [0.2_f64.cos(), 0.2_f64.sin(), 0.0],
1982                    [0.35_f64.cos(), 0.35_f64.sin(), 0.0],
1983                ],
1984            ),
1985            (
1986                "stiefel(k=1)",
1987                array![
1988                    [1.0, 0.0, 0.0],
1989                    [0.2_f64.cos(), 0.2_f64.sin(), 0.0],
1990                    [0.3_f64.cos(), 0.0, 0.3_f64.sin()],
1991                ],
1992            ),
1993            ("poincare", array![[0.1, 0.2], [-0.3, 0.1], [0.2, -0.25]]),
1994        ];
1995        for (label, values) in cases {
1996            let (tangent, base, canonical) =
1997                dispatch_log_map(values.view(), label, None, None).expect("dispatch log");
1998            assert!(canonical.starts_with(label.split('(').next().unwrap()));
1999            let back = dispatch_exp_map(tangent.view(), label, base.view()).expect("dispatch exp");
2000            for row in 0..values.nrows() {
2001                for col in 0..values.ncols() {
2002                    assert!(
2003                        (back[[row, col]] - values[[row, col]]).abs() < 1e-6,
2004                        "{label} exp∘log mismatch at ({row},{col}): {} vs {}",
2005                        back[[row, col]],
2006                        values[[row, col]]
2007                    );
2008                }
2009            }
2010        }
2011    }
2012
2013    #[test]
2014    fn ambient_dim_matches_layout() {
2015        assert_eq!(ResponseManifold::Spd { n: 3 }.ambient_dim(), 9);
2016        assert_eq!(ResponseManifold::Grassmann { k: 2, n: 5 }.ambient_dim(), 10);
2017        assert_eq!(ResponseManifold::Stiefel { k: 2, n: 4 }.ambient_dim(), 8);
2018        assert_eq!(
2019            ResponseManifold::Poincare {
2020                dim: 4,
2021                curvature: -1.0
2022            }
2023            .ambient_dim(),
2024            4
2025        );
2026    }
2027
2028    /// #2125: a weighted response-geometry fit must linearize around the
2029    /// *weighted* Fréchet mean. `dispatch_log_map` picks the tangent base point;
2030    /// before the fix it hard-passed `None` for the weights, so the chart origin
2031    /// was the unweighted intrinsic mean even when the tangent regression was
2032    /// weighted — a biased linearization. Here Stiefel(k=1,n=3) is the sphere S²:
2033    /// two separated clusters, both inside the certified convexity ball, have
2034    /// weights concentrated on the first cluster and must move the base toward it.
2035    #[test]
2036    fn dispatch_log_map_uses_weighted_frechet_mean() {
2037        let a = 0.05_f64;
2038        let separation = 0.6_f64;
2039        // Two clusters on the great circle z = 0: cluster A about [1,0,0]
2040        // (rows 0,1) and cluster B `separation` radians away (rows 2,3).
2041        // Every row is an exact unit vector (cos²+sin²=1).
2042        let values = array![
2043            [a.cos(), a.sin(), 0.0],
2044            [(-a).cos(), (-a).sin(), 0.0],
2045            [(separation - a).cos(), (separation - a).sin(), 0.0],
2046            [(separation + a).cos(), (separation + a).sin(), 0.0],
2047        ];
2048        // Heavily weight cluster A: the weighted mean must sit near [1,0,0],
2049        // whereas the unweighted mean sits near the 45° bisector.
2050        let weights = array![50.0_f64, 50.0, 1.0, 1.0];
2051        let manifold = ResponseManifold::Stiefel { k: 1, n: 3 };
2052
2053        let geodesic = |u: ArrayView1<'_, f64>, v: ArrayView1<'_, f64>| -> f64 {
2054            u.dot(&v).clamp(-1.0, 1.0).acos()
2055        };
2056
2057        let unweighted_ref =
2058            response_frechet_mean(manifold, values.view(), None, 1e-12, 256).expect("unweighted");
2059        let weighted_ref =
2060            response_frechet_mean(manifold, values.view(), Some(weights.view()), 1e-12, 256)
2061                .expect("weighted");
2062        // Sanity: the two intrinsic means genuinely differ, so this design can
2063        // distinguish a weighted from an unweighted base point.
2064        assert!(
2065            geodesic(unweighted_ref.view(), weighted_ref.view()) > 0.2,
2066            "test design degenerate: weighted and unweighted means nearly coincide"
2067        );
2068
2069        let (_t_uw, base_uw, _c) =
2070            dispatch_log_map(values.view(), "stiefel(k=1)", None, None).expect("unweighted chart");
2071        let (_t_w, base_w, _c) =
2072            dispatch_log_map(values.view(), "stiefel(k=1)", None, Some(weights.view()))
2073                .expect("weighted chart");
2074
2075        // (a) Supplying weights must change the base point (before the fix the
2076        // weighted chart origin was byte-identical to the unweighted one).
2077        let moved = base_w
2078            .iter()
2079            .zip(base_uw.iter())
2080            .any(|(w, u)| (w - u).abs() > 1e-9);
2081        assert!(
2082            moved,
2083            "weighted base point is identical to the unweighted one: weights ignored"
2084        );
2085
2086        // (b) The weighted base point must be closer to the WEIGHTED Fréchet
2087        // mean than to the unweighted one.
2088        let d_to_weighted = geodesic(base_w.view(), weighted_ref.view());
2089        let d_to_unweighted = geodesic(base_w.view(), unweighted_ref.view());
2090        assert!(
2091            d_to_weighted < d_to_unweighted,
2092            "weighted base point is nearer the unweighted mean ({d_to_unweighted}) \
2093             than the weighted mean ({d_to_weighted})"
2094        );
2095        // And it should essentially coincide with the weighted mean.
2096        assert!(
2097            d_to_weighted < 1e-6,
2098            "weighted base point is {d_to_weighted} from the weighted Fréchet mean"
2099        );
2100    }
2101
2102    /// Deterministic xorshift64* + Box–Muller standard normals — a dependency-free
2103    /// reproducible source for the synthetic known-κ clouds. Seeded per call so
2104    /// the test is bit-stable across runs and platforms.
2105    struct DetNormal {
2106        state: u64,
2107        spare: Option<f64>,
2108    }
2109    impl DetNormal {
2110        fn new(seed: u64) -> Self {
2111            Self {
2112                state: seed | 1,
2113                spare: None,
2114            }
2115        }
2116        fn u01(&mut self) -> f64 {
2117            // xorshift64*; take the top 53 bits as a (0,1) double.
2118            let mut x = self.state;
2119            x ^= x >> 12;
2120            x ^= x << 25;
2121            x ^= x >> 27;
2122            self.state = x;
2123            let v = x.wrapping_mul(0x2545_F491_4F6C_DD1D);
2124            ((v >> 11) as f64 + 0.5) / (1u64 << 53) as f64
2125        }
2126        fn normal(&mut self) -> f64 {
2127            if let Some(z) = self.spare.take() {
2128                return z;
2129            }
2130            // Box–Muller; clamp u1 away from 0 so ln is finite.
2131            let u1 = self.u01().max(1e-12);
2132            let u2 = self.u01();
2133            let r = (-2.0 * u1.ln()).sqrt();
2134            let theta = 2.0 * std::f64::consts::PI * u2;
2135            self.spare = Some(r * theta.sin());
2136            r * theta.cos()
2137        }
2138    }
2139
2140    /// Build a synthetic cloud at known curvature `k_star`: `n` points whose
2141    /// geodesic normal coordinates about `center` are i.i.d. isotropic Gaussian
2142    /// of scale `sigma`, exp-mapped onto `M_{k_star}`, then mean-centred in the
2143    /// ambient chart to mimic the real (mean-subtracted) response clouds.
2144    fn synth_cloud(dim: usize, k_star: f64, n: usize, sigma: f64, seed: u64) -> Array2<f64> {
2145        let manifold = ResponseManifold::ConstantCurvature { dim, kappa: k_star };
2146        let center = Array1::<f64>::zeros(dim);
2147        let mut rng = DetNormal::new(seed);
2148        let mut values = Array2::<f64>::zeros((n, dim));
2149        for i in 0..n {
2150            let t: Array1<f64> = (0..dim).map(|_| sigma * rng.normal()).collect();
2151            let y = manifold
2152                .exp_point(center.view(), t.view())
2153                .expect("exp tangent to response");
2154            values.row_mut(i).assign(&y);
2155        }
2156        // Mean-centre in the ambient chart (the real-data preprocessing).
2157        let mut mean = Array1::<f64>::zeros(dim);
2158        for row in values.outer_iter() {
2159            mean += &row;
2160        }
2161        mean.mapv_inplace(|v| v / n as f64);
2162        for mut row in values.outer_iter_mut() {
2163            row -= &mean;
2164        }
2165        values
2166    }
2167
2168    #[test]
2169    fn response_curvature_criterion_jet_matches_finite_difference_oracle() {
2170        // Test-only central differences verify the hand-derived score and
2171        // Hessian on both sides of the flat member. Production fitting uses
2172        // only `response_curvature_criterion_jet`.
2173        let values = array![
2174            [0.18, -0.07],
2175            [-0.11, 0.16],
2176            [0.04, 0.21],
2177            [-0.15, -0.09],
2178            [0.09, -0.13],
2179        ];
2180        let h = 1.0e-5;
2181        for kappa in [-0.8, 0.0, 0.9] {
2182            let jet = response_curvature_criterion_jet(values.view(), 2, kappa)
2183                .expect("analytic curvature jet");
2184            let plus = response_curvature_criterion_jet(values.view(), 2, kappa + h)
2185                .expect("positive finite-difference probe");
2186            let minus = response_curvature_criterion_jet(values.view(), 2, kappa - h)
2187                .expect("negative finite-difference probe");
2188            let score_fd = (plus.value - minus.value) / (2.0 * h);
2189            let curvature_fd = (plus.score - minus.score) / (2.0 * h);
2190            let score_scale = 1.0 + jet.score.abs().max(score_fd.abs());
2191            let curvature_scale = 1.0 + jet.curvature.abs().max(curvature_fd.abs());
2192            assert!(
2193                (jet.score - score_fd).abs() <= 2.0e-8 * score_scale,
2194                "kappa={kappa}: analytic score {} != FD {score_fd}",
2195                jet.score
2196            );
2197            assert!(
2198                (jet.curvature - curvature_fd).abs() <= 2.0e-8 * curvature_scale,
2199                "kappa={kappa}: analytic curvature {} != FD {curvature_fd}",
2200                jet.curvature
2201            );
2202        }
2203    }
2204
2205    #[test]
2206    fn response_curvature_budget_exhaustion_is_typed_non_convergence() {
2207        let values = synth_cloud(3, 0.8, 80, 0.15, 0xC0A7_2247);
2208        match fit_response_curvature(values.view(), 3, 0.95, 1.0e-14, 0) {
2209            Err(ResponseGeometryError::CurvatureNonConvergence {
2210                iterations,
2211                max_iter,
2212                kkt_residual,
2213                tolerance,
2214                score,
2215                curvature,
2216                ..
2217            }) => {
2218                assert_eq!(iterations, 0);
2219                assert_eq!(max_iter, 0);
2220                assert!(kkt_residual.is_finite() && kkt_residual > tolerance);
2221                assert!(score.is_finite() && curvature.is_finite());
2222            }
2223            other => panic!("expected typed curvature exhaustion, got {other:?}"),
2224        }
2225    }
2226
2227    /// The #1104 reparameterisation-invariant curvature estimator: on synthetic
2228    /// clouds generated at known κ⋆ the fitted κ̂ must be (a) INTERIOR to the
2229    /// chart bracket (never railed), (b) close to κ⋆ and MONOTONE in κ⋆, (c)
2230    /// produce a smooth (non-degenerate) χ²₁ flatness p-value that does not reject
2231    /// the flat truth, and (d) be correctly COVARIANT under a global rescaling of
2232    /// the cloud (κ has units 1/length², so `y ↦ α y ⇒ κ̂ ↦ κ̂/α²`).
2233    #[test]
2234    fn fit_response_curvature_is_reparameterization_invariant() {
2235        let dim = 3usize;
2236        // Unit-ish scale: σ=0.15 keeps every geodesic radius (≈ a few·σ) well
2237        // inside the κ-stereographic chart for the most hyperbolic κ⋆ = −1.5
2238        // (chart needs ‖y‖² < 1/1.5 ≈ 0.667).
2239        let sigma = 0.15;
2240        let n = 300usize;
2241        let k_stars = [-1.5_f64, -0.5, 0.0, 0.6, 1.2];
2242        let mut k_hats = Vec::new();
2243        for (idx, &k_star) in k_stars.iter().enumerate() {
2244            let values = synth_cloud(dim, k_star, n, sigma, 0xC0FFEE ^ (idx as u64 + 1));
2245            let (kmin, kmax, _rho) = response_kappa_bounds(values.view());
2246            let fit = fit_response_curvature(values.view(), dim, 0.95, 1e-12, 256)
2247                .expect("response curvature fit");
2248            k_hats.push(fit.kappa_hat);
2249
2250            // (a) INTERIOR: κ̂ strictly inside the bracket, not railed to either end.
2251            let span = kmax - kmin;
2252            assert!(
2253                fit.kappa_hat > kmin + 0.02 * span && fit.kappa_hat < kmax - 0.02 * span,
2254                "κ⋆={k_star}: κ̂={} railed to bracket [{kmin}, {kmax}]",
2255                fit.kappa_hat
2256            );
2257
2258            // (b-direct) recovery within a sane tolerance (finite-sample bias is
2259            // O(1/n); the estimator only needs the right region and sign).
2260            assert!(
2261                (fit.kappa_hat - k_star).abs() <= 0.6 + 0.3 * k_star.abs(),
2262                "κ⋆={k_star}: κ̂={} too far",
2263                fit.kappa_hat
2264            );
2265
2266            // (c) the profile CI is a valid interval bracketing κ̂.
2267            assert!(
2268                fit.profile_ci.ci_lo <= fit.kappa_hat && fit.kappa_hat <= fit.profile_ci.ci_hi,
2269                "κ⋆={k_star}: CI [{}, {}] excludes κ̂={}",
2270                fit.profile_ci.ci_lo,
2271                fit.profile_ci.ci_hi,
2272                fit.kappa_hat
2273            );
2274            // The flatness LR statistic and p-value are valid; the p-value is a
2275            // genuine probability strictly between 0 and 1 (smooth, not 0/1).
2276            assert!(fit.flatness.lr_stat >= 0.0);
2277            assert!(
2278                fit.flatness.p_value > 0.0 && fit.flatness.p_value < 1.0,
2279                "κ⋆={k_star}: degenerate flatness p={}",
2280                fit.flatness.p_value
2281            );
2282            // The flat truth κ⋆ = 0 must NOT be rejected at 5% (lr < χ²_{1,.95}).
2283            if k_star == 0.0 {
2284                assert!(
2285                    fit.flatness.lr_stat < 3.84,
2286                    "flat truth wrongly rejected: lr={}",
2287                    fit.flatness.lr_stat
2288                );
2289            }
2290
2291            // (d) RESCALING COVARIANCE: scale the SAME cloud by α and refit; κ̂
2292            // must transform as κ̂/α² (curvature has units 1/length²). We reuse the
2293            // identical points so the only change is the global scale.
2294            let alpha = 1.5_f64;
2295            let scaled = values.mapv(|v| alpha * v);
2296            let fit_scaled = fit_response_curvature(scaled.view(), dim, 0.95, 1e-12, 256)
2297                .expect("scaled response curvature fit");
2298            let expected = fit.kappa_hat / (alpha * alpha);
2299            // Tolerance scales with magnitude; the transform is exact in the
2300            // criterion (V(κ, αy) = V(α²κ, y)) up to the analytic score
2301            // solve's floating-point tolerance.
2302            assert!(
2303                (fit_scaled.kappa_hat - expected).abs() <= 0.05 + 0.05 * expected.abs(),
2304                "κ⋆={k_star}: rescale covariance broken: κ̂(αy)={} vs κ̂(y)/α²={}",
2305                fit_scaled.kappa_hat,
2306                expected
2307            );
2308        }
2309
2310        // (b-monotone) κ̂ is monotone increasing in κ⋆ across the whole sweep.
2311        for w in k_hats.windows(2) {
2312            assert!(w[1] > w[0] - 0.05, "κ̂ not monotone in κ⋆: {:?}", k_hats);
2313        }
2314
2315        // (e) TRANSLATION INVARIANCE (#2351): a rigid ambient translation is a
2316        // no-op for the cloud's intrinsic shape, so κ̂, the verdict, the
2317        // scale-free invariant, and both rail flags must be unchanged to
2318        // numerical identity. This is the direct regression guard for the
2319        // ambient-origin κ_min/conformal-term bug.
2320        let values = synth_cloud(dim, 0.6, n, sigma, 0xC0FFEE ^ 4);
2321        let fit = fit_response_curvature(values.view(), dim, 0.95, 1e-12, 256)
2322            .expect("untranslated fit");
2323        let shifted = &values + 10.0;
2324        let fit_shifted = fit_response_curvature(shifted.view(), dim, 0.95, 1e-12, 256)
2325            .expect("translated fit");
2326        assert!(
2327            (fit.kappa_hat - fit_shifted.kappa_hat).abs()
2328                <= 1.0e-9 * (1.0 + fit.kappa_hat.abs()),
2329            "κ̂ moved under pure translation: {} vs {}",
2330            fit.kappa_hat,
2331            fit_shifted.kappa_hat
2332        );
2333        assert_eq!(fit.profile_ci.verdict, fit_shifted.profile_ci.verdict);
2334        assert!(
2335            (fit.kappa_r2 - fit_shifted.kappa_r2).abs() <= 1.0e-9 * (1.0 + fit.kappa_r2.abs())
2336        );
2337        assert_eq!(
2338            fit.railed_at_resolution_limit,
2339            fit_shifted.railed_at_resolution_limit
2340        );
2341        assert_eq!(
2342            fit.railed_at_hyperbolic_resolution_limit,
2343            fit_shifted.railed_at_hyperbolic_resolution_limit
2344        );
2345    }
2346
2347    /// d = 1 carries REDUCED curvature information: the transverse volume
2348    /// Jacobian is identically 1 (radial isometry), so κ is identified by the
2349    /// conformal-factor restoring force `−d·Σ ln λ_{y_i}` alone (#944 power
2350    /// analysis). The estimator must still run end-to-end, return an INTERIOR
2351    /// κ̂, and produce a valid CI — never divide/exponentiate the absent
2352    /// transverse direction.
2353    #[test]
2354    fn fit_response_curvature_d1_uses_conformal_term_only() {
2355        let sigma = 0.12;
2356        let n = 400usize;
2357        for &k_star in &[-1.0_f64, 0.0, 0.8] {
2358            let values = synth_cloud(1, k_star, n, sigma, 0xD1 ^ (k_star.to_bits()));
2359            let (kmin, kmax, _rho) = response_kappa_bounds(values.view());
2360            let fit = fit_response_curvature(values.view(), 1, 0.95, 1e-12, 256)
2361                .expect("d=1 curvature fit");
2362            let span = kmax - kmin;
2363            assert!(
2364                fit.kappa_hat > kmin + 0.01 * span && fit.kappa_hat < kmax - 0.01 * span,
2365                "d=1 κ⋆={k_star}: κ̂={} railed to [{kmin},{kmax}]",
2366                fit.kappa_hat
2367            );
2368            assert!(
2369                fit.profile_ci.ci_lo <= fit.kappa_hat && fit.kappa_hat <= fit.profile_ci.ci_hi,
2370                "d=1 κ⋆={k_star}: CI excludes κ̂"
2371            );
2372            assert!(fit.kappa_hat.is_finite() && fit.v_p_hat.is_finite());
2373        }
2374    }
2375
2376    /// The criterion guard must reject κ probes AT or PAST the chart boundary
2377    /// gracefully (an `Err`, never a panic / NaN): on the hyperbolic edge
2378    /// `1 + κ‖y‖² ≤ 0` and on the spherical antipode. The `response_kappa_bounds`
2379    /// bracket stays strictly interior, but a stray CI/LR probe can land on the
2380    /// edge, so the criterion itself must be defensive.
2381    #[test]
2382    fn response_curvature_criterion_rejects_boundary_probes() {
2383        // #2351: the chart evaluates on mean-centred coordinates, so the
2384        // hyperbolic edge is κ = −1/max‖y−μ‖² (centroid-relative spread).
2385        let values = array![[0.5_f64, 0.0], [-0.4, 0.3], [0.1, -0.5]];
2386        let centroid = {
2387            let mut c = Array1::<f64>::zeros(2);
2388            for row in values.outer_iter() {
2389                c += &row;
2390            }
2391            c.mapv(|v| v / values.nrows() as f64)
2392        };
2393        let s2_max = values
2394            .outer_iter()
2395            .map(|r| {
2396                let z = &r - &centroid;
2397                z.dot(&z)
2398            })
2399            .fold(0.0_f64, f64::max);
2400        // Exactly on / past the hyperbolic edge: 1 + κ‖y−μ‖² = 0 (or < 0).
2401        let kappa_edge = -1.0 / s2_max;
2402        assert!(
2403            response_curvature_criterion(values.view(), 2, kappa_edge).is_err(),
2404            "criterion must reject the hyperbolic chart edge κ=−1/R²"
2405        );
2406        assert!(
2407            response_curvature_criterion(values.view(), 2, 1.5 * kappa_edge).is_err(),
2408            "criterion must reject past the hyperbolic chart edge"
2409        );
2410        // Interior κ just inside the edge succeeds and is finite.
2411        let (v, _) = response_curvature_criterion(values.view(), 2, 0.9 * kappa_edge)
2412            .expect("interior κ valid");
2413        assert!(v.is_finite());
2414        // Non-finite κ is rejected up front.
2415        assert!(response_curvature_criterion(values.view(), 2, f64::NAN).is_err());
2416        assert!(response_curvature_criterion(values.view(), 2, f64::INFINITY).is_err());
2417    }
2418
2419    // ── Projection residual (distance to candidate manifold) ───────────────
2420
2421    #[test]
2422    fn projection_residual_is_zero_for_on_manifold_points() {
2423        // On-manifold rows are their own nearest point, so the residual is ~0
2424        // row-wise. No base point / Fréchet mean is involved — projection is
2425        // base-independent — so this no longer depends on the inputs forming an
2426        // admissible Karcher seed.
2427        let cases: Vec<(ResponseManifold, Array2<f64>)> = vec![
2428            (
2429                ResponseManifold::Spd { n: 2 }, // PD: eigenvalues {2,1} and {2,1}
2430                array![[2.0, 0.0, 0.0, 1.0], [1.5, 0.5, 0.5, 1.5]],
2431            ),
2432            (
2433                ResponseManifold::Grassmann { k: 1, n: 3 }, // unit columns
2434                array![[1.0, 0.0, 0.0], [0.6, 0.8, 0.0]],
2435            ),
2436            (
2437                ResponseManifold::Poincare {
2438                    dim: 2,
2439                    curvature: -1.0,
2440                }, // strictly inside the ball
2441                array![[0.1, 0.2], [-0.3, 0.1]],
2442            ),
2443        ];
2444        for (manifold, values) in cases {
2445            let (resid, rel) =
2446                response_projection_residual(manifold, values.view()).expect("projection residual");
2447            for row in 0..values.nrows() {
2448                assert!(
2449                    resid[row] < 1e-9,
2450                    "{manifold:?} on-manifold row {row} should have ~0 residual, got {}",
2451                    resid[row]
2452                );
2453                assert!(rel[row] < 1e-9 && rel[row] >= 0.0);
2454            }
2455        }
2456    }
2457
2458    #[test]
2459    fn projection_residual_recovers_known_off_manifold_displacement() {
2460        // Closed-form checks against the exact nearest-point distance.
2461
2462        // Gr(1,3) / sphere: nearest unit vector to x is x/‖x‖, so the distance
2463        // is |‖x‖ − 1|. [2,0,0] ⇒ 1; [0,3,0] ⇒ 2. Relative = dist/‖x‖.
2464        let g = ResponseManifold::Grassmann { k: 1, n: 3 };
2465        let gv = array![[2.0, 0.0, 0.0], [0.0, 3.0, 0.0]];
2466        let (gres, grel) = response_projection_residual(g, gv.view()).expect("grassmann");
2467        assert!((gres[0] - 1.0).abs() < 1e-12, "got {}", gres[0]);
2468        assert!((gres[1] - 2.0).abs() < 1e-12, "got {}", gres[1]);
2469        assert!((grel[0] - 0.5).abs() < 1e-12);
2470        assert!((grel[1] - 2.0 / 3.0).abs() < 1e-12);
2471
2472        // SPD(2): nearest PSD matrix clamps negative eigenvalues to 0, so the
2473        // distance is the norm of the discarded negative part. [[1,0],[0,-1]]
2474        // has eigenvalue −1 discarded ⇒ distance 1; ‖x‖_F = √2.
2475        let s = ResponseManifold::Spd { n: 2 };
2476        let sv = array![[1.0, 0.0, 0.0, -1.0]];
2477        let (sres, srel) = response_projection_residual(s, sv.view()).expect("spd");
2478        assert!((sres[0] - 1.0).abs() < 1e-9, "got {}", sres[0]);
2479        assert!((srel[0] - 1.0 / 2.0_f64.sqrt()).abs() < 1e-9);
2480
2481        // Poincaré ball (c = −1, true radius R = 1): the distance to the open
2482        // ball is max(0, ‖x‖ − R). [3,0] ⇒ exactly 2 (not 3 − (1 − BOUNDARY_EPS)
2483        // — the diagnostic uses the manifold radius, not the safety radius).
2484        let p = ResponseManifold::Poincare {
2485            dim: 2,
2486            curvature: -1.0,
2487        };
2488        let pv = array![[3.0, 0.0]];
2489        let (pres, _prel) = response_projection_residual(p, pv.view()).expect("poincare");
2490        assert!((pres[0] - 2.0).abs() < 1e-12, "got {}", pres[0]);
2491
2492        // A different curvature (c = −4, R = 1/2): [2,0] ⇒ 2 − 0.5 = 1.5.
2493        let p4 = ResponseManifold::Poincare {
2494            dim: 2,
2495            curvature: -4.0,
2496        };
2497        let (p4res, _) =
2498            response_projection_residual(p4, array![[2.0, 0.0]].view()).expect("poincare c=-4");
2499        assert!((p4res[0] - 1.5).abs() < 1e-12, "got {}", p4res[0]);
2500    }
2501
2502    #[test]
2503    fn projection_residual_validates_shapes_and_finiteness() {
2504        let manifold = ResponseManifold::Spd { n: 2 }; // ambient = 4
2505        // Wrong column count.
2506        let bad_cols = array![[1.0, 2.0, 3.0]];
2507        assert!(response_projection_residual(manifold, bad_cols.view()).is_err());
2508        // Non-finite value.
2509        let nan_vals = array![[f64::NAN, 0.0, 0.0, 1.0]];
2510        assert!(response_projection_residual(manifold, nan_vals.view()).is_err());
2511        let inf_vals = array![[f64::INFINITY, 0.0, 0.0, 1.0]];
2512        assert!(response_projection_residual(manifold, inf_vals.view()).is_err());
2513    }
2514
2515    #[test]
2516    fn projection_residual_separates_on_and_off_manifold() {
2517        // The motivating case, now honestly answered: an on-manifold row sits
2518        // at zero distance from the candidate shape; a row pushed off it has a
2519        // clearly positive distance. This is the shape-plausibility signal that
2520        // gates which topology is worth fitting — not the post-fit membership
2521        // decision, which comes from the fitted surface's residual instead.
2522        let manifold = ResponseManifold::Grassmann { k: 1, n: 3 };
2523        let on = array![[0.6, 0.8, 0.0]]; // a genuine unit direction
2524        let off = array![[0.6, 0.8, 1.4]]; // same direction, pushed off-sphere
2525
2526        let (resid_on, _) = response_projection_residual(manifold, on.view()).expect("on");
2527        let (resid_off, _) = response_projection_residual(manifold, off.view()).expect("off");
2528
2529        assert!(
2530            resid_on[0] < 1e-9,
2531            "on-manifold should be ~0, got {}",
2532            resid_on[0]
2533        );
2534        assert!(
2535            resid_off[0] > 1e-2 && resid_off[0] > resid_on[0],
2536            "off-manifold distance ({}) must clearly exceed on-manifold ({})",
2537            resid_off[0],
2538            resid_on[0]
2539        );
2540    }
2541
2542    #[test]
2543    fn projection_residual_supports_k_greater_than_one_frames() {
2544        // k > 1 frames use the closed form √Σ(σ_i − 1)². St(2,3), ambient = 6,
2545        // row-major n×k.
2546        let manifold = ResponseManifold::Stiefel { k: 2, n: 3 };
2547
2548        // An orthonormal frame [e1 | e2] is its own nearest point ⇒ residual 0.
2549        let on = array![[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]];
2550        let (resid_on, _) = response_projection_residual(manifold, on.view()).expect("on");
2551        assert!(
2552            resid_on[0] < 1e-9,
2553            "orthonormal frame should be ~0, got {}",
2554            resid_on[0]
2555        );
2556
2557        // Scale the first column by 2: Y = [2·e1 | e2]. YᵀY = diag(4,1) ⇒
2558        // σ = (2,1), distance √((2−1)²+(1−1)²) = 1, relative = 1/‖Y‖_F = 1/√5.
2559        let off = array![[2.0, 0.0, 0.0, 1.0, 0.0, 0.0]];
2560        let (resid_off, rel_off) = response_projection_residual(manifold, off.view()).expect("off");
2561        assert!((resid_off[0] - 1.0).abs() < 1e-9, "got {}", resid_off[0]);
2562        assert!(
2563            (rel_off[0] - 1.0 / 5.0_f64.sqrt()).abs() < 1e-9,
2564            "got {}",
2565            rel_off[0]
2566        );
2567
2568        // Grassmann(2,4) gives the identical score for the same frame data.
2569        let g = ResponseManifold::Grassmann { k: 2, n: 4 };
2570        let g_on = array![[1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0]];
2571        let (g_resid, _) = response_projection_residual(g, g_on.view()).expect("grassmann");
2572        assert!(g_resid[0] < 1e-9, "got {}", g_resid[0]);
2573    }
2574
2575    #[test]
2576    fn projection_residual_handles_nontrivial_eigenvectors() {
2577        // A frame whose Gram is NOT diagonal, so the singular values come from a
2578        // genuine eigendecomposition. Y = [[1,1],[0,1],[0,0]] (St(2,3)):
2579        // YᵀY = [[1,1],[1,2]], eigenvalues (3±√5)/2, σ = ((1+√5)/2, (√5−1)/2).
2580        // distance² = (σ₁−1)² + (σ₂−1)².
2581        let manifold = ResponseManifold::Stiefel { k: 2, n: 3 };
2582        let y = array![[1.0, 1.0, 0.0, 1.0, 0.0, 0.0]]; // row-major rows [1,1],[0,1],[0,0]
2583        let (resid, _) = response_projection_residual(manifold, y.view()).expect("frame");
2584        let s5 = 5.0_f64.sqrt();
2585        let sig1 = (1.0 + s5) / 2.0;
2586        let sig2 = (s5 - 1.0) / 2.0;
2587        let expect = ((sig1 - 1.0).powi(2) + (sig2 - 1.0).powi(2)).sqrt();
2588        assert!(
2589            (resid[0] - expect).abs() < 1e-9,
2590            "got {} want {}",
2591            resid[0],
2592            expect
2593        );
2594    }
2595
2596    #[test]
2597    fn projection_residual_is_defined_for_rank_deficient_frames() {
2598        // A rank-deficient frame has a well-defined distance even though the
2599        // nearest orthonormal frame is not unique — distance to a compact set is
2600        // always defined, so this must NOT error. Two identical columns e1 give
2601        // YᵀY = [[1,1],[1,1]], σ = (√2, 0), distance √((√2−1)²+(0−1)²) = √(4−2√2).
2602        let manifold = ResponseManifold::Stiefel { k: 2, n: 3 };
2603        let degenerate = array![[1.0, 1.0, 0.0, 0.0, 0.0, 0.0]]; // both columns = e1
2604        let (resid, _) =
2605            response_projection_residual(manifold, degenerate.view()).expect("rank-deficient ok");
2606        let expect = (4.0 - 2.0 * 2.0_f64.sqrt()).sqrt(); // ≈ 1.0823922
2607        assert!(
2608            (resid[0] - expect).abs() < 1e-9,
2609            "got {} want {}",
2610            resid[0],
2611            expect
2612        );
2613
2614        // Minimal case: zero vector on the sphere (Gr(1,3)). Every unit vector is
2615        // a nearest point and the distance is exactly 1 — also must not error.
2616        let sphere = ResponseManifold::Grassmann { k: 1, n: 3 };
2617        let (zres, _) =
2618            response_projection_residual(sphere, array![[0.0, 0.0, 0.0]].view()).expect("zero");
2619        assert!((zres[0] - 1.0).abs() < 1e-12, "got {}", zres[0]);
2620    }
2621
2622    #[test]
2623    fn projection_residual_handles_tiny_full_rank_frame() {
2624        // A tiny but full-rank frame must NOT be rejected as rank-deficient: the
2625        // distance is scale-correct. Y = 1e-7·[e1 | e2] (St(2,3)) ⇒ σ = (1e-7,
2626        // 1e-7), distance √2·(1 − 1e-7) ≈ 1.41421342.
2627        let manifold = ResponseManifold::Stiefel { k: 2, n: 3 };
2628        let tiny = array![[1e-7, 0.0, 0.0, 1e-7, 0.0, 0.0]];
2629        let (resid, _) = response_projection_residual(manifold, tiny.view()).expect("tiny ok");
2630        let expect = 2.0_f64.sqrt() * (1.0 - 1e-7);
2631        assert!(
2632            (resid[0] - expect).abs() < 1e-9,
2633            "got {} want {}",
2634            resid[0],
2635            expect
2636        );
2637    }
2638
2639    #[test]
2640    fn projection_residual_spd_nonsymmetric_and_singular() {
2641        // Non-symmetric input: A = [[1,1],[-1,1]] has sym(A) = I (no negative
2642        // part), but the distance to the PSD cone still counts the skew part:
2643        // ‖A − I‖_F = √2.
2644        let spd = ResponseManifold::Spd { n: 2 };
2645        let asym = array![[1.0, 1.0, -1.0, 1.0]]; // row-major [[1,1],[-1,1]]
2646        let (ares, _) = response_projection_residual(spd, asym.view()).expect("nonsym");
2647        assert!((ares[0] - 2.0_f64.sqrt()).abs() < 1e-9, "got {}", ares[0]);
2648
2649        // A singular PSD matrix diag(1,0) is in the closed cone ⇒ distance 0
2650        // (even though it is not strictly positive definite).
2651        let singular = array![[1.0, 0.0, 0.0, 0.0]];
2652        let (sres, _) = response_projection_residual(spd, singular.view()).expect("singular psd");
2653        assert!(
2654            sres[0] < 1e-12,
2655            "singular PSD should be ~0, got {}",
2656            sres[0]
2657        );
2658    }
2659
2660    #[test]
2661    fn projection_residual_poincare_interior_shell_is_zero() {
2662        // A point in the numerical safety shell R_safe < ‖x‖ < R is a genuine
2663        // interior point of the manifold ball, so it must score exactly 0 — the
2664        // diagnostic uses the true radius, not the projection safety radius.
2665        let p = ResponseManifold::Poincare {
2666            dim: 2,
2667            curvature: -1.0,
2668        };
2669        let shell = array![[0.999999, 0.0]]; // inside R = 1, outside R_safe ≈ 0.99999
2670        let (resid, _) = response_projection_residual(p, shell.view()).expect("shell");
2671        assert!(
2672            resid[0] < 1e-12,
2673            "interior point must be 0, got {}",
2674            resid[0]
2675        );
2676    }
2677
2678    #[test]
2679    fn projection_residual_handles_constant_curvature_domain() {
2680        // ConstantCurvature is a fittable response geometry produced by the
2681        // resolver/parser, so it must return a closed-form distance, not error.
2682        // κ ≥ 0: chart is all of ℝ^d ⇒ every finite row scores 0.
2683        let pos = ResponseManifold::parse("constant_curvature(dim=3,kappa=1.0)", 3)
2684            .expect("parse constant_curvature");
2685        assert!(matches!(pos, ResponseManifold::ConstantCurvature { .. }));
2686        let (pres, _) =
2687            response_projection_residual(pos, array![[0.1, 9.0, -100.0]].view()).expect("kappa>=0");
2688        assert!(pres[0] < 1e-12, "κ≥0 finite row must be 0, got {}", pres[0]);
2689
2690        // κ < 0: chart is the ball of radius 1/√(−κ). For κ = −1, R = 1, so a
2691        // point of norm 3 is at distance 2; an interior point is at 0.
2692        let neg = ResponseManifold::ConstantCurvature {
2693            dim: 2,
2694            kappa: -1.0,
2695        };
2696        let (nres, _) = response_projection_residual(neg, array![[3.0, 0.0], [0.2, 0.1]].view())
2697            .expect("kappa<0");
2698        assert!((nres[0] - 2.0).abs() < 1e-12, "got {}", nres[0]);
2699        assert!(nres[1] < 1e-12, "interior row must be 0, got {}", nres[1]);
2700    }
2701
2702    #[test]
2703    fn projection_residual_accepts_empty_batch() {
2704        // A zero-row batch is valid and returns empty arrays for every geometry.
2705        let manifold = ResponseManifold::Spd { n: 2 }; // ambient = 4
2706        let empty = Array2::<f64>::zeros((0, 4));
2707        let (resid, rel) = response_projection_residual(manifold, empty.view()).expect("empty");
2708        assert_eq!(resid.len(), 0);
2709        assert_eq!(rel.len(), 0);
2710    }
2711}