Skip to main content

gam_terms/smooth/
term_specs.rs

1use coefficient_transforms::{
2    convex_divided_difference_transform_matrix, cumulative_exp, cumulative_sum_transform_matrix,
3    second_cumulative_exp,
4};
5
6pub use error::SmoothError;
7
8use input_standardization::{
9    apply_input_standardization, compensate_length_scale_for_standardization,
10    compensate_optional_length_scale_for_standardization, compute_spatial_input_scales,
11};
12
13use shape_constraints::{
14    build_shape_constraint_design_1d, build_shape_linear_constraints_1d,
15    merge_linear_constraints_global, shape_lower_bounds_local, shape_order_and_sign,
16    shape_supports_basis, shape_uses_box_reparameterization,
17};
18
19pub fn describe_thin_plate_center_request(strategy: &CenterStrategy) -> String {
20    match strategy {
21        CenterStrategy::Auto(inner) => describe_thin_plate_center_request(inner),
22        CenterStrategy::UserProvided(centers) => format!("{} centers", centers.nrows()),
23        CenterStrategy::EqualMass { num_centers }
24        | CenterStrategy::EqualMassCovarRepresentative { num_centers }
25        | CenterStrategy::FarthestPoint { num_centers }
26        | CenterStrategy::KMeans { num_centers, .. } => format!("{num_centers} centers"),
27        CenterStrategy::UniformGrid { points_per_dim } => {
28            format!("uniform grid with {points_per_dim} points per dimension")
29        }
30    }
31}
32
33pub fn rewrite_thin_plate_knots_error(
34    err: BasisError,
35    termname: &str,
36    feature_count: usize,
37    spec: &ThinPlateBasisSpec,
38) -> BasisError {
39    match err {
40        // Polynomial-nullspace shortfall reported directly by the kernel
41        // builder ("thin-plate spline requires at least N centers to span ...").
42        BasisError::InvalidInput(msg)
43            if msg.contains("thin-plate spline requires at least")
44                && (msg.contains("centers to span") || msg.contains("knots to span")) =>
45        {
46            let min_centers = crate::basis::thin_plate_polynomial_basis_dimension(feature_count);
47            let requested = describe_thin_plate_center_request(&spec.center_strategy);
48            BasisError::InvalidInput(format!(
49                "joint TPS term '{termname}' over {feature_count} covariates with {requested} is invalid; minimum centers is {min_centers}"
50            ))
51        }
52        // Insufficient-rows shortfall raised by `select_thin_plate_knots` when
53        // the requested center count exceeds the available row count. Rewrite
54        // it in term language so the diagnostic points at the smooth term and
55        // the polynomial-nullspace minimum the user needs to satisfy.
56        BasisError::InvalidInput(msg)
57            if msg.starts_with("requested ") && msg.contains(" knots but only ") =>
58        {
59            let min_centers = crate::basis::thin_plate_polynomial_basis_dimension(feature_count);
60            let requested = describe_thin_plate_center_request(&spec.center_strategy);
61            BasisError::InvalidInput(format!(
62                "joint TPS term '{termname}' over {feature_count} covariates with {requested} is invalid; minimum centers is {min_centers}"
63            ))
64        }
65        other => other,
66    }
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
70pub enum ShapeConstraint {
71    None,
72    MonotoneIncreasing,
73    MonotoneDecreasing,
74    Convex,
75    Concave,
76}
77
78/// Parse a shape-constraint string into a [`ShapeConstraint`].
79///
80/// This is the single source of truth shared by the formula DSL
81/// (`s(x, shape=...)`) and the `smooths={...}` override path
82/// (`Smooth.shape_constraint`). The accepted spellings cover the canonical
83/// Python `ShapeConstraintLiteral` strings exactly
84/// (`"none"` / `"monotone_increasing"` / `"monotone_decreasing"` /
85/// `"convex"` / `"concave"`) plus a few common aliases. Hyphens and case are
86/// normalized, so `"Monotone-Increasing"` and `"mono_inc"` both resolve to
87/// [`ShapeConstraint::MonotoneIncreasing`].
88pub fn parse_shape_constraint(raw: &str) -> Result<ShapeConstraint, String> {
89    let normalized = raw.trim().to_ascii_lowercase().replace('-', "_");
90    match normalized.as_str() {
91        "" | "none" => Ok(ShapeConstraint::None),
92        "monotone_increasing" | "monotonic_increasing" | "increasing" | "mono_inc" | "mpi" => {
93            Ok(ShapeConstraint::MonotoneIncreasing)
94        }
95        "monotone_decreasing" | "monotonic_decreasing" | "decreasing" | "mono_dec" | "mpd" => {
96            Ok(ShapeConstraint::MonotoneDecreasing)
97        }
98        "convex" | "cvx" => Ok(ShapeConstraint::Convex),
99        "concave" | "ccv" => Ok(ShapeConstraint::Concave),
100        other => Err(format!(
101            "unknown shape constraint {other:?}; expected one of \
102             \"none\", \"monotone_increasing\", \"monotone_decreasing\", \
103             \"convex\", \"concave\""
104        )),
105    }
106}
107
108impl ShapeConstraint {
109    /// Canonical formula-DSL spelling, i.e. the text emitted into
110    /// `s(x, shape=...)`. Round-trips through [`parse_shape_constraint`].
111    pub fn dsl_str(&self) -> &'static str {
112        match self {
113            ShapeConstraint::None => "none",
114            ShapeConstraint::MonotoneIncreasing => "monotone_increasing",
115            ShapeConstraint::MonotoneDecreasing => "monotone_decreasing",
116            ShapeConstraint::Convex => "convex",
117            ShapeConstraint::Concave => "concave",
118        }
119    }
120}
121
122/// Smooth-term head keywords recognised by the formula DSL. A `shape=` option
123/// may be attached to any term whose head is one of these.
124pub const SMOOTH_HEAD_KEYWORDS: [&str; 11] = [
125    "s",
126    "smooth",
127    "te",
128    "tensor",
129    "thinplate",
130    "tps",
131    "duchon",
132    "matern",
133    "sphere",
134    "bs",
135    "bspline",
136];
137
138/// Rewrite smooth-term calls in `formula` so each named smooth carries a
139/// `shape=<kind>` option understood by the formula DSL.
140///
141/// `constraints` pairs the smooth-term text as it appears in the formula
142/// (e.g. `"s(x)"` or `"s(x, type=duchon, centers=8)"`) with a shape-constraint
143/// spelling accepted by [`parse_shape_constraint`]; comparison is exact after
144/// whitespace removal. A `"none"` constraint is a no-op. Referencing a term not
145/// present in the formula is an error.
146///
147/// This is the single source of truth for the `gamfit.fit(..., constraints=…)`
148/// rewrite — the Python wrapper only marshals the mapping across the FFI and
149/// holds no formula-parsing or alias-normalization logic of its own.
150pub fn apply_shape_constraints_to_formula(
151    formula: &str,
152    constraints: &[(String, String)],
153) -> Result<String, String> {
154    use std::collections::{BTreeMap, BTreeSet};
155
156    if constraints.is_empty() {
157        return Ok(formula.to_string());
158    }
159    let strip_ws = |s: &str| -> String { s.chars().filter(|c| !c.is_whitespace()).collect() };
160
161    // Whitespace-stripped term text -> canonical shape spelling.
162    let mut wanted: BTreeMap<String, &'static str> = BTreeMap::new();
163    // Whitespace-stripped term text -> original key (for error labels).
164    let mut originals: BTreeMap<String, String> = BTreeMap::new();
165    for (key, kind_raw) in constraints {
166        let kind = parse_shape_constraint(kind_raw)?;
167        let nk = strip_ws(key);
168        originals.entry(nk.clone()).or_insert_with(|| key.clone());
169        if kind != ShapeConstraint::None {
170            wanted.insert(nk, kind.dsl_str());
171        }
172    }
173    if wanted.is_empty() {
174        return Ok(formula.to_string());
175    }
176
177    let chars: Vec<char> = formula.chars().collect();
178    let n = chars.len();
179    let is_ident = |c: char| c.is_ascii_alphanumeric() || c == '_';
180
181    let mut out = String::with_capacity(formula.len() + 32);
182    let mut matched: BTreeSet<String> = BTreeSet::new();
183    let mut i = 0usize;
184    while i < n {
185        // Locate the next smooth-term head (`<keyword> \s* (`) at or after `i`,
186        // respecting word boundaries so `abs(` never matches the `s(` head.
187        let mut head: Option<(usize, usize)> = None; // (head_start, paren_index)
188        let mut p = i;
189        while p < n {
190            let boundary = p == 0 || !is_ident(chars[p - 1]);
191            if boundary {
192                for kw in SMOOTH_HEAD_KEYWORDS.iter() {
193                    let klen = kw.chars().count();
194                    if p + klen > n || chars[p..p + klen].iter().collect::<String>() != **kw {
195                        continue;
196                    }
197                    let mut q = p + klen;
198                    while q < n && chars[q].is_whitespace() {
199                        q += 1;
200                    }
201                    if q < n && chars[q] == '(' {
202                        head = Some((p, q));
203                        break;
204                    }
205                }
206            }
207            if head.is_some() {
208                break;
209            }
210            p += 1;
211        }
212        let (head_start, paren_open) = match head {
213            Some(h) => h,
214            None => {
215                out.extend(chars[i..].iter());
216                break;
217            }
218        };
219        out.extend(chars[i..head_start].iter());
220
221        // Find the matching close paren, honoring nesting and string literals.
222        let body_start = paren_open + 1;
223        let mut depth = 1i32;
224        let mut j = body_start;
225        let mut in_str: Option<char> = None;
226        let mut closed = false;
227        while j < n {
228            let ch = chars[j];
229            if let Some(quote) = in_str {
230                if ch == quote {
231                    in_str = None;
232                }
233            } else if ch == '\'' || ch == '"' {
234                in_str = Some(ch);
235            } else if ch == '(' {
236                depth += 1;
237            } else if ch == ')' {
238                depth -= 1;
239                if depth == 0 {
240                    closed = true;
241                    break;
242                }
243            }
244            j += 1;
245        }
246
247        if !closed {
248            // Unbalanced — emit the remainder verbatim; the DSL parser will
249            // produce the canonical error.
250            out.extend(chars[head_start..].iter());
251            break;
252        }
253
254        let term_text: String = chars[head_start..=j].iter().collect();
255
256        let key_norm = strip_ws(&term_text);
257
258        match wanted.get(&key_norm) {
259            None => out.extend(chars[head_start..=j].iter()),
260            Some(kind) => {
261                let head_paren: String = chars[head_start..body_start].iter().collect();
262                let inside: String = chars[body_start..j].iter().collect();
263                let inside = inside.trim();
264                if inside.is_empty() {
265                    out.push_str(&format!("{head_paren}shape={kind})"));
266                } else {
267                    out.push_str(&format!("{head_paren}{inside}, shape={kind})"));
268                }
269                matched.insert(key_norm);
270            }
271        }
272
273        i = j + 1;
274    }
275
276    let mut missing: Vec<String> = wanted
277        .keys()
278        .filter(|k| !matched.contains(*k))
279        .map(|k| originals.get(k).cloned().unwrap_or_else(|| k.clone()))
280        .collect();
281
282    if !missing.is_empty() {
283        missing.sort();
284        return Err(format!(
285            "shape constraints referenced smooth term(s) not found in formula: {}",
286            missing.join(", ")
287        ));
288    }
289
290    Ok(out)
291}
292
293#[derive(Debug, Clone, Serialize, Deserialize)]
294pub enum BySmoothKind {
295    Numeric,
296    Level { level_bits: u64 },
297}
298
299#[derive(Debug, Clone, Serialize, Deserialize)]
300pub enum SmoothBasisSpec {
301    /// Row-gated wrapper used for mgcv-style ``by=`` smooths.
302    ///
303    /// ``ByNumeric`` multiplies the inner smooth by a numeric column.
304    /// ``ByLevel`` keeps the inner smooth active only for rows whose encoded
305    /// categorical value has the stored bit pattern.  Unordered factor-by
306    /// smooths are represented as one independent ``ByLevel`` term per level.
307    ///
308    /// `kind` preserves the compact structural discriminator, while `by`
309    /// carries the full row-gating spec used to build the local design.
310    ByVariable {
311        inner: Box<SmoothBasisSpec>,
312        by_col: usize,
313        kind: BySmoothKind,
314        by: ByVariableSpec,
315    },
316    /// Sum-to-zero factor smooth (`bs="sz"`): with L levels, estimate L-1
317    /// deviation coefficient blocks and use the final level as the negative
318    /// sum of the others, enforcing coefficient-wise zero sums across levels.
319    FactorSumToZero {
320        inner: Box<SmoothBasisSpec>,
321        by_col: usize,
322        levels: Vec<u64>,
323        /// Global-orthogonality column map `Z` captured at fit time when this
324        /// term overlapped an owner smooth (`s(x) + s(g, x, bs=sz)`, #978):
325        /// the hierarchical-ownership pass residualized this term's realized
326        /// design as `X ← X·Z`, shrinking its coefficient block. `Z` depends
327        /// on the *training-row* owner designs, so prediction cannot rederive
328        /// it — it must be persisted and replayed
329        /// (`apply_global_smooth_identifiability` consumes it verbatim).
330        /// Chart convention: `Z` lives in the post-restack, post-joint-null-Q
331        /// coordinates — the raw `sz` rebuild reapplies `Q` deterministically
332        /// (#700), then `Z` applies on top. `None` for non-overlapping terms.
333        #[serde(default)]
334        frozen_global_orthogonality: Option<Array2<f64>>,
335    },
336    BSpline1D {
337        feature_col: usize,
338        spec: BSplineBasisSpec,
339    },
340    /// A smooth modulated by a `by=` variable. Numeric `by` scales one inner
341    /// smooth; factor `by` replicates the inner smooth by level.
342    BySmooth {
343        smooth: Box<SmoothBasisSpec>,
344        by_kind: ByVarKind,
345    },
346    /// Factor-smooth interaction families (`bs="fs"`, `bs="sz"`) and
347    /// random slopes (`bs="re"`).
348    FactorSmooth { spec: FactorSmoothSpec },
349    ThinPlate {
350        feature_cols: Vec<usize>,
351        spec: ThinPlateBasisSpec,
352        /// Per-column standard deviations used to standardize input dimensions
353        /// before kernel evaluation when d > 1. `None` means no standardization
354        /// (either d == 1 or explicitly disabled).
355        #[serde(default)]
356        input_scales: Option<Vec<f64>>,
357    },
358    Sphere {
359        feature_cols: Vec<usize>,
360        spec: SphericalSplineBasisSpec,
361    },
362    /// Constant-curvature (`M_κ`) geodesic-kernel smooth over κ-stereographic
363    /// chart coordinates (#944): one construction interpolating
364    /// S^d → ℝ^d → H^d through the spec's fixed κ. The Wahba S² smooth is the
365    /// structural template; the geometry comes from
366    /// `geometry::constant_curvature::ConstantCurvature`.
367    ConstantCurvature {
368        feature_cols: Vec<usize>,
369        spec: ConstantCurvatureBasisSpec,
370    },
371    Matern {
372        feature_cols: Vec<usize>,
373        spec: MaternBasisSpec,
374        #[serde(default)]
375        input_scales: Option<Vec<f64>>,
376    },
377    /// Measure-jet spline smooth: multiscale local-jet-residual energy of the
378    /// empirical measure (centers as μ-quadrature, masses as μ-weights — no
379    /// graph, mesh, or neighbor set inside the statistical object). The
380    /// feature columns are ambient coordinates of data concentrated near an
381    /// unknown low-dimensional, possibly stratified set.
382    MeasureJet {
383        feature_cols: Vec<usize>,
384        spec: MeasureJetBasisSpec,
385        #[serde(default)]
386        input_scales: Option<Vec<f64>>,
387    },
388    Duchon {
389        feature_cols: Vec<usize>,
390        spec: DuchonBasisSpec,
391        #[serde(default)]
392        input_scales: Option<Vec<f64>>,
393    },
394    Pca {
395        feature_cols: Vec<usize>,
396        basis_matrix: Array2<f64>,
397        centered: bool,
398        #[serde(default = "default_pca_smooth_penalty")]
399        smooth_penalty: f64,
400        #[serde(default)]
401        center_mean: Option<Array1<f64>>,
402        #[serde(default)]
403        pca_basis_path: Option<PathBuf>,
404        #[serde(default = "default_pca_chunk_size")]
405        chunk_size: usize,
406    },
407    /// Tensor-product smooth built from 1D B-spline marginals.
408    ///
409    /// This is the `te()`-style construction used when axes have different units/scales
410    /// (for example, space x time) and isotropic radial kernels are not appropriate.
411    TensorBSpline {
412        feature_cols: Vec<usize>,
413        spec: TensorBSplineSpec,
414    },
415}
416
417impl SmoothBasisSpec {
418    /// Conservative lower bound on the number of sample rows needed for this
419    /// smooth basis to have a well-posed REML fit.
420    ///
421    /// Each basis kind answers the question for itself, so the workflow does
422    /// not have to know how many columns a B-spline, tensor product, PCA
423    /// projection, or spatial kernel emits. The contract is a *lower bound*:
424    /// returning too small a number is permitted (the inner solver will catch
425    /// any genuine n-vs-rank failure that slips past); returning too large a
426    /// number is a regression because it rejects legitimate fits.
427    ///
428    /// Rationale: B-spline / tensor / PCA bases have a closed-form column
429    /// count, so we use the exact dimension. Radial bases (TPS, Matern,
430    /// Duchon, Sphere) and factor smooths choose their column count from the
431    /// data (`heuristic_centers`, `unique_count`); we fall back to a small
432    /// constant floor because a fit on fewer than five rows cannot stabilise
433    /// any radial smooth regardless of the configured kernel scale.
434    pub fn min_sample_rows(&self) -> usize {
435        // Floor used for data-driven bases whose column count is not known
436        // from the spec alone. Five rows is the minimum at which the inner
437        // pivot/QR + REML smoothing-parameter search has any chance of being
438        // well-posed for a non-parametric smooth.
439        const RADIAL_FLOOR: usize = 5;
440
441        match self {
442            Self::ByVariable { inner, .. } => inner.min_sample_rows(),
443            Self::FactorSumToZero { inner, levels, .. } => {
444                // L-1 independent deviation blocks each carrying the inner
445                // basis dimension. Skip the levels-multiplier if it doesn't
446                // bring more rows; we want the *lower bound* not the rank.
447                let inner_min = inner.min_sample_rows();
448                let lvls = levels.len().saturating_sub(1).max(1);
449                inner_min.saturating_mul(lvls)
450            }
451            Self::BSpline1D { spec, .. } => bspline_basis_min_rows(spec),
452            Self::BySmooth { smooth, .. } => smooth.min_sample_rows(),
453            Self::FactorSmooth { spec } => {
454                // Replicates the marginal once per level; without a known
455                // level count we conservatively require at least the marginal
456                // basis dimension.
457                bspline_basis_min_rows(&spec.marginal)
458            }
459            Self::ThinPlate { .. }
460            | Self::Sphere { .. }
461            | Self::ConstantCurvature { .. }
462            | Self::Matern { .. }
463            | Self::MeasureJet { .. }
464            | Self::Duchon { .. } => RADIAL_FLOOR,
465            Self::Pca { basis_matrix, .. } => basis_matrix.ncols().max(1),
466            Self::TensorBSpline { spec, .. } => {
467                // A `te(...)` smooth is *penalized*: each margin carries a
468                // difference (wiggliness) penalty and the tensor inherits a
469                // Kronecker-sum penalty `S = Σ_i I ⊗ … ⊗ S_i ⊗ … ⊗ I`. The raw
470                // column count is the *product* of the per-marginal column
471                // counts, but that product is the lower bound for an
472                // *unpenalized* tensor regression — it is the number of rows you
473                // would need to identify every interaction column with no
474                // regularization. The penalty regularizes all of those
475                // interaction directions; only the combined penalty *null space*
476                // (the tensor product of the per-margin polynomial trends, a
477                // handful of columns) must be identified by the data, and the
478                // smoothing-parameter search shrinks the rest. The effective
479                // degrees of freedom of the fitted `te()` are therefore a small
480                // fraction of the column product, which is exactly why mgcv
481                // fits a default `te(x, y)` on a couple hundred rows.
482                //
483                // The honest *penalized* lower bound is the **sum** of the
484                // per-marginal column counts, not their product: a row floor of
485                // `Σ_i k_i` still guarantees enough data to identify each
486                // margin's additive main-effect (the largest sub-block the
487                // penalty cannot shrink to zero), while no longer conflating
488                // unpenalized column-count identifiability with penalized
489                // well-posedness. This accepts moderate-`n` penalized tensors
490                // (e.g. a 20×20 default basis on n=200) yet still rejects a
491                // genuinely undersized fit where `n < Σ_i k_i` and even the
492                // additive part is rank-deficient.
493                //
494                // Binary / low-cardinality margins (#724): gam will accept a
495                // `te(x, badh)` whose `badh ∈ {0, 1}` margin nominally requests
496                // more basis columns than `badh` has unique values, where mgcv
497                // refuses the unpenalized term as ill-posed ("badh has
498                // insufficient unique values to support k knots"). This is
499                // correct-by-design, *not* a degenerate fit: the marginal
500                // wiggliness penalty on the `badh` axis has a null space that is
501                // exactly its identifiable trend (the two cell means of a binary
502                // covariate), and the Kronecker-sum penalty shrinks every tensor
503                // column outside that null space toward zero. The resulting fit
504                // is the well-posed "per-level `x` smooth + binary main effect"
505                // that mgcv reaches only after manually collapsing the basis —
506                // gam reaches it automatically because the penalty, not the raw
507                // column count, sets the effective rank. A genuinely
508                // rank-deficient design (penalty null space wider than the data
509                // can support) is still caught downstream by the inner pivoted
510                // factorization, which owns the exact n-vs-rank decision; this
511                // pre-fit gate only refuses the grossly-undersized formula.
512                let mut total: usize = 0;
513                for marginal in &spec.marginalspecs {
514                    let m = bspline_basis_min_rows(marginal);
515                    total = total.saturating_add(m.max(1));
516                }
517                total.max(RADIAL_FLOOR)
518            }
519        }
520    }
521
522    /// Stable structural discriminant for warm-start cache keying (#869).
523    ///
524    /// Two smooths that produce different bases / penalty structures must map
525    /// to different strings here so they cannot collide on the persistent
526    /// warm-start `cache_key` (which is otherwise blind to topology: it hashes
527    /// only the raw input column count, so e.g. `sphere` vs `torus` vs
528    /// `euclidean` candidates fit on the *same* data would otherwise share one
529    /// key and cross-contaminate each other's β/ρ seed). The string is the
530    /// topology identity, not the fitted coefficients, so same-topology refits
531    /// (the screen→full-refit cascade) still hit the same key and reuse work.
532    pub fn structural_kind(&self) -> &'static str {
533        match self {
534            Self::ByVariable { .. } => "by_variable",
535            Self::FactorSumToZero { .. } => "factor_sum_to_zero",
536            Self::BSpline1D { .. } => "bspline_1d",
537            Self::BySmooth { .. } => "by_smooth",
538            Self::FactorSmooth { .. } => "factor_smooth",
539            Self::ThinPlate { .. } => "thin_plate",
540            Self::Sphere { .. } => "sphere",
541            Self::ConstantCurvature { .. } => "constant_curvature",
542            Self::Matern { .. } => "matern",
543            Self::MeasureJet { .. } => "measurejet",
544            Self::Duchon { .. } => "duchon",
545            Self::Pca { .. } => "pca",
546            Self::TensorBSpline { .. } => "tensor_bspline",
547        }
548    }
549
550    /// True for a tensor-product smooth that is only *marginally* centered
551    /// (`ti(...)`, [`TensorBSplineIdentifiability::MarginalSumToZero`]): its
552    /// per-margin sum-to-zero reparameterization `(B_xZ_x)⊗(B_zZ_z)` has ALREADY
553    /// removed each axis's main effect analytically (mgcv-identical), so its
554    /// main-effect removal is complete and it must take NO additional
555    /// owner-residualization block. Residualizing it a second time against the
556    /// realized main-effect designs is a grid-fragile no-op on an exact tensor
557    /// grid but eats genuine pure-interaction curvature off-grid (#1470).
558    pub fn is_marginally_centered_tensor(&self) -> bool {
559        matches!(
560            self,
561            Self::TensorBSpline { spec, .. }
562                if matches!(spec.identifiability, TensorBSplineIdentifiability::MarginalSumToZero)
563        )
564    }
565
566    /// A sum-to-zero factor smooth (`bs="sz"`) has ALREADY removed the
567    /// cross-group main effect analytically, in coefficient space, via its
568    /// `Σ_g d_g(x) ≡ 0` reparameterization (`L-1` deviation blocks with the
569    /// reference level the negative sum of the others) — exactly mgcv's `sz`
570    /// construction, which is self-identifiable against an overlapping `s(x)`
571    /// with no further constraint. Residualizing it a SECOND time against the
572    /// realized B-spline span of the explicit `s(x)` smooth is redundant in
573    /// exact arithmetic (the common-to-all-groups component is zero by
574    /// construction) and actively HARMFUL on finite data: each deviation block
575    /// is a B-spline in `x` whose realized columns share `s(x)`'s span, so the
576    /// joint residualization collapses the full `L·k`-column deviation design to
577    /// `L·k − rank(s(x))` columns and eats the within-group curvature `s(x)`
578    /// cannot represent. REML then rails the deviation smoothing parameter and
579    /// the factor smooth under-recovers (#1605). This is the exact analogue of
580    /// the marginally-centered tensor (`ti`) exemption (#1470), so such a term
581    /// takes NO owner-residualization block.
582    pub fn is_sum_to_zero_factor_smooth(&self) -> bool {
583        matches!(
584            self,
585            Self::FactorSumToZero { .. }
586                | Self::FactorSmooth {
587                    spec: FactorSmoothSpec {
588                        flavour: FactorSmoothFlavour::Sz,
589                        ..
590                    }
591                }
592        )
593    }
594
595    /// Feature columns this basis consumes, used alongside [`structural_kind`]
596    /// to disambiguate two same-kind smooths on different axes. Wrapper
597    /// variants delegate to their inner basis.
598    pub fn structural_feature_cols(&self) -> Vec<usize> {
599        match self {
600            Self::ByVariable { inner, .. } | Self::FactorSumToZero { inner, .. } => {
601                inner.structural_feature_cols()
602            }
603            Self::BySmooth { smooth, .. } => smooth.structural_feature_cols(),
604            Self::FactorSmooth { .. } => Vec::new(),
605            Self::BSpline1D { feature_col, .. } => vec![*feature_col],
606            Self::ThinPlate { feature_cols, .. }
607            | Self::Sphere { feature_cols, .. }
608            | Self::ConstantCurvature { feature_cols, .. }
609            | Self::Matern { feature_cols, .. }
610            | Self::MeasureJet { feature_cols, .. }
611            | Self::Duchon { feature_cols, .. }
612            | Self::Pca { feature_cols, .. }
613            | Self::TensorBSpline { feature_cols, .. } => feature_cols.clone(),
614        }
615    }
616}
617
618/// Lower bound on the number of sample rows a 1D B-spline smooth needs for a
619/// well-posed *penalized* REML fit. Used as the per-smooth row floor in
620/// [`SmoothBasisSpec::min_sample_rows`].
621///
622/// For a *singly*-penalized smooth the floor is the full column count: the
623/// wiggliness penalty leaves the order-`m` polynomial trend unpenalized, and
624/// gam's original gate conservatively required enough rows for the whole basis.
625/// That conservative floor is kept here unchanged.
626///
627/// A *double*-penalized smooth (mgcv `select=TRUE`) is different: it adds a
628/// second penalty on the wiggliness penalty's null space, so even the
629/// polynomial trend is shrinkable toward zero and *nothing* in the basis
630/// requires unpenalized identification by the data — exactly the reasoning the
631/// `TensorBSpline` arm of [`SmoothBasisSpec::min_sample_rows`] already applies
632/// to a penalized tensor. Its honest floor is therefore a small stabilization
633/// constant, not the column count. This is what lets mgcv (and now gam) fit
634/// several `select=TRUE` smooths on a dataset whose row count is below the
635/// summed basis width (e.g. the n≈30 `wine_gamair` fold, 5 `ps` smooths,
636/// p≈51): the penalties, not the data, set the effective rank. The bounded
637/// outer REML loop still terminates, and the genuine n-vs-rank decision is
638/// owned downstream by the inner pivoted factorization. Without this, gam
639/// rejected the fit outright (or, before the gate existed, the outer REML loop
640/// wandered the flat overparameterized surface until the benchmark wall budget
641/// killed it — #1089).
642pub fn bspline_basis_min_rows(spec: &crate::basis::BSplineBasisSpec) -> usize {
643    use crate::basis::BSplineKnotSpec;
644    let columns = match &spec.knotspec {
645        BSplineKnotSpec::Generate {
646            num_internal_knots, ..
647        } => *num_internal_knots + spec.degree + 1,
648        BSplineKnotSpec::Automatic {
649            num_internal_knots: Some(k),
650            ..
651        } => *k + spec.degree + 1,
652        BSplineKnotSpec::Automatic {
653            num_internal_knots: None,
654            ..
655        } => {
656            // Knot count is data-derived (`default_internal_knot_count_for_data`).
657            // A minimal cubic basis is `degree + 2` columns; below that the
658            // basis cannot represent a non-parametric smooth.
659            spec.degree + 2
660        }
661        BSplineKnotSpec::Provided(knots) => knots.len().saturating_sub(spec.degree + 1).max(1),
662        // cr basis dimension equals the knot count (no degree offset).
663        BSplineKnotSpec::NaturalCubicRegression { knots } => knots.len(),
664        BSplineKnotSpec::PeriodicUniform { num_basis, .. } => *num_basis,
665    };
666    let columns = columns.max(spec.degree + 2);
667
668    if spec.double_penalty {
669        // Fully shrinkable basis: only a small stabilization floor must be
670        // identified by the data, capped by the actual column count.
671        const DOUBLE_PENALTY_FLOOR: usize = 2;
672        DOUBLE_PENALTY_FLOOR.min(columns).max(1)
673    } else {
674        columns
675    }
676}
677
678#[derive(Debug, Clone, Serialize, Deserialize)]
679pub enum ByVariableSpec {
680    Numeric,
681    Level { value_bits: u64, label: String },
682}
683
684
685#[derive(Debug, Clone, Serialize, Deserialize)]
686pub enum ByVarKind {
687    Numeric {
688        feature_col: usize,
689    },
690    Factor {
691        feature_col: usize,
692        ordered: bool,
693        frozen_levels: Option<Vec<u64>>,
694    },
695}
696
697#[derive(Debug, Clone, Serialize, Deserialize)]
698pub struct FactorSmoothSpec {
699    pub continuous_cols: Vec<usize>,
700    pub group_col: usize,
701    pub marginal: BSplineBasisSpec,
702    pub flavour: FactorSmoothFlavour,
703    pub group_frozen_levels: Option<Vec<u64>>,
704    /// Fit-time global-orthogonality chart `Z` for this term (`s(x) + fs(x, g)`
705    /// overlap residualization, #978), in the post-joint-null-`Q` coordinates
706    /// (the raw rebuild recomputes any `Q` itself; `fs` penalties are
707    /// typically full-rank so `Q` is absent). Training-row dependent, hence
708    /// persisted; replayed verbatim by `apply_global_smooth_identifiability`.
709    #[serde(default)]
710    pub frozen_global_orthogonality: Option<Array2<f64>>,
711}
712
713#[derive(Debug, Clone, Serialize, Deserialize)]
714pub enum FactorSmoothFlavour {
715    Fs { m_null_penalty_orders: Vec<usize> },
716    Sz,
717    Re,
718}
719
720#[derive(Debug, Default, Clone, Serialize, Deserialize)]
721pub struct TensorBSplineSpec {
722    pub marginalspecs: Vec<BSplineBasisSpec>,
723    #[serde(default)]
724    pub periods: Vec<Option<f64>>,
725    pub double_penalty: bool,
726    #[serde(default)]
727    pub identifiability: TensorBSplineIdentifiability,
728    #[serde(default)]
729    pub penalty_decomposition: TensorBSplinePenaltyDecomposition,
730}
731
732#[derive(Debug, Default, Clone, Serialize, Deserialize)]
733pub enum TensorBSplineIdentifiability {
734    None,
735    #[default]
736    SumToZero,
737    /// mgcv `ti(...)` semantics: a *tensor interaction* smooth that excludes the
738    /// marginal main effects. A sum-to-zero constraint is applied to **each
739    /// marginal basis independently** before forming the tensor product, so the
740    /// resulting column space contains no function of a single variable alone —
741    /// only the pure interaction survives. The realized identifiability
742    /// transform is the Kronecker product `Z = Z₀ ⊗ Z₁ ⊗ … ⊗ Z_{d-1}` of the
743    /// per-margin sum-to-zero null-space bases, which is exactly the
744    /// reparameterization that turns the full-tensor design into the tensor
745    /// product of the centered margins.
746    MarginalSumToZero,
747    FrozenTransform {
748        transform: Array2<f64>,
749    },
750}
751
752#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
753pub enum TensorBSplinePenaltyDecomposition {
754    /// mgcv `te(...)`: one overlapping Kronecker-product penalty per margin,
755    /// `S_j` embedded against identities in the other tensor factors.
756    #[default]
757    MarginalKroneckerSum,
758    /// mgcv `t2(...)`: split every marginal coefficient space into penalized
759    /// range and penalty-null subspaces, then emit one disjoint tensor-subspace
760    /// penalty for every non-empty penalized/null combination.
761    Separable,
762}
763
764#[derive(Debug, Clone, Serialize, Deserialize)]
765pub struct SmoothTermSpec {
766    pub name: String,
767    pub basis: SmoothBasisSpec,
768    pub shape: ShapeConstraint,
769    /// Joint-null absorption rotation captured at fit time. `Some(Q)` means
770    /// the fitted coefficient vector lives in `γ`-coordinates with
771    /// `β_raw = Q · γ`; prediction must rotate the raw-basis design via
772    /// `X_new = X_new_raw · Q` to match. `None` means either the smooth had
773    /// no joint null space (penalty already full-rank) or rotation was
774    /// suppressed (smooth carries shape constraints whose cone geometry
775    /// would not survive an arbitrary orthogonal rotation). Persisted so
776    /// `save → load → predict` is bit-equivalent to in-memory prediction.
777    #[serde(default)]
778    pub joint_null_rotation: Option<crate::basis::JointNullRotation>,
779}
780
781#[derive(Debug, Clone)]
782pub struct SmoothTerm {
783    pub name: String,
784    pub coeff_range: Range<usize>,
785    pub shape: ShapeConstraint,
786    pub penalties_local: Vec<Array2<f64>>,
787    pub nullspace_dims: Vec<usize>,
788    pub penaltyinfo_local: Vec<PenaltyInfo>,
789    pub metadata: BasisMetadata,
790    /// Optional term-local lower bounds for constrained coefficients.
791    /// `-inf` means unconstrained.
792    pub lower_bounds_local: Option<Array1<f64>>,
793    /// Optional term-local inequality constraints in local coefficient coordinates.
794    /// `A_local * beta_local >= b_local`.
795    pub linear_constraints_local: Option<LinearInequalityConstraints>,
796    /// Optional factored tensor-product representation preserved for operator-backed
797    /// assembly in the main design builder.
798    pub kronecker_factored: Option<KroneckerFactoredBasis>,
799    /// Joint-null absorption rotation. `Some(Q)` records the orthonormal
800    /// `(p_local × p_local)` matrix that was applied to this term's design
801    /// and per-block penalties at construction time:
802    /// `term_design ← X_raw · Q`, `penalties_local[k] ← Qᵀ · S_raw · Q`.
803    /// The smooth's coefficient vector therefore lives in the rotated
804    /// (`γ`) coordinate system, with `β_raw = Q · γ` recovering the raw
805    /// pre-rotation parameterization. `None` means either no joint null
806    /// space (penalty already full-rank) or rotation was suppressed —
807    /// suppression fires when the smooth carries shape constraints
808    /// (lower bounds or local linear inequalities) that would lose their
809    /// cone geometry under a general orthogonal rotation.
810    ///
811    /// Prediction-side replay: callers building a new-data design `X_new_raw`
812    /// from the *raw* basis must call [`SmoothTerm::apply_rotation_to_predict`]
813    /// (or equivalent) to obtain `X_new = X_new_raw · Q` matching this
814    /// term's coefficient system.
815    ///
816    /// Persistence replay: `freeze_term_collection_from_design` copies this
817    /// rotation into `SmoothTermSpec`, which is serialized with fitted-model
818    /// payloads and reused by the predict-time basis builder. Saved models
819    /// therefore replay the same `X_new_raw · Q` transform as in-memory
820    /// prediction.
821    pub joint_null_rotation: Option<crate::basis::JointNullRotation>,
822    /// Global-orthogonality transform that `apply_global_smooth_identifiability`
823    /// applied to this term's design but could NOT embed into `metadata`
824    /// (factor-smooth kinds: `sz` metadata is per-marginal, `fs` metadata has
825    /// no transform slot — #978). `freeze_term_collection_from_design` copies
826    /// it onto the term's basis spec (`frozen_global_orthogonality`) so the
827    /// predict-side rebuild replays it instead of emitting the unresidualized
828    /// (wider) design that the fitted coefficients no longer match.
829    /// Chart convention is per kind: post-`Q` `Z` for `sz` (the raw rebuild
830    /// reapplies `Q` itself, #700), full `Q·Z` chart for `fs`.
831    pub unabsorbed_global_orthogonality: Option<Array2<f64>>,
832}
833
834impl SmoothTerm {
835    /// Apply the joint-null absorption rotation to a raw new-data design
836    /// matrix, returning `X_new_raw · Q` when this term was rotated at
837    /// fit time, or `X_new_raw` unchanged when no rotation was applied.
838    ///
839    /// Callers in the prediction path: after building the smooth's basis
840    /// at new data via the *raw* basis builder (the same builder used at
841    /// fit time, applied to `x_new` instead of the training rows), call
842    /// this method on the resulting matrix before forming `X · β`. The
843    /// fitted `β` lives in `γ`-coordinates if Q was applied; multiplying
844    /// the un-rotated `X_new_raw` by `β` would give a wrong η.
845    ///
846    /// Returns an error if the raw design's column count does not match
847    /// the rotation's `p_local`. The width invariant must hold: the raw
848    /// basis builder MUST emit the same `p_local` columns that the
849    /// fit-time builder did, and the rotation is `(p_local × p_local)`.
850    pub fn apply_rotation_to_predict(
851        &self,
852        x_new_raw: Array2<f64>,
853    ) -> Result<Array2<f64>, BasisError> {
854        let Some(rot) = self.joint_null_rotation.as_ref() else {
855            return Ok(x_new_raw);
856        };
857        let p_local = rot.rotation.nrows();
858        if x_new_raw.ncols() != p_local {
859            crate::bail_dim_basis!(
860                "joint-null rotation replay for term '{}': raw design has {} columns, \
861                 rotation expects {} (the raw basis builder must emit the same column \
862                 count as at fit time)",
863                self.name,
864                x_new_raw.ncols(),
865                p_local,
866            );
867        }
868        Ok(gam_linalg::faer_ndarray::fast_ab(
869            &x_new_raw,
870            &rot.rotation,
871        ))
872    }
873
874    /// Dimension of the **joint** null space of this term's active penalties:
875    /// the coefficient directions penalized by *no* penalty. The smooth-component
876    /// Wald test ([`crate::inference::smooth_test::wood_smooth_test`]) treats this
877    /// many leading coefficients as genuine unpenalized fixed effects and tests
878    /// them at full rank; the remainder is the penalized sub-block tested with a
879    /// rank-`≈EDF` truncated pseudo-inverse.
880    ///
881    /// Because every penalty block `S_k` is positive semi-definite,
882    /// `vᵀ(Σ_k S_k)v = Σ_k vᵀ S_k v = 0` iff `S_k v = 0` for *every* `k`; the
883    /// joint null space is therefore exactly `null(Σ_k S_k)`, of dimension
884    /// `p_local − rank(Σ_k S_k)`. This is the **intersection** of the per-penalty
885    /// null spaces, not their sum.
886    ///
887    /// Summing the per-penalty `nullspace_dims` instead (the historical defect
888    /// behind #1360) *unions* the null spaces and badly over-counts: a
889    /// double-penalty smooth carries a bending penalty (null space = its
890    /// polynomial part) plus a complementary null-space ridge (which penalizes
891    /// exactly that polynomial part), so the two null spaces are disjoint and the
892    /// joint null space is empty — yet the per-penalty dims sum to nearly
893    /// `p_local`. Feeding that inflated count to the Wald test makes it test
894    /// almost the whole shrunk block at full rank, manufacturing overwhelming
895    /// "significance" for a term the fit drove to ~0 EDF.
896    pub fn wald_unpenalized_dim(&self) -> usize {
897        joint_unpenalized_dim(
898            self.coeff_range.len(),
899            &self.penalties_local,
900            &self.nullspace_dims,
901        )
902    }
903}
904
905/// Numeric core of [`SmoothTerm::wald_unpenalized_dim`]: the dimension of the
906/// joint null space `∩_k null(S_k) = null(Σ_k S_k)` of a term's local penalty
907/// blocks, with a conservative fallback when a penalty is not materialized as a
908/// full `p_local × p_local` matrix (e.g. a Kronecker tensor factor).
909pub fn joint_unpenalized_dim(
910    p_local: usize,
911    penalties_local: &[Array2<f64>],
912    nullspace_dims: &[usize],
913) -> usize {
914    use gam_linalg::faer_ndarray::FaerEigh;
915    if p_local == 0 {
916        return 0;
917    }
918    if penalties_local.is_empty() {
919        // No penalty ⇒ a wholly unpenalized (fixed-effect) block.
920        return p_local;
921    }
922    // Sum the penalties that are materialized as full `p_local × p_local`
923    // blocks (the common smooth case). The covariance block the Wald test
924    // slices lives in this same coefficient basis (post joint-null rotation),
925    // so the rank is computed in the right metric.
926    let mut s_total = Array2::<f64>::zeros((p_local, p_local));
927    let mut materialized = 0usize;
928    for s in penalties_local {
929        if s.nrows() == p_local && s.ncols() == p_local {
930            s_total += s;
931            materialized += 1;
932        }
933    }
934    if materialized == penalties_local.len() {
935        let symmetric = {
936            let transpose = s_total.t().to_owned();
937            (&s_total + &transpose) * 0.5
938        };
939        if let Ok((evals, _)) = symmetric.eigh(faer::Side::Lower) {
940            let max_abs = evals.iter().fold(0.0_f64, |acc, &v| acc.max(v.abs()));
941            if max_abs == 0.0 {
942                // All penalties identically zero ⇒ unpenalized block.
943                return p_local;
944            }
945            let tol = max_abs * (p_local as f64) * 1e-12;
946            let rank = evals.iter().filter(|&&v| v > tol).count();
947            return p_local.saturating_sub(rank);
948        }
949    }
950    // Conservative fallback when a penalty is not a materialized full block
951    // (e.g. a Kronecker tensor factor): with ≥2 active penalties the joint
952    // null space is almost always empty (the only over-rejecting direction);
953    // with a single penalty it is exactly that penalty's own null space.
954    if penalties_local.len() >= 2 {
955        0
956    } else {
957        nullspace_dims
958            .iter()
959            .copied()
960            .min()
961            .unwrap_or(0)
962            .min(p_local)
963    }
964}
965
966#[derive(Debug, Clone, Serialize, Deserialize)]
967pub struct PenaltyBlockInfo {
968    pub global_index: usize,
969    pub termname: Option<String>,
970    pub penalty: PenaltyInfo,
971}
972
973#[derive(Debug, Clone, Serialize, Deserialize)]
974pub struct DroppedPenaltyBlockInfo {
975    pub termname: Option<String>,
976    pub penalty: PenaltyInfo,
977}
978
979#[derive(Debug, Clone)]
980pub struct SmoothDesign {
981    pub term_designs: Vec<DesignMatrix>,
982    /// Per-term block-local penalties.  Each `col_range` is relative to the
983    /// smooth block (i.e. indexing into the concatenation of `term_designs`).
984    pub penalties: Vec<BlockwisePenalty>,
985    pub nullspace_dims: Vec<usize>,
986    pub penaltyinfo: Vec<PenaltyBlockInfo>,
987    pub dropped_penaltyinfo: Vec<DroppedPenaltyBlockInfo>,
988    pub terms: Vec<SmoothTerm>,
989    /// Optional smooth-block lower bounds in smooth coefficient coordinates.
990    /// Length equals `total_smooth_cols()` when present.
991    pub coefficient_lower_bounds: Option<Array1<f64>>,
992    /// Optional smooth-block inequality constraints:
993    /// `A_smooth * beta_smooth >= b`.
994    pub linear_constraints: Option<LinearInequalityConstraints>,
995}
996
997impl SmoothDesign {
998    pub fn total_smooth_cols(&self) -> usize {
999        self.term_designs.iter().map(DesignMatrix::ncols).sum()
1000    }
1001    pub fn nrows(&self) -> usize {
1002        self.term_designs.first().map_or(0, DesignMatrix::nrows)
1003    }
1004}
1005
1006#[derive(Debug, Clone)]
1007pub struct RawSmoothDesign {
1008    pub term_designs: Vec<DesignMatrix>,
1009    /// Per-term block-local penalties.  Each `col_range` is relative to the
1010    /// smooth block (i.e. indexing into the concatenation of `term_designs`).
1011    pub penalties: Vec<BlockwisePenalty>,
1012    pub nullspace_dims: Vec<usize>,
1013    pub penaltyinfo: Vec<PenaltyBlockInfo>,
1014    pub dropped_penaltyinfo: Vec<DroppedPenaltyBlockInfo>,
1015    pub terms: Vec<SmoothTerm>,
1016    pub coefficient_lower_bounds: Option<Array1<f64>>,
1017    pub linear_constraints: Option<LinearInequalityConstraints>,
1018}
1019
1020impl RawSmoothDesign {
1021    pub fn total_smooth_cols(&self) -> usize {
1022        self.term_designs.iter().map(DesignMatrix::ncols).sum()
1023    }
1024    pub fn nrows(&self) -> usize {
1025        self.term_designs.first().map_or(0, DesignMatrix::nrows)
1026    }
1027}
1028
1029impl From<RawSmoothDesign> for SmoothDesign {
1030    fn from(value: RawSmoothDesign) -> Self {
1031        Self {
1032            term_designs: value.term_designs,
1033            penalties: value.penalties,
1034            nullspace_dims: value.nullspace_dims,
1035            penaltyinfo: value.penaltyinfo,
1036            dropped_penaltyinfo: value.dropped_penaltyinfo,
1037            terms: value.terms,
1038            coefficient_lower_bounds: value.coefficient_lower_bounds,
1039            linear_constraints: value.linear_constraints,
1040        }
1041    }
1042}
1043
1044#[derive(Debug, Default, Clone, Serialize, Deserialize)]
1045pub enum BoundedCoefficientPriorSpec {
1046    #[default]
1047    None,
1048    Uniform,
1049    Beta {
1050        a: f64,
1051        b: f64,
1052    },
1053}
1054
1055#[derive(Debug, Clone, Serialize, Deserialize, Default)]
1056pub enum LinearCoefficientGeometry {
1057    #[default]
1058    Unconstrained,
1059    Bounded {
1060        min: f64,
1061        max: f64,
1062        #[serde(default)]
1063        prior: BoundedCoefficientPriorSpec,
1064    },
1065}
1066
1067#[derive(Debug, Clone, Serialize, Deserialize)]
1068pub struct LinearTermSpec {
1069    pub name: String,
1070    /// Primary feature column index. For Wilkinson-Rogers `:` interaction
1071    /// terms (`a:b[:c...]`) this is the first column in `feature_cols`; the
1072    /// realized design column is the elementwise product across every entry
1073    /// of `feature_cols`. Plain (non-interaction) linear terms set
1074    /// `feature_cols == vec![feature_col]`.
1075    pub feature_col: usize,
1076    /// Full list of columns whose elementwise product yields this term's
1077    /// design column. `len() >= 1`; `len() == 1` is a plain linear effect.
1078    #[serde(default)]
1079    pub feature_cols: Vec<usize>,
1080    /// Categorical-level gates for a factor-aware `:` interaction.
1081    ///
1082    /// Each `(col, level_bits)` multiplies the realized design column by the
1083    /// indicator `1[data[row, col].to_bits() == level_bits]`. This is how a
1084    /// `factor:x` (or `factor:factor`) interaction is expanded: `build_termspec`
1085    /// emits one `LinearTermSpec` per surviving cell of the categorical
1086    /// operand(s) (treatment-coded, first level dropped per factor), each
1087    /// carrying the numeric operands in `feature_cols` and the cell's level
1088    /// gate(s) here. Empty for a plain numeric `:` interaction or main effect,
1089    /// in which case the realized column is exactly the numeric product.
1090    #[serde(default)]
1091    pub categorical_levels: Vec<(usize, u64)>,
1092    /// Optional ridge (`S = I`, REML-selected `λ`) on this linear coefficient.
1093    /// A parametric linear term carries no wiggliness, so it is **unpenalized by
1094    /// default** — gam reports the MLE, matching mgcv/glm/survreg/VGAM (which
1095    /// penalize parametric terms only under an explicit `paraPen`). Set `true`
1096    /// to opt into an explicit shrinkage ridge (a zero-mean Gaussian prior
1097    /// `β ~ N(0, λ⁻¹)`); doing so adds one outer REML smoothing coordinate.
1098    #[serde(default = "default_linear_term_double_penalty")]
1099    pub double_penalty: bool,
1100    #[serde(default)]
1101    pub coefficient_geometry: LinearCoefficientGeometry,
1102    #[serde(default)]
1103    pub coefficient_min: Option<f64>,
1104    #[serde(default)]
1105    pub coefficient_max: Option<f64>,
1106}
1107
1108impl LinearTermSpec {
1109    /// Return the effective list of feature columns. Backfills from
1110    /// `feature_col` for legacy specs that predate the multi-column field.
1111    pub fn effective_feature_cols(&self) -> Vec<usize> {
1112        if self.feature_cols.is_empty() {
1113            vec![self.feature_col]
1114        } else {
1115            self.feature_cols.clone()
1116        }
1117    }
1118
1119    /// True when this term is a Wilkinson-Rogers `:` interaction (multi-col).
1120    pub fn is_interaction(&self) -> bool {
1121        self.feature_cols.len() > 1 || !self.categorical_levels.is_empty()
1122    }
1123
1124    /// Realize this linear term's `(n,)` design column from `data`.
1125    ///
1126    /// The column is the elementwise product of every numeric feature column
1127    /// (`effective_feature_cols`) gated by the categorical-level indicators in
1128    /// `categorical_levels`: each `(col, level_bits)` multiplies the running
1129    /// column by `1[data[row, col].to_bits() == level_bits]`. A plain numeric
1130    /// term (no `categorical_levels`) reduces to the bare product, matching the
1131    /// historical behaviour. A pure categorical interaction (empty
1132    /// `feature_cols`, non-empty `categorical_levels`) reduces to the cell
1133    /// indicator. Bounds are validated here; the returned column has length
1134    /// `data.nrows()`.
1135    pub fn realized_design_column(&self, data: ArrayView2<'_, f64>) -> Result<Array1<f64>, String> {
1136        let n = data.nrows();
1137        let p = data.ncols();
1138        let bounds = |col: usize| -> Result<(), String> {
1139            if col >= p {
1140                Err(format!(
1141                    "linear term '{}' feature column {} out of bounds for {} columns",
1142                    self.name, col, p
1143                ))
1144            } else {
1145                Ok(())
1146            }
1147        };
1148
1149        // Numeric operands. When `categorical_levels` is set we treat
1150        // `feature_cols` as the (possibly empty) numeric operand list and start
1151        // from a column of ones; otherwise we preserve the legacy backfill from
1152        // `feature_col` so a plain term with no `feature_cols` still resolves.
1153        let mut column = if self.categorical_levels.is_empty() {
1154            let cols = self.effective_feature_cols();
1155            for &c in &cols {
1156                bounds(c)?;
1157            }
1158            let mut acc = data.column(cols[0]).to_owned();
1159            for &c in cols.iter().skip(1) {
1160                acc *= &data.column(c);
1161            }
1162            acc
1163        } else {
1164            let mut acc = Array1::<f64>::ones(n);
1165            for &c in &self.feature_cols {
1166                bounds(c)?;
1167                acc *= &data.column(c);
1168            }
1169            acc
1170        };
1171
1172        for &(col, level_bits) in &self.categorical_levels {
1173            bounds(col)?;
1174            let gate = data.column(col);
1175            for (out, &v) in column.iter_mut().zip(gate.iter()) {
1176                if v.to_bits() != level_bits {
1177                    *out = 0.0;
1178                }
1179            }
1180        }
1181
1182        Ok(column)
1183    }
1184}
1185
1186pub const fn default_linear_term_double_penalty() -> bool {
1187    // Parametric/linear terms are unpenalized by default — a single linear
1188    // coefficient has no roughness for a smoothing penalty to control, so the
1189    // historical `S = I`, REML-selected `λ` shrank every linear coefficient off
1190    // the MLE and injected a spurious outer smoothing coordinate (#749). Mature
1191    // tools (mgcv/glm/survreg/VGAM) leave parametric terms unpenalized; gam now
1192    // matches that and reports the MLE. An explicit `double_penalty = true`
1193    // still opts a term into a ridge.
1194    false
1195}
1196
1197pub const fn default_pca_smooth_penalty() -> f64 {
1198    1.0
1199}
1200
1201pub const fn default_pca_chunk_size() -> usize {
1202    4096
1203}
1204
1205/// Random-effects term specification.
1206///
1207/// The selected feature column is interpreted as a categorical grouping variable.
1208/// The term contributes a one-hot dummy block with an identity penalty on group
1209/// coefficients, equivalent to i.i.d. Gaussian random effects.
1210#[derive(Debug, Clone, Serialize, Deserialize)]
1211pub struct RandomEffectTermSpec {
1212    pub name: String,
1213    pub feature_col: usize,
1214    /// If true, drop the lexicographically first group level to use treatment coding.
1215    /// If false, keep all levels (full one-hot block, still identifiable under ridge).
1216    pub drop_first_level: bool,
1217    /// If true, add a ridge penalty and estimate this block as a random effect.
1218    /// If false, leave the one-hot/treatment-coded block unpenalized so it is a
1219    /// fixed categorical main effect.  The default preserves older saved models.
1220    #[serde(default = "default_random_effect_penalized")]
1221    pub penalized: bool,
1222    /// Optional fixed kept-level set (sorted by f64 bit pattern) captured at fit time.
1223    /// When present, prediction uses exactly these columns to avoid design drift.
1224    #[serde(default)]
1225    pub frozen_levels: Option<Vec<u64>>,
1226    /// Whether an *unseen* level of this grouping column is tolerated at predict
1227    /// time (encoded as an out-of-vocabulary code and shrunk toward the
1228    /// population mean) instead of raising a schema mismatch.
1229    ///
1230    /// Only an EXPLICIT random effect — `group(g)`/`re(g)`/`factor(g)` /
1231    /// `s(g, bs="re")` — is lenient: the held-out-group policy is a deliberate
1232    /// contract. A BARE categorical main effect (`+ g`), although auto-promoted
1233    /// to a penalized random block internally, is a FIXED parametric factor, and
1234    /// an out-of-vocabulary level of it must raise `SchemaMismatchError` at
1235    /// predict rather than being silently mapped to the factor's centering point
1236    /// (#2102). The `true` default preserves the pre-#2102 (uniformly lenient)
1237    /// behavior for models serialized before this field existed.
1238    #[serde(default = "default_random_effect_lenient_unseen")]
1239    pub lenient_unseen: bool,
1240}
1241
1242pub fn default_random_effect_penalized() -> bool {
1243    true
1244}
1245
1246pub fn default_random_effect_lenient_unseen() -> bool {
1247    true
1248}
1249
1250pub fn validate_measure_jet_positive_vec_len(
1251    label: &str,
1252    term_name: &str,
1253    field: &str,
1254    values: &[f64],
1255    expected: usize,
1256) -> Result<(), String> {
1257    if values.len() != expected {
1258        return Err(SmoothError::invalid_config(format!(
1259            "{label} term '{term_name}' frozen MeasureJet {field} has length {}, expected {expected}",
1260            values.len()
1261        ))
1262        .into());
1263    }
1264    if values
1265        .iter()
1266        .any(|value| !(value.is_finite() && *value > 0.0))
1267    {
1268        return Err(SmoothError::invalid_config(format!(
1269            "{label} term '{term_name}' frozen MeasureJet {field} values must be positive and finite"
1270        ))
1271        .into());
1272    }
1273    Ok(())
1274}
1275
1276#[derive(Debug, Clone, Serialize, Deserialize)]
1277pub struct TermCollectionSpec {
1278    pub linear_terms: Vec<LinearTermSpec>,
1279    pub random_effect_terms: Vec<RandomEffectTermSpec>,
1280    pub smooth_terms: Vec<SmoothTermSpec>,
1281}
1282
1283pub fn validate_smooth_basis_frozen(
1284    basis: &SmoothBasisSpec,
1285    label: &str,
1286    term_name: &str,
1287) -> Result<(), String> {
1288    match basis {
1289        SmoothBasisSpec::ByVariable { inner, .. }
1290        | SmoothBasisSpec::FactorSumToZero { inner, .. } => {
1291            validate_smooth_basis_frozen(inner, label, term_name)
1292        }
1293        SmoothBasisSpec::BSpline1D { spec, .. } => {
1294            if !matches!(
1295                spec.knotspec,
1296                BSplineKnotSpec::Provided(_)
1297                    | BSplineKnotSpec::PeriodicUniform { .. }
1298                    | BSplineKnotSpec::NaturalCubicRegression { .. }
1299            ) {
1300                return Err(format!(
1301                    "{label} term '{term_name}' is not frozen: BSpline knotspec must be Provided, PeriodicUniform, or NaturalCubicRegression"
1302                ));
1303            }
1304            Ok(())
1305        }
1306        SmoothBasisSpec::ThinPlate { spec, .. } => {
1307            if !matches!(spec.center_strategy, CenterStrategy::UserProvided(_)) {
1308                return Err(format!(
1309                    "{label} term '{term_name}' is not frozen: ThinPlate centers must be UserProvided"
1310                ));
1311            }
1312            if matches!(
1313                spec.identifiability,
1314                SpatialIdentifiability::OrthogonalToParametric
1315            ) {
1316                return Err(format!(
1317                    "{label} term '{term_name}' is not frozen: ThinPlate identifiability must be FrozenTransform or None"
1318                ));
1319            }
1320            Ok(())
1321        }
1322        _ => Ok(()),
1323    }
1324}
1325
1326impl TermCollectionSpec {
1327    /// Write this collection's topology identity into a warm-start cache
1328    /// fingerprint (#869).
1329    ///
1330    /// The persistent warm-start `cache_key` hashes only family + raw input
1331    /// dimensions, so two fits on the same data that differ *only* in their
1332    /// smooth topology (the `s(..., type=AUTO)` candidate enumeration: sphere
1333    /// vs torus vs euclidean vs duchon) collide on one key and seed each other
1334    /// with geometrically incompatible β/ρ. Folding the per-term structural
1335    /// kind + feature columns + linear/random-effect counts into the shape hash
1336    /// gives each candidate its own key, so the screen→full-refit reuse of one
1337    /// candidate is preserved while cross-candidate contamination is removed.
1338    /// Only the structural identity is hashed (not fitted coefficients or
1339    /// frozen knot values), so a refit of the *same* topology still hits.
1340    pub fn write_structural_shape_hash(&self, h: &mut gam_runtime::warm_start::Fingerprinter) {
1341        h.write_str("term-collection");
1342        h.write_usize(self.linear_terms.len());
1343        for linear in &self.linear_terms {
1344            h.write_str(&linear.name);
1345        }
1346        h.write_usize(self.random_effect_terms.len());
1347        h.write_usize(self.smooth_terms.len());
1348        for smooth in &self.smooth_terms {
1349            h.write_str(&smooth.name);
1350            h.write_str(smooth.basis.structural_kind());
1351            for col in smooth.basis.structural_feature_cols() {
1352                h.write_usize(col);
1353            }
1354        }
1355    }
1356
1357    /// Validate that a term collection spec represents a fully frozen model
1358    /// (i.e. all knots/centers are pre-computed, identifiability transforms are
1359    /// baked in, and random-effect levels are fixed).
1360    pub fn validate_frozen(&self, label: &str) -> Result<(), String> {
1361        for linear in &self.linear_terms {
1362            if let (Some(min), Some(max)) = (linear.coefficient_min, linear.coefficient_max)
1363                && (!min.is_finite() || !max.is_finite() || min > max)
1364            {
1365                return Err(SmoothError::invalid_config(format!(
1366                    "{label} linear term '{}' has invalid coefficient constraint [{min}, {max}]",
1367                    linear.name
1368                ))
1369                .into());
1370            }
1371            if let Some(min) = linear.coefficient_min
1372                && !min.is_finite()
1373            {
1374                return Err(SmoothError::invalid_config(format!(
1375                    "{label} linear term '{}' has non-finite coefficient minimum {min}",
1376                    linear.name
1377                ))
1378                .into());
1379            }
1380            if let Some(max) = linear.coefficient_max
1381                && !max.is_finite()
1382            {
1383                return Err(SmoothError::invalid_config(format!(
1384                    "{label} linear term '{}' has non-finite coefficient maximum {max}",
1385                    linear.name
1386                ))
1387                .into());
1388            }
1389            if let LinearCoefficientGeometry::Bounded { min, max, prior } =
1390                &linear.coefficient_geometry
1391            {
1392                if !min.is_finite() || !max.is_finite() || min >= max {
1393                    return Err(SmoothError::invalid_config(format!(
1394                        "{label} bounded term '{}' has invalid bounds [{min}, {max}]",
1395                        linear.name
1396                    ))
1397                    .into());
1398                }
1399                match prior {
1400                    BoundedCoefficientPriorSpec::None | BoundedCoefficientPriorSpec::Uniform => {}
1401                    BoundedCoefficientPriorSpec::Beta { a, b } => {
1402                        if !a.is_finite() || !b.is_finite() || *a < 1.0 || *b < 1.0 {
1403                            return Err(SmoothError::invalid_config(format!(
1404                                "{label} bounded term '{}' has invalid Beta prior ({a}, {b})",
1405                                linear.name
1406                            ))
1407                            .into());
1408                        }
1409                    }
1410                }
1411            }
1412        }
1413        for st in &self.smooth_terms {
1414            match &st.basis {
1415                SmoothBasisSpec::ByVariable { inner, .. } => {
1416                    validate_smooth_basis_frozen(inner, label, &st.name)?;
1417                    let nested = SmoothTermSpec {
1418                        name: st.name.clone(),
1419                        basis: (**inner).clone(),
1420                        shape: st.shape,
1421                        joint_null_rotation: None,
1422                    };
1423                    TermCollectionSpec {
1424                        linear_terms: Vec::new(),
1425                        random_effect_terms: Vec::new(),
1426                        smooth_terms: vec![nested],
1427                    }
1428                    .validate_frozen(label)?;
1429                }
1430                SmoothBasisSpec::FactorSumToZero { inner, levels, .. } => {
1431                    if levels.len() < 2 {
1432                        return Err(format!(
1433                            "{label} term '{}' has invalid frozen sz levels",
1434                            st.name
1435                        ));
1436                    }
1437                    validate_smooth_basis_frozen(inner, label, &st.name)?;
1438                }
1439                SmoothBasisSpec::BSpline1D { spec, .. } => {
1440                    if !matches!(
1441                        spec.knotspec,
1442                        BSplineKnotSpec::Provided(_)
1443                            | BSplineKnotSpec::PeriodicUniform { .. }
1444                            | BSplineKnotSpec::NaturalCubicRegression { .. }
1445                    ) {
1446                        return Err(SmoothError::invalid_config(format!(
1447                            "{label} term '{}' is not frozen: BSpline knotspec must be Provided, PeriodicUniform, or NaturalCubicRegression",
1448                            st.name
1449                        ))
1450                        .into());
1451                    }
1452                }
1453                SmoothBasisSpec::ThinPlate { spec, .. } => {
1454                    if !matches!(spec.center_strategy, CenterStrategy::UserProvided(_)) {
1455                        return Err(SmoothError::invalid_config(format!(
1456                            "{label} term '{}' is not frozen: ThinPlate centers must be UserProvided",
1457                            st.name
1458                        ))
1459                        .into());
1460                    }
1461                    if matches!(
1462                        spec.identifiability,
1463                        SpatialIdentifiability::OrthogonalToParametric
1464                    ) {
1465                        return Err(SmoothError::invalid_config(format!(
1466                            "{label} term '{}' is not frozen: ThinPlate identifiability must be FrozenTransform or None",
1467                            st.name
1468                        ))
1469                        .into());
1470                    }
1471                }
1472                SmoothBasisSpec::Sphere { spec, .. } => {
1473                    if !matches!(spec.center_strategy, CenterStrategy::UserProvided(_)) {
1474                        return Err(SmoothError::invalid_config(format!(
1475                            "{label} term '{}' is not frozen: Sphere centers must be UserProvided",
1476                            st.name
1477                        ))
1478                        .into());
1479                    }
1480                    if matches!(spec.method, crate::basis::SphereMethod::Harmonic)
1481                        && spec.max_degree.is_none_or(|d| d == 0)
1482                    {
1483                        return Err(format!(
1484                            "{label} term '{}' is not frozen: sphere max_degree must be positive",
1485                            st.name
1486                        ));
1487                    }
1488                }
1489                SmoothBasisSpec::ConstantCurvature { spec, .. } => {
1490                    if !matches!(spec.center_strategy, CenterStrategy::UserProvided(_)) {
1491                        return Err(SmoothError::invalid_config(format!(
1492                            "{label} term '{}' is not frozen: ConstantCurvature centers must be UserProvided",
1493                            st.name
1494                        ))
1495                        .into());
1496                    }
1497                    if !(spec.length_scale.is_finite() && spec.length_scale > 0.0) {
1498                        return Err(SmoothError::invalid_config(format!(
1499                            "{label} term '{}' is not frozen: ConstantCurvature length_scale must be the realized positive value",
1500                            st.name
1501                        ))
1502                        .into());
1503                    }
1504                }
1505                SmoothBasisSpec::MeasureJet { spec, .. } => {
1506                    let centers = match &spec.center_strategy {
1507                        CenterStrategy::UserProvided(centers) => centers,
1508                        _ => {
1509                            return Err(SmoothError::invalid_config(format!(
1510                                "{label} term '{}' is not frozen: MeasureJet centers must be UserProvided",
1511                                st.name
1512                            ))
1513                            .into());
1514                        }
1515                    };
1516                    if centers.nrows() == 0 {
1517                        return Err(SmoothError::invalid_config(format!(
1518                            "{label} term '{}' is not frozen: MeasureJet centers are empty",
1519                            st.name
1520                        ))
1521                        .into());
1522                    }
1523                    if !(spec.length_scale.is_finite() && spec.length_scale > 0.0) {
1524                        return Err(SmoothError::invalid_config(format!(
1525                            "{label} term '{}' is not frozen: MeasureJet length_scale must be the realized positive value",
1526                            st.name
1527                        ))
1528                        .into());
1529                    }
1530                    // Exact replay needs the fit-data penalty quadrature and
1531                    // normalization payload (`BasisMetadata::MeasureJet`).
1532                    let frozen = spec.frozen_quadrature.as_ref().ok_or_else(|| {
1533                        SmoothError::invalid_config(format!(
1534                            "{label} term '{}' is not frozen: MeasureJet frozen_quadrature payload is missing",
1535                            st.name
1536                        ))
1537                    })?;
1538                    if frozen.masses.len() != centers.nrows() {
1539                        return Err(SmoothError::invalid_config(format!(
1540                            "{label} term '{}' frozen MeasureJet has {} masses for {} centers",
1541                            st.name,
1542                            frozen.masses.len(),
1543                            centers.nrows()
1544                        ))
1545                        .into());
1546                    }
1547                    let total_mass = frozen.masses.sum();
1548                    if frozen
1549                        .masses
1550                        .iter()
1551                        .any(|mass| !(mass.is_finite() && *mass >= 0.0))
1552                        || !(total_mass.is_finite() && total_mass > 0.0)
1553                    {
1554                        return Err(SmoothError::invalid_config(format!(
1555                            "{label} term '{}' frozen MeasureJet masses must be finite, nonnegative, and have positive total mass",
1556                            st.name
1557                        ))
1558                        .into());
1559                    }
1560                    let n_levels = frozen.eps_band.len();
1561                    if n_levels == 0
1562                        || frozen
1563                            .eps_band
1564                            .iter()
1565                            .any(|eps| !(eps.is_finite() && *eps > 0.0))
1566                    {
1567                        return Err(SmoothError::invalid_config(format!(
1568                            "{label} term '{}' frozen MeasureJet eps_band must be nonempty, finite, and positive",
1569                            st.name
1570                        ))
1571                        .into());
1572                    }
1573                    for (idx, pair) in frozen.eps_band.windows(2).enumerate() {
1574                        if pair[1] <= pair[0] {
1575                            return Err(SmoothError::invalid_config(format!(
1576                                "{label} term '{}' frozen MeasureJet eps_band is not strictly ascending at {idx}: {} then {}",
1577                                st.name,
1578                                pair[0],
1579                                pair[1]
1580                            ))
1581                            .into());
1582                        }
1583                    }
1584                    validate_measure_jet_positive_vec_len(
1585                        label,
1586                        &st.name,
1587                        "support_means",
1588                        &frozen.support_means,
1589                        n_levels,
1590                    )?;
1591                    // Mode predicate MUST match the builder's
1592                    // (`measure_jet_multiscale_mode`): per-level/multiscale is the
1593                    // explicit `spec.multiscale` opt-in (#1116). In single-scale
1594                    // mode the builder emits a single FUSED penalty (empty
1595                    // per-level scales + `fused_penalty_normalization_scale:
1596                    // Some`); only the multiscale opt-in carries `n_levels`
1597                    // per-level scales.
1598                    let per_level = crate::basis::measure_jet_multiscale_mode(spec);
1599                    if per_level {
1600                        validate_measure_jet_positive_vec_len(
1601                            label,
1602                            &st.name,
1603                            "penalty_normalization_scales",
1604                            &frozen.penalty_normalization_scales,
1605                            n_levels,
1606                        )?;
1607                        validate_measure_jet_positive_vec_len(
1608                            label,
1609                            &st.name,
1610                            "raw_penalty_normalization_scales",
1611                            &frozen.raw_penalty_normalization_scales,
1612                            n_levels,
1613                        )?;
1614                        if frozen.fused_penalty_normalization_scale.is_some() {
1615                            return Err(SmoothError::invalid_config(format!(
1616                                "{label} term '{}' per-level MeasureJet must not carry a fused penalty normalization scale",
1617                                st.name
1618                            ))
1619                            .into());
1620                        }
1621                    } else {
1622                        if !frozen.penalty_normalization_scales.is_empty()
1623                            || !frozen.raw_penalty_normalization_scales.is_empty()
1624                        {
1625                            return Err(SmoothError::invalid_config(format!(
1626                                "{label} term '{}' fused MeasureJet must not carry per-level penalty normalization scales",
1627                                st.name
1628                            ))
1629                            .into());
1630                        }
1631                        match frozen.fused_penalty_normalization_scale {
1632                            Some(scale) if scale.is_finite() && scale > 0.0 => {}
1633                            Some(scale) => {
1634                                return Err(SmoothError::invalid_config(format!(
1635                                    "{label} term '{}' fused MeasureJet penalty normalization scale must be positive and finite, got {scale}",
1636                                    st.name
1637                                ))
1638                                .into());
1639                            }
1640                            None => {
1641                                return Err(SmoothError::invalid_config(format!(
1642                                    "{label} term '{}' fused MeasureJet is missing its penalty normalization scale",
1643                                    st.name
1644                                ))
1645                                .into());
1646                            }
1647                        }
1648                    }
1649                }
1650                SmoothBasisSpec::Matern { spec, .. } => {
1651                    if !matches!(spec.center_strategy, CenterStrategy::UserProvided(_)) {
1652                        return Err(SmoothError::invalid_config(format!(
1653                            "{label} term '{}' is not frozen: Matern centers must be UserProvided",
1654                            st.name
1655                        ))
1656                        .into());
1657                    }
1658                }
1659                SmoothBasisSpec::Duchon { spec, .. } => {
1660                    if !matches!(spec.center_strategy, CenterStrategy::UserProvided(_)) {
1661                        return Err(SmoothError::invalid_config(format!(
1662                            "{label} term '{}' is not frozen: Duchon centers must be UserProvided",
1663                            st.name
1664                        ))
1665                        .into());
1666                    }
1667                    if matches!(
1668                        spec.identifiability,
1669                        SpatialIdentifiability::OrthogonalToParametric
1670                    ) {
1671                        return Err(SmoothError::invalid_config(format!(
1672                            "{label} term '{}' is not frozen: Duchon identifiability must be FrozenTransform or None",
1673                            st.name
1674                        ))
1675                        .into());
1676                    }
1677                }
1678                SmoothBasisSpec::Pca {
1679                    centered,
1680                    center_mean,
1681                    pca_basis_path,
1682                    ..
1683                } => {
1684                    if *centered && center_mean.is_none() && pca_basis_path.is_none() {
1685                        return Err(SmoothError::invalid_config(format!(
1686                            "{label} term '{}' is not frozen: centered Pca missing center_mean",
1687                            st.name
1688                        ))
1689                        .into());
1690                    }
1691                }
1692                SmoothBasisSpec::BySmooth { smooth, by_kind } => {
1693                    if let SmoothBasisSpec::BySmooth { .. } = smooth.as_ref() {
1694                        return Err(format!("{label} term '{}' has nested by-smooths", st.name));
1695                    }
1696                    match by_kind {
1697                        ByVarKind::Numeric { .. } => {}
1698                        ByVarKind::Factor { frozen_levels, .. } if frozen_levels.is_none() => {
1699                            return Err(format!(
1700                                "{label} term '{}' is not frozen: by-factor levels missing",
1701                                st.name
1702                            ));
1703                        }
1704                        ByVarKind::Factor { .. } => {}
1705                    }
1706                    let nested = TermCollectionSpec {
1707                        linear_terms: vec![],
1708                        random_effect_terms: vec![],
1709                        smooth_terms: vec![SmoothTermSpec {
1710                            name: st.name.clone(),
1711                            basis: (**smooth).clone(),
1712                            shape: st.shape,
1713                            joint_null_rotation: None,
1714                        }],
1715                    };
1716                    nested.validate_frozen(label)?;
1717                }
1718                SmoothBasisSpec::FactorSmooth { spec } => {
1719                    if spec.group_frozen_levels.is_none() {
1720                        return Err(format!(
1721                            "{label} term '{}' is not frozen: factor-smooth levels missing",
1722                            st.name
1723                        ));
1724                    }
1725                    if !matches!(
1726                        spec.marginal.knotspec,
1727                        BSplineKnotSpec::Provided(_)
1728                            | BSplineKnotSpec::PeriodicUniform { .. }
1729                            // mgcv's `bs="sz"` default marginal is a cubic
1730                            // regression spline (#1074), and the freeze step
1731                            // restores it as a `NaturalCubicRegression` knotspec
1732                            // carrying its `k` value-knots (spatial_optimization.rs
1733                            // `marginal_is_cr` branch) — the SAME treatment the
1734                            // tensor margin already gets in the arm below. Without
1735                            // this variant a frozen `sz` factor smooth fails its own
1736                            // predict-time freeze check ("factor-smooth marginal
1737                            // knots missing") even though its knots are fully
1738                            // materialized; the validation simply was not updated
1739                            // when the cr marginal landed.
1740                            | BSplineKnotSpec::NaturalCubicRegression { .. }
1741                    ) {
1742                        return Err(format!(
1743                            "{label} term '{}' is not frozen: factor-smooth marginal knots missing",
1744                            st.name
1745                        ));
1746                    }
1747                }
1748                SmoothBasisSpec::TensorBSpline { spec, .. } => {
1749                    for (dim, marginal) in spec.marginalspecs.iter().enumerate() {
1750                        if !matches!(
1751                            marginal.knotspec,
1752                            BSplineKnotSpec::Provided(_)
1753                                | BSplineKnotSpec::PeriodicUniform { .. }
1754                                | BSplineKnotSpec::NaturalCubicRegression { .. }
1755                        ) {
1756                            return Err(SmoothError::invalid_config(format!(
1757                                "{label} term '{}' dim {} is not frozen: tensor marginal knotspec must be Provided, PeriodicUniform, or NaturalCubicRegression",
1758                                st.name, dim
1759                            ))
1760                            .into());
1761                        }
1762                    }
1763                    if matches!(
1764                        spec.identifiability,
1765                        TensorBSplineIdentifiability::SumToZero
1766                            | TensorBSplineIdentifiability::MarginalSumToZero
1767                    ) {
1768                        return Err(SmoothError::invalid_config(format!(
1769                            "{label} term '{}' is not frozen: tensor identifiability must be FrozenTransform or None",
1770                            st.name
1771                        ))
1772                        .into());
1773                    }
1774                }
1775            }
1776        }
1777
1778        for rt in &self.random_effect_terms {
1779            if rt.frozen_levels.is_none() {
1780                return Err(SmoothError::invalid_config(format!(
1781                    "{label} random-effect term '{}' is not frozen: missing frozen_levels",
1782                    rt.name
1783                ))
1784                .into());
1785            }
1786        }
1787
1788        Ok(())
1789    }
1790
1791    /// Re-resolve every stored feature-column index through `remap`, returning a
1792    /// spec that addresses a different column layout.
1793    ///
1794    /// A frozen `TermCollectionSpec` stores feature columns as *absolute indices
1795    /// into the training table*. To replay it on a fresh dataset whose columns
1796    /// sit at different positions — the common case at prediction time, where
1797    /// the response column is unknown and may be absent entirely — every index
1798    /// must be re-resolved against the new layout. `remap` receives each
1799    /// training-table index and returns its position in the runtime table;
1800    /// callers typically implement it as "look the name up in the training
1801    /// headers, then resolve that name against the prediction dataset".
1802    ///
1803    /// This is the single authority on *which* fields carry a column index
1804    /// across every basis variant (linear, random-effect, the `by=` column of
1805    /// `ByVariable`/`FactorSumToZero`/`BySmooth`, the continuous and group
1806    /// columns of a `FactorSmooth`, and the multi-axis `feature_cols` of every
1807    /// spatial/tensor basis), so a predict-time realignment cannot silently miss
1808    /// one and dereference a stale training index.
1809    pub fn remap_feature_columns<E, F>(&self, mut remap: F) -> Result<TermCollectionSpec, E>
1810    where
1811        F: FnMut(usize) -> Result<usize, E>,
1812    {
1813        let mut out = self.clone();
1814        for lt in &mut out.linear_terms {
1815            lt.feature_col = remap(lt.feature_col)?;
1816            // Also remap the full interaction-factor list. The design builder
1817            // (`build_term_collection_design_inner`) materializes the column from
1818            // `effective_feature_cols()` — which returns `feature_cols` whenever
1819            // it is non-empty (i.e. essentially always, including a plain linear
1820            // term where `feature_cols == [feature_col]`). Remapping only the
1821            // singular `feature_col` left these at their saved *training* indices
1822            // at predict time, so a parametric `Surv(...) ~ x` (and any `:`
1823            // interaction) bailed with "feature column N out of bounds" once the
1824            // response/time columns shift the runtime layout (issue #898).
1825            for fc in lt.feature_cols.iter_mut() {
1826                *fc = remap(*fc)?;
1827            }
1828            // A factor-aware `:` interaction also gates on categorical columns;
1829            // those indices live in the same training-time layout and must be
1830            // realigned to the runtime table alongside the numeric operands, or
1831            // the predict-time level indicator would dereference a stale column.
1832            for (col, _bits) in lt.categorical_levels.iter_mut() {
1833                *col = remap(*col)?;
1834            }
1835        }
1836        for rt in &mut out.random_effect_terms {
1837            rt.feature_col = remap(rt.feature_col)?;
1838        }
1839        for st in &mut out.smooth_terms {
1840            remap_smooth_basis_feature_columns(&mut st.basis, &mut remap)?;
1841        }
1842        Ok(out)
1843    }
1844}
1845
1846/// Walk a `SmoothBasisSpec` tree, re-resolving every column index through
1847/// `remap`. Shared by all predict-time column realignment (see
1848/// [`TermCollectionSpec::remap_feature_columns`]); kept exhaustive so a newly
1849/// added index-bearing variant fails to compile until it is handled here.
1850pub fn remap_smooth_basis_feature_columns<E, F>(
1851    basis: &mut SmoothBasisSpec,
1852    remap: &mut F,
1853) -> Result<(), E>
1854where
1855    F: FnMut(usize) -> Result<usize, E>,
1856{
1857    match basis {
1858        SmoothBasisSpec::ByVariable { inner, by_col, .. }
1859        | SmoothBasisSpec::FactorSumToZero { inner, by_col, .. } => {
1860            *by_col = remap(*by_col)?;
1861            remap_smooth_basis_feature_columns(inner, remap)?;
1862        }
1863        SmoothBasisSpec::BSpline1D { feature_col, .. } => {
1864            *feature_col = remap(*feature_col)?;
1865        }
1866        SmoothBasisSpec::BySmooth { smooth, by_kind } => {
1867            let by_feature_col = match by_kind {
1868                ByVarKind::Numeric { feature_col } | ByVarKind::Factor { feature_col, .. } => {
1869                    feature_col
1870                }
1871            };
1872            *by_feature_col = remap(*by_feature_col)?;
1873            remap_smooth_basis_feature_columns(smooth, remap)?;
1874        }
1875        SmoothBasisSpec::FactorSmooth { spec } => {
1876            for fc in spec.continuous_cols.iter_mut() {
1877                *fc = remap(*fc)?;
1878            }
1879            spec.group_col = remap(spec.group_col)?;
1880        }
1881        SmoothBasisSpec::ThinPlate { feature_cols, .. }
1882        | SmoothBasisSpec::Sphere { feature_cols, .. }
1883        | SmoothBasisSpec::ConstantCurvature { feature_cols, .. }
1884        | SmoothBasisSpec::Matern { feature_cols, .. }
1885        | SmoothBasisSpec::MeasureJet { feature_cols, .. }
1886        | SmoothBasisSpec::Duchon { feature_cols, .. }
1887        | SmoothBasisSpec::Pca { feature_cols, .. }
1888        | SmoothBasisSpec::TensorBSpline { feature_cols, .. } => {
1889            for fc in feature_cols.iter_mut() {
1890                *fc = remap(*fc)?;
1891            }
1892        }
1893    }
1894    Ok(())
1895}
1896
1897#[derive(Debug, Clone)]
1898pub enum PenaltyStructureHint {
1899    Ridge(f64),
1900    Kronecker(Vec<Array2<f64>>),
1901}
1902
1903/// A penalty matrix stored at its natural block size together with the
1904/// column range it occupies in the global coefficient vector.
1905///
1906/// Instead of embedding every penalty into a full `p_total × p_total` dense
1907/// matrix filled with zeros, we keep the compact local matrix and reconstruct
1908/// the global view only when a downstream consumer explicitly requires it.
1909#[derive(Clone)]
1910pub struct BlockwisePenalty {
1911    /// Column range in the global coefficient vector that this penalty covers.
1912    pub col_range: Range<usize>,
1913    /// The local penalty matrix — dimensions `block_p × block_p` where
1914    /// `block_p = col_range.len()`.
1915    pub local: Array2<f64>,
1916    /// Optional nonzero centering vector for this coefficient block.
1917    pub prior_mean: gam_problem::CoefficientPriorMean,
1918    /// Optional structural hint so downstream spectral/logdet code can stay
1919    /// block-local or factorized without reverse-engineering the matrix.
1920    pub structure_hint: Option<PenaltyStructureHint>,
1921    /// Optional operator-form handle bit-equivalent to `local`. Populated when
1922    /// the originating closed-form factory emitted an op-form penalty so exact
1923    /// operator algebra can use matvec instead of materializing the dense
1924    /// `block_p × block_p` Gram. `None` for ordinary dense penalties.
1925    pub op: Option<std::sync::Arc<dyn crate::analytic_penalties::PenaltyOp>>,
1926}
1927
1928impl std::fmt::Debug for BlockwisePenalty {
1929    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1930        f.debug_struct("BlockwisePenalty")
1931            .field("col_range", &self.col_range)
1932            .field(
1933                "local",
1934                &format_args!("{}×{}", self.local.nrows(), self.local.ncols()),
1935            )
1936            .field("prior_mean", &self.prior_mean)
1937            .field("structure_hint", &self.structure_hint)
1938            .field("op", &self.op.as_ref().map(|o| o.dim()))
1939            .finish()
1940    }
1941}
1942
1943impl BlockwisePenalty {
1944    /// Create a new blockwise penalty.
1945    pub fn new(col_range: Range<usize>, local: Array2<f64>) -> Self {
1946        assert_eq!(col_range.len(), local.nrows());
1947        assert_eq!(col_range.len(), local.ncols());
1948        Self {
1949            col_range,
1950            local,
1951            prior_mean: gam_problem::CoefficientPriorMean::Zero,
1952            structure_hint: None,
1953            op: None,
1954        }
1955    }
1956
1957    pub fn with_prior_mean(
1958        mut self,
1959        prior_mean: gam_problem::CoefficientPriorMean,
1960    ) -> Self {
1961        self.prior_mean = prior_mean;
1962        self
1963    }
1964
1965    /// Attach an op-form penalty handle bit-equivalent to `local`.
1966    pub fn with_op(
1967        mut self,
1968        op: Option<std::sync::Arc<dyn crate::analytic_penalties::PenaltyOp>>,
1969    ) -> Self {
1970        self.op = op;
1971        self
1972    }
1973
1974    pub fn ridge(col_range: Range<usize>, scale: f64) -> Self {
1975        let block_size = col_range.len();
1976        let mut local = Array2::<f64>::zeros((block_size, block_size));
1977        for i in 0..block_size {
1978            local[[i, i]] = scale;
1979        }
1980        Self {
1981            col_range,
1982            local,
1983            prior_mean: gam_problem::CoefficientPriorMean::Zero,
1984            structure_hint: Some(PenaltyStructureHint::Ridge(scale)),
1985            op: None,
1986        }
1987    }
1988
1989    pub fn kronecker(
1990        col_range: Range<usize>,
1991        local: Array2<f64>,
1992        factors: Vec<Array2<f64>>,
1993    ) -> Self {
1994        assert_eq!(col_range.len(), local.nrows());
1995        assert_eq!(col_range.len(), local.ncols());
1996        Self {
1997            col_range,
1998            local,
1999            prior_mean: gam_problem::CoefficientPriorMean::Zero,
2000            structure_hint: Some(PenaltyStructureHint::Kronecker(factors)),
2001            op: None,
2002        }
2003    }
2004
2005    /// Expand this blockwise penalty into a full `p_total × p_total` dense
2006    /// matrix (mostly zeros). Use sparingly — the whole point of blockwise
2007    /// storage is to avoid this allocation.
2008    pub fn to_global(&self, p_total: usize) -> Array2<f64> {
2009        let mut g = Array2::<f64>::zeros((p_total, p_total));
2010        let r = &self.col_range;
2011        assert!(
2012            r.end <= p_total && self.local.nrows() == r.len() && self.local.ncols() == r.len(),
2013            "BlockwisePenalty::to_global shape invariant violated: \
2014             col_range={}..{}, local={}x{}, p_total={}",
2015            r.start,
2016            r.end,
2017            self.local.nrows(),
2018            self.local.ncols(),
2019            p_total,
2020        );
2021        g.slice_mut(s![r.start..r.end, r.start..r.end])
2022            .assign(&self.local);
2023        g
2024    }
2025
2026    /// Convert into a blockwise [`gam_problem::PenaltyMatrix`] without
2027    /// expanding to full dimensions.
2028    pub fn to_penalty_matrix(
2029        &self,
2030        total_dim: usize,
2031    ) -> gam_problem::PenaltyMatrix {
2032        gam_problem::PenaltyMatrix::Blockwise {
2033            local: self.local.clone(),
2034            col_range: self.col_range.clone(),
2035            total_dim,
2036        }
2037    }
2038
2039    /// The block size of this penalty.
2040    #[inline]
2041    pub fn block_size(&self) -> usize {
2042        self.col_range.len()
2043    }
2044}
2045
2046/// Compute `Σ_k λ_k S_k` directly from blockwise penalties, accumulating
2047/// into a pre-allocated `p_total × p_total` output without ever materializing
2048/// individual global matrices.
2049pub fn weighted_blockwise_penalty_sum(
2050    penalties: &[BlockwisePenalty],
2051    lambdas: &[f64],
2052    p_total: usize,
2053) -> Array2<f64> {
2054    assert_eq!(penalties.len(), lambdas.len());
2055    // Smoothing parameters λ_k must be non-negative and finite. A negative
2056    // λ would flip the sign of the corresponding block S_k, turning the
2057    // total penalty matrix indefinite and silently corrupting every
2058    // downstream Cholesky / PIRLS / REML / pseudo-logdet computation that
2059    // assumes S_λ ⪰ 0. Catch this at the boundary rather than after it
2060    // has propagated.
2061    for (idx, &lam) in lambdas.iter().enumerate() {
2062        assert!(
2063            lam.is_finite() && lam >= 0.0,
2064            "weighted_blockwise_penalty_sum: lambdas[{idx}] = {lam} is invalid (must be finite and non-negative; negative smoothing parameters violate S_λ ⪰ 0)",
2065        );
2066    }
2067    // Block column ranges must also fit inside the declared total parameter
2068    // dimension; an out-of-bounds slice would otherwise panic from ndarray
2069    // with a far less informative message.
2070    for (idx, bp) in penalties.iter().enumerate() {
2071        let r = &bp.col_range;
2072        assert!(
2073            r.end <= p_total,
2074            "weighted_blockwise_penalty_sum: penalties[{idx}] col_range {:?} exceeds p_total = {p_total}",
2075            r,
2076        );
2077    }
2078    let mut out = Array2::<f64>::zeros((p_total, p_total));
2079    for (bp, &lam) in penalties.iter().zip(lambdas.iter()) {
2080        let r = &bp.col_range;
2081        let mut slice = out.slice_mut(s![r.start..r.end, r.start..r.end]);
2082        slice.scaled_add(lam, &bp.local);
2083    }
2084    out
2085}
2086
2087// ---------------------------------------------------------------------------
2088// KroneckerPenaltySystem — factored tensor-product penalty representation
2089// ---------------------------------------------------------------------------
2090
2091/// Factored representation of tensor-product penalties with precomputed
2092/// marginal eigensystems for O(∏q_j) logdet and penalty operations.
2093#[derive(Debug, Clone)]
2094pub struct KroneckerPenaltySystem {
2095    /// Marginal penalty matrices: `marginal_penalties[k]` is `(q_k, q_k)`.
2096    pub marginal_penalties: Vec<Array2<f64>>,
2097    /// Precomputed eigensystems: `(eigenvalues, eigenvectors)` per marginal.
2098    pub marginal_eigensystems: Vec<(Array1<f64>, Array2<f64>)>,
2099    /// Marginal basis dimensions.
2100    pub marginal_dims: Vec<usize>,
2101    /// Whether a global ridge (double) penalty is present.
2102    pub has_double_penalty: bool,
2103}
2104
2105impl KroneckerPenaltySystem {
2106    pub fn new(
2107        marginal_penalties: Vec<Array2<f64>>,
2108        marginal_dims: Vec<usize>,
2109        has_double_penalty: bool,
2110    ) -> Result<Self, BasisError> {
2111        if marginal_penalties.len() != marginal_dims.len() {
2112            crate::bail_dim_basis!(
2113                "KroneckerPenaltySystem: {} penalties vs {} dims",
2114                marginal_penalties.len(),
2115                marginal_dims.len()
2116            );
2117        }
2118        let eigensystems =
2119            kronecker_marginal_eigensystems(&marginal_penalties, "KroneckerPenaltySystem")
2120                .map_err(|e| BasisError::InvalidInput(e.to_string()))?;
2121        Ok(Self {
2122            marginal_penalties,
2123            marginal_eigensystems: eigensystems,
2124            marginal_dims,
2125            has_double_penalty,
2126        })
2127    }
2128
2129    pub fn p_total(&self) -> usize {
2130        self.marginal_dims.iter().copied().product()
2131    }
2132
2133    pub fn ndim(&self) -> usize {
2134        self.marginal_dims.len()
2135    }
2136
2137    pub fn num_penalties(&self) -> usize {
2138        self.marginal_dims.len() + if self.has_double_penalty { 1 } else { 0 }
2139    }
2140
2141    /// Compute `log|S|₊` and its first/second derivatives w.r.t. `ρ_k = log(λ_k)`.
2142    ///
2143    /// Iterates over the ∏q_j multi-index grid. Cost: O(d · ∏q_j), no O(p²) storage.
2144    pub fn logdet_and_derivatives(
2145        &self,
2146        lambdas: &[f64],
2147        ridge: f64,
2148    ) -> (f64, Array1<f64>, Array2<f64>) {
2149        let n_pen = self.num_penalties();
2150        assert_eq!(lambdas.len(), n_pen, "lambda count mismatch");
2151        let marginal_evals: Vec<_> = self
2152            .marginal_eigensystems
2153            .iter()
2154            .map(|(evals, _)| evals.view())
2155            .collect();
2156        kronecker_logdet_and_derivatives(
2157            &marginal_evals,
2158            &self.marginal_dims,
2159            lambdas,
2160            self.has_double_penalty,
2161            ridge,
2162        )
2163    }
2164
2165    pub fn logdet_rank_and_derivatives(
2166        &self,
2167        lambdas: &[f64],
2168        ridge: f64,
2169    ) -> (f64, usize, Array1<f64>, Array2<f64>) {
2170        let n_pen = self.num_penalties();
2171        assert_eq!(lambdas.len(), n_pen, "lambda count mismatch");
2172        let d = self.marginal_dims.len();
2173        let mut logdet = 0.0;
2174        let mut rank = 0usize;
2175        let mut grad = Array1::<f64>::zeros(n_pen);
2176        let mut hess = Array2::<f64>::zeros((n_pen, n_pen));
2177        // Positivity floor for a penalized eigenvalue `σ`: below this the mode
2178        // is treated as an unpenalized (null-space) direction and excluded from
2179        // both the rank count and the pseudo-log-determinant.
2180        const EIGENVALUE_POSITIVITY_FLOOR: f64 = 1e-12;
2181        // Floor on the *structural* eigenvalue sum (λ-independent) used to decide
2182        // whether a mode lives in the penalty range space and so should receive
2183        // the stabilizing ridge; a structurally-null mode gets no ridge.
2184        const STRUCTURAL_ZERO_FLOOR: f64 = 1e-12;
2185        let mut multi_idx = vec![0usize; d];
2186        loop {
2187            let mut sigma = 0.0;
2188            let mut structural_sigma = 0.0;
2189            for k in 0..d {
2190                let marginal_eigenvalue = self.marginal_eigensystems[k].0[multi_idx[k]];
2191                structural_sigma += marginal_eigenvalue;
2192                sigma += lambdas[k] * marginal_eigenvalue;
2193            }
2194            let joint_null = structural_sigma <= STRUCTURAL_ZERO_FLOOR;
2195            if self.has_double_penalty && joint_null {
2196                sigma += lambdas[d];
2197            }
2198            if structural_sigma > STRUCTURAL_ZERO_FLOOR {
2199                sigma += ridge;
2200            }
2201
2202            if sigma > EIGENVALUE_POSITIVITY_FLOOR {
2203                rank += 1;
2204                logdet += sigma.ln();
2205                let inv_sigma = 1.0 / sigma;
2206                let inv_sigma2 = inv_sigma * inv_sigma;
2207                for k in 0..n_pen {
2208                    let ck = if k < d {
2209                        lambdas[k] * self.marginal_eigensystems[k].0[multi_idx[k]]
2210                    } else if joint_null {
2211                        lambdas[d]
2212                    } else {
2213                        0.0
2214                    };
2215                    grad[k] += ck * inv_sigma;
2216                    hess[[k, k]] += ck * inv_sigma - ck * ck * inv_sigma2;
2217                    for l in (k + 1)..n_pen {
2218                        let cl = if l < d {
2219                            lambdas[l] * self.marginal_eigensystems[l].0[multi_idx[l]]
2220                        } else if joint_null {
2221                            lambdas[d]
2222                        } else {
2223                            0.0
2224                        };
2225                        let off = -ck * cl * inv_sigma2;
2226                        hess[[k, l]] += off;
2227                        hess[[l, k]] += off;
2228                    }
2229                }
2230            }
2231
2232            let mut carry = true;
2233            for dim in (0..d).rev() {
2234                if carry {
2235                    multi_idx[dim] += 1;
2236                    if multi_idx[dim] < self.marginal_dims[dim] {
2237                        carry = false;
2238                    } else {
2239                        multi_idx[dim] = 0;
2240                    }
2241                }
2242            }
2243            if carry {
2244                break;
2245            }
2246        }
2247        (logdet, rank, grad, hess)
2248    }
2249}
2250
2251#[cfg(test)]
2252mod joint_unpenalized_dim_tests {
2253    use super::joint_unpenalized_dim;
2254    use ndarray::{Array2, array};
2255
2256    #[test]
2257    fn no_penalty_is_fully_unpenalized() {
2258        assert_eq!(joint_unpenalized_dim(4, &[], &[]), 4);
2259    }
2260
2261    #[test]
2262    fn single_penalty_returns_its_own_null_space() {
2263        // A 3×3 penalty that penalizes only the last coordinate ⇒ 2-dim null
2264        // space (the first two coordinates are unpenalized).
2265        let s = array![[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 5.0]];
2266        assert_eq!(joint_unpenalized_dim(3, std::slice::from_ref(&s), &[2]), 2);
2267    }
2268
2269    #[test]
2270    fn complementary_double_penalty_has_empty_joint_null_space() {
2271        // The #1360 case in miniature: a "bending" penalty that leaves the
2272        // first coordinate (its 2-dim... here 1-dim) null, plus a
2273        // complementary "null-space ridge" that penalizes exactly that
2274        // coordinate. Per-penalty null dims are {1, 2} and sum to 3 (≈ p),
2275        // but the INTERSECTION is empty: every coordinate is penalized by
2276        // someone, so the joint unpenalized dim is 0.
2277        let bending = array![[0.0, 0.0, 0.0], [0.0, 4.0, 0.0], [0.0, 0.0, 4.0]];
2278        let ridge = array![[2.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]];
2279        assert_eq!(joint_unpenalized_dim(3, &[bending, ridge], &[1, 2]), 0);
2280    }
2281
2282    #[test]
2283    fn partial_overlap_keeps_shared_null_direction() {
2284        // Two penalties that BOTH leave coordinate 0 unpenalized ⇒ the shared
2285        // null direction survives the intersection (joint unpenalized dim 1),
2286        // even though naively summing the per-penalty dims would give 4.
2287        let a = array![[0.0, 0.0, 0.0], [0.0, 3.0, 0.0], [0.0, 0.0, 0.0]];
2288        let b = array![[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 3.0]];
2289        assert_eq!(joint_unpenalized_dim(3, &[a, b], &[2, 2]), 1);
2290    }
2291
2292    #[test]
2293    fn non_materialized_penalty_falls_back_conservatively() {
2294        // A penalty whose stored block is not p_local × p_local (e.g. a
2295        // Kronecker tensor factor). With ≥2 penalties the conservative joint
2296        // dim is 0 (never over-rejecting).
2297        let full: Array2<f64> = array![[0.0, 0.0], [0.0, 1.0]];
2298        let factor: Array2<f64> = array![[1.0]]; // wrong shape for p_local=2
2299        assert_eq!(
2300            joint_unpenalized_dim(2, &[full, factor.clone()], &[1, 0]),
2301            0
2302        );
2303        // With a single non-materialized penalty, fall back to its own null dim.
2304        assert_eq!(joint_unpenalized_dim(4, std::slice::from_ref(&factor), &[2]), 2);
2305    }
2306}
2307
2308#[cfg(test)]
2309mod kronecker_penalty_system_tests {
2310    use super::KroneckerPenaltySystem;
2311    use ndarray::array;
2312
2313    #[test]
2314    fn double_penalty_rank_derivatives_use_only_joint_null_space() {
2315        let penalties = vec![
2316            array![[0.0, 0.0], [0.0, 2.0]],
2317            array![[0.0, 0.0], [0.0, 3.0]],
2318        ];
2319        let system = KroneckerPenaltySystem::new(penalties, vec![2usize, 2usize], true).unwrap();
2320        let lambdas = vec![5.0, 7.0, 11.0];
2321
2322        let (logdet, rank, grad, hess) = system.logdet_rank_and_derivatives(&lambdas, 0.0);
2323
2324        let expected_diag = [11.0_f64, 21.0, 10.0, 31.0];
2325        let expected_logdet: f64 = expected_diag.iter().map(|v| v.ln()).sum();
2326        assert_eq!(rank, 4);
2327        assert!((logdet - expected_logdet).abs() <= 1e-12);
2328        assert!(
2329            (grad[2] - 1.0).abs() <= 1e-12,
2330            "double-penalty rank derivative must count only the joint null mode, got {}",
2331            grad[2]
2332        );
2333        assert!(hess[[2, 2]].abs() <= 1e-12);
2334    }
2335}
2336
2337#[derive(Clone, Debug)]
2338pub struct TermCollectionDesign {
2339    /// The full design matrix.
2340    ///
2341    /// Prefer a true sparse matrix when every block is sparse-compatible.
2342    /// If the collection already contains intrinsically sparse blocks, preserve
2343    /// that storage and let PIRLS decide later whether the penalized system is
2344    /// sparse-native eligible. Purely dense materialized blocks still fall back
2345    /// to the lazy block operator when sparse storage would just re-encode a
2346    /// dense matrix.
2347    pub design: DesignMatrix,
2348    pub penalties: Vec<BlockwisePenalty>,
2349    pub nullspace_dims: Vec<usize>,
2350    pub penaltyinfo: Vec<PenaltyBlockInfo>,
2351    pub dropped_penaltyinfo: Vec<DroppedPenaltyBlockInfo>,
2352    /// Optional global coefficient lower bounds for constrained fitting.
2353    /// Length equals `design.ncols()` when present. Unconstrained entries are `-inf`.
2354    pub coefficient_lower_bounds: Option<Array1<f64>>,
2355    /// Optional global inequality constraints:
2356    /// `A * beta >= b`.
2357    pub linear_constraints: Option<LinearInequalityConstraints>,
2358    pub intercept_range: Range<usize>,
2359    pub linear_ranges: Vec<(String, Range<usize>)>,
2360    pub random_effect_ranges: Vec<(String, Range<usize>)>,
2361    pub random_effect_levels: Vec<(String, Vec<u64>)>,
2362    pub smooth: SmoothDesign,
2363}
2364
2365impl TermCollectionDesign {
2366    /// Number of global penalty blocks that precede the smooth-term penalty
2367    /// blocks in the flat smoothing-parameter / EDF-trace layout.
2368    ///
2369    /// The prefix is not the same as `random_effect_ranges.len()`: unpenalized
2370    /// (or empty) random-effect ranges contribute coefficient columns but no
2371    /// penalty block. The authoritative layout is the recorded `penaltyinfo`
2372    /// sequence assembled alongside `penalties`.
2373    pub fn leading_penalty_blocks_before_smooth(&self) -> usize {
2374        self.penaltyinfo
2375            .iter()
2376            .take_while(|info| {
2377                matches!(
2378                    &info.penalty.source,
2379                    crate::basis::PenaltySource::Other(source)
2380                        if source == "LinearTermRidge"
2381                            || source.starts_with("RandomEffectRidge(")
2382                )
2383            })
2384            .count()
2385    }
2386
2387    /// Convert blockwise penalties to `PenaltyMatrix::Blockwise` without
2388    /// expanding to `p_total × p_total`. This is the preferred path for
2389    /// family modules that accept `Vec<PenaltyMatrix>`.
2390    pub fn penalties_as_penalty_matrix(&self) -> Vec<gam_problem::PenaltyMatrix> {
2391        let p = self.design.ncols();
2392        self.penalties
2393            .iter()
2394            .map(|bp| bp.to_penalty_matrix(p))
2395            .collect()
2396    }
2397
2398    /// Number of penalty blocks.
2399    #[inline]
2400    pub fn num_penalties(&self) -> usize {
2401        self.penalties.len()
2402    }
2403
2404    /// Resolve coefficient groups against this design's global coefficient
2405    /// layout and append their penalties after the existing term penalties.
2406    pub fn realize_coefficient_groups(
2407        &self,
2408        groups: &[CoefficientGroupSpec],
2409        base_prior: &gam_spec::RhoPrior,
2410    ) -> Result<RealizedCoefficientGroups, BasisError> {
2411        realize_coefficient_groups(self, groups, base_prior)
2412    }
2413
2414    /// Extract a `KroneckerPenaltySystem` when the model's *only* smooth term is
2415    /// a single Kronecker-factored tensor.
2416    ///
2417    /// This is a deliberate single-tensor fast path, not a partial feature: any
2418    /// other shape — zero Kronecker terms, several of them, or a tensor mixed
2419    /// with non-tensor smooth terms — is served correctly by the standard
2420    /// block-separable assembly, so this returns `None` and the caller falls
2421    /// back to it. The two former conditions (`len != 1` and "a non-Kronecker
2422    /// smooth term exists") are jointly equivalent to "the sole smooth term is
2423    /// Kronecker", which the slice pattern below expresses directly in one pass.
2424    pub fn kronecker_penalty_system(&self) -> Option<KroneckerPenaltySystem> {
2425        let [only_term] = self.smooth.terms.as_slice() else {
2426            return None;
2427        };
2428        let kron = only_term.kronecker_factored.as_ref()?;
2429        // A genuine tensor product needs at least two margins, and the marginal
2430        // design / penalty / dim collections must agree in length. A degenerate
2431        // (single-margin) or internally inconsistent factored basis cannot feed
2432        // the Kronecker fast path, so fall back to the standard assembly rather
2433        // than construct a malformed `KroneckerPenaltySystem` from it.
2434        if kron.marginal_dims.len() < 2
2435            || kron.marginal_penalties.len() != kron.marginal_dims.len()
2436            || kron.marginal_designs.len() != kron.marginal_dims.len()
2437        {
2438            return None;
2439        }
2440        KroneckerPenaltySystem::new(
2441            kron.marginal_penalties.clone(),
2442            kron.marginal_dims.clone(),
2443            kron.has_double_penalty,
2444        )
2445        .ok()
2446    }
2447}
2448
2449// `FittedTermCollection`, `SpatialLengthScaleOptimizationTiming`, and
2450// `FittedTermCollectionWithSpec` were relocated with the GAM fit-orchestration
2451// drivers to `gam-models` (`crate::fit_orchestration::drivers`) — they hold a
2452// `gam_solve::UnifiedFitResult` and are consumed only by those drivers (#1521).
2453
2454#[derive(Clone)]
2455pub struct StandardLatentCoordConfig {
2456    pub values: std::sync::Arc<crate::latent::LatentCoordValues>,
2457    pub term_index: gam_problem::types::SmoothTermIdx,
2458    pub feature_cols: Vec<usize>,
2459    pub manifold: crate::latent::LatentManifold,
2460    pub manifold_auto: bool,
2461    pub retraction_registry: gam_problem::LatentRetractionRegistry,
2462    pub analytic_penalties: Option<std::sync::Arc<crate::AnalyticPenaltyRegistry>>,
2463}
2464
2465#[derive(Clone, Debug, Serialize, Deserialize)]
2466pub struct AdaptiveSpatialMap {
2467    pub termname: String,
2468    pub feature_cols: Vec<usize>,
2469    pub collocation_points: Array2<f64>,
2470    pub inv_magweight: Array1<f64>,
2471    pub invgradweight: Array1<f64>,
2472    pub inv_lapweight: Array1<f64>,
2473}
2474
2475#[derive(Clone, Debug, Serialize, Deserialize)]
2476pub struct AdaptiveRegularizationDiagnostics {
2477    pub epsilon_0: f64,
2478    pub epsilon_g: f64,
2479    pub epsilon_c: f64,
2480    pub epsilon_outer_iterations: usize,
2481    pub mm_iterations: usize,
2482    pub converged: bool,
2483    pub maps: Vec<AdaptiveSpatialMap>,
2484}
2485
2486#[derive(Debug, Clone)]
2487pub struct LinearColumnConditioning {
2488    col_idx: usize,
2489    mean: f64,
2490    scale: f64,
2491}
2492
2493#[derive(Debug, Clone, Default)]
2494pub struct LinearFitConditioning {
2495    pub intercept_idx: usize,
2496    pub columns: Vec<LinearColumnConditioning>,
2497}
2498
2499#[derive(Clone)]
2500pub struct SpatialPsiDerivative {
2501    // These are derivatives with respect to psi = log(kappa), not log(length_scale).
2502    pub penalty_index: usize,
2503    pub penalty_indices: Vec<usize>,
2504    pub global_range: Range<usize>,
2505    pub total_p: usize,
2506    pub x_psi_local: Array2<f64>,
2507    pub s_psi_components_local: Vec<Array2<f64>>,
2508    pub x_psi_psi_local: Array2<f64>,
2509    pub s_psi_psi_components_local: Vec<Array2<f64>>,
2510    pub aniso_group_id: Option<usize>,
2511    /// Pre-computed cross-derivative design matrices for other axes
2512    /// in the same aniso group: Vec of (axis_offset_in_group, matrix).
2513    pub aniso_cross_designs: Option<Vec<(usize, Array2<f64>)>>,
2514    /// On-demand cross-penalty second derivatives ∂²S_m/∂ψ_a∂ψ_b for axes in
2515    /// the same anisotropy group. The input is the other axis offset in the
2516    /// group, and the output is one local penalty matrix per active penalty.
2517    pub aniso_cross_penalty_provider: Option<
2518        std::sync::Arc<
2519            dyn Fn(usize) -> Result<Vec<Array2<f64>>, EstimationError> + Send + Sync + 'static,
2520        >,
2521    >,
2522    /// Optional implicit design-derivative operator (shared across all axes
2523    /// in the same aniso group). When present, `x_psi_local` and
2524    /// `x_psi_psi_local` may be zero-sized, and design-derivative matvecs
2525    /// should go through this operator using `implicit_axis` as the axis index.
2526    pub implicit_operator: Option<std::sync::Arc<crate::basis::ImplicitDesignPsiDerivative>>,
2527    /// Which axis in the implicit operator this entry corresponds to.
2528    pub implicit_axis: usize,
2529}
2530
2531#[derive(Debug, Clone)]
2532pub struct SpatialLogKappaCoords {
2533    /// Flattened ψ values. For isotropic terms, one entry per term.
2534    /// For anisotropic terms, d entries per term (one ψ_a per axis).
2535    pub values: Array1<f64>,
2536    /// Dimensionality of each term: 1 for isotropic, d for anisotropic.
2537    pub dims_per_term: Vec<usize>,
2538}
2539
2540/// Which end of the ψ bound the shared `aniso_bounds_from_data` helper is
2541/// computing. The lower end uses `-max_length_scale.ln()` as the pure-Duchon
2542/// fallback and the `.0` element of `spatial_term_psi_bounds`; the upper end
2543/// uses `-min_length_scale.ln()` and `.1`. Everything else is identical.
2544#[derive(Clone, Copy)]
2545pub enum AnisoBoundEnd {
2546    Lower,
2547    Upper,
2548}
2549
2550impl SpatialLogKappaCoords {
2551    /// Construct from an explicit dims layout plus values.
2552    pub fn new_with_dims(values: Array1<f64>, dims_per_term: Vec<usize>) -> Self {
2553        assert_eq!(
2554            values.len(),
2555            dims_per_term.iter().sum::<usize>(),
2556            "SpatialLogKappaCoords: values length {} != sum of dims_per_term {}",
2557            values.len(),
2558            dims_per_term.iter().sum::<usize>(),
2559        );
2560        Self {
2561            values,
2562            dims_per_term,
2563        }
2564    }
2565
2566    /// Isotropic initialization (backward-compatible path).
2567    pub fn from_length_scales(
2568        spec: &TermCollectionSpec,
2569        term_indices: &[usize],
2570        options: &SpatialLengthScaleOptimizationOptions,
2571    ) -> Self {
2572        let mut out = Array1::<f64>::zeros(term_indices.len());
2573        for (slot, &term_idx) in term_indices.iter().enumerate() {
2574            // Constant-curvature: the single ψ slot is the raw signed κ, seeded
2575            // from the spec (default κ = 0). The −ln(length_scale) convention is
2576            // log-κ semantics and must not touch the raw-κ coordinate; the κ
2577            // window projection happens later via `clamp_to_bounds`. Mirrors the
2578            // aniso constructor's κ branch.
2579            if let Some(cc) = constant_curvature_term_spec(spec, term_idx) {
2580                out[slot] = cc.kappa;
2581                continue;
2582            }
2583            let length_scale = get_spatial_length_scale(spec, term_idx)
2584                .unwrap_or(options.min_length_scale)
2585                .clamp(options.min_length_scale, options.max_length_scale);
2586            out[slot] = -length_scale.ln();
2587        }
2588        Self {
2589            values: out,
2590            dims_per_term: vec![1; term_indices.len()],
2591        }
2592    }
2593
2594    /// Anisotropic-aware initialization.
2595    ///
2596    /// Initialization strategy (per math team recommendation): standardize the
2597    /// knot cloud axiswise, then run the existing isotropic κ initializer in
2598    /// the standardized space. This reuses the trusted isotropic initializer
2599    /// and gives initial η_a = −ln(σ_a) + mean(ln(σ_a)), which satisfies
2600    /// Ση_a = 0 by construction.
2601    ///
2602    /// For each term, checks whether it has `aniso_log_scales` set on its basis spec.
2603    /// - If isotropic (no aniso_log_scales, or 1-D): 1 entry = −ln(length_scale).
2604    /// - If anisotropic with a scalar length scale: d entries, one ψ_a per axis.
2605    ///   Initialized as ψ_a = −ln(length_scale) + η_a  where η_a are the existing
2606    ///   aniso_log_scales (which sum to zero). Multi-dimensional terms without
2607    ///   explicit anisotropy stay scalar here so the seed dimensionality matches
2608    ///   `spatial_dims_per_term`.
2609    /// - If pure Duchon anisotropic: d - 1 free entries store the leading η_a
2610    ///   values directly; the final axis is reconstructed to keep Ση_a = 0.
2611    pub fn from_length_scales_aniso(
2612        spec: &TermCollectionSpec,
2613        term_indices: &[usize],
2614        options: &SpatialLengthScaleOptimizationOptions,
2615    ) -> Self {
2616        let mut vals = Vec::new();
2617        let mut dims = Vec::new();
2618        for &term_idx in term_indices {
2619            // Measure-jet: dial coordinates seeded directly from the term's
2620            // realized (α, τ[, s]); the −ln(length_scale) convention below is
2621            // κ-semantics and never applies to dials.
2622            if let Some(mj) = measure_jet_term_spec(spec, term_idx) {
2623                let seed = measure_jet_psi_seed(mj);
2624                dims.push(seed.len());
2625                vals.extend(seed);
2626                continue;
2627            }
2628            // Constant-curvature: one signed κ slot seeded from the spec's κ
2629            // (clamped feasible). The −ln(length_scale) convention below is
2630            // log-κ semantics and must not touch the raw-κ coordinate. Bounds
2631            // are unavailable here (no data view), so this is the raw spec κ;
2632            // `reseed_from_data` / `clamp_to_bounds` later project it feasible.
2633            if let Some(cc) = constant_curvature_term_spec(spec, term_idx) {
2634                vals.push(cc.kappa);
2635                dims.push(1);
2636                continue;
2637            }
2638            let length_scale = get_spatial_length_scale(spec, term_idx)
2639                .unwrap_or(options.min_length_scale)
2640                .clamp(options.min_length_scale, options.max_length_scale);
2641            let psi_bar = -length_scale.ln(); // global scale = −ln(length_scale)
2642
2643            if spatial_term_uses_per_axis_psi(spec, term_idx) {
2644                // Per-axis anisotropy is enrolled in the joint outer vector:
2645                // ψ_a = ψ̄ + η_a, one slot per axis. The hyper_dirs builder
2646                // produces matching per-axis derivatives in
2647                // `try_build_spatial_term_log_kappa_aniso_derivativeinfos`.
2648                let d = get_spatial_feature_dim(spec, term_idx).unwrap_or(1);
2649                let eta_raw = get_spatial_aniso_log_scales(spec, term_idx)
2650                    .expect("predicate guarantees aniso_log_scales is Some");
2651                let eta = center_aniso_log_scales(&eta_raw);
2652                for &eta_a in &eta {
2653                    vals.push(psi_bar + eta_a);
2654                }
2655                dims.push(d);
2656            } else {
2657                // Isotropic enrollment — either a 1-D term, a multi-D term
2658                // without explicit anisotropy, or a basis (e.g. Duchon) whose
2659                // η is a fixed geometry parameter rather than a REML hyper
2660                // axis. Exactly one ψ̄ slot, matching the single
2661                // `SpatialPsiDerivative` produced by
2662                // `try_build_spatial_term_log_kappa_derivativeinfo`.
2663                vals.push(psi_bar);
2664                dims.push(1);
2665            }
2666        }
2667        Self {
2668            values: Array1::from_vec(vals),
2669            dims_per_term: dims,
2670        }
2671    }
2672
2673    /// Isotropic lower bounds derived from per-term data geometry.
2674    /// Each entry gets the ψ_lo bound returned by `spatial_term_psi_bounds`
2675    /// for the corresponding term, intersected with the options window.
2676    pub fn lower_bounds_from_data(
2677        data: ArrayView2<'_, f64>,
2678        spec: &TermCollectionSpec,
2679        term_indices: &[usize],
2680        options: &SpatialLengthScaleOptimizationOptions,
2681    ) -> Self {
2682        let mut values = Array1::<f64>::zeros(term_indices.len());
2683        for (slot, &term_idx) in term_indices.iter().enumerate() {
2684            values[slot] = spatial_term_psi_bounds(data, spec, term_idx, options).0;
2685        }
2686        Self {
2687            values,
2688            dims_per_term: vec![1; term_indices.len()],
2689        }
2690    }
2691
2692    /// Isotropic upper bounds derived from per-term data geometry.
2693    pub fn upper_bounds_from_data(
2694        data: ArrayView2<'_, f64>,
2695        spec: &TermCollectionSpec,
2696        term_indices: &[usize],
2697        options: &SpatialLengthScaleOptimizationOptions,
2698    ) -> Self {
2699        let mut values = Array1::<f64>::zeros(term_indices.len());
2700        for (slot, &term_idx) in term_indices.iter().enumerate() {
2701            values[slot] = spatial_term_psi_bounds(data, spec, term_idx, options).1;
2702        }
2703        Self {
2704            values,
2705            dims_per_term: vec![1; term_indices.len()],
2706        }
2707    }
2708
2709    /// Anisotropic-aware lower bounds derived from per-term data geometry.
2710    /// For hybrid anisotropic terms the scalar ψ_lo bound applies to the
2711    /// mean `ψ̄`, not directly to every raw axis coordinate `ψ_a = ψ̄ + η_a`.
2712    /// Shift each axis by the current centered `η_a` so projecting/clamping
2713    /// the seed moves only the global scale direction and does not silently
2714    /// shrink anisotropy that is already consistent with the current
2715    /// `length_scale`.
2716    ///
2717    /// Pure Duchon anisotropy is structurally different: its stored
2718    /// coordinates are (d-1) free η_a values representing log axis-scale
2719    /// ratios, NOT log-κ. For those terms the κ-range geometry bound is
2720    /// over-restrictive (η_a = ±5 is normal, but that corresponds to 7+
2721    /// orders of magnitude in κ-space and would be rejected by the data
2722    /// window). Fall back to the options window `[-ln(max_ls), -ln(min_ls)]`
2723    /// for those coordinates — that's the same bound the pre-data-geometry
2724    /// code used, which is calibrated to allow legitimate anisotropy.
2725    pub fn lower_bounds_aniso_from_data(
2726        data: ArrayView2<'_, f64>,
2727        spec: &TermCollectionSpec,
2728        term_indices: &[usize],
2729        dims_per_term: &[usize],
2730        options: &SpatialLengthScaleOptimizationOptions,
2731    ) -> Self {
2732        Self::aniso_bounds_from_data(
2733            data,
2734            spec,
2735            term_indices,
2736            dims_per_term,
2737            options,
2738            AnisoBoundEnd::Lower,
2739        )
2740    }
2741
2742    /// Anisotropic-aware upper bounds derived from per-term data geometry.
2743    /// See `lower_bounds_aniso_from_data` for the hybrid-aniso offsetting and
2744    /// pure-Duchon dispatch rationale.
2745    pub fn upper_bounds_aniso_from_data(
2746        data: ArrayView2<'_, f64>,
2747        spec: &TermCollectionSpec,
2748        term_indices: &[usize],
2749        dims_per_term: &[usize],
2750        options: &SpatialLengthScaleOptimizationOptions,
2751    ) -> Self {
2752        Self::aniso_bounds_from_data(
2753            data,
2754            spec,
2755            term_indices,
2756            dims_per_term,
2757            options,
2758            AnisoBoundEnd::Upper,
2759        )
2760    }
2761
2762    /// Shared implementation for the lower/upper aniso bounds. The bound end
2763    /// only changes which options scale (`max_length_scale` vs
2764    /// `min_length_scale`) becomes the pure-Duchon fallback bound and which
2765    /// element of the `(lo, hi)` data-geometry tuple is consumed; the
2766    /// per-term cursor walk and aniso-offset handling are identical.
2767    fn aniso_bounds_from_data(
2768        data: ArrayView2<'_, f64>,
2769        spec: &TermCollectionSpec,
2770        term_indices: &[usize],
2771        dims_per_term: &[usize],
2772        options: &SpatialLengthScaleOptimizationOptions,
2773        end: AnisoBoundEnd,
2774    ) -> Self {
2775        assert_eq!(term_indices.len(), dims_per_term.len());
2776        let total: usize = dims_per_term.iter().sum();
2777        let mut values = Array1::<f64>::zeros(total);
2778        let mut cursor = 0;
2779        for (slot, &term_idx) in term_indices.iter().enumerate() {
2780            let d = dims_per_term[slot];
2781            // Measure-jet: per-coordinate dial boxes, never κ-window geometry
2782            // (which would reject legitimate dial values outright).
2783            if let Some(mj) = measure_jet_term_spec(spec, term_idx) {
2784                let bounds = measure_jet_psi_bound_values(mj, matches!(end, AnisoBoundEnd::Upper));
2785                for (offset, bound) in bounds.into_iter().enumerate() {
2786                    if offset < d {
2787                        values[cursor + offset] = bound;
2788                    }
2789                }
2790                cursor += d;
2791                continue;
2792            }
2793            // Constant-curvature: the single signed-κ box from the data chart
2794            // window (symmetric about κ = 0), never a κ = log-scale window.
2795            if constant_curvature_term_spec(spec, term_idx).is_some() {
2796                let (lo, hi) = constant_curvature_kappa_bounds(data, spec, term_idx);
2797                if d >= 1 {
2798                    values[cursor] = match end {
2799                        AnisoBoundEnd::Lower => lo,
2800                        AnisoBoundEnd::Upper => hi,
2801                    };
2802                }
2803                cursor += d;
2804                continue;
2805            }
2806            let psi_bound = {
2807                let (lo, hi) = spatial_term_psi_bounds(data, spec, term_idx, options);
2808                match end {
2809                    AnisoBoundEnd::Lower => lo,
2810                    AnisoBoundEnd::Upper => hi,
2811                }
2812            };
2813            let axis_offsets = if d <= 1 {
2814                vec![0.0; d]
2815            } else {
2816                get_spatial_aniso_log_scales(spec, term_idx)
2817                    .filter(|eta| eta.len() == d)
2818                    .map(|eta| center_aniso_log_scales(&eta))
2819                    .unwrap_or_else(|| vec![0.0; d])
2820            };
2821            for offset in 0..d {
2822                values[cursor + offset] = psi_bound + axis_offsets[offset];
2823            }
2824            cursor += d;
2825        }
2826        Self {
2827            values,
2828            dims_per_term: dims_per_term.to_vec(),
2829        }
2830    }
2831
2832    /// Rewrite any ψ entries whose originating term lacks an explicit
2833    /// `length_scale` so they sit at the midpoint of the per-term data-derived
2834    /// ψ window. Used so the outer optimizer starts inside the physically
2835    /// meaningful region instead of at an arbitrary `options.max_length_scale`
2836    /// derived seed. For terms with an explicit length_scale, the user's
2837    /// choice is respected. Anisotropy offsets η_a (those stored by
2838    /// `from_length_scales_aniso`) are preserved: we re-center around the new
2839    /// ψ̄, keeping Ση_a = 0.
2840    pub fn reseed_from_data(
2841        mut self,
2842        data: ArrayView2<'_, f64>,
2843        spec: &TermCollectionSpec,
2844        term_indices: &[usize],
2845        options: &SpatialLengthScaleOptimizationOptions,
2846    ) -> Self {
2847        assert_eq!(term_indices.len(), self.dims_per_term.len());
2848        let mut cursor = 0;
2849        for (slot, &term_idx) in term_indices.iter().enumerate() {
2850            let d = self.dims_per_term[slot];
2851            // Measure-jet dials are seeded from the realized spec and must
2852            // not be recentered into a κ data window.
2853            if measure_jet_term_spec(spec, term_idx).is_some() {
2854                cursor += d;
2855                continue;
2856            }
2857            // Constant-curvature κ is seeded from the spec (the user's curvature
2858            // hint, default κ = 0); `clamp_to_bounds` projects it feasible. It
2859            // is not a log-scale, so the log-κ recenter below never applies.
2860            if constant_curvature_term_spec(spec, term_idx).is_some() {
2861                cursor += d;
2862                continue;
2863            }
2864            let Some(psi_bar_new) = spatial_term_psi_seed(data, spec, term_idx, options) else {
2865                cursor += d;
2866                continue;
2867            };
2868            if d == 0 {
2869                continue;
2870            }
2871            let current: Vec<f64> = self.values.slice(s![cursor..cursor + d]).to_vec();
2872            let psi_bar_old = current.iter().sum::<f64>() / d as f64;
2873            for (offset, &old_value) in current.iter().enumerate() {
2874                self.values[cursor + offset] = psi_bar_new + (old_value - psi_bar_old);
2875            }
2876            cursor += d;
2877        }
2878        self
2879    }
2880
2881    /// Project ψ values into `[lower, upper]` element-wise. Used after
2882    /// `from_length_scales*` + `reseed_from_data` when a user-supplied
2883    /// `spec.length_scale` falls outside the data-derived ψ window set by
2884    /// `{lower,upper}_bounds*_from_data`. BFGS requires theta0 ∈ [lower,
2885    /// upper]; projecting is the unique closest feasible seed. The user's
2886    /// length_scale was always a hint for the outer optimizer (the optimizer
2887    /// is authoritative for κ), not a hard constraint — so clipping preserves
2888    /// their intent as far as the geometry allows. Emits `log::info!` when
2889    /// any coordinate moves, so the outside-window case is diagnostically
2890    /// visible (not silent).
2891    pub fn clamp_to_bounds(
2892        mut self,
2893        lower: &SpatialLogKappaCoords,
2894        upper: &SpatialLogKappaCoords,
2895    ) -> Self {
2896        assert_eq!(self.values.len(), lower.values.len());
2897        assert_eq!(self.values.len(), upper.values.len());
2898        let mut n_projected = 0usize;
2899        let mut worst_delta = 0.0_f64;
2900        for idx in 0..self.values.len() {
2901            let lo = lower.values[idx];
2902            let hi = upper.values[idx];
2903            if !(lo.is_finite() && hi.is_finite()) {
2904                continue;
2905            }
2906            let v = self.values[idx];
2907            if v < lo {
2908                worst_delta = worst_delta.max(lo - v);
2909                self.values[idx] = lo;
2910                n_projected += 1;
2911            } else if v > hi {
2912                worst_delta = worst_delta.max(v - hi);
2913                self.values[idx] = hi;
2914                n_projected += 1;
2915            }
2916        }
2917        if n_projected > 0 {
2918            log::info!(
2919                "[spatial-kappa] projected {n_projected}/{} ψ seed coords into data-derived bounds \
2920                 (worst excess={worst_delta:.3} log units); user length_scale falls outside \
2921                 [{KERNEL_RANGE_MIN_DIAMETER_FRACTION}/r_max, {KERNEL_RANGE_MAX_SPACING_MULTIPLE}/r_min] geometry window",
2922                self.values.len()
2923            );
2924        }
2925        self
2926    }
2927
2928    /// Reconstruct from theta tail with known dimensionality layout.
2929    pub fn from_theta_tail_with_dims(
2930        theta: &Array1<f64>,
2931        start: usize,
2932        dims_per_term: Vec<usize>,
2933    ) -> Self {
2934        let total: usize = dims_per_term.iter().sum();
2935        Self {
2936            values: theta.slice(s![start..start + total]).to_owned(),
2937            dims_per_term,
2938        }
2939    }
2940
2941    /// Total number of ψ values in the flat array (= sum of dims_per_term).
2942    pub fn len(&self) -> usize {
2943        self.values.len()
2944    }
2945
2946    /// Dimensionality layout: how many ψ values each term contributes.
2947    pub fn dims_per_term(&self) -> &[usize] {
2948        &self.dims_per_term
2949    }
2950
2951    /// Get the offset into the flat array for logical term i.
2952    fn term_offset(&self, term_idx: usize) -> usize {
2953        self.dims_per_term[..term_idx].iter().sum()
2954    }
2955
2956    /// Get the slice of ψ values for logical term i.
2957    pub fn term_slice(&self, term_idx: usize) -> &[f64] {
2958        let offset = self.term_offset(term_idx);
2959        let d = self.dims_per_term[term_idx];
2960        &self.values.as_slice().unwrap()[offset..offset + d]
2961    }
2962
2963    pub fn as_array(&self) -> &Array1<f64> {
2964        &self.values
2965    }
2966
2967    /// #1464: overwrite the single ψ value of a scalar (1-D) logical term by its
2968    /// position `slot` in this coords vector (the same ordering as the
2969    /// `term_indices` slice the constructors were built from). Used to inject the
2970    /// fixed-κ sign-basin seed into a constant-curvature term's raw-κ slot before
2971    /// the joint solve. No-op (returns `false`) when the slot is not scalar.
2972    pub fn set_scalar_slot(&mut self, slot: usize, value: f64) -> bool {
2973        if slot >= self.dims_per_term.len() || self.dims_per_term[slot] != 1 {
2974            return false;
2975        }
2976        let offset = self.term_offset(slot);
2977        self.values[offset] = value;
2978        true
2979    }
2980
2981    /// Split at a logical-term boundary. `mid` is the number of terms in the
2982    /// first half (not a flat-array index).
2983    pub fn split_at(&self, mid: usize) -> (Self, Self) {
2984        let flat_mid: usize = self.dims_per_term[..mid].iter().sum();
2985        (
2986            Self {
2987                values: self.values.slice(s![0..flat_mid]).to_owned(),
2988                dims_per_term: self.dims_per_term[..mid].to_vec(),
2989            },
2990            Self {
2991                values: self.values.slice(s![flat_mid..]).to_owned(),
2992                dims_per_term: self.dims_per_term[mid..].to_vec(),
2993            },
2994        )
2995    }
2996
2997    /// Apply optimized ψ values back to the spec.
2998    ///
2999    /// For isotropic terms (dims=1): sets scalar length_scale = exp(−ψ).
3000    /// For anisotropic terms (dims=d): hybrid/isotropic families set
3001    /// length_scale = exp(−ψ̄) with centered η_a = ψ_a − ψ̄, while pure Duchon
3002    /// writes only centered η_a and leaves length_scale = None.
3003    pub fn apply_tospec(
3004        &self,
3005        spec: &TermCollectionSpec,
3006        term_indices: &[usize],
3007    ) -> Result<TermCollectionSpec, EstimationError> {
3008        if term_indices.len() != self.dims_per_term.len() {
3009            crate::bail_invalid_estim!(
3010                "SpatialLogKappaCoords::apply_tospec: term count mismatch: \
3011                 term_indices={} dims_per_term={}",
3012                term_indices.len(),
3013                self.dims_per_term.len()
3014            );
3015        }
3016        let mut updated = spec.clone();
3017        for (slot, &term_idx) in term_indices.iter().enumerate() {
3018            let psi = self.term_slice(slot);
3019            let d = self.dims_per_term[slot];
3020            // Measure-jet: write the dial coordinates straight back; the
3021            // κ-translation below would misread them as log-scales.
3022            if measure_jet_term_spec(&updated, term_idx).is_some() {
3023                set_measure_jet_psi_dials(&mut updated, term_idx, psi)?;
3024                continue;
3025            }
3026            // Constant-curvature: write the optimized signed κ straight back;
3027            // the −exp(ψ) length-scale translation below is log-κ semantics and
3028            // would misread the raw curvature.
3029            if constant_curvature_term_spec(&updated, term_idx).is_some() {
3030                set_constant_curvature_kappa(&mut updated, term_idx, psi)?;
3031                continue;
3032            }
3033            let (next_length_scale, next_aniso) = spatial_term_psi_to_length_scale_and_aniso(psi);
3034            if (d == 1 || next_length_scale.is_some())
3035                && let Some(length_scale) = next_length_scale
3036            {
3037                set_spatial_length_scale(&mut updated, term_idx, length_scale)?;
3038            }
3039            if let Some(eta) = next_aniso {
3040                set_spatial_aniso_log_scales(&mut updated, term_idx, eta)?;
3041            }
3042        }
3043        Ok(updated)
3044    }
3045}
3046
3047pub fn center_aniso_log_scales(eta: &[f64]) -> Vec<f64> {
3048    if eta.len() <= 1 {
3049        return eta.to_vec();
3050    }
3051    let mean = eta.iter().sum::<f64>() / eta.len() as f64;
3052    eta.iter()
3053        .map(|&v| {
3054            let centered = v - mean;
3055            if centered.abs() <= 1e-15 {
3056                0.0
3057            } else {
3058                centered
3059            }
3060        })
3061        .collect()
3062}
3063
3064/// Whether a spatial term contributes per-axis ψ entries to the outer joint
3065/// hyperparameter vector.
3066pub fn spatial_term_uses_per_axis_psi(resolvedspec: &TermCollectionSpec, term_idx: usize) -> bool {
3067    if let Some(mj) = measure_jet_term_spec(resolvedspec, term_idx) {
3068        return measure_jet_enrolls_psi(mj);
3069    }
3070    let Some(d) = get_spatial_feature_dim(resolvedspec, term_idx) else {
3071        return false;
3072    };
3073    if d <= 1 {
3074        return false;
3075    }
3076    let Some(eta) = get_spatial_aniso_log_scales(resolvedspec, term_idx) else {
3077        return false;
3078    };
3079    if eta.len() != d {
3080        return false;
3081    }
3082    !matches!(
3083        resolvedspec.smooth_terms.get(term_idx).map(|term| &term.basis),
3084        Some(SmoothBasisSpec::Duchon { .. })
3085    )
3086}
3087
3088pub fn set_spatial_length_scale(
3089    spec: &mut TermCollectionSpec,
3090    term_idx: usize,
3091    length_scale: f64,
3092) -> Result<(), EstimationError> {
3093    let Some(term) = spec.smooth_terms.get_mut(term_idx) else {
3094        crate::bail_invalid_estim!("spatial length-scale term index {term_idx} out of range");
3095    };
3096    match &mut term.basis {
3097        SmoothBasisSpec::ThinPlate { spec, .. } => {
3098            spec.length_scale = length_scale;
3099            Ok(())
3100        }
3101        SmoothBasisSpec::Matern { spec, .. } => {
3102            spec.length_scale = length_scale;
3103            Ok(())
3104        }
3105        SmoothBasisSpec::Duchon { spec, .. } => {
3106            spec.length_scale = Some(length_scale);
3107            Ok(())
3108        }
3109        _ => Err(EstimationError::InvalidInput(format!(
3110            "term '{}' does not expose a spatial length scale",
3111            term.name
3112        ))),
3113    }
3114}
3115
3116pub fn get_spatial_length_scale(spec: &TermCollectionSpec, term_idx: usize) -> Option<f64> {
3117    spec.smooth_terms
3118        .get(term_idx)
3119        .and_then(|term| match &term.basis {
3120            SmoothBasisSpec::ThinPlate { spec, .. } => Some(spec.length_scale),
3121            SmoothBasisSpec::Matern { spec, .. } => Some(spec.length_scale),
3122            SmoothBasisSpec::Duchon { spec, .. } => spec.length_scale,
3123            _ => None,
3124        })
3125}
3126
3127pub fn spatial_term_supports_hyper_optimization(spec: &TermCollectionSpec, term_idx: usize) -> bool {
3128    // Ordinary penalized thin-plate regression splines do not have an
3129    // identifiable kernel scale once REML is already learning the smoothing
3130    // penalty. Treat the resolved length scale as fixed geometry; enrolling a
3131    // scalar TPS kappa axis creates the flat ρ/κ valleys reported in #718,
3132    // #721, #731, and #732.
3133    if let Some(term) = spec.smooth_terms.get(term_idx)
3134        && let SmoothBasisSpec::ThinPlate { .. } = &term.basis
3135    {
3136        return false;
3137    }
3138
3139    // Duchon anisotropy η is a FIXED, geometry-derived basis parameter, NOT a
3140    // REML hyper axis: the metric is estimated once from the knot-cloud spread
3141    // (`auto_seed_aniso_contrasts`, applied on every Duchon basis build) and the
3142    // Hilbert-scale λ's carry all learned smoothness. So a pure Duchon (no κ)
3143    // contributes no outer optimization axis even when `scale_dims` is on —
3144    // "standardize the geometry, then learn the smoothness." Only an explicit
3145    // kernel length scale κ (the Matérn / hybrid path) is optimized here.
3146    //
3147    // ISOTROPIC Matérn: the *default* `matern(x1, x2)` is isotropic
3148    // (`scale_dims=false` → `aniso_log_scales = None`). It contributes exactly
3149    // ONE κ optimization axis — its scalar log-κ. The shared GAMLSS /
3150    // location-scale exact-joint ψ engine and the spatial-κ joint outer solver
3151    // both require an isotropic Matérn block to expose this single isotropic κ
3152    // axis (#822/#851); without it the per-block ψ-derivative lists are empty
3153    // and the joint-ψ hooks degenerate to `None`. The isotropic κ is the lone
3154    // kernel hyper axis here, mirroring the per-axis ψ ARD that the anisotropic
3155    // path exposes (just collapsed to one dimension).
3156    //
3157    // ANISOTROPIC Matérn (`scale_dims=true` → `aniso_log_scales = Some`) keeps
3158    // its per-axis kernel-η ARD: the d-dimensional ψ search is the *point* of
3159    // the anisotropic request ("Matérn keeps its kernel-η ARD").
3160    //
3161    // Either way a Matérn term always enrolls a κ/ψ axis (1 isotropic, or d
3162    // anisotropic), so `spatial_dims_per_term` reports the correct count.
3163    if let Some(term) = spec.smooth_terms.get(term_idx)
3164        && let SmoothBasisSpec::Matern { .. } = &term.basis
3165    {
3166        return true;
3167    }
3168
3169    // Measure-jet geometry dials are outer ψ coordinates; enrollment is
3170    // owned by `measure_jet_enrolls_psi`.
3171    if let Some(mj) = measure_jet_term_spec(spec, term_idx) {
3172        return measure_jet_enrolls_psi(mj);
3173    }
3174
3175    // Constant-curvature smooths always enroll their single signed curvature κ
3176    // as an outer ψ-coordinate (#944 stage 3): κ̂ is the headline estimand, so
3177    // unlike a fixed-ℓ kernel it is fitted by default, not gated on a
3178    // user-supplied scale. The coordinate is raw κ (interior κ = 0), and its
3179    // exact design/penalty κ-derivatives come from
3180    // `build_constant_curvature_basis_kappa_derivatives`.
3181    if constant_curvature_term_spec(spec, term_idx).is_some() {
3182        return true;
3183    }
3184
3185    get_spatial_length_scale(spec, term_idx).is_some()
3186}
3187
3188/// The measure-jet term's spec, when `term_idx` is a measure-jet smooth.
3189/// Single accessor for every dial-plumbing dispatch below.
3190pub fn measure_jet_term_spec(
3191    spec: &TermCollectionSpec,
3192    term_idx: usize,
3193) -> Option<&crate::basis::MeasureJetBasisSpec> {
3194    spec.smooth_terms
3195        .get(term_idx)
3196        .and_then(|term| match &term.basis {
3197            SmoothBasisSpec::MeasureJet { spec, .. } => Some(spec),
3198            _ => None,
3199        })
3200}
3201
3202/// Single source for measure-jet outer-ψ enrollment: the lnτ dial is
3203/// undefined in the τ = 0 pseudo-inverse oracle mode (see
3204/// `build_measure_jet_basis_psi_derivatives`), so only a positive ridge
3205/// enrolls the dial group. `spatial_term_supports_hyper_optimization` and
3206/// `spatial_term_uses_per_axis_psi` both defer here so the θ-layout
3207/// sources cannot disagree.
3208pub fn measure_jet_enrolls_psi(mj: &crate::basis::MeasureJetBasisSpec) -> bool {
3209    // Two independent enrollment sources (#1116), both explicit:
3210    //   * the design-moving representer length-scale ℓ (`learn_length_scale`),
3211    //     available in every mode when the spec opts in;
3212    //   * the multiscale penalty dials (s, α, lnτ): the per-scale spectral
3213    //     split's (α, lnτ) ride the explicit `multiscale` opt-in, and the lnτ
3214    //     channel additionally needs a positive ridge (τ = 0 is the
3215    //     pseudo-inverse oracle mode where lnτ is undefined).
3216    // A term enrolls if EITHER source is active.
3217    measure_jet_learns_length_scale(mj)
3218        || (mj.tau0 > 0.0 && crate::basis::measure_jet_multiscale_mode(mj))
3219}
3220
3221/// Whether the design-moving ℓ dial is enrolled for this term. ℓ is fixed by
3222/// default and learnable in every mode only when `learn_length_scale = true`.
3223pub fn measure_jet_learns_length_scale(mj: &crate::basis::MeasureJetBasisSpec) -> bool {
3224    mj.learn_length_scale
3225}
3226
3227pub fn freeze_measure_jet_length_scale_learning(spec: &mut TermCollectionSpec) -> usize {
3228    let mut frozen = 0;
3229    for term in spec.smooth_terms.iter_mut() {
3230        if let SmoothBasisSpec::MeasureJet { spec: mj, .. } = &mut term.basis
3231            && mj.learn_length_scale
3232        {
3233            mj.learn_length_scale = false;
3234            frozen += 1;
3235        }
3236    }
3237    frozen
3238}
3239
3240/// Measure-jet ψ dial boxes. The dials are NOT log-kernel-scales, so the
3241/// κ-window machinery never applies: `α` spans density-weighted (0) through
3242/// past-Coifman–Lafon (>1) normalization, and `lnτ` covers the ridge from
3243/// numerically-exact-projection to heavy noise-floor damping. (The energy
3244/// order `s` is the pinned explicit value or absorbed by the REML-learned
3245/// per-scale amplitudes — see `measure_jet_penalty_psi_dim` — so it carries no
3246/// dial box.)
3247pub const MEASURE_JET_PSI_ALPHA_BOUNDS: (f64, f64) = (-1.0, 3.0);
3248
3249pub const MEASURE_JET_PSI_LN_TAU_BOUNDS: (f64, f64) = (-18.420680743952367, 4.605170185988092);
3250
3251/// Log-ℓ box for the design-moving representer length-scale dial (#1116). An
3252/// ABSOLUTE window in the data coordinate scale (ln of ℓ ∈ [1e-3, 1e2]) used
3253/// only when the spec explicitly enrolls the learned representer range. Absolute
3254/// (not seed-relative) so the bound producer needs no data view, matching the
3255/// other dial boxes. `ln(1e-3) = -6.9077…`, `ln(1e2) = 4.6051…`.
3256pub const MEASURE_JET_PSI_LN_LENGTH_SCALE_BOUNDS: (f64, f64) = (-6.907755278982137, 4.605170185988092);
3257
3258/// Number of multiscale PENALTY dials (excluding the design-moving ℓ):
3259/// multiscale (per-scale spectral) mode carries (α, lnτ) = 2 — the order is
3260/// either the pinned explicit `s` or absorbed by the REML-learned per-scale
3261/// amplitudes, so it is NOT a dial; single-scale (the default) carries none.
3262/// MUST agree with the penalty-coordinate layout of
3263/// `build_measure_jet_basis_psi_derivatives` (its `per_level` branch always
3264/// emits exactly the (α, lnτ) coordinate pair).
3265pub fn measure_jet_penalty_psi_dim(mj: &crate::basis::MeasureJetBasisSpec) -> usize {
3266    if crate::basis::measure_jet_multiscale_mode(mj) {
3267        2
3268    } else {
3269        0
3270    }
3271}
3272
3273/// ψ dimension of a measure-jet term. The design-moving ℓ dial (when enrolled)
3274/// is coordinate 0; the multiscale penalty dials follow. MUST agree with the
3275/// coordinate layout of `build_measure_jet_basis_psi_derivatives` (ℓ first).
3276pub fn measure_jet_psi_dim(mj: &crate::basis::MeasureJetBasisSpec) -> usize {
3277    usize::from(measure_jet_learns_length_scale(mj)) + measure_jet_penalty_psi_dim(mj)
3278}
3279
3280/// Seed ψ from the term's realized dials, in producer coordinate order: ℓ first
3281/// (when enrolled), then the multiscale penalty dials. The ℓ seed is the
3282/// realized representer range `ln(length_scale)` (the resolved spec carries the
3283/// concrete auto value after the design build/freeze).
3284pub fn measure_jet_psi_seed(mj: &crate::basis::MeasureJetBasisSpec) -> Vec<f64> {
3285    let mut seed = Vec::with_capacity(measure_jet_psi_dim(mj));
3286    if measure_jet_learns_length_scale(mj) {
3287        // length_scale > 0 after resolution; the 0.0 sentinel (pre-resolution)
3288        // falls back to the centre of the log-ℓ box so the optimizer still
3289        // starts feasible and the first data-aware reseed corrects it.
3290        let ell = if mj.length_scale > 0.0 {
3291            mj.length_scale
3292        } else {
3293            1.0
3294        };
3295        seed.push(ell.ln());
3296    }
3297    if measure_jet_penalty_psi_dim(mj) > 0 {
3298        // Multiscale penalty dials, producer order: (α, lnτ).
3299        let ln_tau = mj.tau0.max(f64::MIN_POSITIVE).ln();
3300        seed.extend_from_slice(&[mj.alpha, ln_tau]);
3301    }
3302    seed
3303}
3304
3305/// One end of the per-coordinate dial boxes, in producer coordinate order
3306/// (ℓ first when enrolled, then the multiscale penalty dials).
3307pub fn measure_jet_psi_bound_values(mj: &crate::basis::MeasureJetBasisSpec, upper: bool) -> Vec<f64> {
3308    let pick = |b: (f64, f64)| if upper { b.1 } else { b.0 };
3309    let mut bounds = Vec::with_capacity(measure_jet_psi_dim(mj));
3310    if measure_jet_learns_length_scale(mj) {
3311        bounds.push(pick(MEASURE_JET_PSI_LN_LENGTH_SCALE_BOUNDS));
3312    }
3313    if measure_jet_penalty_psi_dim(mj) > 0 {
3314        // Multiscale penalty dials, producer order: (α, lnτ).
3315        bounds.push(pick(MEASURE_JET_PSI_ALPHA_BOUNDS));
3316        bounds.push(pick(MEASURE_JET_PSI_LN_TAU_BOUNDS));
3317    }
3318    bounds
3319}
3320
3321/// Write optimized ψ dials back into a measure-jet spec. Returns `true` when
3322/// any dial actually moved. The geometry (centers, masses, band, ℓ, z) is
3323/// ψ-FIXED by contract — only the dials change, so frozen-quadrature
3324/// rebuilds reproduce the identical penalty layout at the new dials.
3325pub fn apply_measure_jet_psi(
3326    mj: &mut crate::basis::MeasureJetBasisSpec,
3327    psi: &[f64],
3328) -> Result<bool, EstimationError> {
3329    if psi.len() != measure_jet_psi_dim(mj) {
3330        crate::bail_invalid_estim!(
3331            "measure-jet ψ write-back dimension mismatch: got {} values for a {}-dial term",
3332            psi.len(),
3333            measure_jet_psi_dim(mj)
3334        );
3335    }
3336    let mut changed = false;
3337    // Coordinate 0 (when enrolled) is the design-moving ln(ℓ); the multiscale
3338    // penalty dials follow. Same order as `measure_jet_psi_seed` and the
3339    // producer (`build_measure_jet_basis_psi_derivatives`).
3340    let mut cursor = 0usize;
3341    if measure_jet_learns_length_scale(mj) {
3342        let next_ell = psi[cursor].exp();
3343        cursor += 1;
3344        if !(next_ell.is_finite() && next_ell > 0.0) {
3345            crate::bail_invalid_estim!(
3346                "measure-jet ψ write-back produced a non-finite/non-positive length_scale (ℓ={next_ell})"
3347            );
3348        }
3349        if next_ell != mj.length_scale {
3350            mj.length_scale = next_ell;
3351            changed = true;
3352        }
3353    }
3354    if measure_jet_penalty_psi_dim(mj) > 0 {
3355        // Multiscale penalty dials, producer order: (α, lnτ). The order `s` is
3356        // not a dial (pinned explicit or absorbed by the per-scale amplitudes).
3357        let next_alpha = psi[cursor];
3358        let next_tau = psi[cursor + 1].exp();
3359        if !(next_alpha.is_finite() && next_tau.is_finite() && next_tau > 0.0) {
3360            crate::bail_invalid_estim!(
3361                "measure-jet ψ write-back produced non-finite dials (alpha={next_alpha}, tau={next_tau})"
3362            );
3363        }
3364        if next_alpha != mj.alpha {
3365            mj.alpha = next_alpha;
3366            changed = true;
3367        }
3368        if next_tau != mj.tau0 {
3369            mj.tau0 = next_tau;
3370            changed = true;
3371        }
3372    }
3373    Ok(changed)
3374}
3375
3376/// Collection-level measure-jet dial write-back (the `apply_tospec` /
3377/// realizer-side entry). Returns whether anything moved.
3378pub fn set_measure_jet_psi_dials(
3379    spec: &mut TermCollectionSpec,
3380    term_idx: usize,
3381    psi: &[f64],
3382) -> Result<bool, EstimationError> {
3383    let Some(term) = spec.smooth_terms.get_mut(term_idx) else {
3384        crate::bail_invalid_estim!("measure-jet ψ write-back: term index {term_idx} out of range");
3385    };
3386    set_single_term_measure_jet_psi_dials(term, psi)
3387}
3388
3389/// Single-term dial write-back: the shared match+apply core, also used
3390/// directly on the cached per-trial build spec (whose caller has already
3391/// change-checked at the collection level and rebuilds regardless of the
3392/// moved flag).
3393pub fn set_single_term_measure_jet_psi_dials(
3394    term: &mut SmoothTermSpec,
3395    psi: &[f64],
3396) -> Result<bool, EstimationError> {
3397    let SmoothBasisSpec::MeasureJet { spec: mj, .. } = &mut term.basis else {
3398        crate::bail_invalid_estim!("measure-jet ψ write-back targeted a non-measure-jet term");
3399    };
3400    apply_measure_jet_psi(mj, psi)
3401}
3402
3403/// The constant-curvature smooth's spec, when `term_idx` is one. Single
3404/// accessor for every κ-ψ dispatch below, mirroring `measure_jet_term_spec`.
3405pub fn constant_curvature_term_spec(
3406    spec: &TermCollectionSpec,
3407    term_idx: usize,
3408) -> Option<&crate::basis::ConstantCurvatureBasisSpec> {
3409    spec.smooth_terms
3410        .get(term_idx)
3411        .and_then(|term| match &term.basis {
3412            SmoothBasisSpec::ConstantCurvature { spec, .. } => Some(spec),
3413            _ => None,
3414        })
3415}
3416
3417/// Hard positive cap on |κ| relative to the data's inverse squared chart
3418/// radius. The κ-stereographic chart is valid for `1 + κ‖x‖² > 0`; at
3419/// `|κ| = 1/R²` (R² = max squared chart radius) the gauge `1 + κ‖x‖²` reaches
3420/// the chart edge for the farthest data point, so the optimizer is boxed to a
3421/// safe fraction of that scale on both sides. κ = 0 (flat) is the centre of
3422/// the window, an interior point of the `S^d ← ℝ^d → H^d` family — exactly the
3423/// reachability the raw-κ (not log-κ) coordinate exists to preserve.
3424pub const CONSTANT_CURVATURE_KAPPA_CHART_FRACTION: f64 = 0.5;
3425
3426/// Floor on the data's squared chart radius used to scale the κ window, so a
3427/// degenerate (near-origin) point cloud still yields a finite, usable bracket
3428/// rather than an unbounded one.
3429pub const CONSTANT_CURVATURE_MIN_CHART_RADIUS2: f64 = 1e-8;
3430
3431/// `(κ_min, κ_max)` outer-optimization window for a constant-curvature term,
3432/// derived from the data's maximum squared chart radius `R²` so the κ-jets
3433/// never leave the κ-stereographic chart. Symmetric about κ = 0:
3434/// `±CONSTANT_CURVATURE_KAPPA_CHART_FRACTION / R²`.
3435pub fn constant_curvature_kappa_bounds(
3436    data: ArrayView2<'_, f64>,
3437    spec: &TermCollectionSpec,
3438    term_idx: usize,
3439) -> (f64, f64) {
3440    let feature_cols = match spec.smooth_terms.get(term_idx).map(|t| &t.basis) {
3441        Some(SmoothBasisSpec::ConstantCurvature { feature_cols, .. }) => feature_cols,
3442        _ => return (-1.0, 1.0),
3443    };
3444    let mut max_r2 = CONSTANT_CURVATURE_MIN_CHART_RADIUS2;
3445    for row in data.outer_iter() {
3446        let mut r2 = 0.0_f64;
3447        for &c in feature_cols.iter() {
3448            if let Some(&v) = row.get(c)
3449                && v.is_finite()
3450            {
3451                r2 += v * v;
3452            }
3453        }
3454        if r2 > max_r2 {
3455            max_r2 = r2;
3456        }
3457    }
3458    let half = CONSTANT_CURVATURE_KAPPA_CHART_FRACTION / max_r2;
3459    (-half, half)
3460}
3461
3462/// Write the optimized κ back into a constant-curvature term spec. Returns
3463/// `true` when κ moved. Centers, ℓ, and the constraint transform `z` are
3464/// κ-FIXED by the basis κ-contract, so only `kappa` changes.
3465pub fn set_constant_curvature_kappa(
3466    spec: &mut TermCollectionSpec,
3467    term_idx: usize,
3468    psi: &[f64],
3469) -> Result<bool, EstimationError> {
3470    let Some(term) = spec.smooth_terms.get_mut(term_idx) else {
3471        crate::bail_invalid_estim!(
3472            "constant-curvature κ write-back: term index {term_idx} out of range"
3473        );
3474    };
3475    set_single_term_constant_curvature_kappa(term, psi)
3476}
3477
3478/// Single-term κ write-back: the shared validate+apply core, also used directly
3479/// on the cached per-trial build spec in the incremental realizer (whose caller
3480/// has already change-checked at the collection level and rebuilds regardless
3481/// of the moved flag). Mirrors [`set_single_term_measure_jet_psi_dials`].
3482pub fn set_single_term_constant_curvature_kappa(
3483    term: &mut SmoothTermSpec,
3484    psi: &[f64],
3485) -> Result<bool, EstimationError> {
3486    if psi.len() != 1 {
3487        crate::bail_invalid_estim!(
3488            "constant-curvature κ write-back expects exactly one value, got {}",
3489            psi.len()
3490        );
3491    }
3492    let next_kappa = psi[0];
3493    if !next_kappa.is_finite() {
3494        crate::bail_invalid_estim!(
3495            "constant-curvature κ write-back produced a non-finite κ = {next_kappa}"
3496        );
3497    }
3498    let SmoothBasisSpec::ConstantCurvature { spec: cc, .. } = &mut term.basis else {
3499        crate::bail_invalid_estim!(
3500            "constant-curvature κ write-back targeted a non-constant-curvature term"
3501        );
3502    };
3503    if cc.kappa != next_kappa {
3504        cc.kappa = next_kappa;
3505        Ok(true)
3506    } else {
3507        Ok(false)
3508    }
3509}
3510
3511/// Returns `true` when a spatial term has NO outer optimization axes — i.e.
3512/// the user provided an explicit `length_scale` and the term does not enroll
3513/// REML-side per-axis ψ contrasts, so both the scalar κ and any fixed geometry
3514/// anisotropy are anchored.
3515///
3516/// This is the per-term predicate that distinguishes "fixed kernel scale"
3517/// from "optimize the kernel scale" within the family entry points that
3518/// want to honor an explicit user-supplied scale (e.g. Bernoulli
3519/// marginal-slope, where the joint-spatial outer solver otherwise spends
3520/// ~80 iters stalled on the user's chosen ρ at high gradient).
3521pub fn spatial_term_has_locked_kappa(spec: &TermCollectionSpec, term_idx: usize) -> bool {
3522    get_spatial_length_scale(spec, term_idx).is_some()
3523        && !spatial_term_uses_per_axis_psi(spec, term_idx)
3524}
3525
3526pub fn all_spatial_terms_kappa_fixed(spec: &TermCollectionSpec) -> bool {
3527    spec.smooth_terms.iter().enumerate().all(|(idx, _)| {
3528        !spatial_term_supports_hyper_optimization(spec, idx)
3529            || spatial_term_has_locked_kappa(spec, idx)
3530    })
3531}
3532
3533pub fn spatial_identifiability_policy(termspec: &SmoothTermSpec) -> Option<&SpatialIdentifiability> {
3534    match &termspec.basis {
3535        SmoothBasisSpec::ThinPlate { spec, .. } => Some(&spec.identifiability),
3536        SmoothBasisSpec::Duchon { spec, .. } => Some(&spec.identifiability),
3537        _ => None,
3538    }
3539}
3540
3541/// Standard deviation of the wide, weakly-informative symmetric `Normal` prior
3542/// placed on a relaxable double-penalty smooth's `DoublePenaltyNullspace`
3543/// selection coordinate when the fit is well-determined.
3544pub const NULLSPACE_WELLDET_DEGENERACY_RHO_SD: f64 = 15.0;
3545
3546/// True iff `prior` is the well-determined double-penalty null-space
3547/// degeneracy prior placed on a `DoublePenaltyNullspace` selection coordinate.
3548pub fn is_nullspace_degeneracy_prior(prior: &gam_spec::RhoPrior) -> bool {
3549    matches!(
3550        prior,
3551        gam_spec::RhoPrior::Normal { mean, sd }
3552            if *mean == 0.0 && *sd == NULLSPACE_WELLDET_DEGENERACY_RHO_SD
3553    )
3554}
3555
3556/// Per-term data-derived ψ = log κ bounds.
3557///
3558/// Uses the same safe operating range documented in
3559/// [`crate::basis::build_matern_basis`] / [`crate::basis::build_duchon_basis`]:
3560///   κ ∈ [2 / r_max, 1e2 / r_min]
3561/// where (r_min, r_max) are pairwise-distance extrema of the term's resolved
3562/// centers (post-fit) or the standardized feature data columns (pre-fit).
3563/// Lower edge of the data-derived kernel-range window, as a fraction of the
3564/// maximum pairwise distance `r_max`: length scales below `2/r_max` resolve
3565/// structure finer than the closest center pair, so the kernel range floor is
3566/// set at twice the maximum spacing.
3567pub const KERNEL_RANGE_MIN_DIAMETER_FRACTION: f64 = 2.0;
3568
3569/// Upper edge of the data-derived kernel-range window, as a multiple of the
3570/// minimum pairwise distance `r_min`: beyond `100/r_min` the radial columns go
3571/// nearly collinear with the polynomial nullspace, so the kernel range is
3572/// capped here to keep the basis geometry well-conditioned.
3573pub const KERNEL_RANGE_MAX_SPACING_MULTIPLE: f64 = 1e2;
3574
3575
3576/// Returns ψ-space bounds (ψ_lo = ln(κ_lo), ψ_hi = ln(κ_hi)).
3577///
3578/// When geometry is unavailable (e.g., fewer than 2 distinct points), falls
3579/// back to the scalar `options.min_length_scale` / `options.max_length_scale`
3580/// window so the outer optimizer never sees NaN bounds.
3581///
3582/// The returned window is intersected with the options window so user-set
3583/// `min_length_scale` / `max_length_scale` remain hard limits.
3584pub fn spatial_term_psi_bounds(
3585    data: ArrayView2<'_, f64>,
3586    spec: &TermCollectionSpec,
3587    term_idx: usize,
3588    options: &SpatialLengthScaleOptimizationOptions,
3589) -> (f64, f64) {
3590    let fallback = (
3591        -options.max_length_scale.ln(),
3592        -options.min_length_scale.ln(),
3593    );
3594    // Constant-curvature: the ψ coordinate is the raw signed κ, so its window is
3595    // the chart-feasible κ bracket, NOT a log-ℓ window. Mirrors the aniso bounds
3596    // path's `constant_curvature_kappa_bounds` branch so the isotropic
3597    // (non-aniso) seed clamp projects κ into the right interval.
3598    if constant_curvature_term_spec(spec, term_idx).is_some() {
3599        return constant_curvature_kappa_bounds(data, spec, term_idx);
3600    }
3601    let Some(term) = spec.smooth_terms.get(term_idx) else {
3602        return fallback;
3603    };
3604    // Prefer resolved centers (post-fit) since they live in the same standardized
3605    // space the kernel actually sees. Centers are capped at `default_num_centers`
3606    // (<=2000), so exact pairwise bounds are cheap (<4M ops). If centers are
3607    // not yet UserProvided, fall back to the standardized feature data columns
3608    // with the capped-sample path (O(K²·d), K=1024) — the sample is
3609    // conservative for κ bounds (see `pairwise_distance_bounds_sampled`
3610    // docs): it never excludes a feasible κ the exact method would include.
3611    //
3612    // Under anisotropy the kernel metric is y-space (y_a = exp(η_a) x_a),
3613    // so r_min/r_max must be y-space distances. This matters only when the
3614    // spec already carries calibrated η_a at setup time (e.g., warm-start
3615    // or refit paths); for fresh optimization η_a starts at 0 and y = x.
3616    let aniso = get_spatial_aniso_log_scales(spec, term_idx);
3617    let r_bounds = match spatial_term_center_strategy(term) {
3618        Some(CenterStrategy::UserProvided(centers)) if centers.nrows() >= 2 => {
3619            match aniso.as_deref() {
3620                Some(eta) if eta.len() == centers.ncols() => {
3621                    let y = points_in_aniso_y_space(centers.view(), eta);
3622                    pairwise_distance_bounds(y.view())
3623                }
3624                _ => pairwise_distance_bounds(centers.view()),
3625            }
3626        }
3627        _ => standardized_spatial_term_data(data, term)
3628            .ok()
3629            .and_then(|x| match aniso.as_deref() {
3630                Some(eta) if eta.len() == x.ncols() => {
3631                    let y = points_in_aniso_y_space(x.view(), eta);
3632                    pairwise_distance_bounds_sampled(y.view())
3633                }
3634                _ => pairwise_distance_bounds_sampled(x.view()),
3635            }),
3636    };
3637    let Some((r_min, r_max)) = r_bounds else {
3638        return fallback;
3639    };
3640    // Length scales substantially larger than the data diameter make radial
3641    // TPS/Matern columns nearly collinear with their polynomial nullspace.
3642    // The nullspace already carries constant/linear low-frequency structure,
3643    // so cap the kernel range at the diameter scale instead of letting the
3644    // optimizer enter a numerically degenerate basis geometry.
3645    let psi_lo_data = (KERNEL_RANGE_MIN_DIAMETER_FRACTION / r_max).ln();
3646    let psi_hi_data = (KERNEL_RANGE_MAX_SPACING_MULTIPLE / r_min).ln();
3647    // #1074: the Matérn-specific length-scale ceiling that used to live here was
3648    // deleted. It was masking, not fixing, the real defect: a hard upper bound on
3649    // the kernel range that pinned the κ-optimizer short rather than letting the
3650    // optimizer find the REML optimum. Matérn now shares the same generic geometry
3651    // window as Duchon / TPS (`KERNEL_RANGE_MIN_DIAMETER_FRACTION / r_max` floor,
3652    // `KERNEL_RANGE_MAX_SPACING_MULTIPLE / r_min` ceiling); the #1357 fully-flat
3653    // collapse corner is guarded by the EDF-collapse guard in
3654    // `spatial_optimization.rs`, which acts on the realized fit, not on a clamp.
3655    // Intersect with the options window so min/max_length_scale remain hard caps.
3656    let psi_lo = psi_lo_data.max(fallback.0);
3657    let psi_hi = psi_hi_data.min(fallback.1);
3658    if psi_lo >= psi_hi {
3659        // Degenerate intersection — fall back to the options window to keep the
3660        // outer optimizer from collapsing to a point.
3661        return fallback;
3662    }
3663    (psi_lo, psi_hi)
3664}
3665
3666/// Data-derived ψ seed for a spatial term when the user has not set an
3667/// explicit length_scale on its basis spec. Uses the geometric mean of the
3668/// data-informed kappa range (i.e., the midpoint of the ψ window).
3669pub fn spatial_term_psi_seed(
3670    data: ArrayView2<'_, f64>,
3671    spec: &TermCollectionSpec,
3672    term_idx: usize,
3673    options: &SpatialLengthScaleOptimizationOptions,
3674) -> Option<f64> {
3675    if get_spatial_length_scale(spec, term_idx).is_some() {
3676        return None; // user/spec-provided length_scale wins
3677    }
3678    let (psi_lo, psi_hi) = spatial_term_psi_bounds(data, spec, term_idx, options);
3679    Some(0.5 * (psi_lo + psi_hi))
3680}
3681
3682pub fn spatial_term_psi_to_length_scale_and_aniso(psi: &[f64]) -> (Option<f64>, Option<Vec<f64>>) {
3683    if psi.len() <= 1 {
3684        (Some((-psi.first().copied().unwrap_or(0.0)).exp()), None)
3685    } else {
3686        let psi_bar = psi.iter().sum::<f64>() / psi.len() as f64;
3687        (
3688            Some((-psi_bar).exp()),
3689            Some(psi.iter().map(|&value| value - psi_bar).collect()),
3690        )
3691    }
3692}
3693
3694/// Get the `aniso_log_scales` from a spatial term, if present.
3695pub fn get_spatial_aniso_log_scales(
3696    spec: &TermCollectionSpec,
3697    term_idx: usize,
3698) -> Option<Vec<f64>> {
3699    spec.smooth_terms
3700        .get(term_idx)
3701        .and_then(|term| match &term.basis {
3702            SmoothBasisSpec::Matern { spec, .. } => spec.aniso_log_scales.clone(),
3703            SmoothBasisSpec::Duchon { spec, .. } => spec.aniso_log_scales.clone(),
3704            _ => None,
3705        })
3706}
3707
3708/// Per-axis response-structure score for anisotropy seeding.
3709///
3710/// For each spatial axis `a`, sort the response `y` by the axis coordinate
3711/// `x_a` and measure the total squared successive variation of the sorted
3712/// response, `tv_a = Σ_i (y_{σ(i+1)} − y_{σ(i)})²` where `σ` orders rows by
3713/// `x_a`. An axis that carries real (possibly nonlinear) signal makes `y` vary
3714/// SMOOTHLY when the rows are walked in that axis's order, so `tv_a` is SMALL;
3715/// a pure-nuisance axis leaves `y` looking unordered, so `tv_a` is LARGE.
3716///
3717/// This deliberately does NOT use a linear correlation `corr(x_a, y)`: for an
3718/// odd, symmetric signal such as `sin(2·x1)` over a symmetric domain the linear
3719/// correlation is ~0 on the *signal* axis, which would misdirect the seed. The
3720/// total-variation-of-sorted-response score captures nonlinear association.
3721///
3722/// Returns `score_a = −½·ln(tv_a + ε)` (larger ⇒ more signal on axis `a`),
3723/// centered to sum to zero, or `None` when the data is degenerate (too few
3724/// rows, non-finite, or all axes equally (un)structured). The caller adds a
3725/// BOUNDED multiple of this to the geometry seed — it is a conservative nudge,
3726/// never a hard override.
3727pub fn response_aware_axis_contrasts(
3728    x: ndarray::ArrayView2<'_, f64>,
3729    y: ndarray::ArrayView1<'_, f64>,
3730) -> Option<Vec<f64>> {
3731    let n = x.nrows();
3732    let d = x.ncols();
3733    if d <= 1 || n < 4 || y.len() != n {
3734        return None;
3735    }
3736    if x.iter().any(|v| !v.is_finite()) || y.iter().any(|v| !v.is_finite()) {
3737        return None;
3738    }
3739    let mut scores = Vec::with_capacity(d);
3740    for a in 0..d {
3741        let mut order: Vec<usize> = (0..n).collect();
3742        let col = x.column(a);
3743        order.sort_by(|&i, &j| {
3744            col[i]
3745                .partial_cmp(&col[j])
3746                .unwrap_or(std::cmp::Ordering::Equal)
3747        });
3748        let mut tv = 0.0_f64;
3749        for w in order.windows(2) {
3750            let diff = y[w[1]] - y[w[0]];
3751            tv += diff * diff;
3752        }
3753        // ε guards against ln(0) on a perfectly flat / constant response.
3754        scores.push(-0.5 * (tv + 1e-12).ln());
3755    }
3756    if scores.iter().any(|v| !v.is_finite()) {
3757        return None;
3758    }
3759    let mean = scores.iter().sum::<f64>() / d as f64;
3760    let centered: Vec<f64> = scores.iter().map(|&s| s - mean).collect();
3761    // If every axis is equally structured the centered scores are ~0 and the
3762    // nudge is a no-op — return None so the geometry seed is used unchanged.
3763    if centered.iter().all(|&v| v.abs() < 1e-9) {
3764        return None;
3765    }
3766    Some(centered)
3767}
3768
3769/// Conservative, response-aware anisotropy seed nudge applied before the κ outer
3770/// loop. For each anisotropic spatial term it adds a BOUNDED multiple of the
3771/// per-axis response-structure contrast (`response_aware_axis_contrasts`) on top
3772/// of the existing geometry seed, so the optimizer starts in the correct basin
3773/// instead of at a response-blind near-symmetric point (the #1376 under-recovery
3774/// where a signal axis and a nuisance axis with equal coordinate spread seed to
3775/// ~[0,0]). The nudge is clamped to keep this a perturbation, never a hard
3776/// override, so shared aniso Matérn/Duchon fits cannot be destabilized by it.
3777pub fn apply_response_aware_anisotropy_seed(
3778    data: ArrayView2<'_, f64>,
3779    y: ndarray::ArrayView1<'_, f64>,
3780    spec: &mut TermCollectionSpec,
3781    spatial_terms: &[usize],
3782) {
3783    // Bound on the per-axis contrast nudge (in η units). One LN_2 ≈ 0.69 halves
3784    // the effective per-axis length scale; capping at LN_2 keeps the seed within
3785    // one optimizer log-step of the geometry seed while still breaking the
3786    // symmetric-seed trap.
3787    const MAX_NUDGE: f64 = std::f64::consts::LN_2;
3788    for &term_idx in spatial_terms {
3789        let Some(current_eta) = get_spatial_aniso_log_scales(spec, term_idx) else {
3790            continue;
3791        };
3792        let d = current_eta.len();
3793        if d <= 1 {
3794            continue;
3795        }
3796        let Some(term) = spec.smooth_terms.get(term_idx) else {
3797            continue;
3798        };
3799        let feature_cols = term.basis.structural_feature_cols();
3800        if feature_cols.len() != d {
3801            continue;
3802        }
3803        let Ok(x) = select_columns(data, &feature_cols) else {
3804            continue;
3805        };
3806        let Some(contrast) = response_aware_axis_contrasts(x.view(), y) else {
3807            continue;
3808        };
3809        let nudged: Vec<f64> = current_eta
3810            .iter()
3811            .zip(contrast.iter())
3812            .map(|(&eta_a, &c_a)| eta_a + c_a.clamp(-MAX_NUDGE, MAX_NUDGE))
3813            .collect();
3814        // `set_spatial_aniso_log_scales` re-centers to Σ η = 0. A term that does
3815        // not support aniso scales is silently skipped (the seed is optional).
3816        if let Err(err) = set_spatial_aniso_log_scales(spec, term_idx, nudged) {
3817            log::debug!(
3818                "[spatial-kappa] response-aware anisotropy seed skipped for term {term_idx}: {err}"
3819            );
3820        }
3821    }
3822}
3823
3824/// Get the number of feature columns (spatial dimensionality) for a spatial term.
3825pub fn get_spatial_feature_dim(spec: &TermCollectionSpec, term_idx: usize) -> Option<usize> {
3826    spec.smooth_terms
3827        .get(term_idx)
3828        .and_then(|term| match &term.basis {
3829            SmoothBasisSpec::ThinPlate { feature_cols, .. } => Some(feature_cols.len()),
3830            SmoothBasisSpec::Matern { feature_cols, .. } => Some(feature_cols.len()),
3831            SmoothBasisSpec::Duchon { feature_cols, .. } => Some(feature_cols.len()),
3832            _ => None,
3833        })
3834}
3835
3836/// Log the learned per-axis spatial anisotropy for all spatial terms that
3837/// have `aniso_log_scales` set after optimization.
3838///
3839/// For scalar-scale families this reports eta, effective per-axis length
3840/// scales, and per-axis kappa values. For pure Duchon it reports the centered
3841/// eta contrasts only.
3842pub fn log_spatial_aniso_scales(spec: &TermCollectionSpec) {
3843    for (term_idx, term) in spec.smooth_terms.iter().enumerate() {
3844        let (aniso, length_scale) = match &term.basis {
3845            SmoothBasisSpec::Matern { spec, .. } => {
3846                (spec.aniso_log_scales.as_ref(), Some(spec.length_scale))
3847            }
3848            SmoothBasisSpec::Duchon { spec, .. } => {
3849                (spec.aniso_log_scales.as_ref(), spec.length_scale)
3850            }
3851            _ => (None, None),
3852        };
3853        let Some(eta) = aniso else { continue };
3854        if eta.is_empty() {
3855            continue;
3856        }
3857        let mut lines = match length_scale {
3858            Some(ls) => format!(
3859                "[spatial-kappa] term {} (\"{}\"): anisotropic length scales optimized (global length_scale={:.4})",
3860                term_idx, term.name, ls
3861            ),
3862            None => format!(
3863                "[spatial-kappa] term {} (\"{}\"): pure Duchon shape anisotropy optimized",
3864                term_idx, term.name
3865            ),
3866        };
3867        for (a, &eta_a) in eta.iter().enumerate() {
3868            if let Some(ls) = length_scale {
3869                let length_a = ls * (-eta_a).exp();
3870                let kappa_a = (1.0 / ls) * eta_a.exp();
3871                lines.push_str(&format!(
3872                    "\n  axis {}: eta={:+.4}, length={:.4}, kappa={:.4}",
3873                    a, eta_a, length_a, kappa_a
3874                ));
3875            } else {
3876                lines.push_str(&format!("\n  axis {}: eta={:+.4}", a, eta_a));
3877            }
3878        }
3879        log::info!("{}", lines);
3880    }
3881}
3882
3883/// Set `aniso_log_scales` on a spatial term's basis spec.
3884pub fn set_spatial_aniso_log_scales(
3885    spec: &mut TermCollectionSpec,
3886    term_idx: usize,
3887    eta: Vec<f64>,
3888) -> Result<(), EstimationError> {
3889    let eta = center_aniso_log_scales(&eta);
3890    let Some(term) = spec.smooth_terms.get_mut(term_idx) else {
3891        crate::bail_invalid_estim!("spatial aniso_log_scales term index {term_idx} out of range");
3892    };
3893    match &mut term.basis {
3894        SmoothBasisSpec::Matern { spec, .. } => {
3895            spec.aniso_log_scales = Some(eta);
3896            Ok(())
3897        }
3898        SmoothBasisSpec::Duchon { spec, .. } => {
3899            spec.aniso_log_scales = Some(eta);
3900            Ok(())
3901        }
3902        _ => Err(EstimationError::InvalidInput(format!(
3903            "term '{}' does not support aniso_log_scales",
3904            term.name
3905        ))),
3906    }
3907}
3908
3909/// Sync knot-cloud-derived anisotropy contrasts from basis metadata back into
3910/// the mutable spec so the optimizer starts from the correct eta values.
3911///
3912/// Call this after building the smooth design but before initializing the
3913/// optimizer's psi coordinates. For each spatial term whose metadata contains
3914/// computed `aniso_log_scales`, this writes them into the spec.
3915pub fn sync_aniso_contrasts_from_metadata(
3916    spec: &mut TermCollectionSpec,
3917    design: &SmoothDesign,
3918) {
3919    for (term_idx, term) in design.terms.iter().enumerate() {
3920        let meta_aniso = match &term.metadata {
3921            BasisMetadata::Matern {
3922                aniso_log_scales, ..
3923            } => aniso_log_scales.clone(),
3924            BasisMetadata::Duchon {
3925                aniso_log_scales, ..
3926            } => aniso_log_scales.clone(),
3927            _ => None,
3928        };
3929        if let Some(eta) = meta_aniso
3930            && eta.len() > 1
3931        {
3932            set_spatial_aniso_log_scales(spec, term_idx, eta).ok();
3933        }
3934    }
3935}
3936
3937#[derive(Debug, Clone)]
3938pub struct SpatialLengthScaleOptimizationOptions {
3939    /// Enable outer-loop optimization over spatial κ (= 1 / length_scale)
3940    /// for supported radial-kernel smooths.
3941    /// This applies to ThinPlate, Matérn, and Duchon terms.
3942    pub enabled: bool,
3943    /// Maximum number of outer iterations in the exact joint [rho, psi] solve.
3944    pub max_outer_iter: usize,
3945    /// Relative improvement threshold for terminating the outer solve.
3946    pub rel_tol: f64,
3947    /// Initial log(length_scale) perturbation used for seed construction.
3948    pub log_step: f64,
3949    /// Minimum allowed length_scale during κ search.
3950    pub min_length_scale: f64,
3951    /// Maximum allowed length_scale during κ search.
3952    pub max_length_scale: f64,
3953    /// Automatic geometry-initializer threshold for large-scale spatial fits.
3954    ///
3955    /// When n exceeds twice this value, the fitter uses a spatially stratified
3956    /// subsample only to seed κ/anisotropy geometry: centers are resolved,
3957    /// axis contrasts are initialized from center/data spread, and one or two
3958    /// cheap ψ reseeding updates are applied. It never runs PIRLS, REML, ARC,
3959    /// BFGS, or any recursive optimizer on the pilot.
3960    ///
3961    /// The final coefficients, smoothing parameters, and spatial geometry are
3962    /// always optimized on the full dataset.
3963    ///
3964    /// Set to 0 to skip the pilot geometry initializer.
3965    pub pilot_subsample_threshold: usize,
3966}
3967
3968impl Default for SpatialLengthScaleOptimizationOptions {
3969    fn default() -> Self {
3970        Self {
3971            enabled: true,
3972            max_outer_iter: 80,
3973            rel_tol: 1e-4,
3974            log_step: std::f64::consts::LN_2,
3975            min_length_scale: 1e-3,
3976            max_length_scale: 1e3,
3977            pilot_subsample_threshold: 10_000,
3978        }
3979    }
3980}
3981
3982impl SpatialLengthScaleOptimizationOptions {
3983    /// Validate the struct's invariants. Callers that construct these options
3984    /// from external input (CLI, config, Python API) should call this before
3985    /// passing the options into the fitter. Returns `Err` with a descriptive
3986    /// message when an invariant is violated; the fitter then panics or
3987    /// returns `EstimationError` at its own boundary.
3988    ///
3989    /// Invariants:
3990    ///   * `min_length_scale > 0`, finite
3991    ///   * `max_length_scale > 0`, finite
3992    ///   * `min_length_scale < max_length_scale`
3993    ///   * `rel_tol > 0`, finite
3994    ///   * `log_step > 0`, finite
3995    ///
3996    /// These invariants are what the downstream κ-bound and ψ-window code
3997    /// assumes (`-log(max_ls)` must be finite, `(min,max)` must not be
3998    /// inverted, etc.). Without validation, invalid options produce silent
3999    /// NaN-propagation inside the outer optimizer.
4000    pub fn validate(&self) -> Result<(), String> {
4001        if !self.min_length_scale.is_finite() || self.min_length_scale <= 0.0 {
4002            return Err(SmoothError::invalid_config(format!(
4003                "SpatialLengthScaleOptimizationOptions::min_length_scale must be > 0 and finite, got {}",
4004                self.min_length_scale
4005            ))
4006            .into());
4007        }
4008        if !self.max_length_scale.is_finite() || self.max_length_scale <= 0.0 {
4009            return Err(SmoothError::invalid_config(format!(
4010                "SpatialLengthScaleOptimizationOptions::max_length_scale must be > 0 and finite, got {}",
4011                self.max_length_scale
4012            ))
4013            .into());
4014        }
4015        if self.min_length_scale >= self.max_length_scale {
4016            return Err(SmoothError::invalid_config(format!(
4017                "SpatialLengthScaleOptimizationOptions requires min_length_scale < max_length_scale, got min={} max={}",
4018                self.min_length_scale, self.max_length_scale
4019            ))
4020            .into());
4021        }
4022        if !self.rel_tol.is_finite() || self.rel_tol <= 0.0 {
4023            return Err(SmoothError::invalid_config(format!(
4024                "SpatialLengthScaleOptimizationOptions::rel_tol must be > 0 and finite, got {}",
4025                self.rel_tol
4026            ))
4027            .into());
4028        }
4029        if !self.log_step.is_finite() || self.log_step <= 0.0 {
4030            return Err(SmoothError::invalid_config(format!(
4031                "SpatialLengthScaleOptimizationOptions::log_step must be > 0 and finite, got {}",
4032                self.log_step
4033            ))
4034            .into());
4035        }
4036        Ok(())
4037    }
4038}
4039
4040#[derive(Debug, Clone)]
4041pub struct RandomEffectBlock {
4042    pub name: String,
4043    /// O(n) group-label vector: group_ids[i] = column index in [0, num_groups).
4044    /// `None` if the observation's level is not in the kept set.
4045    pub group_ids: Vec<Option<usize>>,
4046    pub num_groups: usize,
4047    pub kept_levels: Vec<u64>,
4048}
4049
4050pub const BLOCK_SPARSE_ZERO_EPS: f64 = 1e-12;
4051
4052pub const BLOCK_SPARSE_MAX_DENSITY: f64 = 0.20;
4053
4054pub fn blocks_have_intrinsic_sparse_structure(blocks: &[DesignBlock]) -> bool {
4055    blocks
4056        .iter()
4057        .any(|block| matches!(block, DesignBlock::Sparse(_) | DesignBlock::RandomEffect(_)))
4058}
4059
4060pub fn sparse_compatible_block_nnz(block: &DesignBlock) -> Option<usize> {
4061    match block {
4062        DesignBlock::Intercept(n) => Some(*n),
4063        DesignBlock::RandomEffect(op) => {
4064            Some(op.group_ids.iter().filter(|gid| gid.is_some()).count())
4065        }
4066        DesignBlock::Sparse(sparse) => Some(sparse.val().len()),
4067        DesignBlock::Dense(dense) => dense.as_dense_ref().map(|matrix| {
4068            matrix
4069                .iter()
4070                .filter(|&&value| value.abs() > BLOCK_SPARSE_ZERO_EPS)
4071                .count()
4072        }),
4073    }
4074}
4075
4076pub fn try_build_sparse_design_from_blocks(
4077    blocks: &[DesignBlock],
4078) -> Result<Option<DesignMatrix>, BasisError> {
4079    if blocks.is_empty() {
4080        return Ok(None);
4081    }
4082    let nrows = blocks[0].nrows();
4083    let ncols: usize = blocks.iter().map(DesignBlock::ncols).sum();
4084    if nrows == 0 || ncols == 0 || ncols <= 32 {
4085        return Ok(None);
4086    }
4087
4088    let preserve_sparse_storage = blocks_have_intrinsic_sparse_structure(blocks);
4089    let sparse_nnz_limit = if preserve_sparse_storage {
4090        usize::MAX
4091    } else {
4092        let total_cells = nrows.saturating_mul(ncols);
4093        ((total_cells as f64) * BLOCK_SPARSE_MAX_DENSITY).floor() as usize
4094    };
4095    let mut nnz = 0usize;
4096    for block in blocks {
4097        let block_nnz = if let Some(block_nnz) = sparse_compatible_block_nnz(block) {
4098            block_nnz
4099        } else {
4100            return Ok(None);
4101        };
4102        nnz = nnz.saturating_add(block_nnz);
4103        if nnz > sparse_nnz_limit {
4104            return Ok(None);
4105        }
4106    }
4107
4108    let mut triplets = Vec::<Triplet<usize, usize, f64>>::with_capacity(nnz);
4109    let mut col_offset = 0usize;
4110    for block in blocks {
4111        match block {
4112            DesignBlock::Intercept(n) => {
4113                for row in 0..*n {
4114                    triplets.push(Triplet::new(row, col_offset, 1.0));
4115                }
4116            }
4117            DesignBlock::RandomEffect(op) => {
4118                for (row, group_id) in op.group_ids.iter().enumerate() {
4119                    if let Some(group) = group_id {
4120                        triplets.push(Triplet::new(row, col_offset + group, 1.0));
4121                    }
4122                }
4123            }
4124            DesignBlock::Sparse(sparse) => {
4125                let (symbolic, values) = sparse.parts();
4126                let col_ptr = symbolic.col_ptr();
4127                let row_idx = symbolic.row_idx();
4128                for col in 0..sparse.ncols() {
4129                    for idx in col_ptr[col]..col_ptr[col + 1] {
4130                        let value = values[idx];
4131                        if value.abs() > BLOCK_SPARSE_ZERO_EPS {
4132                            triplets.push(Triplet::new(row_idx[idx], col_offset + col, value));
4133                        }
4134                    }
4135                }
4136            }
4137            DesignBlock::Dense(dense) => {
4138                let matrix = dense.as_dense_ref().ok_or_else(|| {
4139                    BasisError::InvalidInput(
4140                        "sparse-compatible block assembly requires materialized dense blocks"
4141                            .to_string(),
4142                    )
4143                })?;
4144                for row in 0..matrix.nrows() {
4145                    for col in 0..matrix.ncols() {
4146                        let value = matrix[[row, col]];
4147                        if value.abs() > BLOCK_SPARSE_ZERO_EPS {
4148                            triplets.push(Triplet::new(row, col_offset + col, value));
4149                        }
4150                    }
4151                }
4152            }
4153        }
4154        col_offset += block.ncols();
4155    }
4156
4157    let sparse = SparseColMat::try_new_from_triplets(nrows, ncols, &triplets).map_err(|_| {
4158        BasisError::SparseCreation("failed to assemble sparse term-collection design".to_string())
4159    })?;
4160    Ok(Some(DesignMatrix::Sparse(
4161        gam_linalg::matrix::SparseDesignMatrix::new(sparse),
4162    )))
4163}
4164
4165pub fn assemble_term_collection_design_matrix(
4166    blocks: Vec<DesignBlock>,
4167) -> Result<DesignMatrix, BasisError> {
4168    if let Some(sparse) = try_build_sparse_design_from_blocks(&blocks)? {
4169        return Ok(sparse);
4170    }
4171    let block_op = BlockDesignOperator::new(blocks).map_err(|e| {
4172        BasisError::InvalidInput(format!("failed to build block design operator: {e}"))
4173    })?;
4174    Ok(DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(
4175        Arc::new(block_op),
4176    )))
4177}
4178
4179pub fn select_columns(data: ArrayView2<'_, f64>, cols: &[usize]) -> Result<Array2<f64>, BasisError> {
4180    let n = data.nrows();
4181    let p = data.ncols();
4182    for &c in cols {
4183        if c >= p {
4184            crate::bail_dim_basis!("feature column {c} is out of bounds for data with {p} columns");
4185        }
4186    }
4187    let mut out = Array2::<f64>::zeros((n, cols.len()));
4188    for (j, &c) in cols.iter().enumerate() {
4189        out.column_mut(j).assign(&data.column(c));
4190    }
4191    Ok(out)
4192}
4193
4194pub fn nonfinite_value_label(value: f64) -> &'static str {
4195    if value.is_nan() {
4196        "NaN"
4197    } else if value.is_sign_positive() {
4198        "+Inf"
4199    } else {
4200        "-Inf"
4201    }
4202}
4203
4204pub fn validate_term_feature_column_finite(
4205    data: ArrayView2<'_, f64>,
4206    term_kind: &str,
4207    term_name: &str,
4208    feature_col: usize,
4209) -> Result<(), BasisError> {
4210    let p = data.ncols();
4211    if feature_col >= p {
4212        crate::bail_dim_basis!(
4213            "{term_kind} term '{term_name}' feature column {feature_col} out of bounds for {p} columns"
4214        );
4215    }
4216    for (row, &value) in data.column(feature_col).iter().enumerate() {
4217        if !value.is_finite() {
4218            crate::bail_invalid_basis!(
4219                "{term_kind} term '{term_name}' feature column {feature_col} row {row} contains non-finite value {}",
4220                nonfinite_value_label(value)
4221            );
4222        }
4223    }
4224    Ok(())
4225}
4226
4227pub fn validate_smooth_terms_finite_inputs(
4228    data: ArrayView2<'_, f64>,
4229    terms: &[SmoothTermSpec],
4230) -> Result<(), BasisError> {
4231    for term in terms {
4232        for feature_col in smooth_term_feature_cols(term) {
4233            validate_term_feature_column_finite(data, "smooth", &term.name, feature_col)?;
4234        }
4235    }
4236    Ok(())
4237}
4238
4239pub fn validate_term_collection_finite_inputs(
4240    data: ArrayView2<'_, f64>,
4241    spec: &TermCollectionSpec,
4242) -> Result<(), BasisError> {
4243    for term in &spec.linear_terms {
4244        validate_term_feature_column_finite(data, "linear", &term.name, term.feature_col)?;
4245    }
4246    for term in &spec.random_effect_terms {
4247        validate_term_feature_column_finite(data, "random-effect", &term.name, term.feature_col)?;
4248    }
4249    validate_smooth_terms_finite_inputs(data, &spec.smooth_terms)
4250}
4251
4252#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
4253pub struct JointSpatialCenterGroupKey {
4254    feature_cols: Vec<usize>,
4255    strategy_kind: CenterStrategyKind,
4256    strategy_aux: usize,
4257    requested_num_centers: usize,
4258    input_scale_bits: Option<Vec<u64>>,
4259}
4260
4261pub fn spatial_term_min_center_count(term: &SmoothTermSpec) -> usize {
4262    match &term.basis {
4263        SmoothBasisSpec::ThinPlate { feature_cols, .. } => feature_cols.len() + 1,
4264        SmoothBasisSpec::Duchon {
4265            feature_cols, spec, ..
4266        } => match spec.nullspace_order {
4267            crate::basis::DuchonNullspaceOrder::Zero => 1,
4268            crate::basis::DuchonNullspaceOrder::Linear => feature_cols.len() + 1,
4269            crate::basis::DuchonNullspaceOrder::Degree(degree) => {
4270                crate::basis::duchon_nullspace_dimension(feature_cols.len(), degree)
4271            }
4272        },
4273        SmoothBasisSpec::Matern { .. } => 1,
4274        _ => 1,
4275    }
4276}
4277
4278pub fn spatial_term_group_key(term: &SmoothTermSpec) -> Option<JointSpatialCenterGroupKey> {
4279    let (feature_cols, strategy, input_scales) = match &term.basis {
4280        SmoothBasisSpec::ThinPlate {
4281            feature_cols,
4282            spec,
4283            input_scales,
4284        } => (feature_cols, &spec.center_strategy, input_scales.as_ref()),
4285        SmoothBasisSpec::Matern {
4286            feature_cols,
4287            spec,
4288            input_scales,
4289        } => (feature_cols, &spec.center_strategy, input_scales.as_ref()),
4290        SmoothBasisSpec::Duchon {
4291            feature_cols,
4292            spec,
4293            input_scales,
4294        } => (feature_cols, &spec.center_strategy, input_scales.as_ref()),
4295        _ => return None,
4296    };
4297    let strategy_kind = center_strategy_kind(strategy);
4298    let strategy_aux = match strategy {
4299        CenterStrategy::Auto(inner) => match inner.as_ref() {
4300            CenterStrategy::KMeans { max_iter, .. } => *max_iter,
4301            CenterStrategy::UniformGrid { points_per_dim } => *points_per_dim,
4302            _ => 0,
4303        },
4304        CenterStrategy::KMeans { max_iter, .. } => *max_iter,
4305        CenterStrategy::UniformGrid { points_per_dim } => *points_per_dim,
4306        _ => 0,
4307    };
4308    Some(JointSpatialCenterGroupKey {
4309        feature_cols: feature_cols.clone(),
4310        strategy_kind,
4311        strategy_aux,
4312        requested_num_centers: center_strategy_num_centers(strategy)?,
4313        input_scale_bits: input_scales
4314            .map(|values| values.iter().map(|value| value.to_bits()).collect()),
4315    })
4316}
4317
4318pub fn spatial_term_center_strategy(term: &SmoothTermSpec) -> Option<&CenterStrategy> {
4319    match &term.basis {
4320        SmoothBasisSpec::ThinPlate { spec, .. } => Some(&spec.center_strategy),
4321        SmoothBasisSpec::Matern { spec, .. } => Some(&spec.center_strategy),
4322        SmoothBasisSpec::Duchon { spec, .. } => Some(&spec.center_strategy),
4323        _ => None,
4324    }
4325}
4326
4327pub fn set_spatial_term_centers(
4328    term: &mut SmoothTermSpec,
4329    centers: Array2<f64>,
4330) -> Result<(), BasisError> {
4331    match &mut term.basis {
4332        SmoothBasisSpec::ThinPlate { spec, .. } => {
4333            spec.center_strategy = CenterStrategy::UserProvided(centers);
4334            Ok(())
4335        }
4336        SmoothBasisSpec::Matern { spec, .. } => {
4337            spec.center_strategy = CenterStrategy::UserProvided(centers);
4338            Ok(())
4339        }
4340        SmoothBasisSpec::Duchon { spec, .. } => {
4341            spec.center_strategy = CenterStrategy::UserProvided(centers);
4342            Ok(())
4343        }
4344        _ => Err(BasisError::InvalidInput(format!(
4345            "term '{}' does not support spatial center planning",
4346            term.name
4347        ))),
4348    }
4349}
4350
4351pub fn standardized_spatial_term_data(
4352    data: ArrayView2<'_, f64>,
4353    term: &SmoothTermSpec,
4354) -> Result<Array2<f64>, BasisError> {
4355    let (feature_cols, input_scales) = match &term.basis {
4356        SmoothBasisSpec::ThinPlate {
4357            feature_cols,
4358            input_scales,
4359            ..
4360        }
4361        | SmoothBasisSpec::Matern {
4362            feature_cols,
4363            input_scales,
4364            ..
4365        }
4366        | SmoothBasisSpec::Duchon {
4367            feature_cols,
4368            input_scales,
4369            ..
4370        } => (feature_cols, input_scales.as_ref()),
4371        _ => {
4372            crate::bail_invalid_basis!("term '{}' is not a spatial smooth", term.name);
4373        }
4374    };
4375    let mut x = select_columns(data, feature_cols)?;
4376    if let Some(scales) = input_scales {
4377        apply_input_standardization(&mut x, scales);
4378    } else if let Some(scales) = compute_spatial_input_scales(x.view()) {
4379        apply_input_standardization(&mut x, &scales);
4380    }
4381    Ok(x)
4382}
4383
4384pub fn plan_joint_spatial_centers_for_term_blocks(
4385    data: ArrayView2<'_, f64>,
4386    term_blocks: &[Vec<SmoothTermSpec>],
4387) -> Result<Vec<Vec<SmoothTermSpec>>, BasisError> {
4388    let mut planned_blocks = term_blocks.to_vec();
4389    let n = data.nrows();
4390    let mut groups: BTreeMap<JointSpatialCenterGroupKey, Vec<(usize, usize)>> = BTreeMap::new();
4391
4392    for (block_idx, terms) in planned_blocks.iter().enumerate() {
4393        for (term_idx, term) in terms.iter().enumerate() {
4394            let Some(strategy) = spatial_term_center_strategy(term) else {
4395                continue;
4396            };
4397            if !center_strategy_is_auto(strategy) {
4398                continue;
4399            }
4400            let Some(group_key) = spatial_term_group_key(term) else {
4401                continue;
4402            };
4403            if !matches!(
4404                group_key.strategy_kind,
4405                CenterStrategyKind::EqualMass
4406                    | CenterStrategyKind::EqualMassCovarRepresentative
4407                    | CenterStrategyKind::FarthestPoint
4408                    | CenterStrategyKind::KMeans
4409            ) {
4410                continue;
4411            }
4412            if center_strategy_num_centers(strategy).is_none() {
4413                continue;
4414            }
4415            groups
4416                .entry(group_key)
4417                .or_default()
4418                .push((block_idx, term_idx));
4419        }
4420    }
4421
4422    for (group_key, members) in groups {
4423        if members.len() < 2 {
4424            continue;
4425        }
4426        let min_required = members
4427            .iter()
4428            .map(|&(block_idx, term_idx)| {
4429                spatial_term_min_center_count(&planned_blocks[block_idx][term_idx])
4430            })
4431            .max()
4432            .unwrap_or(1);
4433        let joint_centers = group_key
4434            .requested_num_centers
4435            .max(min_required)
4436            .min(n.max(1));
4437        let (first_block_idx, first_term_idx) = members[0];
4438        let prototype = &planned_blocks[first_block_idx][first_term_idx];
4439        let standardized = standardized_spatial_term_data(data, prototype)?;
4440        let strategy = spatial_term_center_strategy(prototype).ok_or_else(|| {
4441            BasisError::InvalidInput(format!(
4442                "term '{}' lost its spatial center strategy during joint planning",
4443                prototype.name
4444            ))
4445        })?;
4446        let joint_strategy = center_strategy_with_num_centers(strategy, joint_centers)?;
4447        let shared_centers = select_centers_by_strategy(standardized.view(), &joint_strategy)?;
4448        log::info!(
4449            "sharing {} spatial centers across {} smooth terms over columns {:?} (requested {} centers)",
4450            shared_centers.nrows(),
4451            members.len(),
4452            group_key.feature_cols,
4453            group_key.requested_num_centers,
4454        );
4455        for (block_idx, term_idx) in members {
4456            set_spatial_term_centers(
4457                &mut planned_blocks[block_idx][term_idx],
4458                shared_centers.clone(),
4459            )?;
4460        }
4461    }
4462
4463    // Sentinel auto-init: Matern and thin-plate builders write length_scale =
4464    // 0.0 when the user didn't pass `length_scale=...`. Replace those with a
4465    // data-driven initialization here so REML starts in a regime where it can
4466    // escape; the hard-coded 1.0 default was a basin from which ν ≥ 5/2 Matern
4467    // could not recover on high-frequency truths, silently collapsing the fit
4468    // to a near-constant prediction.
4469    for block in planned_blocks.iter_mut() {
4470        for term in block.iter_mut() {
4471            auto_init_length_scale_in_place(data, term);
4472        }
4473    }
4474
4475    Ok(planned_blocks)
4476}
4477
4478/// Tiny positive floor for the auto length scale, guarding against a zero
4479/// kernel range when every feature column is (near-)constant.
4480const AUTO_LENGTH_SCALE_FLOOR: f64 = 1e-6;
4481
4482/// Widest per-axis range of the selected feature columns. Returns `None` when
4483/// every selected column is constant / non-finite (no usable spatial scale).
4484fn feature_columns_max_range(data: ArrayView2<'_, f64>, feature_cols: &[usize]) -> Option<f64> {
4485    let mut max_range = 0.0_f64;
4486    for &c in feature_cols {
4487        if c >= data.ncols() {
4488            continue;
4489        }
4490        let col = data.column(c);
4491        let mut lo = f64::INFINITY;
4492        let mut hi = f64::NEG_INFINITY;
4493        for &v in col.iter() {
4494            if v.is_finite() {
4495                if v < lo {
4496                    lo = v;
4497                }
4498                if v > hi {
4499                    hi = v;
4500                }
4501            }
4502        }
4503        if hi > lo {
4504            let r = hi - lo;
4505            if r > max_range {
4506                max_range = r;
4507            }
4508        }
4509    }
4510    if max_range.is_finite() && max_range > 0.0 {
4511        Some(max_range)
4512    } else {
4513        None
4514    }
4515}
4516
4517/// Compute a data-driven initial length scale from the per-axis range of the
4518/// feature columns. The heuristic `max_range / sqrt(n)` puts the kernel on
4519/// the wiggly side of REML's basin so the optimizer can grow it back if the
4520/// signal is smooth, but is small enough that high-frequency truths remain
4521/// reachable for smoother kernels (ν ≥ 5/2). Clamped to a tiny positive
4522/// floor so degenerate constant-input columns can't produce 0.
4523pub fn auto_initial_length_scale(data: ArrayView2<'_, f64>, feature_cols: &[usize]) -> f64 {
4524    let n = data.nrows();
4525    if n == 0 || feature_cols.is_empty() {
4526        return 1.0;
4527    }
4528    let Some(max_range) = feature_columns_max_range(data, feature_cols) else {
4529        return 1.0;
4530    };
4531    let init = max_range / (n as f64).sqrt();
4532    init.max(AUTO_LENGTH_SCALE_FLOOR).min(max_range)
4533}
4534
4535/// Density-adaptive auto length scale for a kernel basis with `num_centers`
4536/// requested centers (#1731).
4537///
4538/// The plain [`auto_initial_length_scale`] seed `max_range / sqrt(n)` is the
4539/// fill distance of the *n data points*; it is independent of the requested
4540/// center count `k`. For a radial kernel at a FIXED length scale, packing more
4541/// centers into the same cloud makes neighbouring basis functions overlap and
4542/// go numerically collinear, so the realized basis saturates in rank (the
4543/// `matern_rank_reduce_centers` cap) and a richer `k` becomes a no-op — or even
4544/// shrinks the basis. The kernel stays well-conditioned only while the length
4545/// scale tracks the *center* spacing, not the data spacing.
4546///
4547/// We seed the length scale at the fill distance of `max(n, k)` points,
4548/// `max_range / sqrt(max(n, k))`. When `n ≥ k` (the usual case) this is exactly
4549/// the existing `max_range / sqrt(n)` seed, so every current result and small-`k`
4550/// basis size is preserved bit-for-bit (in every covariate dimension). When
4551/// `k > n` (a dense center request on a small cloud, the regime where an
4552/// `n`-sized seed sits above the center spacing and over-smooths the centers
4553/// into collinearity) the seed shrinks with `k` to the center spacing, keeping
4554/// the requested centers numerically independent. This is the Matérn analogue of
4555/// the Duchon-promotion "length_scale from center spacing" rule
4556/// (`hybrid_duchon_promotion_length_scale`).
4557pub fn auto_initial_length_scale_for_centers(
4558    data: ArrayView2<'_, f64>,
4559    feature_cols: &[usize],
4560    num_centers: usize,
4561) -> f64 {
4562    let n = data.nrows();
4563    if n == 0 || feature_cols.is_empty() {
4564        return 1.0;
4565    }
4566    let Some(max_range) = feature_columns_max_range(data, feature_cols) else {
4567        return 1.0;
4568    };
4569    // Resolution density: at least the data points, but no coarser than the
4570    // center spacing once more centers than data are requested. Using the same
4571    // `sqrt` fill-distance law as `auto_initial_length_scale` keeps the seed
4572    // bit-identical whenever `n ≥ num_centers` (every dimension), and only
4573    // shrinks it — never grows it — when `num_centers > n`.
4574    let resolution_points = n.max(num_centers).max(1) as f64;
4575    let spacing = max_range / resolution_points.sqrt();
4576    spacing.max(AUTO_LENGTH_SCALE_FLOOR).min(max_range)
4577}
4578
4579/// Low-rank radial-basis length-scale seed tied to the requested center spacing.
4580///
4581/// Thin-plate regression splines with `k << n` represent the surface through a
4582/// compact set of centers; seeding the kernel at the observation fill distance
4583/// (`max_range / sqrt(n)`) makes the center Gram nearly diagonal and turns the
4584/// bending penalty into an ill-scaled ridge on the radial coefficients. REML then
4585/// sees a weakly identified smoothing surface and can settle on under-recovered
4586/// spatial fits. Seed at the center fill distance instead, so neighbouring
4587/// centers interact at O(1) scale before REML tunes the smoothing parameter.
4588pub fn auto_initial_length_scale_for_low_rank_centers(
4589    data: ArrayView2<'_, f64>,
4590    feature_cols: &[usize],
4591    num_centers: usize,
4592) -> f64 {
4593    if data.nrows() == 0 || feature_cols.is_empty() {
4594        return 1.0;
4595    }
4596    let Some(max_range) = feature_columns_max_range(data, feature_cols) else {
4597        return 1.0;
4598    };
4599    let resolution_points = num_centers.max(1) as f64;
4600    let spacing = max_range / resolution_points.sqrt();
4601    spacing.max(AUTO_LENGTH_SCALE_FLOOR).min(max_range)
4602}
4603
4604/// Requested center count encoded by a [`CenterStrategy`], if it carries an
4605/// explicit count (used to make the Matérn auto length scale density-adaptive).
4606fn center_strategy_requested_count(strategy: &CenterStrategy) -> Option<usize> {
4607    match strategy {
4608        CenterStrategy::Auto(inner) => center_strategy_requested_count(inner),
4609        CenterStrategy::UserProvided(centers) => Some(centers.nrows()),
4610        CenterStrategy::EqualMass { num_centers }
4611        | CenterStrategy::EqualMassCovarRepresentative { num_centers }
4612        | CenterStrategy::FarthestPoint { num_centers }
4613        | CenterStrategy::KMeans { num_centers, .. } => Some(*num_centers),
4614        CenterStrategy::UniformGrid { .. } => None,
4615    }
4616}
4617
4618/// Walk a term and, if it is a Matern or thin-plate smooth whose length_scale
4619/// was left at the auto sentinel (`0.0`), overwrite it with
4620/// [`auto_initial_length_scale`].
4621pub fn auto_init_length_scale_in_place(data: ArrayView2<'_, f64>, term: &mut SmoothTermSpec) {
4622    auto_init_length_scale_in_basis(data, &mut term.basis);
4623}
4624
4625/// Replace the `0.0` auto-init length-scale sentinel with a data-derived value
4626/// for any Matern / thin-plate kernel reachable from this basis — including the
4627/// inner kernel of a `by=`/factor-smooth wrapper.
4628///
4629/// `by=<factor>` and the sum-to-zero factor smooth wrap a spatial kernel inside
4630/// `SmoothBasisSpec::ByVariable` / `SmoothBasisSpec::FactorSumToZero` /
4631/// `SmoothBasisSpec::BySmooth`, so the wrapper variant is what the planner sees.
4632/// Without recursing into the wrapped basis the inner ThinPlate/Matern keeps the
4633/// `0.0` sentinel (the post-`1605b3a6e` builder default), which makes the kernel
4634/// distance divide by `length_scale² = 0`, producing a non-finite design at both
4635/// fit and predict time. Recurse so the inner kernel is initialized identically
4636/// to a top-level one.
4637pub fn auto_init_length_scale_in_basis(data: ArrayView2<'_, f64>, basis: &mut SmoothBasisSpec) {
4638    match basis {
4639        SmoothBasisSpec::Matern {
4640            feature_cols, spec, ..
4641        } => {
4642            if spec.length_scale == 0.0 {
4643                // Density-adaptive seed (#1731): when the requested center count
4644                // is known, scale the auto length scale with the *center*
4645                // spacing so a richer `k` stays numerically full-rank instead of
4646                // saturating against `matern_rank_reduce_centers`. For `n ≥ k`
4647                // (the usual case) this is identical to the plain `max_range /
4648                // sqrt(n)` seed in 2-D, so small-`k` results are unchanged. The
4649                // unconstrained / non-explicit `UniformGrid` strategy falls back
4650                // to the plain seed.
4651                spec.length_scale = match center_strategy_requested_count(&spec.center_strategy) {
4652                    Some(k) => auto_initial_length_scale_for_centers(data, feature_cols, k),
4653                    None => auto_initial_length_scale(data, feature_cols),
4654                };
4655            }
4656        }
4657        SmoothBasisSpec::ThinPlate {
4658            feature_cols, spec, ..
4659        } => {
4660            if spec.length_scale == 0.0 {
4661                spec.length_scale = match center_strategy_requested_count(&spec.center_strategy) {
4662                    Some(k) => {
4663                        auto_initial_length_scale_for_low_rank_centers(data, feature_cols, k)
4664                    }
4665                    None => auto_initial_length_scale(data, feature_cols),
4666                };
4667            }
4668        }
4669        SmoothBasisSpec::ByVariable { inner, .. }
4670        | SmoothBasisSpec::FactorSumToZero { inner, .. } => {
4671            auto_init_length_scale_in_basis(data, inner);
4672        }
4673        SmoothBasisSpec::BySmooth { smooth, .. } => {
4674            auto_init_length_scale_in_basis(data, smooth);
4675        }
4676        _ => {}
4677    }
4678}
4679
4680impl LinearFitConditioning {
4681    pub fn from_columns(design: &TermCollectionDesign, selected_cols: &[usize]) -> Self {
4682        const SCALE_EPS: f64 = 1e-12;
4683        let n = design.design.nrows();
4684        let p = design.design.ncols();
4685        let mut columns = Vec::with_capacity(selected_cols.len());
4686        if n == 0 || selected_cols.is_empty() {
4687            return Self {
4688                intercept_idx: design.intercept_range.start,
4689                columns,
4690            };
4691        }
4692        let chunk_rows = gam_linalg::utils::row_chunk_for_byte_budget(n, p);
4693        // Two-pass mean/variance so operator-backed designs don't need to
4694        // materialize the full dense matrix. Pass 1 accumulates per-column
4695        // sums; pass 2 accumulates the sum of squared deviations from the
4696        // pass-1 mean. This matches the original `Σ (x − mean)² / n` formula
4697        // without the catastrophic cancellation of `E[X²] − E[X]²`.
4698        let mut sums = vec![0.0_f64; selected_cols.len()];
4699        for start in (0..n).step_by(chunk_rows) {
4700            let end = (start + chunk_rows).min(n);
4701            let chunk = design
4702                .design
4703                .try_row_chunk(start..end)
4704                .expect("LinearFitConditioning::from_columns row chunk failed");
4705            for (k, &col_idx) in selected_cols.iter().enumerate() {
4706                let column = chunk.column(col_idx);
4707                for &v in column.iter() {
4708                    sums[k] += v;
4709                }
4710            }
4711        }
4712        let inv_n = 1.0_f64 / n as f64;
4713        let means: Vec<f64> = sums.iter().map(|&s| s * inv_n).collect();
4714        let mut sq_devs = vec![0.0_f64; selected_cols.len()];
4715        for start in (0..n).step_by(chunk_rows) {
4716            let end = (start + chunk_rows).min(n);
4717            let chunk = design
4718                .design
4719                .try_row_chunk(start..end)
4720                .expect("LinearFitConditioning::from_columns row chunk failed");
4721            for (k, &col_idx) in selected_cols.iter().enumerate() {
4722                let mean_k = means[k];
4723                let column = chunk.column(col_idx);
4724                for &v in column.iter() {
4725                    let d = v - mean_k;
4726                    sq_devs[k] += d * d;
4727                }
4728            }
4729        }
4730        for (k, &col_idx) in selected_cols.iter().enumerate() {
4731            let mean = means[k];
4732            let var = sq_devs[k] * inv_n;
4733            let (mean, scale) = if var.is_finite() && var > SCALE_EPS * SCALE_EPS {
4734                (mean, var.sqrt())
4735            } else {
4736                // Leave nearly-constant columns untouched; centering them would collapse
4737                // the design column to ~0 and change the model rather than just condition it.
4738                (0.0, 1.0)
4739            };
4740            columns.push(LinearColumnConditioning {
4741                col_idx,
4742                mean,
4743                scale,
4744            });
4745        }
4746        Self {
4747            intercept_idx: design.intercept_range.start,
4748            columns,
4749        }
4750    }
4751
4752    pub fn apply_to_design(&self, design: &Array2<f64>) -> Array2<f64> {
4753        let mut out = design.clone();
4754        for col in &self.columns {
4755            {
4756                let mut dst = out.column_mut(col.col_idx);
4757                dst -= col.mean;
4758            }
4759            if col.scale != 1.0 {
4760                out.column_mut(col.col_idx).mapv_inplace(|v| v / col.scale);
4761            }
4762        }
4763        out
4764    }
4765
4766    fn transform_matrix_columnswith_a(&self, mat: &Array2<f64>) -> Array2<f64> {
4767        let mut out = mat.clone();
4768        let intercept = self.intercept_idx;
4769        for col in &self.columns {
4770            let intercept_col = out.column(intercept).to_owned();
4771            let mut target = out.column_mut(col.col_idx);
4772            target -= &(intercept_col * col.mean);
4773            if col.scale != 1.0 {
4774                target.mapv_inplace(|v| v / col.scale);
4775            }
4776        }
4777        out
4778    }
4779
4780    fn transform_matrixrowswith_a_transpose(&self, mat: &Array2<f64>) -> Array2<f64> {
4781        let mut out = mat.clone();
4782        let intercept = self.intercept_idx;
4783        for col in &self.columns {
4784            let interceptrow = out.row(intercept).to_owned();
4785            let mut target = out.row_mut(col.col_idx);
4786            target -= &(interceptrow * col.mean);
4787            if col.scale != 1.0 {
4788                target.mapv_inplace(|v| v / col.scale);
4789            }
4790        }
4791        out
4792    }
4793
4794    /// Left-multiply `mat_internal` by `M⁻ᵀ` where `M⁻¹[intercept, j] = mean_j`
4795    /// and `M⁻¹[j, j] = scale_j` for each conditioned column. Used together
4796    /// with [`Self::right_multiply_by_m_inv`] to back-transform an internal
4797    /// penalized Hessian to the original coefficient basis.
4798    fn left_multiply_by_m_inv_transpose(&self, mat_internal: &Array2<f64>) -> Array2<f64> {
4799        let mut out = mat_internal.clone();
4800        let intercept = self.intercept_idx;
4801        let interceptrow_snapshot = mat_internal.row(intercept).to_owned();
4802        for col in &self.columns {
4803            if col.scale != 1.0 {
4804                out.row_mut(col.col_idx).mapv_inplace(|v| v * col.scale);
4805            }
4806            if col.mean != 0.0 {
4807                let mut target = out.row_mut(col.col_idx);
4808                target += &(&interceptrow_snapshot * col.mean);
4809            }
4810        }
4811        out
4812    }
4813
4814    /// Right-multiply `mat_internal` by `M⁻¹`. Mirror of
4815    /// [`Self::left_multiply_by_m_inv_transpose`] on columns.
4816    fn right_multiply_by_m_inv(&self, mat_internal: &Array2<f64>) -> Array2<f64> {
4817        let mut out = mat_internal.clone();
4818        let intercept = self.intercept_idx;
4819        let intercept_col_snapshot = mat_internal.column(intercept).to_owned();
4820        for col in &self.columns {
4821            if col.scale != 1.0 {
4822                out.column_mut(col.col_idx).mapv_inplace(|v| v * col.scale);
4823            }
4824            if col.mean != 0.0 {
4825                let mut target = out.column_mut(col.col_idx);
4826                target += &(&intercept_col_snapshot * col.mean);
4827            }
4828        }
4829        out
4830    }
4831
4832    /// Transform blockwise penalties through the conditioning.
4833    ///
4834    /// For block-local penalties whose `col_range` does not overlap with any
4835    /// conditioning column, the transform is identity (the conditioning only
4836    /// affects unpenalized linear columns). In that common case the penalty
4837    /// passes through unchanged, avoiding O(p²) materialization entirely.
4838    pub fn transform_blockwise_penalties_to_internal(
4839        &self,
4840        penalties: &[BlockwisePenalty],
4841        p: usize,
4842    ) -> Vec<crate::penalty_spec::PenaltySpec> {
4843        let conditioning_cols: std::collections::HashSet<usize> =
4844            self.columns.iter().map(|c| c.col_idx).collect();
4845        penalties
4846            .iter()
4847            .map(|bp| {
4848                let overlaps =
4849                    (bp.col_range.start..bp.col_range.end).any(|j| conditioning_cols.contains(&j));
4850                if overlaps {
4851                    // Rare: penalty block overlaps conditioning columns.
4852                    // Fall back to dense transform.
4853                    let global = bp.to_global(p);
4854                    let right = self.transform_matrix_columnswith_a(&global);
4855                    let transformed = self.transform_matrixrowswith_a_transpose(&right);
4856                    crate::penalty_spec::PenaltySpec::Dense(transformed)
4857                } else {
4858                    // Common: smooth penalty block doesn't touch linear columns.
4859                    // The conditioning is identity on this block.
4860                    crate::penalty_spec::PenaltySpec::from_blockwise(bp.clone())
4861                }
4862            })
4863            .collect()
4864    }
4865
4866    pub fn backtransform_beta(&self, beta_internal: &Array1<f64>) -> Array1<f64> {
4867        let mut beta = beta_internal.clone();
4868        let intercept = self.intercept_idx;
4869        for col in &self.columns {
4870            beta[intercept] -= beta_internal[col.col_idx] * col.mean / col.scale;
4871            beta[col.col_idx] = beta_internal[col.col_idx] / col.scale;
4872        }
4873        beta
4874    }
4875
4876    /// `H_orig = M⁻ᵀ · H_int · M⁻¹`, derived from
4877    /// `L_int(β_int) = L_orig(M · β_int)` via the chain rule.
4878    pub fn transform_penalized_hessian_to_original(&self, h_internal: &Array2<f64>) -> Array2<f64> {
4879        let right = self.right_multiply_by_m_inv(h_internal);
4880        self.left_multiply_by_m_inv_transpose(&right)
4881    }
4882
4883    pub fn internal_bounds_for(&self, col_idx: usize, min: f64, max: f64) -> (f64, f64) {
4884        if let Some(col) = self.columns.iter().find(|c| c.col_idx == col_idx) {
4885            (min * col.scale, max * col.scale)
4886        } else {
4887            (min, max)
4888        }
4889    }
4890}
4891
4892pub fn freeze_raw_spatial_metadata(metadata: BasisMetadata, raw_cols: usize) -> BasisMetadata {
4893    match metadata {
4894        BasisMetadata::ThinPlate {
4895            centers,
4896            length_scale,
4897            periodic,
4898            identifiability_transform: None,
4899            input_scales,
4900            radial_reparam,
4901        } => BasisMetadata::ThinPlate {
4902            centers,
4903            length_scale,
4904            periodic,
4905            identifiability_transform: Some(Array2::eye(raw_cols)),
4906            input_scales,
4907            radial_reparam,
4908        },
4909        BasisMetadata::Duchon {
4910            centers,
4911            length_scale,
4912            periodic,
4913            power,
4914            nullspace_order,
4915            identifiability_transform: None,
4916            input_scales,
4917            aniso_log_scales,
4918            operator_collocation_points,
4919            radial_reparam,
4920        } => BasisMetadata::Duchon {
4921            centers,
4922            length_scale,
4923            periodic,
4924            power,
4925            nullspace_order,
4926            identifiability_transform: Some(Array2::eye(raw_cols)),
4927            input_scales,
4928            aniso_log_scales,
4929            operator_collocation_points,
4930            radial_reparam,
4931        },
4932        other => other,
4933    }
4934}
4935
4936pub fn matern_operator_penalty_triplet_from_metadata(
4937    metadata: &BasisMetadata,
4938) -> Result<(Vec<Array2<f64>>, Vec<usize>, Vec<PenaltyInfo>), BasisError> {
4939    let BasisMetadata::Matern {
4940        centers,
4941        length_scale,
4942        periodic,
4943        nu,
4944        include_intercept,
4945        identifiability_transform,
4946        aniso_log_scales,
4947        input_scales,
4948        ..
4949    } = metadata
4950    else {
4951        crate::bail_invalid_basis!("Matérn operator penalties require Matérn metadata");
4952    };
4953    // The metadata records `length_scale` in *original* (un-standardized) data
4954    // coordinates, while `centers` live in the *standardized* coordinate frame
4955    // (per-axis division by `input_scales`). The realized design built the
4956    // kernel against those standardized centers using the σ_geom-compensated
4957    // effective length scale `length_scale / σ_geom`. The collocation operators
4958    // here are evaluated on the same standardized centers, so they must use the
4959    // SAME effective length scale — otherwise the penalty regularizes a
4960    // different RKHS range than the design lives in, leaving rough coefficient
4961    // directions effectively unpenalized. That mismatch is benign in 1-D
4962    // (no standardization) but produces a catastrophic out-of-sample blow-up in
4963    // d ≥ 2 where σ_geom ≠ 1 (#706).
4964    let penalty_length_scale = match input_scales.as_deref() {
4965        Some(scales) => compensate_length_scale_for_standardization(*length_scale, scales),
4966        None => *length_scale,
4967    };
4968    matern_operator_penalty_triplet_at_length_scale(
4969        centers.view(),
4970        periodic.as_deref(),
4971        identifiability_transform.as_ref(),
4972        *nu,
4973        *include_intercept,
4974        aniso_log_scales.as_deref(),
4975        penalty_length_scale,
4976    )
4977}
4978
4979/// Build the canonical Matérn operator-penalty triplet (mass / tension /
4980/// stiffness) at an explicit **effective** length scale — i.e. the
4981/// σ_geom-compensated, standardized-frame scale the design's kernel was built
4982/// against (NOT the original-coordinate `length_scale` stored in metadata).
4983///
4984/// This is the SINGLE source of truth for the Matérn penalty topology. Two
4985/// callers route through it and must therefore stay byte-for-byte consistent:
4986///   * the cold/slow design rebuild (`matern_operator_penalty_triplet_from_metadata`,
4987///     compensating the frozen metadata `length_scale`), and
4988///   * the n-free κ-optimizer re-key (`FrozenTermCollectionIncrementalRealizer::
4989///     canonical_penalties_at_psi`, compensating the trial `ψ → exp(-ψ)` scale).
4990///
4991/// Sharing the body makes the penalty BLOCK COUNT and the per-block numerics
4992/// one deterministic function of `(geometry, ν, η, ℓ_eff)`. The active-operator
4993/// gate is `m = ν + d/2`, which is independent of ℓ, so the block count is
4994/// **ψ-stable by construction**: the re-key can never produce a different number
4995/// of blocks than the frozen design (the desync that #1270 hard-errored on).
4996pub fn matern_operator_penalty_triplet_at_length_scale(
4997    centers: ArrayView2<'_, f64>,
4998    periodic: Option<&[Option<f64>]>,
4999    identifiability_transform: Option<&Array2<f64>>,
5000    nu: crate::basis::MaternNu,
5001    include_intercept: bool,
5002    aniso_log_scales: Option<&[f64]>,
5003    effective_length_scale: f64,
5004) -> Result<(Vec<Array2<f64>>, Vec<usize>, Vec<PenaltyInfo>), BasisError> {
5005    let penalty_centers = crate::basis::expand_periodic_centers(&centers.to_owned(), periodic)?;
5006    let ops = build_matern_collocation_operator_matrices(
5007        penalty_centers.view(),
5008        None,
5009        effective_length_scale,
5010        nu,
5011        include_intercept,
5012        identifiability_transform.map(|z| z.view()),
5013        aniso_log_scales,
5014    )?;
5015    // Gate the operator dials on the Matérn-ν RKHS Sobolev order m = ν + d/2:
5016    // mass (j=0) is always on, tension (j=1) is on for m > 1, stiffness (j=2)
5017    // is on for m > 2. The threshold is strict so the roughest kernel ν=1/2 in
5018    // d=1 (m=1, the exponential/OU H¹ process) sheds both higher operators —
5019    // its kernel already encodes the H¹ control, so adding an extra tension
5020    // dial over-smooths the oscillation it is meant to track (#707). The
5021    // matching gate lives at `DuchonOperatorPenaltySpec::matern_for_smoothness`.
5022    const ORDER_EPS: f64 = 1e-9;
5023    let d = penalty_centers.ncols();
5024    let m = nu.half_integer_value() + 0.5 * d as f64;
5025    let mut candidates = Vec::with_capacity(3);
5026    for (raw, source, min_order) in [
5027        (ops.d0.t().dot(&ops.d0), PenaltySource::OperatorMass, 0.0),
5028        (ops.d1.t().dot(&ops.d1), PenaltySource::OperatorTension, 1.0),
5029        (
5030            ops.d2.t().dot(&ops.d2),
5031            PenaltySource::OperatorStiffness,
5032            2.0,
5033        ),
5034    ] {
5035        if min_order > 0.0 && m <= min_order + ORDER_EPS {
5036            continue;
5037        }
5038        let sym = (&raw + &raw.t()) * 0.5;
5039        let (matrix, normalization_scale) = normalize_penalty_in_constrained_space(&sym);
5040        candidates.push(PenaltyCandidate {
5041            matrix,
5042            nullspace_dim_hint: 0,
5043            source,
5044            normalization_scale,
5045            kronecker_factors: None,
5046            op: None,
5047        });
5048    }
5049    filter_active_penalty_candidates(candidates)
5050}
5051
5052pub fn normalize_penalty_in_constrained_space(matrix: &Array2<f64>) -> (Array2<f64>, f64) {
5053    // Constrained-space normalization:
5054    //   c = ||S_con||_F,  S_tilde = S_con / c.
5055    // This is the only normalization coherent with a REML objective that is
5056    // evaluated entirely in constrained coordinates.
5057    let matrix = (matrix + &matrix.t().to_owned()) * 0.5;
5058    // Clamp noise-floor negative eigenvalues so β'Sβ is non-negative as a contract, not just in exact arithmetic.
5059    let matrix = crate::basis::project_penalty_to_psd_cone(&matrix);
5060    let c = matrix.iter().map(|v| v * v).sum::<f64>().sqrt();
5061    if c.is_finite() && c > 0.0 {
5062        (matrix.mapv(|v| v / c), c)
5063    } else {
5064        (matrix, 1.0)
5065    }
5066}
5067
5068pub fn tensor_product_design_from_sparse_marginals(
5069    marginal_sparse: &[&SparseColMat<usize, f64>],
5070) -> Result<SparseColMat<usize, f64>, BasisError> {
5071    if marginal_sparse.is_empty() {
5072        crate::bail_invalid_basis!("TensorBSpline requires at least one marginal basis");
5073    }
5074    let n = marginal_sparse[0].nrows();
5075    for (i, m) in marginal_sparse.iter().enumerate().skip(1) {
5076        if m.nrows() != n {
5077            crate::bail_dim_basis!(
5078                "tensor sparse marginal row mismatch at dim {i}: expected {n}, got {}",
5079                m.nrows()
5080            );
5081        }
5082    }
5083    let dims: Vec<usize> = marginal_sparse.iter().map(|m| m.ncols()).collect();
5084    let total_cols = dims.iter().try_fold(1usize, |acc, &q| {
5085        acc.checked_mul(q)
5086            .ok_or_else(|| BasisError::DimensionMismatch("tensor basis too large".to_string()))
5087    })?;
5088    let mut strides = vec![1usize; dims.len()];
5089    for d in (0..dims.len().saturating_sub(1)).rev() {
5090        strides[d] = strides[d + 1]
5091            .checked_mul(dims[d + 1])
5092            .ok_or_else(|| BasisError::DimensionMismatch("tensor basis too large".to_string()))?;
5093    }
5094
5095    use faer::sparse::SparseRowMat;
5096    let csrs: Vec<SparseRowMat<usize, f64>> = marginal_sparse
5097        .iter()
5098        .enumerate()
5099        .map(|(d, m)| {
5100            m.as_ref().to_row_major().map_err(|e| {
5101                BasisError::SparseCreation(format!(
5102                    "tensor sparse marginal {d} CSR conversion failed: {e:?}"
5103                ))
5104            })
5105        })
5106        .collect::<Result<Vec<_>, _>>()?;
5107    let row_ptrs: Vec<&[usize]> = csrs.iter().map(|c| c.symbolic().row_ptr()).collect();
5108    let col_idxs: Vec<&[usize]> = csrs.iter().map(|c| c.symbolic().col_idx()).collect();
5109    let vals: Vec<&[f64]> = csrs.iter().map(|c| c.val()).collect();
5110
5111    use rayon::prelude::*;
5112    const CHUNK: usize = 1024;
5113    let num_chunks = n.div_ceil(CHUNK);
5114    let per_chunk: Vec<Vec<Triplet<usize, usize, f64>>> = (0..num_chunks)
5115        .into_par_iter()
5116        .map(|chunk_idx| {
5117            let row_start = chunk_idx * CHUNK;
5118            let row_end = (row_start + CHUNK).min(n);
5119            let mut chunk_triplets = Vec::<Triplet<usize, usize, f64>>::new();
5120            let mut cur_cols = Vec::<usize>::with_capacity(64);
5121            let mut cur_vals = Vec::<f64>::with_capacity(64);
5122            let mut next_cols = Vec::<usize>::with_capacity(64);
5123            let mut next_vals = Vec::<f64>::with_capacity(64);
5124            for i in row_start..row_end {
5125                cur_cols.clear();
5126                cur_vals.clear();
5127                cur_cols.push(0);
5128                cur_vals.push(1.0);
5129                let mut row_is_zero = false;
5130                for d in 0..dims.len() {
5131                    let row_start_d = row_ptrs[d][i];
5132                    let row_end_d = row_ptrs[d][i + 1];
5133                    if row_start_d == row_end_d {
5134                        row_is_zero = true;
5135                        break;
5136                    }
5137                    let stride = strides[d];
5138                    next_cols.clear();
5139                    next_vals.clear();
5140                    next_cols.reserve(cur_cols.len() * (row_end_d - row_start_d));
5141                    next_vals.reserve(cur_vals.len() * (row_end_d - row_start_d));
5142                    for (&prev_col, &prev_val) in cur_cols.iter().zip(cur_vals.iter()) {
5143                        for ptr in row_start_d..row_end_d {
5144                            let cj = col_idxs[d][ptr];
5145                            let vj = vals[d][ptr];
5146                            next_cols.push(prev_col + cj * stride);
5147                            next_vals.push(prev_val * vj);
5148                        }
5149                    }
5150                    std::mem::swap(&mut cur_cols, &mut next_cols);
5151                    std::mem::swap(&mut cur_vals, &mut next_vals);
5152                }
5153                if row_is_zero {
5154                    continue;
5155                }
5156                for (&col, &val) in cur_cols.iter().zip(cur_vals.iter()) {
5157                    chunk_triplets.push(Triplet::new(i, col, val));
5158                }
5159            }
5160            chunk_triplets
5161        })
5162        .collect();
5163    let total_nnz: usize = per_chunk.iter().map(Vec::len).sum();
5164    let mut triplets = Vec::<Triplet<usize, usize, f64>>::with_capacity(total_nnz);
5165    for chunk in per_chunk {
5166        triplets.extend(chunk);
5167    }
5168    SparseColMat::try_new_from_triplets(n, total_cols, &triplets).map_err(|e| {
5169        BasisError::SparseCreation(format!(
5170            "failed to assemble sparse tensor product design: {e:?}"
5171        ))
5172    })
5173}
5174
5175pub fn dense_local_margin_to_sparse(
5176    dense: &Array2<f64>,
5177) -> Result<SparseColMat<usize, f64>, BasisError> {
5178    let expected_row_nnz = dense.ncols().min(4);
5179    let mut triplets =
5180        Vec::<Triplet<usize, usize, f64>>::with_capacity(dense.nrows() * expected_row_nnz);
5181    for ((row, col), &value) in dense.indexed_iter() {
5182        if value != 0.0 {
5183            triplets.push(Triplet::new(row, col, value));
5184        }
5185    }
5186    SparseColMat::try_new_from_triplets(dense.nrows(), dense.ncols(), &triplets).map_err(|e| {
5187        BasisError::SparseCreation(format!(
5188            "failed to convert tensor marginal design to sparse form: {e:?}"
5189        ))
5190    })
5191}
5192
5193pub struct TensorMarginRangeNullProjectors {
5194    range: Array2<f64>,
5195    null: Array2<f64>,
5196}
5197
5198pub fn projector_from_columns(columns: &Array2<f64>, indices: &[usize]) -> Array2<f64> {
5199    if indices.is_empty() {
5200        return Array2::<f64>::zeros((columns.nrows(), columns.nrows()));
5201    }
5202    let basis = columns.select(Axis(1), indices);
5203    basis.dot(&basis.t())
5204}
5205
5206pub fn tensor_margin_range_null_projectors(
5207    normalized_marginal_penalties: &[(Array2<f64>, f64)],
5208) -> Result<Vec<TensorMarginRangeNullProjectors>, BasisError> {
5209    normalized_marginal_penalties
5210        .iter()
5211        .enumerate()
5212        .map(|(dim, (penalty, _))| {
5213            let analysis = crate::basis::analyze_penalty_block(penalty)?;
5214            if analysis.rank == 0 {
5215                crate::bail_invalid_basis!(
5216                    "t2 separable tensor penalty margin {dim} has rank-zero penalty; \
5217                     cannot split penalized and null subspaces"
5218                );
5219            }
5220            let mut range_idx = Vec::<usize>::new();
5221            let mut null_idx = Vec::<usize>::new();
5222            for (idx, &ev) in analysis.eigenvalues.iter().enumerate() {
5223                if ev > analysis.tol {
5224                    range_idx.push(idx);
5225                } else {
5226                    null_idx.push(idx);
5227                }
5228            }
5229            Ok(TensorMarginRangeNullProjectors {
5230                range: projector_from_columns(&analysis.eigenvectors, &range_idx),
5231                null: projector_from_columns(&analysis.eigenvectors, &null_idx),
5232            })
5233        })
5234        .collect()
5235}
5236
5237pub fn build_tensor_bspline_basis(
5238    data: ArrayView2<'_, f64>,
5239    feature_cols: &[usize],
5240    spec: &TensorBSplineSpec,
5241) -> Result<BasisBuildResult, BasisError> {
5242    if feature_cols.is_empty() {
5243        crate::bail_invalid_basis!("TensorBSpline requires at least one feature column");
5244    }
5245    if feature_cols.len() != spec.marginalspecs.len() {
5246        crate::bail_dim_basis!(
5247            "TensorBSpline feature/spec mismatch: feature_cols={}, marginalspecs={}",
5248            feature_cols.len(),
5249            spec.marginalspecs.len()
5250        );
5251    }
5252    if !spec.periods.is_empty() && spec.periods.len() != feature_cols.len() {
5253        crate::bail_dim_basis!(
5254            "TensorBSpline periods length {} does not match feature count {}",
5255            spec.periods.len(),
5256            feature_cols.len()
5257        );
5258    }
5259    let p = data.ncols();
5260    for &c in feature_cols {
5261        if c >= p {
5262            crate::bail_dim_basis!(
5263                "tensor feature column {c} is out of bounds for data with {p} columns"
5264            );
5265        }
5266    }
5267
5268    let mut marginal_knots = Vec::<Array1<f64>>::with_capacity(feature_cols.len());
5269    // Per-margin cr flag (#1074): `true` when the margin is a natural cubic
5270    // regression spline, so the tensor freeze rebuilds the cr knotspec.
5271    let mut marginal_is_cr_flags = Vec::<bool>::with_capacity(feature_cols.len());
5272    let mut marginal_degrees = Vec::<usize>::with_capacity(feature_cols.len());
5273    let mut marginalnum_basis = Vec::<usize>::with_capacity(feature_cols.len());
5274    let mut marginal_penalties = Vec::<Array2<f64>>::with_capacity(feature_cols.len());
5275    let mut marginal_designs = Vec::<Array2<f64>>::with_capacity(feature_cols.len());
5276    // Per-margin effective period: either user-set via `spec.periods` or
5277    // implied by a `PeriodicUniform` marginal knotspec (which the 1D B-spline
5278    // builder realizes as a cyclic B-spline basis).
5279    // Captured here so freeze→reload round-trips both routes back to a
5280    // `PeriodicUniform` marginal knotspec; otherwise a `PeriodicUniform`
5281    // margin specified without `spec.periods` would freeze as a plain
5282    // `Provided(knots)` open spline and lose its wrap-around at predict time.
5283    let mut marginal_effective_periods = Vec::<Option<f64>>::with_capacity(feature_cols.len());
5284    // Per-marginal sparse representation, populated when the 1D builder returned
5285    // a `DesignMatrix::Sparse`. Used to assemble the Khatri-Rao tensor product
5286    // sparsely (only ∏(degree+1) nonzeros per row) instead of densifying to
5287    // shape (n, ∏ q_j) up front. Periodic B-spline margins are local-support
5288    // bases too; when the 1D builder returns them densely, we convert that
5289    // marginal back to sparse form so cylinder/torus tensor products keep the
5290    // same scale behavior as open tensor products.
5291    let mut marginal_sparse =
5292        Vec::<Option<SparseColMat<usize, f64>>>::with_capacity(feature_cols.len());
5293
5294    // Reuse the robust 1D builder to ensure the same knot validation and
5295    // marginal difference-penalty construction as standalone smooth terms.
5296    for (dim, (&col, marginalspec)) in feature_cols
5297        .iter()
5298        .zip(spec.marginalspecs.iter())
5299        .enumerate()
5300    {
5301        // Tensor basis uses raw marginal knot-product columns. Applying 1D
5302        // identifiability constraints here would change marginal penalty sizes
5303        // without changing the tensor design construction, causing dimension
5304        // mismatch. Keep marginal builders unconstrained at this stage.
5305        let mut marginal_unconstrained = marginalspec.clone();
5306        marginal_unconstrained.identifiability = BSplineIdentifiability::None;
5307        let built = build_bspline_basis_1d(data.column(col), &marginal_unconstrained)?;
5308        // A cr (`NaturalCubicRegression`) margin emits `CubicRegression1D`
5309        // metadata whose `knots` are the k value-knots; a B-spline margin emits
5310        // `BSpline1D` with the clamped knot vector. Capture either so the
5311        // tensor freeze can rebuild the exact same marginal knotspec (#1074).
5312        let (knots, marginal_is_cr) = match built.metadata {
5313            BasisMetadata::BSpline1D { knots, .. } => (knots, false),
5314            BasisMetadata::CubicRegression1D { knots, .. } => (knots, true),
5315            _ => {
5316                crate::bail_invalid_basis!(
5317                    "internal TensorBSpline error at dim {dim}: expected BSpline1D or CubicRegression1D metadata"
5318                );
5319            }
5320        };
5321        let metadata_knots = match marginalspec.knotspec {
5322            BSplineKnotSpec::PeriodicUniform {
5323                data_range,
5324                num_basis,
5325            } => Array1::linspace(data_range.0, data_range.1, num_basis),
5326            _ => knots,
5327        };
5328        marginal_knots.push(metadata_knots);
5329        marginal_is_cr_flags.push(marginal_is_cr);
5330        marginal_degrees.push(marginalspec.degree);
5331        marginalnum_basis.push(built.design.ncols());
5332        // Capture the sparse representation of this marginal (when the
5333        // 1D builder produced one) before densifying for the dense
5334        // marginal cache used by `tensor_product_design_from_marginals`
5335        // and `TensorProductDesignOperator`.
5336        let dense_marginal = built.design.to_dense();
5337        let sparse_view: Option<SparseColMat<usize, f64>> = match built.design.as_sparse() {
5338            Some(sd) => {
5339                let inner: &SparseColMat<usize, f64> = sd;
5340                Some(inner.clone())
5341            }
5342            None => match marginalspec.knotspec {
5343                BSplineKnotSpec::PeriodicUniform { .. } => {
5344                    Some(dense_local_margin_to_sparse(&dense_marginal)?)
5345                }
5346                _ => None,
5347            },
5348        };
5349        marginal_sparse.push(sparse_view);
5350        marginal_designs.push(dense_marginal);
5351        marginal_penalties.push(
5352            built
5353                .penalties
5354                .first()
5355                .ok_or_else(|| {
5356                    BasisError::InvalidInput(format!(
5357                        "internal TensorBSpline error at dim {dim}: missing marginal penalty"
5358                    ))
5359                })?
5360                .clone(),
5361        );
5362        built.nullspace_dims.first().ok_or_else(|| {
5363            BasisError::InvalidInput(format!(
5364                "internal TensorBSpline error at dim {dim}: missing marginal nullspace dim"
5365            ))
5366        })?;
5367        // A `PeriodicUniform` marginal knotspec implies the margin is
5368        // wrap-around: the 1D builder already realized it as a periodic
5369        // basis, so the tensor product inherits that periodicity. Record
5370        // the period derived from the knotspec's data range so freeze
5371        // restores `PeriodicUniform` on the marginal — otherwise the
5372        // round-trip downgrades it to `Provided(knots)` (an open spline)
5373        // and predict-time wraps disappear.
5374        let implied_period = match marginalspec.knotspec {
5375            BSplineKnotSpec::PeriodicUniform { data_range, .. } => {
5376                Some(data_range.1 - data_range.0)
5377            }
5378            _ => spec.periods.get(dim).and_then(|p| *p),
5379        };
5380        marginal_effective_periods.push(implied_period);
5381    }
5382
5383    let total_cols: usize = marginalnum_basis.iter().product();
5384    let mut dense_design = (!matches!(spec.identifiability, TensorBSplineIdentifiability::None))
5385        .then(|| tensor_product_design_from_marginals(&marginal_designs))
5386        .transpose()?;
5387    let mut candidates = Vec::<PenaltyCandidate>::with_capacity(
5388        match spec.penalty_decomposition {
5389            TensorBSplinePenaltyDecomposition::MarginalKroneckerSum => marginal_penalties.len(),
5390            TensorBSplinePenaltyDecomposition::Separable => marginal_penalties.len() * 2,
5391        } + if spec.double_penalty { 1 } else { 0 },
5392    );
5393
5394    // Tensor-product smoothing parameters are one-per-margin.  Therefore the
5395    // physical penalty attached to a margin must be normalized in that margin's
5396    // own working coordinates before it is embedded in the full tensor product.
5397    // Normalizing only the already-Kroneckered matrix would fold arbitrary
5398    // dimension-dependent identity factors into the margin's lambda and would
5399    // make anisotropic REML/LAML smoothing depend on the other margins' basis
5400    // sizes rather than on the marginal roughness operator itself.
5401    let normalized_marginal_penalties: Vec<(Array2<f64>, f64)> = marginal_penalties
5402        .iter()
5403        .map(normalize_penalty_in_constrained_space)
5404        .collect();
5405    let mut kronecker_marginal_penalties =
5406        Vec::<Array2<f64>>::with_capacity(normalized_marginal_penalties.len());
5407
5408    match spec.penalty_decomposition {
5409        TensorBSplinePenaltyDecomposition::MarginalKroneckerSum => {
5410            // Accumulate the Kronecker-sum of the per-margin penalties,
5411            // `Σ_dim S_dim`, whose null space is exactly the *joint* null space
5412            // of all marginal penalties — the tensor of marginal polynomial
5413            // null spaces. The tensor double penalty (below) shrinks only this
5414            // joint null, never the already-penalized interaction range.
5415            let mut marginal_kron_sum = Array2::<f64>::zeros((total_cols, total_cols));
5416
5417            for dim in 0..normalized_marginal_penalties.len() {
5418                let mut s_dim = Array2::<f64>::eye(1);
5419                let mut factors = Vec::<Array2<f64>>::with_capacity(marginalnum_basis.len());
5420                for (j, &qj) in marginalnum_basis.iter().enumerate() {
5421                    let factor = if j == dim {
5422                        normalized_marginal_penalties[j].0.clone()
5423                    } else {
5424                        Array2::<f64>::eye(qj)
5425                    };
5426                    factors.push(factor.clone());
5427                    s_dim = kronecker_product(&s_dim, &factor);
5428                }
5429                if dim == kronecker_marginal_penalties.len() {
5430                    kronecker_marginal_penalties.push(normalized_marginal_penalties[dim].0.clone());
5431                }
5432                marginal_kron_sum += &s_dim;
5433
5434                candidates.push(PenaltyCandidate {
5435                    matrix: s_dim,
5436                    nullspace_dim_hint: 0,
5437                    source: PenaltySource::TensorMarginal { dim },
5438                    normalization_scale: normalized_marginal_penalties[dim].1,
5439                    kronecker_factors: Some(factors),
5440                    op: None,
5441                });
5442            }
5443
5444            if spec.double_penalty
5445                && let Some(shrink) =
5446                    crate::basis::build_nullspace_shrinkage_penalty(&marginal_kron_sum)?
5447            {
5448                let (matrix, normalization_scale) =
5449                    normalize_penalty_in_constrained_space(&shrink.sym_penalty);
5450                candidates.push(PenaltyCandidate {
5451                    matrix,
5452                    nullspace_dim_hint: 0,
5453                    source: PenaltySource::TensorGlobalRidge,
5454                    normalization_scale,
5455                    kronecker_factors: None,
5456                    op: None,
5457                });
5458            }
5459        }
5460        TensorBSplinePenaltyDecomposition::Separable => {
5461            let projectors = tensor_margin_range_null_projectors(&normalized_marginal_penalties)?;
5462            let n_masks = 1usize.checked_shl(projectors.len() as u32).ok_or_else(|| {
5463                BasisError::InvalidInput(format!(
5464                    "t2 separable tensor penalty supports at most {} margins, got {}",
5465                    usize::BITS - 1,
5466                    projectors.len()
5467                ))
5468            })?;
5469            for mask in 1..n_masks {
5470                let mut matrix = Array2::<f64>::eye(1);
5471                let mut factors = Vec::<Array2<f64>>::with_capacity(projectors.len());
5472                let mut penalized_margins = Vec::<usize>::new();
5473                for (dim, projector) in projectors.iter().enumerate() {
5474                    let use_range = ((mask >> dim) & 1) == 1;
5475                    let factor = if use_range {
5476                        penalized_margins.push(dim);
5477                        projector.range.clone()
5478                    } else {
5479                        projector.null.clone()
5480                    };
5481                    matrix = kronecker_product(&matrix, &factor);
5482                    factors.push(factor);
5483                }
5484                let (matrix, normalization_scale) = normalize_penalty_in_constrained_space(&matrix);
5485                candidates.push(PenaltyCandidate {
5486                    matrix,
5487                    nullspace_dim_hint: 0,
5488                    source: PenaltySource::TensorSeparable { penalized_margins },
5489                    normalization_scale,
5490                    kronecker_factors: Some(factors),
5491                    op: None,
5492                });
5493            }
5494
5495            if spec.double_penalty {
5496                let mut matrix = Array2::<f64>::eye(1);
5497                let mut factors = Vec::<Array2<f64>>::with_capacity(projectors.len());
5498                for projector in &projectors {
5499                    matrix = kronecker_product(&matrix, &projector.null);
5500                    factors.push(projector.null.clone());
5501                }
5502                let (matrix, normalization_scale) = normalize_penalty_in_constrained_space(&matrix);
5503                candidates.push(PenaltyCandidate {
5504                    matrix,
5505                    nullspace_dim_hint: 0,
5506                    source: PenaltySource::TensorGlobalRidge,
5507                    normalization_scale,
5508                    kronecker_factors: Some(factors),
5509                    op: None,
5510                });
5511            }
5512        }
5513    }
5514
5515    let z_opt = match &spec.identifiability {
5516        TensorBSplineIdentifiability::None => None,
5517        TensorBSplineIdentifiability::SumToZero => {
5518            if total_cols < 2 {
5519                crate::bail_invalid_basis!(
5520                    "TensorBSpline requires at least 2 basis coefficients to enforce sum-to-zero identifiability"
5521                );
5522            }
5523            let dense_design_ref = dense_design.as_ref().ok_or_else(|| {
5524                BasisError::InvalidInput(
5525                    "tensor sum-to-zero identifiability requires a realized basis".to_string(),
5526                )
5527            })?;
5528            let (_, z) = apply_sum_to_zero_constraint(dense_design_ref.view(), None)?;
5529            let gauge = gam_problem::Gauge::sum_to_zero(z);
5530            Some(gauge.block_transform(0))
5531        }
5532        TensorBSplineIdentifiability::MarginalSumToZero => {
5533            // `ti(...)`: drop the marginal main effects by centering every
5534            // margin independently, then form the tensor product of the
5535            // centered margins. Concretely, each margin `j` is reparameterized
5536            // by its own sum-to-zero null basis `Z_j` (so the constant — i.e.
5537            // the marginal intercept — is removed from that axis), and the
5538            // combined reparameterization is the Kronecker product
5539            // `Z = Z₀ ⊗ Z₁ ⊗ … ⊗ Z_{d-1}`. Applying `Z` to the full-tensor
5540            // design `B = B₀ ⊗ … ⊗ B_{d-1}` yields `B Z = (B₀ Z₀) ⊗ … ⊗
5541            // (B_{d-1} Z_{d-1})`, the tensor product of the centered margins,
5542            // which by construction contains no pure main effect.
5543            if marginal_designs.len() < 2 {
5544                crate::bail_invalid_basis!(
5545                    "tensor interaction (ti) identifiability requires at least 2 margins"
5546                );
5547            }
5548            let mut z = Array2::<f64>::eye(1);
5549            for (dim, marginal) in marginal_designs.iter().enumerate() {
5550                if marginal.ncols() < 2 {
5551                    crate::bail_invalid_basis!(
5552                        "tensor interaction (ti) margin {dim} has fewer than 2 basis functions; \
5553                         cannot remove its marginal main effect"
5554                    );
5555                }
5556                let (_, z_dim) = apply_sum_to_zero_constraint(marginal.view(), None)?;
5557                let gauge_dim = gam_problem::Gauge::sum_to_zero(z_dim);
5558                let z_dim = gauge_dim.block_transform(0);
5559                z = kronecker_product(&z, &z_dim);
5560            }
5561            Some(z)
5562        }
5563        TensorBSplineIdentifiability::FrozenTransform { transform } => {
5564            if transform.nrows() != total_cols {
5565                crate::bail_dim_basis!(
5566                    "frozen tensor identifiability transform mismatch: design has {} columns but transform has {} rows",
5567                    total_cols,
5568                    transform.nrows()
5569                );
5570            }
5571            Some(transform.clone())
5572        }
5573    };
5574
5575    if let Some(z) = z_opt.as_ref() {
5576        let gauge = gam_problem::Gauge::from_block_transforms(&[z.clone()]);
5577        let dense = dense_design.as_mut().ok_or_else(|| {
5578            BasisError::InvalidInput(
5579                "tensor identifiability transform requires a realized basis".to_string(),
5580            )
5581        })?;
5582        let restricted_design = gauge.restrict_design(dense);
5583        *dense = restricted_design;
5584        candidates = candidates
5585            .into_iter()
5586            .map(|candidate| -> Result<PenaltyCandidate, BasisError> {
5587                let matrix = gauge.restrict_penalty(&candidate.matrix);
5588                // Re-normalize in the *actual* coefficient chart used by the
5589                // fit.  The tensor sum-to-zero transform is not norm-preserving
5590                // for each overlapping marginal penalty, so carrying the raw
5591                // marginal Frobenius scale into the restricted space changes the
5592                // relative amount of smoothing seen by the LAML/REML optimizer.
5593                // Keep the physical scale in metadata and give the optimizer
5594                // unit-scale constrained penalties for every tensor margin.
5595                let (matrix, c_new) = normalize_penalty_in_constrained_space(&matrix);
5596                Ok(PenaltyCandidate {
5597                    nullspace_dim_hint: candidate.nullspace_dim_hint,
5598                    matrix,
5599                    source: candidate.source,
5600                    normalization_scale: candidate.normalization_scale * c_new,
5601                    // Z^T S Z is no longer a Kronecker product of the original
5602                    // marginal factors, so the Kronecker fast path in construction.rs
5603                    // must not be taken. Clearing kronecker_factors forces the generic
5604                    // block-local eigendecomposition path, which operates on the
5605                    // transformed matrix and is correct.
5606                    kronecker_factors: None,
5607                    op: candidate.op.clone(),
5608                })
5609            })
5610            .collect::<Result<Vec<_>, _>>()?;
5611    }
5612
5613    let (penalties, nullspace_dims, penaltyinfo, null_eigenvectors, ops) =
5614        filter_active_penalty_candidates_with_ops(candidates)?;
5615    let identifiability_is_none =
5616        matches!(spec.identifiability, TensorBSplineIdentifiability::None);
5617    // All marginals expose a sparse representation iff each `marginal_sparse`
5618    // slot is `Some(...)`. Currently this is true when every marginal is a
5619    // free-boundary, non-periodic 1D B-spline returned as
5620    // `DesignMatrix::Sparse` from `build_bspline_basis_1d`. Periodic B-splines
5621    // and other dense-only marginals leave a `None` and trigger the fall-back
5622    // path. Identifiability transforms (`SumToZero`, `FrozenTransform`) make
5623    // the tensor design dense in general, so we also gate on that.
5624    let all_marginals_sparse = marginal_sparse.iter().all(Option::is_some);
5625    let design = if let Some(dense_design) = dense_design {
5626        DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(dense_design))
5627    } else if identifiability_is_none && all_marginals_sparse {
5628        // Sparse Khatri-Rao path: assemble the (n, ∏ q_j) tensor product
5629        // directly as a SparseColMat, preserving the ∏(degree_j+1) nonzero
5630        // structure per row instead of densifying to ∏ q_j columns. This is
5631        // mathematically identical to `tensor_product_design_from_marginals`
5632        // applied to the corresponding dense marginals.
5633        let sparse_marginals: Vec<&SparseColMat<usize, f64>> = marginal_sparse
5634            .iter()
5635            .map(|m| m.as_ref().expect("all_marginals_sparse just verified"))
5636            .collect();
5637        let sparse_design = tensor_product_design_from_sparse_marginals(&sparse_marginals)?;
5638        DesignMatrix::Sparse(gam_linalg::matrix::SparseDesignMatrix::new(sparse_design))
5639    } else {
5640        let marginals: Vec<Arc<Array2<f64>>> = marginal_designs
5641            .iter()
5642            .map(|m| Arc::new(m.clone()))
5643            .collect();
5644        let op = TensorProductDesignOperator::new(marginals).map_err(|e| {
5645            BasisError::InvalidInput(format!("TensorProductDesignOperator build failed: {e}"))
5646        })?;
5647        DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(Arc::new(op)))
5648    };
5649
5650    Ok(BasisBuildResult {
5651        design,
5652        penalties,
5653        nullspace_dims,
5654        penaltyinfo,
5655        ops,
5656        null_eigenvectors,
5657        joint_null_rotation: None,
5658        metadata: BasisMetadata::TensorBSpline {
5659            feature_cols: feature_cols.to_vec(),
5660            knots: marginal_knots,
5661            degrees: marginal_degrees,
5662            // Prefer the per-margin effective period derived in the loop —
5663            // it captures both the explicit `spec.periods` route and the
5664            // implied period from a `PeriodicUniform` marginal knotspec.
5665            // Falling back to `spec.periods` when populated keeps any
5666            // user-supplied explicit period authoritative even if the
5667            // marginal knotspec carried no periodicity hint.
5668            periods: marginal_effective_periods,
5669            is_cr: marginal_is_cr_flags,
5670            identifiability_transform: z_opt,
5671        },
5672        kronecker_factored: if matches!(spec.identifiability, TensorBSplineIdentifiability::None)
5673            && matches!(
5674                spec.penalty_decomposition,
5675                TensorBSplinePenaltyDecomposition::MarginalKroneckerSum
5676            ) {
5677            Some(KroneckerFactoredBasis::new(
5678                marginal_designs,
5679                kronecker_marginal_penalties,
5680                marginalnum_basis.clone(),
5681                spec.double_penalty,
5682            ))
5683        } else {
5684            None
5685        },
5686    })
5687}
5688
5689pub fn tensor_product_design_from_marginals(
5690    marginal_designs: &[Array2<f64>],
5691) -> Result<Array2<f64>, BasisError> {
5692    if marginal_designs.is_empty() {
5693        crate::bail_invalid_basis!("TensorBSpline requires at least one marginal basis");
5694    }
5695    let n = marginal_designs[0].nrows();
5696    for (i, b) in marginal_designs.iter().enumerate().skip(1) {
5697        if b.nrows() != n {
5698            crate::bail_dim_basis!(
5699                "tensor marginal row mismatch at dim {i}: expected {n}, got {}",
5700                b.nrows()
5701            );
5702        }
5703    }
5704    let total_cols = marginal_designs.iter().try_fold(1usize, |acc, b| {
5705        acc.checked_mul(b.ncols())
5706            .ok_or_else(|| BasisError::DimensionMismatch("tensor basis too large".to_string()))
5707    })?;
5708    // Tensor-product Khatri-Rao: design[i, j] = Π_d marginal_d[i, j_d]
5709    // where j is the multi-index (j_1, ..., j_D) flattened. Independent
5710    // across rows; parallelize row chunks and fill the pre-allocated
5711    // contiguous Array2 in place (no Vec-flatten-collect intermediate,
5712    // which doubled the peak memory at large-scale N).
5713    use ndarray::parallel::prelude::*;
5714    use rayon::iter::{IntoParallelIterator, ParallelIterator};
5715    let mut design = Array2::<f64>::zeros((n, total_cols));
5716    design
5717        .axis_chunks_iter_mut(ndarray::Axis(0), 1024)
5718        .into_par_iter()
5719        .enumerate()
5720        .for_each(|(chunk_idx, mut block)| {
5721            let row_offset = chunk_idx * 1024;
5722            // Scratch buffers reused across rows in this chunk.
5723            let mut cur = Vec::<f64>::with_capacity(total_cols);
5724            let mut next = Vec::<f64>::with_capacity(total_cols);
5725            for (local_i, mut out_row) in block.outer_iter_mut().enumerate() {
5726                let i = row_offset + local_i;
5727                cur.clear();
5728                cur.push(1.0);
5729                for b in marginal_designs {
5730                    let q = b.ncols();
5731                    next.clear();
5732                    next.resize(cur.len() * q, 0.0);
5733                    // Hoist the row view out of the inner `col` loop so the
5734                    // q reads per `a_idx` reuse a single contiguous slice
5735                    // instead of recomputing `b[[i, col]]` strides per cell.
5736                    let b_row = b.row(i);
5737                    let b_slice = b_row
5738                        .as_slice()
5739                        .expect("Array2 row from outer_iter is contiguous");
5740                    for (a_idx, &aval) in cur.iter().enumerate() {
5741                        let off = a_idx * q;
5742                        let dst = &mut next[off..off + q];
5743                        for col in 0..q {
5744                            dst[col] = aval * b_slice[col];
5745                        }
5746                    }
5747                    std::mem::swap(&mut cur, &mut next);
5748                }
5749                // `out_row` is a row of the contiguous C-major `design`
5750                // Array2, so it is backed by a contiguous slice. Use a
5751                // bulk slice copy instead of an element-by-element write
5752                // loop.
5753                let out_slice = out_row
5754                    .as_slice_mut()
5755                    .expect("design row is contiguous in C-major Array2");
5756                out_slice.copy_from_slice(&cur);
5757            }
5758        });
5759    Ok(design)
5760}
5761
5762pub fn build_random_effect_block(
5763    data: ArrayView2<'_, f64>,
5764    spec: &RandomEffectTermSpec,
5765) -> Result<RandomEffectBlock, BasisError> {
5766    let n = data.nrows();
5767    let p = data.ncols();
5768    if spec.feature_col >= p {
5769        crate::bail_dim_basis!(
5770            "random-effect term '{}' feature column {} out of bounds for {} columns",
5771            spec.name,
5772            spec.feature_col,
5773            p
5774        );
5775    }
5776
5777    let col = data.column(spec.feature_col);
5778    if col.iter().any(|v| !v.is_finite()) {
5779        crate::bail_invalid_basis!(
5780            "random-effect term '{}' contains non-finite group values",
5781            spec.name
5782        );
5783    }
5784
5785    let kept_levels: Vec<u64> = if let Some(levels) = spec.frozen_levels.as_ref() {
5786        if levels.is_empty() {
5787            crate::bail_invalid_basis!(
5788                "random-effect term '{}' has empty frozen_levels",
5789                spec.name
5790            );
5791        }
5792        levels.clone()
5793    } else {
5794        let mut seen = BTreeSet::<u64>::new();
5795        let mut levels = Vec::<u64>::new();
5796        for &v in col {
5797            let bits = v.to_bits();
5798            if seen.insert(bits) {
5799                levels.push(bits);
5800            }
5801        }
5802        if levels.is_empty() {
5803            crate::bail_invalid_basis!("random-effect term '{}' has no observed levels", spec.name);
5804        }
5805        let start_idx = if spec.drop_first_level && levels.len() > 1 {
5806            1usize
5807        } else {
5808            0usize
5809        };
5810        levels[start_idx..].to_vec()
5811    };
5812
5813    if kept_levels.is_empty() {
5814        crate::bail_invalid_basis!(
5815            "random-effect term '{}' drops all levels; keep at least one level",
5816            spec.name
5817        );
5818    }
5819
5820    let q = kept_levels.len();
5821    let mut level_to_col = BTreeMap::<u64, usize>::new();
5822    for (idx, &bits) in kept_levels.iter().enumerate() {
5823        if level_to_col.insert(bits, idx).is_some() {
5824            crate::bail_invalid_basis!(
5825                "random-effect term '{}' has duplicate frozen level bits {bits}",
5826                spec.name
5827            );
5828        }
5829    }
5830    let mut group_ids = Vec::with_capacity(n);
5831    for &v in col {
5832        let bits = v.to_bits();
5833        group_ids.push(level_to_col.get(&bits).copied());
5834    }
5835
5836    Ok(RandomEffectBlock {
5837        name: spec.name.clone(),
5838        group_ids,
5839        num_groups: q,
5840        kept_levels,
5841    })
5842}
5843
5844impl SmoothDesign {
5845    /// Map an unconstrained term coefficient vector to its constrained shape space.
5846    /// This is useful for nonlinear fits that optimize unconstrained parameters.
5847    pub fn map_term_coefficients(
5848        unconstrained: &Array1<f64>,
5849        shape: ShapeConstraint,
5850    ) -> Result<Array1<f64>, BasisError> {
5851        if unconstrained.is_empty() {
5852            crate::bail_invalid_basis!("unconstrained coefficient vector cannot be empty");
5853        }
5854        let mapped = match shape {
5855            ShapeConstraint::None => unconstrained.clone(),
5856            ShapeConstraint::MonotoneIncreasing => cumulative_exp(unconstrained, 1.0),
5857            ShapeConstraint::MonotoneDecreasing => cumulative_exp(unconstrained, -1.0),
5858            ShapeConstraint::Convex => second_cumulative_exp(unconstrained, 1.0),
5859            ShapeConstraint::Concave => second_cumulative_exp(unconstrained, -1.0),
5860        };
5861        Ok(mapped)
5862    }
5863}
5864
5865pub struct LocalSmoothTermBuild {
5866    pub dim: usize,
5867    pub design: DesignMatrix,
5868    pub penalties: Vec<Array2<f64>>,
5869    pub ops: Vec<Option<std::sync::Arc<dyn crate::analytic_penalties::PenaltyOp>>>,
5870    pub nullspaces: Vec<usize>,
5871    /// Per-active-penalty null-space eigenvector matrices, parallel to
5872    /// `penalties` / `ops` / `nullspaces`. `Some(U_null)` when
5873    /// `nullspaces[k] > 0`, with `U_null` orthonormal columns spanning
5874    /// `null(penalties[k])` in this smooth's local coordinate system; `None`
5875    /// when the active block is full-rank. Stage 1 plumbing; Stage 2
5876    /// consumes this to absorb the smooth's null space into the parametric
5877    /// block at `TermCollectionDesign` construction.
5878    pub null_eigenvectors: Vec<Option<Array2<f64>>>,
5879    /// Joint-null absorption rotation for this smooth. `Some(rotation)`
5880    /// records `Q = [U_range | U_null]` spanning `null(Σ_k penalties[k])`,
5881    /// the joint null across all active penalty blocks on this smooth.
5882    /// `None` means the joint penalty is full-rank (joint nullity = 0) or
5883    /// there are no penalties. Stage-2 commit A: plumbing only — populated
5884    /// by commit B, applied by commit D.
5885    pub joint_null_rotation: Option<crate::basis::JointNullRotation>,
5886    pub penaltyinfo: Vec<PenaltyInfo>,
5887    pub pre_dropped_penaltyinfo: Vec<PenaltyInfo>,
5888    pub metadata: BasisMetadata,
5889    pub linear_constraints: Option<LinearInequalityConstraints>,
5890    pub box_reparam: bool,
5891    pub kronecker_factored: Option<KroneckerFactoredBasis>,
5892}
5893
5894#[derive(Clone)]
5895pub struct PcaScoresMemmapDesignOperator {
5896    mmap: Arc<memmap2::Mmap>,
5897    data_offset: usize,
5898    nrows: usize,
5899    ncols: usize,
5900    chunk_size: usize,
5901}
5902
5903impl PcaScoresMemmapDesignOperator {
5904    fn open(path: PathBuf, chunk_size: usize) -> Result<Self, BasisError> {
5905        let file = File::open(&path).map_err(|err| {
5906            BasisError::InvalidInput(format!(
5907                "failed to open lazy Pca .npy scores '{}': {err}",
5908                path.display()
5909            ))
5910        })?;
5911        // The .npy scores file is read-only training-cache data; this
5912        // module never mutates it. The error path below converts mmap
5913        // failure to a typed `BasisError::InvalidInput`.
5914        // SAFETY: `memmap2::Mmap::map` requires no concurrent writers; the
5915        // contract is held by this module's read-only access pattern.
5916        let mmap = unsafe {
5917            memmap2::Mmap::map(&file).map_err(|err| {
5918                BasisError::InvalidInput(format!(
5919                    "failed to memmap lazy Pca .npy scores '{}': {err}",
5920                    path.display()
5921                ))
5922            })?
5923        };
5924        let (data_offset, nrows, ncols) = parse_f64_2d_npy_header(&mmap, &path)?;
5925        let expected = data_offset
5926            .checked_add(nrows.saturating_mul(ncols).saturating_mul(8))
5927            .ok_or_else(|| {
5928                BasisError::InvalidInput(format!(
5929                    "lazy Pca .npy scores '{}' shape is too large",
5930                    path.display()
5931                ))
5932            })?;
5933        if mmap.len() < expected {
5934            crate::bail_invalid_basis!(
5935                "lazy Pca .npy scores '{}' is truncated: header expects {} bytes, file has {}",
5936                path.display(),
5937                expected,
5938                mmap.len()
5939            );
5940        }
5941        Ok(Self {
5942            mmap: Arc::new(mmap),
5943            data_offset,
5944            nrows,
5945            ncols,
5946            chunk_size: chunk_size.max(1),
5947        })
5948    }
5949
5950    fn value(&self, row: usize, col: usize) -> f64 {
5951        let offset = self.data_offset + (row * self.ncols + col) * 8;
5952        let mut bytes = [0_u8; 8];
5953        bytes.copy_from_slice(&self.mmap[offset..offset + 8]);
5954        f64::from_le_bytes(bytes)
5955    }
5956
5957    fn chunk_rows(&self) -> usize {
5958        self.chunk_size.min(self.nrows.max(1))
5959    }
5960}
5961
5962impl LinearOperator for PcaScoresMemmapDesignOperator {
5963    fn nrows(&self) -> usize {
5964        self.nrows
5965    }
5966
5967    fn ncols(&self) -> usize {
5968        self.ncols
5969    }
5970
5971    fn apply(&self, vector: &Array1<f64>) -> Array1<f64> {
5972        assert_eq!(
5973            vector.len(),
5974            self.ncols,
5975            "lazy Pca apply vector length mismatch"
5976        );
5977        let mut out = Array1::<f64>::zeros(self.nrows);
5978        for start in (0..self.nrows).step_by(self.chunk_rows()) {
5979            let end = (start + self.chunk_rows()).min(self.nrows);
5980            for row in start..end {
5981                let mut acc = 0.0;
5982                for col in 0..self.ncols {
5983                    acc += self.value(row, col) * vector[col];
5984                }
5985                out[row] = acc;
5986            }
5987        }
5988        out
5989    }
5990
5991    fn apply_transpose(&self, vector: &Array1<f64>) -> Array1<f64> {
5992        assert_eq!(
5993            vector.len(),
5994            self.nrows,
5995            "lazy Pca apply_transpose vector length mismatch"
5996        );
5997        let mut out = Array1::<f64>::zeros(self.ncols);
5998        for start in (0..self.nrows).step_by(self.chunk_rows()) {
5999            let end = (start + self.chunk_rows()).min(self.nrows);
6000            for row in start..end {
6001                let scale = vector[row];
6002                if scale == 0.0 {
6003                    continue;
6004                }
6005                for col in 0..self.ncols {
6006                    out[col] += scale * self.value(row, col);
6007                }
6008            }
6009        }
6010        out
6011    }
6012
6013    fn diag_xtw_x(&self, weights: &Array1<f64>) -> Result<Array2<f64>, String> {
6014        if weights.len() != self.nrows {
6015            return Err(format!(
6016                "lazy Pca diag_xtw_x weight length mismatch: weights={}, nrows={}",
6017                weights.len(),
6018                self.nrows
6019            ));
6020        }
6021        let mut gram = Array2::<f64>::zeros((self.ncols, self.ncols));
6022        for start in (0..self.nrows).step_by(self.chunk_rows()) {
6023            let end = (start + self.chunk_rows()).min(self.nrows);
6024            for row in start..end {
6025                let w = weights[row];
6026                if w == 0.0 {
6027                    continue;
6028                }
6029                for a in 0..self.ncols {
6030                    let xa = self.value(row, a);
6031                    if xa == 0.0 {
6032                        continue;
6033                    }
6034                    for b in a..self.ncols {
6035                        gram[[a, b]] += w * xa * self.value(row, b);
6036                    }
6037                }
6038            }
6039        }
6040        for a in 0..self.ncols {
6041            for b in 0..a {
6042                gram[[a, b]] = gram[[b, a]];
6043            }
6044        }
6045        Ok(gram)
6046    }
6047
6048    fn apply_weighted_normal(
6049        &self,
6050        weights: &Array1<f64>,
6051        vector: &Array1<f64>,
6052        penalty: Option<&Array2<f64>>,
6053        ridge: f64,
6054    ) -> Array1<f64> {
6055        assert_eq!(
6056            weights.len(),
6057            self.nrows,
6058            "lazy Pca weighted-normal weight mismatch"
6059        );
6060        assert_eq!(
6061            vector.len(),
6062            self.ncols,
6063            "lazy Pca weighted-normal vector mismatch"
6064        );
6065        let mut out = Array1::<f64>::zeros(self.ncols);
6066        for start in (0..self.nrows).step_by(self.chunk_rows()) {
6067            let end = (start + self.chunk_rows()).min(self.nrows);
6068            for row in start..end {
6069                let w = weights[row].max(0.0);
6070                if w == 0.0 {
6071                    continue;
6072                }
6073                let mut row_dot = 0.0;
6074                for col in 0..self.ncols {
6075                    row_dot += self.value(row, col) * vector[col];
6076                }
6077                if row_dot == 0.0 {
6078                    continue;
6079                }
6080                let scaled = w * row_dot;
6081                for col in 0..self.ncols {
6082                    out[col] += scaled * self.value(row, col);
6083                }
6084            }
6085        }
6086        if let Some(pen) = penalty {
6087            out += &pen.dot(vector);
6088        }
6089        if ridge > 0.0 {
6090            out += &vector.mapv(|x| ridge * x);
6091        }
6092        out
6093    }
6094}
6095
6096impl DenseDesignOperator for PcaScoresMemmapDesignOperator {
6097    fn compute_xtwy(&self, weights: &Array1<f64>, y: &Array1<f64>) -> Result<Array1<f64>, String> {
6098        if weights.len() != self.nrows || y.len() != self.nrows {
6099            return Err(format!(
6100                "lazy Pca compute_xtwy dimension mismatch: weights={}, y={}, nrows={}",
6101                weights.len(),
6102                y.len(),
6103                self.nrows
6104            ));
6105        }
6106        let mut out = Array1::<f64>::zeros(self.ncols);
6107        for start in (0..self.nrows).step_by(self.chunk_rows()) {
6108            let end = (start + self.chunk_rows()).min(self.nrows);
6109            for row in start..end {
6110                let scale = weights[row] * y[row];
6111                if scale == 0.0 {
6112                    continue;
6113                }
6114                for col in 0..self.ncols {
6115                    out[col] += scale * self.value(row, col);
6116                }
6117            }
6118        }
6119        Ok(out)
6120    }
6121
6122    fn row_chunk_into(
6123        &self,
6124        rows: Range<usize>,
6125        mut out: ArrayViewMut2<'_, f64>,
6126    ) -> Result<(), MatrixMaterializationError> {
6127        if rows.end > self.nrows || rows.start > rows.end {
6128            return Err(MatrixMaterializationError::MissingRowChunk {
6129                context: "lazy Pca row range out of bounds",
6130            });
6131        }
6132        if out.nrows() != rows.end - rows.start || out.ncols() != self.ncols {
6133            return Err(MatrixMaterializationError::MissingRowChunk {
6134                context: "lazy Pca row_chunk_into shape mismatch",
6135            });
6136        }
6137        for (local, row) in (rows.start..rows.end).enumerate() {
6138            for col in 0..self.ncols {
6139                out[[local, col]] = self.value(row, col);
6140            }
6141        }
6142        Ok(())
6143    }
6144
6145    fn to_dense(&self) -> Array2<f64> {
6146        let mut out = Array2::<f64>::zeros((self.nrows, self.ncols));
6147        self.row_chunk_into(0..self.nrows, out.view_mut())
6148            .expect("lazy Pca full materialization failed");
6149        out
6150    }
6151}
6152
6153pub fn parse_f64_2d_npy_header(
6154    bytes: &[u8],
6155    path: &PathBuf,
6156) -> Result<(usize, usize, usize), BasisError> {
6157    if bytes.len() < 10 || &bytes[0..6] != b"\x93NUMPY" {
6158        crate::bail_invalid_basis!("lazy Pca scores '{}' is not a .npy file", path.display());
6159    }
6160    let major = bytes[6];
6161    let header_len = match major {
6162        1 => u16::from_le_bytes([bytes[8], bytes[9]]) as usize,
6163        2 | 3 => {
6164            if bytes.len() < 12 {
6165                crate::bail_invalid_basis!(
6166                    "lazy Pca scores '{}' has a truncated .npy header",
6167                    path.display()
6168                );
6169            }
6170            u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]) as usize
6171        }
6172        other => {
6173            crate::bail_invalid_basis!(
6174                "lazy Pca scores '{}' uses unsupported .npy version {}",
6175                path.display(),
6176                other
6177            );
6178        }
6179    };
6180    let header_start = if major == 1 { 10 } else { 12 };
6181    let data_offset = header_start + header_len;
6182    if bytes.len() < data_offset {
6183        crate::bail_invalid_basis!(
6184            "lazy Pca scores '{}' has a truncated .npy header",
6185            path.display()
6186        );
6187    }
6188    let header = std::str::from_utf8(&bytes[header_start..data_offset]).map_err(|err| {
6189        BasisError::InvalidInput(format!(
6190            "lazy Pca scores '{}' has a non-UTF8 .npy header: {err}",
6191            path.display()
6192        ))
6193    })?;
6194    if !(header.contains("'descr': '<f8'")
6195        || header.contains("\"descr\": \"<f8\"")
6196        || header.contains("'descr': '|f8'")
6197        || header.contains("\"descr\": \"|f8\""))
6198    {
6199        crate::bail_invalid_basis!(
6200            "lazy Pca scores '{}' must be float64 little-endian .npy",
6201            path.display()
6202        );
6203    }
6204    if header.contains("True") {
6205        crate::bail_invalid_basis!(
6206            "lazy Pca scores '{}' must be C-contiguous, not Fortran-ordered",
6207            path.display()
6208        );
6209    }
6210    let shape_pos = header.find("shape").ok_or_else(|| {
6211        BasisError::InvalidInput(format!(
6212            "lazy Pca scores '{}' .npy header is missing shape",
6213            path.display()
6214        ))
6215    })?;
6216    let open = header[shape_pos..].find('(').ok_or_else(|| {
6217        BasisError::InvalidInput(format!(
6218            "lazy Pca scores '{}' .npy header has malformed shape",
6219            path.display()
6220        ))
6221    })? + shape_pos;
6222    let close = header[open..].find(')').ok_or_else(|| {
6223        BasisError::InvalidInput(format!(
6224            "lazy Pca scores '{}' .npy header has malformed shape",
6225            path.display()
6226        ))
6227    })? + open;
6228    let dims = header[open + 1..close]
6229        .split(',')
6230        .map(str::trim)
6231        .filter(|part| !part.is_empty())
6232        .map(|part| part.parse::<usize>())
6233        .collect::<Result<Vec<_>, _>>()
6234        .map_err(|err| {
6235            BasisError::InvalidInput(format!(
6236                "lazy Pca scores '{}' .npy shape is not integral: {err}",
6237                path.display()
6238            ))
6239        })?;
6240    if dims.len() != 2 {
6241        crate::bail_invalid_basis!(
6242            "lazy Pca scores '{}' must have shape (N, K), got {:?}",
6243            path.display(),
6244            dims
6245        );
6246    }
6247    Ok((data_offset, dims[0], dims[1]))
6248}
6249
6250pub fn pca_center_mean(x: ArrayView2<'_, f64>) -> Result<Array1<f64>, BasisError> {
6251    if x.nrows() == 0 {
6252        crate::bail_invalid_basis!("Pca basis requires at least one row to compute center mean");
6253    }
6254    let mut mean = Array1::<f64>::zeros(x.ncols());
6255    for row in x.rows() {
6256        mean += &row;
6257    }
6258    mean.mapv_inplace(|v| v / x.nrows() as f64);
6259    Ok(mean)
6260}
6261
6262pub fn build_pca_smooth_basis(
6263    data: ArrayView2<'_, f64>,
6264    feature_cols: &[usize],
6265    basis_matrix: &Array2<f64>,
6266    centered: bool,
6267    smooth_penalty: f64,
6268    center_mean: Option<&Array1<f64>>,
6269    pca_basis_path: Option<&PathBuf>,
6270    chunk_size: usize,
6271) -> Result<BasisBuildResult, BasisError> {
6272    if let Some(path) = pca_basis_path {
6273        let op = PcaScoresMemmapDesignOperator::open(path.clone(), chunk_size)?;
6274        if op.nrows != data.nrows() {
6275            crate::bail_dim_basis!(
6276                "lazy Pca scores row mismatch: .npy has {}, data has {}",
6277                op.nrows,
6278                data.nrows()
6279            );
6280        }
6281        let k = op.ncols;
6282        let mut penalty = Array2::<f64>::eye(k);
6283        penalty.mapv_inplace(|v| v * smooth_penalty);
6284        let (penalties, nullspace_dims, penaltyinfo, null_eigenvectors, ops) =
6285            filter_active_penalty_candidates_with_ops(vec![PenaltyCandidate {
6286                matrix: penalty,
6287                nullspace_dim_hint: 0,
6288                source: PenaltySource::Other("PcaRidge".to_string()),
6289                normalization_scale: 1.0,
6290                kronecker_factors: None,
6291                op: None,
6292            }])?;
6293        return Ok(BasisBuildResult {
6294            design: DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(Arc::new(op))),
6295            penalties,
6296            nullspace_dims,
6297            penaltyinfo,
6298            ops,
6299            null_eigenvectors,
6300            joint_null_rotation: None,
6301            metadata: BasisMetadata::Pca {
6302                feature_cols: feature_cols.to_vec(),
6303                basis_matrix: basis_matrix.clone(),
6304                centered,
6305                smooth_penalty,
6306                center_mean: center_mean.cloned(),
6307                pca_basis_path: Some(path.clone()),
6308                chunk_size: chunk_size.max(1),
6309            },
6310            kronecker_factored: None,
6311        });
6312    }
6313    if basis_matrix.nrows() != feature_cols.len() {
6314        crate::bail_dim_basis!(
6315            "Pca basis row mismatch: basis rows={}, feature columns={}",
6316            basis_matrix.nrows(),
6317            feature_cols.len()
6318        );
6319    }
6320    let mut x = select_columns(data, feature_cols)?;
6321    let mean = if centered {
6322        match center_mean {
6323            Some(mean) => mean.clone(),
6324            None => pca_center_mean(x.view())?,
6325        }
6326    } else {
6327        Array1::<f64>::zeros(feature_cols.len())
6328    };
6329    if centered {
6330        for mut row in x.rows_mut() {
6331            row -= &mean;
6332        }
6333    }
6334    let design = fast_ab(&x, basis_matrix);
6335    let k = basis_matrix.ncols();
6336    let mut penalty = Array2::<f64>::eye(k);
6337    penalty.mapv_inplace(|v| v * smooth_penalty);
6338    let (penalties, nullspace_dims, penaltyinfo, null_eigenvectors, ops) =
6339        filter_active_penalty_candidates_with_ops(vec![PenaltyCandidate {
6340            matrix: penalty,
6341            nullspace_dim_hint: 0,
6342            source: PenaltySource::Other("PcaRidge".to_string()),
6343            normalization_scale: 1.0,
6344            kronecker_factors: None,
6345            op: None,
6346        }])?;
6347    Ok(BasisBuildResult {
6348        design: DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(design)),
6349        penalties,
6350        nullspace_dims,
6351        penaltyinfo,
6352        ops,
6353        null_eigenvectors,
6354        joint_null_rotation: None,
6355        metadata: BasisMetadata::Pca {
6356            feature_cols: feature_cols.to_vec(),
6357            basis_matrix: basis_matrix.clone(),
6358            centered,
6359            smooth_penalty,
6360            center_mean: centered.then_some(mean),
6361            pca_basis_path: None,
6362            chunk_size: chunk_size.max(1),
6363        },
6364        kronecker_factored: None,
6365    })
6366}
6367
6368/// A factor-level `by=` wrapper owns the model-space centering of its inner
6369/// smooth: it gates the raw/structurally-constrained basis to the level rows
6370/// and then centers that gated block exactly once against the level indicator
6371/// (`build_parametric_constraint_block_for_term` in `design_construction`).
6372/// Leaving the inner B-spline's default pooled weighted-sum-to-zero active here
6373/// would impose two generically-independent constraints — the pooled column
6374/// moment `m = Σ_h m_h` and the per-level moment `m_g` — so a raw `k`-column
6375/// basis collapses to `k-2` columns per level instead of `k-1`, deleting one
6376/// genuine nonconstant spline direction *before REML runs* (#1427). The group
6377/// main effect carries only the constant, so it cannot restore that direction.
6378///
6379/// Only the *default model-space* centering is deferred. Explicit structural or
6380/// frozen transforms (`RemoveLinearTrend`, `OrthogonalToDesignColumns`,
6381/// `FrozenTransform`, `None`) are user/structural choices and are preserved
6382/// verbatim.
6383pub fn defer_inner_model_centering_to_factor_level_wrapper(basis: &mut SmoothBasisSpec) {
6384    if let SmoothBasisSpec::BSpline1D { spec, .. } = basis
6385        && matches!(
6386            spec.identifiability,
6387            BSplineIdentifiability::WeightedSumToZero { .. }
6388        )
6389    {
6390        spec.identifiability = BSplineIdentifiability::None;
6391    }
6392}
6393
6394pub fn apply_by_variable_to_local_build(
6395    mut built: LocalSmoothTermBuild,
6396    data: ArrayView2<'_, f64>,
6397    by_col: usize,
6398    by: &ByVariableSpec,
6399    term_name: &str,
6400) -> Result<LocalSmoothTermBuild, BasisError> {
6401    if by_col >= data.ncols() {
6402        crate::bail_dim_basis!(
6403            "by-variable smooth term '{term_name}' references column {by_col}, but data has {} columns",
6404            data.ncols()
6405        );
6406    }
6407    let weights = match by {
6408        ByVariableSpec::Numeric => data.column(by_col).to_owned(),
6409        ByVariableSpec::Level { value_bits, .. } => data.column(by_col).mapv(|value| {
6410            if value.to_bits() == *value_bits {
6411                1.0
6412            } else {
6413                0.0
6414            }
6415        }),
6416    };
6417    if weights.iter().any(|value| !value.is_finite()) {
6418        crate::bail_invalid_basis!(
6419            "by-variable smooth term '{term_name}' has non-finite by-column values"
6420        );
6421    }
6422
6423    let mut dense = built
6424        .design
6425        .try_to_dense_by_chunks("by-variable smooth row gating")
6426        .map_err(BasisError::InvalidInput)?;
6427    for (mut row, &weight) in dense.rows_mut().into_iter().zip(weights.iter()) {
6428        row.mapv_inplace(|value| value * weight);
6429    }
6430    built.design = DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(dense));
6431    built.kronecker_factored = None;
6432    Ok(built)
6433}
6434
6435/// Build the local smooth term for a `BySmooth` spec, which unifies numeric-by
6436/// and factor-by modulation into a single `SmoothTermSpec`.
6437///
6438/// For a **numeric** by-variable the inner smooth is built once and every row
6439/// is multiplied by the by-column value (identical to `ByVariable::Numeric`).
6440///
6441/// For a **factor** by-variable the inner smooth is built once and gated per
6442/// level into side-by-side column blocks, producing a `n × (L * p)` design
6443/// matrix.  The penalties are block-diagonalised (one copy of the inner penalty
6444/// per level) exactly as `build_factor_smooth` does for `bs="fs"/"sz"`.
6445pub fn build_by_smooth_local(
6446    data: ArrayView2<'_, f64>,
6447    term: &SmoothTermSpec,
6448    smooth: &SmoothBasisSpec,
6449    by_kind: &ByVarKind,
6450    workspace: &mut crate::basis::BasisWorkspace,
6451) -> Result<LocalSmoothTermBuild, BasisError> {
6452    let inner_term = SmoothTermSpec {
6453        name: term.name.clone(),
6454        basis: (*smooth).clone(),
6455        shape: term.shape,
6456        joint_null_rotation: None,
6457    };
6458    let inner = build_single_local_smooth_term(data, &inner_term, workspace)?;
6459
6460    match by_kind {
6461        ByVarKind::Numeric { feature_col } => {
6462            let inner_meta = inner.metadata.clone();
6463            let mut built = apply_by_variable_to_local_build(
6464                inner,
6465                data,
6466                *feature_col,
6467                &ByVariableSpec::Numeric,
6468                &term.name,
6469            )?;
6470            built.metadata = BasisMetadata::BySmooth {
6471                inner: Box::new(inner_meta),
6472                by_col: *feature_col,
6473                levels: None,
6474                ordered: false,
6475            };
6476            Ok(built)
6477        }
6478        ByVarKind::Factor {
6479            feature_col,
6480            frozen_levels,
6481            ordered,
6482        } => {
6483            // Collect factor levels: prefer the frozen set (replay path), else
6484            // scan the data column (first-fit path).
6485            let level_bits: Vec<u64> = if let Some(fl) = frozen_levels {
6486                fl.clone()
6487            } else {
6488                let col = data.column(*feature_col);
6489                let mut seen = BTreeSet::<u64>::new();
6490                for &v in col.iter() {
6491                    if v.is_finite() {
6492                        seen.insert(v.to_bits());
6493                    }
6494                }
6495                seen.into_iter().collect()
6496            };
6497            let n_levels = level_bits.len();
6498            if n_levels == 0 {
6499                crate::bail_invalid_basis!(
6500                    "by-factor smooth term '{}': factor column {} has no observed levels",
6501                    term.name,
6502                    feature_col
6503                );
6504            }
6505            let p = inner.dim;
6506            let q = n_levels * p;
6507            let n = data.nrows();
6508
6509            let inner_dense = inner
6510                .design
6511                .try_to_dense_by_chunks("by-factor smooth design gating")
6512                .map_err(BasisError::InvalidInput)?;
6513
6514            // Gate each level into its own p-wide column block.
6515            let mut combined = Array2::<f64>::zeros((n, q));
6516            for (lvl_idx, &bits) in level_bits.iter().enumerate() {
6517                let col_start = lvl_idx * p;
6518                for row in 0..n {
6519                    if data[[row, *feature_col]].to_bits() == bits {
6520                        combined
6521                            .slice_mut(s![row, col_start..col_start + p])
6522                            .assign(&inner_dense.row(row));
6523                    }
6524                }
6525            }
6526
6527            // Build per-level INDEPENDENT penalties (#1427): one copy of each
6528            // inner penalty per level, but each confined to that single level's
6529            // diagonal block, so every (level, inner-penalty) pair is its OWN
6530            // smoothing-parameter coordinate. `s(x, by=g)` selects the per-group
6531            // curve wiggliness independently — the design is block-diagonal and
6532            // block-separable, so a correct REML must reproduce gamfit's own
6533            // independent per-group fits. Tiling a single inner penalty across
6534            // every level (as the `bs="fs"` shared-λ random-effect construction
6535            // does) collapses all groups onto ONE λ, which cannot match uneven
6536            // per-level smoothness and degrades as data grows (under-recovery up
6537            // to ~16× at n=2000). Emit `n_levels * n_penalties` blocks instead.
6538            let inner_meta = inner.metadata.clone();
6539            let n_penalties = inner.penalties.len();
6540            let n_blocks = n_penalties.saturating_mul(n_levels);
6541            let mut penalties = Vec::<Array2<f64>>::with_capacity(n_blocks);
6542            let mut penaltyinfo = Vec::<PenaltyInfo>::with_capacity(n_blocks);
6543            let mut nullspaces = Vec::<usize>::with_capacity(n_blocks);
6544            for (pen_pos, s_inner) in inner.penalties.iter().enumerate() {
6545                for lvl in 0..n_levels {
6546                    let off = lvl * p;
6547                    let mut s_big = Array2::<f64>::zeros((q, q));
6548                    s_big
6549                        .slice_mut(s![off..off + p, off..off + p])
6550                        .assign(s_inner);
6551                    let (s_big, scale) = normalize_penalty_in_constrained_space(&s_big);
6552                    let mut info = inner.penaltyinfo[pen_pos].clone();
6553                    // Distinct original_index per (penalty, level) so each λ is a
6554                    // separate identifiable coordinate downstream.
6555                    info.original_index = pen_pos * n_levels + lvl;
6556                    info.normalization_scale *= scale;
6557                    // Each block now spans exactly ONE level → per-level nullity,
6558                    // not the tiled (× n_levels) hint of the shared construction.
6559                    info.kronecker_factors = None;
6560                    penalties.push(s_big);
6561                    penaltyinfo.push(info);
6562                    nullspaces.push(inner.nullspaces[pen_pos]);
6563                }
6564            }
6565
6566            let null_eigenvectors = vec![None; penalties.len()];
6567            let ops = vec![None; penalties.len()];
6568
6569            Ok(LocalSmoothTermBuild {
6570                dim: q,
6571                design: DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(combined)),
6572                penalties,
6573                ops,
6574                nullspaces,
6575                null_eigenvectors,
6576                joint_null_rotation: None,
6577                penaltyinfo,
6578                pre_dropped_penaltyinfo: inner.pre_dropped_penaltyinfo,
6579                metadata: BasisMetadata::BySmooth {
6580                    inner: Box::new(inner_meta),
6581                    by_col: *feature_col,
6582                    levels: Some(level_bits),
6583                    ordered: *ordered,
6584                },
6585                linear_constraints: None,
6586                box_reparam: false,
6587                kronecker_factored: None,
6588            })
6589        }
6590    }
6591}
6592
6593pub fn ensure_by_variable_specs_match(
6594    kind: &BySmoothKind,
6595    by: &ByVariableSpec,
6596    term_name: &str,
6597) -> Result<(), BasisError> {
6598    match (kind, by) {
6599        (BySmoothKind::Numeric, ByVariableSpec::Numeric) => Ok(()),
6600        (BySmoothKind::Level { level_bits }, ByVariableSpec::Level { value_bits, .. })
6601            if level_bits == value_bits =>
6602        {
6603            Ok(())
6604        }
6605        _ => Err(BasisError::InvalidInput(format!(
6606            "by-variable smooth term '{term_name}' has inconsistent by-variable specifications"
6607        ))),
6608    }
6609}
6610
6611/// Build a factor-smooth interaction basis (`bs="fs"`/`"sz"`/`"re"`).
6612///
6613/// A factor smooth replicates a shared marginal smooth in the continuous
6614/// covariate(s) once per level of a grouping factor, coupling all level blocks
6615/// through a *single* set of smoothing parameters (one per marginal penalty).
6616/// This is mgcv's `smooth.construct.fs.smooth.spec` realization and the
6617/// random-effect interpretation of a smooth: the per-level deviations are an
6618/// exchangeable family whose joint wiggliness/shrinkage is governed by the
6619/// shared λ, so the construction scales to many levels with a fixed parameter
6620/// count.
6621///
6622/// Flavours:
6623/// * `Fs` — full random factor-smooth. The marginal carries its wiggliness
6624///   penalty *and* a null-space ridge (double penalty), so the replicated
6625///   design is a proper full-rank random effect: each level's curve is shrunk
6626///   toward zero (intercept + linear trend included), recovering the mgcv
6627///   `bs="fs"` penalty structure `I_L ⊗ S_j` for every marginal penalty `S_j`.
6628/// * `Sz` — sum-to-zero factor smooth. Delegates to the existing
6629///   [`SmoothBasisSpec::FactorSumToZero`] construction (`L-1` deviation blocks,
6630///   coefficient-wise zero sum across levels).
6631/// * `Re` — pure random effect / random slope (`bs="re"`). A degree-1 marginal
6632///   gives the per-level `[1, x]` span; the penalty is the identity over each
6633///   level block (iid Gaussian coefficients), matching mgcv's `bs="re"` ridge.
6634///
6635/// The grouping levels are resolved once at fit time (sorted unique bit
6636/// patterns of the factor column) and frozen into the returned metadata so the
6637/// predict-time rebuild evaluates every row against its own level's block.
6638pub fn build_factor_smooth(
6639    data: ArrayView2<'_, f64>,
6640    spec: &FactorSmoothSpec,
6641    term_name: &str,
6642    workspace: &mut crate::basis::BasisWorkspace,
6643) -> Result<LocalSmoothTermBuild, BasisError> {
6644    if spec.continuous_cols.len() != 1 {
6645        crate::bail_invalid_basis!(
6646            "factor smooth term '{}' currently supports exactly one continuous covariate; found {}",
6647            term_name,
6648            spec.continuous_cols.len()
6649        );
6650    }
6651    let feature_col = spec.continuous_cols[0];
6652    let group_col = spec.group_col;
6653    if feature_col >= data.ncols() || group_col >= data.ncols() {
6654        crate::bail_dim_basis!(
6655            "factor smooth term '{}' references columns ({}, {}) out of bounds for {} columns",
6656            term_name,
6657            feature_col,
6658            group_col,
6659            data.ncols()
6660        );
6661    }
6662
6663    // `Sz` is exactly the existing sum-to-zero factor smooth: reuse it verbatim
6664    // so there is a single source of truth for the zero-sum construction.
6665    if matches!(spec.flavour, FactorSmoothFlavour::Sz) {
6666        let levels = resolve_factor_smooth_levels(data, group_col, spec, term_name)?;
6667        let inner = SmoothBasisSpec::BSpline1D {
6668            feature_col,
6669            spec: factor_smooth_marginal_for_replay(&spec.marginal),
6670        };
6671        let sz_term = SmoothTermSpec {
6672            name: term_name.to_string(),
6673            basis: SmoothBasisSpec::FactorSumToZero {
6674                inner: Box::new(inner),
6675                by_col: group_col,
6676                levels: levels.clone(),
6677                frozen_global_orthogonality: None,
6678            },
6679            shape: ShapeConstraint::None,
6680            joint_null_rotation: None,
6681        };
6682        let mut built = build_single_local_smooth_term(data, &sz_term, workspace)?;
6683        // The delegated `FactorSumToZero` build returns the BARE inner B-spline
6684        // metadata (`BasisMetadata::BSpline1D`), but the term that owns this
6685        // build carries a `SmoothBasisSpec::FactorSmooth { Sz }` spec. Two
6686        // things break if we hand that mismatched pair downstream:
6687        //   1. `freeze_smooth_basis_from_metadata` matches on (spec, metadata)
6688        //      and has no `(FactorSmooth, BSpline1D)` arm, so any refit / spatial
6689        //      re-optimization that freezes the basis aborts with a "smooth
6690        //      metadata/spec type mismatch" error.
6691        //   2. The bare B-spline metadata carries no grouping levels, so a
6692        //      predict-time rebuild cannot replay the SAME replicated design.
6693        // Re-wrap the marginal geometry as `FactorSmooth` metadata exactly as
6694        // the Fs/Re path below does, giving all three factor-smooth flavours a
6695        // single, freeze-consistent metadata shape that also pins the levels.
6696        // Since #1605 the sz marginal is ALWAYS the penalized B-spline the `fs`
6697        // sibling uses (a natural cubic regression marginal hard-enforces f''=0
6698        // at the boundary and cannot represent curved deviations — a consistency
6699        // failure). The `CubicRegression1D` arm below is therefore unreachable on
6700        // a freshly-built sz spec; it is retained only as defense / backward
6701        // compatibility for a frozen spec that still carries a cr marginal, so
6702        // the predict-time freeze restores whatever marginal class it finds.
6703        let (knots, degree, periodic, marginal_is_cr) = match &built.metadata {
6704            BasisMetadata::BSpline1D {
6705                knots,
6706                periodic,
6707                degree,
6708                ..
6709            } => (
6710                knots.clone(),
6711                degree.unwrap_or(spec.marginal.degree),
6712                *periodic,
6713                false,
6714            ),
6715            BasisMetadata::CubicRegression1D { knots, .. } => {
6716                (knots.clone(), spec.marginal.degree, None, true)
6717            }
6718            other => {
6719                crate::bail_invalid_basis!(
6720                    "sz factor smooth term '{}' produced an unexpected marginal metadata variant {:?}",
6721                    term_name,
6722                    other
6723                );
6724            }
6725        };
6726        built.metadata = BasisMetadata::FactorSmooth {
6727            continuous_cols: spec.continuous_cols.clone(),
6728            group_col,
6729            knots,
6730            degree,
6731            periodic,
6732            group_levels: levels,
6733            flavour: "sz".to_string(),
6734            marginal_is_cr,
6735        };
6736        return Ok(built);
6737    }
6738
6739    let levels = resolve_factor_smooth_levels(data, group_col, spec, term_name)?;
6740    let n_levels = levels.len();
6741    if n_levels < 2 {
6742        crate::bail_invalid_basis!(
6743            "factor smooth term '{}' requires at least two grouping levels; found {}",
6744            term_name,
6745            n_levels
6746        );
6747    }
6748
6749    // `Fs` (order ≥ 1, the default) is the random-effect flavour: it penalizes
6750    // each null-space dimension of the marginal wiggliness penalty separately
6751    // below (mgcv's `bs="fs"` construction). That replaces the marginal's single
6752    // *combined* double penalty, so disable the latter here to avoid penalizing
6753    // the null space twice (once combined, once per dimension). The explicit
6754    // `m=0` opt-out keeps the legacy combined double penalty and adds no
6755    // per-dimension penalties.
6756    let use_per_dim_null = matches!(
6757        &spec.flavour,
6758        FactorSmoothFlavour::Fs { m_null_penalty_orders }
6759            if m_null_penalty_orders.iter().copied().max().unwrap_or(0) >= 1
6760    );
6761
6762    // Build the shared marginal design + penalties from the 1-D B-spline.
6763    // `Re` forces a degree-1 marginal (linear span) and replaces the marginal
6764    // wiggliness with an identity ridge below; `Fs` keeps the user's marginal
6765    // (cubic by default) and, under the per-dimension null path, gets its null
6766    // space penalized one dimension at a time after replication.
6767    let mut marginal_spec = factor_smooth_marginal_for_replay(&spec.marginal);
6768    if use_per_dim_null {
6769        marginal_spec.double_penalty = false;
6770    }
6771    let inner_term = SmoothTermSpec {
6772        name: format!("{term_name}::marginal"),
6773        basis: SmoothBasisSpec::BSpline1D {
6774            feature_col,
6775            spec: marginal_spec,
6776        },
6777        shape: ShapeConstraint::None,
6778        joint_null_rotation: None,
6779    };
6780    let inner = build_single_local_smooth_term(data, &inner_term, workspace)?;
6781    let mut base = inner
6782        .design
6783        .try_to_dense_by_chunks("factor smooth marginal")
6784        .map_err(BasisError::InvalidInput)?;
6785    if matches!(spec.flavour, FactorSmoothFlavour::Re) {
6786        // `bs="re"` is a parametric random intercept+slope, not a B-spline
6787        // smooth evaluated through clamped knot support.  A degree-1 B-spline
6788        // with no internal knots spans the training rows, but outside the
6789        // boundary knots its basis is not the model matrix for `(1 + x | g)`;
6790        // held-out extrapolation then loses the random slope contribution.
6791        // Build the random-effect marginal directly as `[1, x - c]`, centered
6792        // at the frozen marginal domain, so fit-time and replay-time rows use
6793        // the same well-conditioned parametric columns on and off the training
6794        // interval.
6795        let center = match &inner.metadata {
6796            BasisMetadata::BSpline1D { knots, .. } if !knots.is_empty() => {
6797                0.5 * (knots[0] + knots[knots.len() - 1])
6798            }
6799            _ => 0.0,
6800        };
6801        let mut linear = Array2::<f64>::ones((data.nrows(), 2));
6802        linear
6803            .column_mut(1)
6804            .assign(&data.column(feature_col).mapv(|x| x - center));
6805        base = linear;
6806    }
6807    let n = base.nrows();
6808    let p = base.ncols();
6809    let q = p * n_levels;
6810
6811    // Block-diagonal replicated design: row i contributes its marginal row to
6812    // the column block owned by its grouping level, zeros elsewhere.
6813    let mut dense = Array2::<f64>::zeros((n, q));
6814    for i in 0..n {
6815        let bits = data[[i, group_col]].to_bits();
6816        let level_idx = levels.iter().position(|b| *b == bits).ok_or_else(|| {
6817            BasisError::InvalidInput(format!(
6818                "factor smooth term '{term_name}' saw an unseen grouping level at row {}",
6819                i + 1
6820            ))
6821        })?;
6822        let start = level_idx * p;
6823        dense
6824            .slice_mut(s![i, start..start + p])
6825            .assign(&base.row(i));
6826    }
6827
6828    // Penalties: replicate each marginal penalty into a block-diagonal
6829    // `I_L ⊗ S_j` so every level shares the same smoothing parameter λ_j (one
6830    // λ per marginal penalty), the defining feature of a factor smooth. For
6831    // `Re` the marginal penalty is replaced by one ridge per parametric
6832    // coordinate so intercept and slope variances can be learned separately.
6833    let marginal_penalties: Vec<Array2<f64>> = if matches!(spec.flavour, FactorSmoothFlavour::Re) {
6834        (0..p)
6835            .map(|j| {
6836                let mut s = Array2::<f64>::zeros((p, p));
6837                s[[j, j]] = 1.0;
6838                s
6839            })
6840            .collect()
6841    } else {
6842        inner.penalties.clone()
6843    };
6844    let marginal_penaltyinfo: Vec<PenaltyInfo> = if matches!(spec.flavour, FactorSmoothFlavour::Re)
6845    {
6846        (0..p)
6847            .map(|j| PenaltyInfo {
6848                source: PenaltySource::Primary,
6849                original_index: j,
6850                active: true,
6851                effective_rank: 1,
6852                dropped_reason: None,
6853                nullspace_dim_hint: p.saturating_sub(1),
6854                normalization_scale: 1.0,
6855                kronecker_factors: None,
6856            })
6857            .collect()
6858    } else {
6859        inner.penaltyinfo.clone()
6860    };
6861    if marginal_penalties.len() != marginal_penaltyinfo.len() {
6862        crate::bail_invalid_basis!(
6863            "internal factor-smooth penalty metadata mismatch for term '{}': penalties={}, infos={}",
6864            term_name,
6865            marginal_penalties.len(),
6866            marginal_penaltyinfo.len()
6867        );
6868    }
6869
6870    let mut penalties = Vec::<Array2<f64>>::with_capacity(marginal_penalties.len());
6871    let mut penaltyinfo = Vec::<PenaltyInfo>::with_capacity(marginal_penalties.len());
6872    for (penalty_pos, s_inner) in marginal_penalties.iter().enumerate() {
6873        let mut s_big = Array2::<f64>::zeros((q, q));
6874        for level in 0..n_levels {
6875            let start = level * p;
6876            s_big
6877                .slice_mut(s![start..start + p, start..start + p])
6878                .assign(s_inner);
6879        }
6880        let (s_big, factor_smooth_scale) = normalize_penalty_in_constrained_space(&s_big);
6881        let mut info = marginal_penaltyinfo[penalty_pos].clone();
6882        info.original_index = penalty_pos;
6883        info.normalization_scale *= factor_smooth_scale;
6884        info.nullspace_dim_hint = info.nullspace_dim_hint.saturating_mul(n_levels);
6885        info.kronecker_factors = None;
6886        penalties.push(s_big);
6887        penaltyinfo.push(info);
6888    }
6889
6890    let mut nullspaces: Vec<usize> = if matches!(spec.flavour, FactorSmoothFlavour::Re) {
6891        vec![q.saturating_sub(n_levels); p]
6892    } else {
6893        inner
6894            .nullspaces
6895            .iter()
6896            .map(|ns| ns.saturating_mul(n_levels))
6897            .collect()
6898    };
6899
6900    // `Fs` is the random-effect flavour of a smooth: the per-group curve is an
6901    // exchangeable Gaussian *function*, so EVERY coefficient — including the
6902    // {const, linear} null space of the marginal wiggliness penalty — must be
6903    // shrinkable toward zero under its own shared variance. The wiggliness
6904    // penalty `S_wiggle` shapes curvature but leaves the per-group intercept and
6905    // slope (its null space) completely UNPENALIZED. With the null space free,
6906    // each group fits its own intercept and slope with NO partial pooling, so
6907    // the held-out per-subject forecast inherits the full no-pooling variance
6908    // and curves away from the true per-group line (gam#712 real arm, gam#713;
6909    // gam#903 sleepstudy forecast ran ~74% over the lme4 BLUP bar).
6910    //
6911    // mgcv's `bs="fs"` fixes this by penalizing each null-space dimension
6912    // SEPARATELY (`smooth.construct.fs.smooth.spec` adds one rank-1 penalty per
6913    // null coordinate), each replicated block-diagonally across levels under a
6914    // single shared smoothing parameter — so REML fits a distinct
6915    // random-intercept variance and random-slope variance, the partial pooling
6916    // that makes the forecast track lme4's correlated random-effect BLUP. A
6917    // single *combined* null penalty (one λ for intercept+slope together) cannot
6918    // express the typically very different intercept and slope variances, which
6919    // is the residual forecast gap. We mirror mgcv exactly: for each orthonormal
6920    // null direction `z_k` of the marginal wiggliness penalty add
6921    // `I_L ⊗ (z_k z_kᵀ)` as its own penalty. The marginal's combined double
6922    // penalty was disabled above, so the null space is penalized once, per
6923    // dimension. With linear data REML drives the curvature λ up and degrades
6924    // `fs` to a linear random slope (edf → ≈2/group); with genuine curvature the
6925    // wiggliness λ stays small and the wiggle survives (data-adaptive, not a
6926    // cap). Gated by `m_null_penalty_orders`: order ≥ 1 (default) enables the
6927    // per-dimension null penalties; `m=0` keeps the legacy combined double
6928    // penalty and adds nothing here.
6929    if use_per_dim_null
6930        && let Some(Some(z)) = inner.null_eigenvectors.first()
6931        && z.nrows() == p
6932    {
6933        for k in 0..z.ncols() {
6934            // Rank-1 marginal penalty `z_k z_kᵀ`, replicated block-diagonally
6935            // across levels into `I_L ⊗ (z_k z_kᵀ)`. Its own λ is one shared
6936            // variance for this null component (intercept or slope) across all
6937            // groups — the random-effect structure of mgcv `fs`.
6938            let zk = z.column(k);
6939            let mut p_k = Array2::<f64>::zeros((p, p));
6940            for a in 0..p {
6941                for b in 0..p {
6942                    p_k[[a, b]] = zk[a] * zk[b];
6943                }
6944            }
6945            let mut s_null = Array2::<f64>::zeros((q, q));
6946            for level in 0..n_levels {
6947                let start = level * p;
6948                s_null
6949                    .slice_mut(s![start..start + p, start..start + p])
6950                    .assign(&p_k);
6951            }
6952            let (s_null, null_scale) = normalize_penalty_in_constrained_space(&s_null);
6953            let null_block = crate::basis::analyze_penalty_block_with_op(&s_null, None)?;
6954            if null_block.rank > 0 {
6955                let original_index = penalties.len();
6956                penalties.push(null_block.sym_penalty);
6957                nullspaces.push(null_block.nullity);
6958                penaltyinfo.push(PenaltyInfo {
6959                    source: PenaltySource::Primary,
6960                    original_index,
6961                    active: true,
6962                    effective_rank: null_block.rank,
6963                    dropped_reason: None,
6964                    nullspace_dim_hint: null_block.nullity,
6965                    normalization_scale: null_scale,
6966                    kronecker_factors: None,
6967                });
6968            }
6969        }
6970    }
6971    let null_eigenvectors = crate::basis::recompute_null_eigenvectors(&penalties)?;
6972    let joint_null_rotation = crate::basis::compute_joint_null_rotation(&penalties)?;
6973
6974    // Metadata: carry the marginal knot geometry + frozen levels so prediction
6975    // reconstructs an identical replicated design.
6976    let (knots, degree, periodic) = match &inner.metadata {
6977        BasisMetadata::BSpline1D {
6978            knots,
6979            periodic,
6980            degree,
6981            ..
6982        } => (
6983            knots.clone(),
6984            degree.unwrap_or(spec.marginal.degree),
6985            *periodic,
6986        ),
6987        other => {
6988            crate::bail_invalid_basis!(
6989                "factor smooth term '{}' produced an unexpected marginal metadata variant {:?}",
6990                term_name,
6991                other
6992            );
6993        }
6994    };
6995    let flavour_tag = match &spec.flavour {
6996        FactorSmoothFlavour::Fs { .. } => "fs",
6997        FactorSmoothFlavour::Sz => "sz",
6998        FactorSmoothFlavour::Re => "re",
6999    }
7000    .to_string();
7001    let metadata = BasisMetadata::FactorSmooth {
7002        continuous_cols: spec.continuous_cols.clone(),
7003        group_col,
7004        knots,
7005        degree,
7006        periodic,
7007        group_levels: levels,
7008        flavour: flavour_tag,
7009        // fs/re marginals are always B-spline; the cr marginal is sz-only and
7010        // handled on the dedicated Sz path above.
7011        marginal_is_cr: false,
7012    };
7013
7014    let ops = vec![None; penalties.len()];
7015    Ok(LocalSmoothTermBuild {
7016        dim: q,
7017        design: DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(dense)),
7018        penalties,
7019        ops,
7020        nullspaces,
7021        null_eigenvectors,
7022        joint_null_rotation,
7023        penaltyinfo,
7024        pre_dropped_penaltyinfo: Vec::new(),
7025        metadata,
7026        linear_constraints: None,
7027        box_reparam: false,
7028        kronecker_factored: None,
7029    })
7030}
7031
7032/// Resolve the grouping levels for a factor smooth: replay the frozen level
7033/// list when present (predict path), otherwise discover the sorted unique bit
7034/// patterns of the factor column (fit path).
7035pub fn resolve_factor_smooth_levels(
7036    data: ArrayView2<'_, f64>,
7037    group_col: usize,
7038    spec: &FactorSmoothSpec,
7039    term_name: &str,
7040) -> Result<Vec<u64>, BasisError> {
7041    if let Some(frozen) = &spec.group_frozen_levels {
7042        if frozen.is_empty() {
7043            crate::bail_invalid_basis!(
7044                "factor smooth term '{}' has an empty frozen level list",
7045                term_name
7046            );
7047        }
7048        return Ok(frozen.clone());
7049    }
7050    let mut bits: Vec<u64> = data.column(group_col).iter().map(|v| v.to_bits()).collect();
7051    bits.sort_by(|a, b| {
7052        f64::from_bits(*a)
7053            .partial_cmp(&f64::from_bits(*b))
7054            .unwrap_or(std::cmp::Ordering::Equal)
7055    });
7056    bits.dedup();
7057    Ok(bits)
7058}
7059
7060/// Marginal B-spline spec for a factor-smooth block. The marginal always builds
7061/// without an identifiability constraint (the per-level replication, not a
7062/// sum-to-zero side constraint, provides identifiability against the parametric
7063/// block). At predict time the marginal's knot geometry has already been pinned
7064/// into `marginal.knotspec` by the metadata replay, so the spec is used
7065/// verbatim aside from clearing the identifiability transform.
7066pub fn factor_smooth_marginal_for_replay(marginal: &BSplineBasisSpec) -> BSplineBasisSpec {
7067    let mut m = marginal.clone();
7068    m.identifiability = BSplineIdentifiability::None;
7069    m
7070}
7071
7072pub fn build_single_local_smooth_term(
7073    data: ArrayView2<'_, f64>,
7074    term: &SmoothTermSpec,
7075    workspace: &mut crate::basis::BasisWorkspace,
7076) -> Result<LocalSmoothTermBuild, BasisError> {
7077    if term.shape != ShapeConstraint::None && !shape_supports_basis(term) {
7078        crate::bail_invalid_basis!(
7079            "ShapeConstraint::{:?} is unsupported for term '{}'",
7080            term.shape,
7081            term.name
7082        );
7083    }
7084    if let SmoothBasisSpec::ByVariable {
7085        inner,
7086        by_col,
7087        kind,
7088        by,
7089    } = &term.basis
7090    {
7091        ensure_by_variable_specs_match(kind, by, &term.name)?;
7092        let mut inner_basis = (**inner).clone();
7093        // Factor-level `by=` owns model-space centering (it centers the gated
7094        // block against the level indicator downstream). Defer the inner
7095        // basis's default pooled centering so the level block is not
7096        // double-centered down to `k-2` columns (#1427). Numeric-by smooths are
7097        // untouched: they are not row-gated to a level and keep ordinary
7098        // intercept centering.
7099        if matches!(by, ByVariableSpec::Level { .. }) {
7100            defer_inner_model_centering_to_factor_level_wrapper(&mut inner_basis);
7101        }
7102        let inner_term = SmoothTermSpec {
7103            name: term.name.clone(),
7104            basis: inner_basis,
7105            shape: term.shape,
7106            joint_null_rotation: None,
7107        };
7108        let built = build_single_local_smooth_term(data, &inner_term, workspace)?;
7109        return apply_by_variable_to_local_build(built, data, *by_col, by, &term.name);
7110    }
7111
7112    // BySmooth: a `by=` smooth that unifies numeric or factor modulation into a
7113    // single term.  Lower it here so the downstream match does not need an arm.
7114    if let SmoothBasisSpec::BySmooth { smooth, by_kind } = &term.basis {
7115        return build_by_smooth_local(data, term, smooth, by_kind, workspace);
7116    }
7117
7118    let mut shape_axis_col: Option<usize> = None;
7119    let mut built: BasisBuildResult = match &term.basis {
7120        SmoothBasisSpec::FactorSumToZero {
7121            inner,
7122            by_col,
7123            levels,
7124            ..
7125        } => {
7126            if *by_col >= data.ncols() {
7127                crate::bail_dim_basis!(
7128                    "term '{}' by column {} out of bounds for {} columns",
7129                    term.name,
7130                    by_col,
7131                    data.ncols()
7132                );
7133            }
7134            if levels.len() < 2 {
7135                crate::bail_invalid_basis!(
7136                    "sum-to-zero factor smooth term '{}' requires at least two levels",
7137                    term.name
7138                );
7139            }
7140            if term.shape != ShapeConstraint::None {
7141                crate::bail_invalid_basis!(
7142                    "ShapeConstraint::{:?} is unsupported for sum-to-zero factor smooth term '{}'",
7143                    term.shape,
7144                    term.name
7145                );
7146            }
7147            let inner_term = SmoothTermSpec {
7148                name: format!("{}::inner", term.name),
7149                basis: (**inner).clone(),
7150                shape: ShapeConstraint::None,
7151                joint_null_rotation: None,
7152            };
7153            let mut inner_built = build_single_local_smooth_term(data, &inner_term, workspace)?;
7154            // Capture the marginal penalty's null directions BEFORE the penalty
7155            // vector is rebuilt below; the sum-to-zero null-space ridge replicates
7156            // these `z_k` into the contrast space (mgcv `bs="fs"` double-penalty).
7157            let inner_null_eigenvectors = inner_built.null_eigenvectors.clone();
7158            let base = inner_built
7159                .design
7160                .try_to_dense_by_chunks("sum-to-zero factor smooth")
7161                .map_err(BasisError::InvalidInput)?;
7162            let n = base.nrows();
7163            let p = base.ncols();
7164            let l_minus_one = levels.len() - 1;
7165            let mut dense = Array2::<f64>::zeros((n, p * l_minus_one));
7166            for i in 0..n {
7167                let bits = data[[i, *by_col]].to_bits();
7168                let level_idx = levels.iter().position(|b| *b == bits).ok_or_else(|| {
7169                    BasisError::InvalidInput(format!(
7170                        "sum-to-zero factor smooth term '{}' saw an unseen level at row {}",
7171                        term.name,
7172                        i + 1
7173                    ))
7174                })?;
7175                if level_idx < l_minus_one {
7176                    let start = level_idx * p;
7177                    dense
7178                        .slice_mut(s![i, start..start + p])
7179                        .assign(&base.row(i));
7180                } else {
7181                    for level in 0..l_minus_one {
7182                        let start = level * p;
7183                        dense
7184                            .slice_mut(s![i, start..start + p])
7185                            .assign(&base.row(i).mapv(|v| -v));
7186                    }
7187                }
7188            }
7189            let mut penalties = Vec::<Array2<f64>>::with_capacity(inner_built.penalties.len());
7190            let active_penalty_indices = inner_built
7191                .penaltyinfo
7192                .iter()
7193                .enumerate()
7194                .filter_map(|(idx, info)| info.active.then_some(idx))
7195                .collect::<Vec<_>>();
7196            if active_penalty_indices.len() != inner_built.penalties.len() {
7197                crate::bail_invalid_basis!(
7198                    "internal sz penalty metadata mismatch: activeinfos={}, penalties={}",
7199                    active_penalty_indices.len(),
7200                    inner_built.penalties.len()
7201                );
7202            }
7203            // Replicate each marginal penalty into the sum-to-zero contrast
7204            // space. With `L-1` free deviation blocks and the reference level
7205            // `d_L = -Σ_{k<L} d_k`, the marginal penalty summed over ALL `L`
7206            // levels, `Σ_{k=1}^{L} d_kᵀ S d_k`, expands to the `(I + 11ᵀ) ⊗ S`
7207            // contrast form (factor 2 on the diagonal blocks, 1 off-diagonal).
7208            //
7209            // PER-GROUP SMOOTHING PARAMETERS (#1074). mgcv's `bs="sz"` does NOT
7210            // pool that sum under one λ: `smooth.construct.sz` emits ONE penalty
7211            // matrix per factor level (here 6 separate `S`s, each with its own
7212            // smoothing parameter), so REML can shrink a low-amplitude group's
7213            // deviation curve hard while leaving a high-amplitude group nearly
7214            // unpenalized. A single shared wiggliness λ (the old construction)
7215            // forces every group to the SAME curvature budget, so a group whose
7216            // true curve is flat drags curvature into the noise of the busy
7217            // groups and vice-versa — systematic truth-recovery loss even when
7218            // the pooled total edf matches mgcv's (the observed `sz` 1.23× gap).
7219            //
7220            // We mirror mgcv exactly by splitting the per-marginal penalty
7221            // `Σ_{k=1}^{L} d_kᵀ S d_k` back into its `L` independent
7222            // rank-controlled summands BEFORE mapping to the contrast space, each
7223            // carrying its own λ:
7224            //   * level k < L (free block):  `d_kᵀ S d_k` → block-diagonal
7225            //     `(e_k e_kᵀ) ⊗ S`  (only the (k,k) block is `S`).
7226            //   * level L (reference):       `d_Lᵀ S d_L = (Σ_{j<L} d_j)ᵀ S (·)`
7227            //     → the fully-coupled `(11ᵀ) ⊗ S` block.
7228            // Summed at equal λ these `L` blocks recover the old `(I + 11ᵀ) ⊗ S`
7229            // exactly (`Σ_k e_k e_kᵀ = I`), so this is a strict generalization:
7230            // the pooled fit is still reachable, REML only GAINS the freedom to
7231            // spend curvature per group. The zero-sum reparameterization (hence
7232            // the `sz` vs `fs` identifiability) is untouched.
7233            //
7234            // `which_level ∈ 0..=l_minus_one`: `< l_minus_one` selects the single
7235            // free deviation block; `== l_minus_one` selects the reference-level
7236            // coupling block.
7237            let stz_per_group_penalty = |s_inner: &Array2<f64>, which_level: usize| -> Array2<f64> {
7238                let mut s_big = Array2::<f64>::zeros((p * l_minus_one, p * l_minus_one));
7239                if which_level < l_minus_one {
7240                    // (e_k e_kᵀ) ⊗ S: a single diagonal block.
7241                    let k = which_level;
7242                    let mut block = s_big.slice_mut(s![k * p..(k + 1) * p, k * p..(k + 1) * p]);
7243                    block.assign(s_inner);
7244                } else {
7245                    // (11ᵀ) ⊗ S: every block (diagonal and off-diagonal) is S.
7246                    for a in 0..l_minus_one {
7247                        for b in 0..l_minus_one {
7248                            let mut block =
7249                                s_big.slice_mut(s![a * p..(a + 1) * p, b * p..(b + 1) * p]);
7250                            block.assign(s_inner);
7251                        }
7252                    }
7253                }
7254                s_big
7255            };
7256            // One nullspace-dim entry per emitted penalty (must stay parallel to
7257            // `penalties`). Each per-group wiggliness block carries the marginal's
7258            // OWN nullity (a rank-`p` block touching a single level for the free
7259            // blocks; the coupling block is rank-`p` over the diagonal sum), and
7260            // the null ridges below record their own nullity.
7261            let mut nullspaces = Vec::<usize>::with_capacity(penalties.capacity());
7262            for (penalty_pos, s_inner) in inner_built.penalties.iter().enumerate() {
7263                let info_idx = active_penalty_indices[penalty_pos];
7264                let base_info = inner_built.penaltyinfo[info_idx].clone();
7265                let marginal_nullity = inner_built.nullspaces.get(penalty_pos).copied().unwrap_or(0);
7266                // Emit `L` independent per-level blocks for this marginal penalty.
7267                for which_level in 0..=l_minus_one {
7268                    let raw = stz_per_group_penalty(s_inner, which_level);
7269                    let (s_big, group_scale) = normalize_penalty_in_constrained_space(&raw);
7270                    let block = crate::basis::analyze_penalty_block_with_op(&s_big, None)?;
7271                    if block.rank == 0 {
7272                        continue;
7273                    }
7274                    if which_level == 0 {
7275                        // Reuse the marginal's own info slot for the first block so
7276                        // the existing normalization bookkeeping stays attached.
7277                        inner_built.penaltyinfo[info_idx].normalization_scale *= group_scale;
7278                        inner_built.penaltyinfo[info_idx].original_index = penalties.len();
7279                        inner_built.penaltyinfo[info_idx].effective_rank = block.rank;
7280                        inner_built.penaltyinfo[info_idx].nullspace_dim_hint = block.nullity;
7281                    } else {
7282                        let mut info = base_info.clone();
7283                        info.original_index = penalties.len();
7284                        info.normalization_scale = base_info.normalization_scale * group_scale;
7285                        info.effective_rank = block.rank;
7286                        info.nullspace_dim_hint = block.nullity;
7287                        info.kronecker_factors = None;
7288                        inner_built.penaltyinfo.push(info);
7289                    }
7290                    penalties.push(block.sym_penalty);
7291                    // The coupling block (which_level == l_minus_one) spans the
7292                    // marginal range on the diagonal-sum direction; the free
7293                    // blocks touch one level. Both leave the marginal null space
7294                    // unpenalized, recorded here so the null ridges below complete
7295                    // the double penalty.
7296                    nullspaces.push(marginal_nullity);
7297                }
7298            }
7299
7300            // Null-space ridge, mirroring the `bs="fs"` double-penalty
7301            // construction (#1605, same defect class as #700/#712/#713). The
7302            // marginal wiggliness penalty `S` shapes curvature but leaves the
7303            // {const, linear} null space of each deviation curve COMPLETELY
7304            // unpenalized. With that null space free, the single combined
7305            // wiggliness smoothing parameter cannot separate the per-group
7306            // intercept/slope variance from the curvature variance, so REML
7307            // parks the wiggliness `λ` high — over-smoothing (under-fitting) the
7308            // deviation blocks even when the truth lives in their span (the `sz`
7309            // recovery gap vs the `fs` superset). mgcv's `bs="fs"` fixes the
7310            // analogous gap by penalizing each null-space dimension SEPARATELY
7311            // under its own shared variance; we mirror that here while keeping
7312            // the zero-sum reparameterization, so the constraint (and the
7313            // identifiability of `sz` vs `fs`) is preserved. For each orthonormal
7314            // null direction `z_k` of the marginal penalty, add the rank-1
7315            // marginal penalty `z_k z_kᵀ` mapped into the SAME `(I + 11ᵀ)`
7316            // sum-to-zero contrast space, each carrying its own `λ`.
7317            if let Some(Some(z)) = inner_null_eigenvectors.first()
7318                && z.nrows() == p
7319            {
7320                for k in 0..z.ncols() {
7321                    let zk = z.column(k);
7322                    let mut p_k = Array2::<f64>::zeros((p, p));
7323                    for a in 0..p {
7324                        for b in 0..p {
7325                            p_k[[a, b]] = zk[a] * zk[b];
7326                        }
7327                    }
7328                    // Null ridges stay POOLED (the `(I + 11ᵀ) ⊗ z_k z_kᵀ` form):
7329                    // they govern the per-group intercept/slope shrinkage, which
7330                    // mgcv pools under one variance even for `sz`; only the
7331                    // curvature (wiggliness) penalty is split per group above.
7332                    let stz_pooled_null = {
7333                        let mut s_big = Array2::<f64>::zeros((p * l_minus_one, p * l_minus_one));
7334                        for a in 0..l_minus_one {
7335                            for b in 0..l_minus_one {
7336                                let factor = if a == b { 2.0 } else { 1.0 };
7337                                let mut block =
7338                                    s_big.slice_mut(s![a * p..(a + 1) * p, b * p..(b + 1) * p]);
7339                                block.assign(&p_k.mapv(|v| v * factor));
7340                            }
7341                        }
7342                        s_big
7343                    };
7344                    let (s_null, null_scale) =
7345                        normalize_penalty_in_constrained_space(&stz_pooled_null);
7346                    let null_block = crate::basis::analyze_penalty_block_with_op(&s_null, None)?;
7347                    if null_block.rank > 0 {
7348                        let original_index = penalties.len();
7349                        penalties.push(null_block.sym_penalty);
7350                        nullspaces.push(null_block.nullity);
7351                        inner_built.penaltyinfo.push(PenaltyInfo {
7352                            source: PenaltySource::Primary,
7353                            original_index,
7354                            active: true,
7355                            effective_rank: null_block.rank,
7356                            dropped_reason: None,
7357                            nullspace_dim_hint: null_block.nullity,
7358                            normalization_scale: null_scale,
7359                            kronecker_factors: None,
7360                        });
7361                    }
7362                }
7363            }
7364            inner_built.dim = p * l_minus_one;
7365            inner_built.design = DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(dense));
7366            inner_built.penalties = penalties;
7367            inner_built.ops = vec![None; inner_built.penalties.len()];
7368            inner_built.nullspaces = nullspaces;
7369            // Invariant: `null_eigenvectors[k]` must mirror `penalties[k]`'s
7370            // spectral null space. We just rebuilt `inner_built.penalties` from
7371            // Kronecker-like `S_big` blocks, so the previously-plumbed
7372            // `null_eigenvectors` (still parallel to the OLD per-level penalty)
7373            // is stale. Recompute from the rebuilt penalties to restore the
7374            // invariant; ditto for the joint-null absorption rotation.
7375            inner_built.null_eigenvectors =
7376                crate::basis::recompute_null_eigenvectors(&inner_built.penalties)?;
7377            inner_built.joint_null_rotation =
7378                crate::basis::compute_joint_null_rotation(&inner_built.penalties)?;
7379            inner_built.kronecker_factored = None;
7380            return Ok(inner_built);
7381        }
7382        SmoothBasisSpec::BSpline1D { feature_col, spec } => {
7383            if *feature_col >= data.ncols() {
7384                crate::bail_dim_basis!(
7385                    "term '{}' feature column {} out of bounds for {} columns",
7386                    term.name,
7387                    feature_col,
7388                    data.ncols()
7389                );
7390            }
7391            let mut spec_local = spec.clone();
7392            if term.shape != ShapeConstraint::None {
7393                // Shape-constrained B-splines are anchored by construction.
7394                // Sum-to-zero side constraints conflict with monotonic/convex cones.
7395                spec_local.identifiability = BSplineIdentifiability::None;
7396            }
7397            // Endpoint boundary conditions are structural for B-splines: the
7398            // basis builder bakes their homogeneous nullspace transform into
7399            // the design, penalties, and stored raw-basis transform.
7400            build_bspline_basis_1d(data.column(*feature_col), &spec_local)?
7401        }
7402        SmoothBasisSpec::ThinPlate {
7403            feature_cols,
7404            spec,
7405            input_scales,
7406        } => {
7407            if term.shape != ShapeConstraint::None {
7408                if feature_cols.len() != 1 {
7409                    crate::bail_invalid_basis!(
7410                        "ShapeConstraint::{:?} for term '{}' on ThinPlate basis requires exactly 1 feature axis; found {}",
7411                        term.shape,
7412                        term.name,
7413                        feature_cols.len()
7414                    );
7415                }
7416                shape_axis_col = Some(feature_cols[0]);
7417            }
7418            let mut x = select_columns(data, feature_cols)?;
7419            // Auto-standardize multivariate inputs: use stored scales (prediction)
7420            // or compute fresh ones (training). Same standardization-vs-
7421            // length-scale compensation as Matérn / hybrid Duchon: divide
7422            // the user's L by σ_geom so kernel(‖x_std − c_std‖/L_eff)
7423            // matches the original-coord kernel for uniform σ.
7424            let (scales, length_scale_eff) = if let Some(s) = input_scales {
7425                apply_input_standardization(&mut x, s);
7426                (
7427                    Some(s.clone()),
7428                    compensate_length_scale_for_standardization(spec.length_scale, s),
7429                )
7430            } else if let Some(s) = compute_spatial_input_scales(x.view()) {
7431                apply_input_standardization(&mut x, &s);
7432                let l_eff = compensate_length_scale_for_standardization(spec.length_scale, &s);
7433                (Some(s), l_eff)
7434            } else {
7435                (None, spec.length_scale)
7436            };
7437            let mut spec_local = spec.clone();
7438            spec_local.length_scale = length_scale_eff;
7439            if matches!(
7440                spec_local.identifiability,
7441                SpatialIdentifiability::OrthogonalToParametric
7442            ) {
7443                spec_local.identifiability = SpatialIdentifiability::None;
7444            }
7445            let mut result = build_thin_plate_basis(x.view(), &spec_local).map_err(|err| {
7446                rewrite_thin_plate_knots_error(err, &term.name, feature_cols.len(), spec)
7447            })?;
7448            // Inject input scales into metadata; also restore the user's
7449            // original length_scale (not the σ_geom-compensated one) so a
7450            // metadata-driven rebuild that re-applies compensation does not
7451            // double-divide. The build may auto-promote to Duchon when
7452            // canonical TPS is infeasible (k < polynomial-nullspace size);
7453            // in that case patch the Duchon metadata variant so predict-time
7454            // round-trips through the same standardized data path.
7455            match &mut result.metadata {
7456                BasisMetadata::ThinPlate {
7457                    input_scales: ms,
7458                    length_scale,
7459                    ..
7460                } => {
7461                    *ms = scales;
7462                    *length_scale = spec.length_scale;
7463                }
7464                BasisMetadata::Duchon {
7465                    input_scales: ms,
7466                    length_scale,
7467                    ..
7468                } => {
7469                    // Auto-promotion (canonical TPS infeasible at this (d, k)).
7470                    // Since #1091 the promotion does NOT forward the incoming
7471                    // σ_geom-compensated `spec_local.length_scale` to the Duchon
7472                    // builder — it DISCARDS it and substitutes the geometric mean
7473                    // of the center pairwise distances (`promotion_length_scale`,
7474                    // the natural radial-kernel scale where κ·r ≈ O(1)). So the
7475                    // realized kernel bandwidth recorded in this metadata bears no
7476                    // fixed relation to the user's `spec.length_scale`; clobbering
7477                    // it to the user-facing value (the pre-#1091 behavior) makes
7478                    // freeze→replay re-derive `compensate(spec.length_scale, σ) ≠
7479                    // promotion_length_scale`, evaluating the kernel at the wrong
7480                    // bandwidth and corrupting the replayed design (#1091 broke the
7481                    // e7ff5ed83 freeze contract for the auto-promoted path).
7482                    //
7483                    // The freeze→replay round trip rebuilds through the Duchon arm,
7484                    // which re-applies σ_geom compensation:
7485                    //   replay_eff = compensate(metadata.length_scale, σ)
7486                    //              = metadata.length_scale / σ_geom.
7487                    // For replay_eff to reproduce the realized `promotion_length_scale`
7488                    // we must store the UN-compensated value `promotion_length_scale
7489                    // · σ_geom`. `compensate(1.0, σ) = 1/σ_geom`, so divide the
7490                    // realized scale by it to multiply back through σ_geom. With no
7491                    // standardization (`scales == None`) replay does not compensate,
7492                    // so the realized value is kept verbatim.
7493                    if let (Some(s), Some(realized)) = (scales.as_ref(), *length_scale) {
7494                        let inv_sigma_geom =
7495                            compensate_length_scale_for_standardization(1.0, s);
7496                        if inv_sigma_geom.is_finite() && inv_sigma_geom > 0.0 {
7497                            *length_scale = Some(realized / inv_sigma_geom);
7498                        }
7499                    }
7500                    *ms = scales;
7501                }
7502                _ => {}
7503            }
7504            result
7505        }
7506        SmoothBasisSpec::Sphere { feature_cols, spec } => {
7507            if term.shape != ShapeConstraint::None {
7508                crate::bail_invalid_basis!(
7509                    "ShapeConstraint::{:?} for term '{}' is not supported on spherical splines",
7510                    term.shape,
7511                    term.name
7512                );
7513            }
7514            let x = select_columns(data, feature_cols)?;
7515            build_spherical_spline_basis(x.view(), spec)?
7516        }
7517        SmoothBasisSpec::ConstantCurvature { feature_cols, spec } => {
7518            if term.shape != ShapeConstraint::None {
7519                crate::bail_invalid_basis!(
7520                    "ShapeConstraint::{:?} for term '{}' is not supported on constant-curvature smooths",
7521                    term.shape,
7522                    term.name
7523                );
7524            }
7525            // Chart coordinates are consumed verbatim: NO auto-standardization.
7526            // Rescaling axes would change the chart gauge `1 + κ‖x‖²` and
7527            // silently redefine which curvature κ refers to (the same point
7528            // cloud at a different chart scale has a different κ̂); the user's
7529            // coordinates ARE the geometry here, exactly as for the sphere
7530            // smooth's (lat, lon).
7531            let x = select_columns(data, feature_cols)?;
7532            build_constant_curvature_basis(x.view(), spec)?
7533        }
7534        SmoothBasisSpec::MeasureJet {
7535            feature_cols,
7536            spec,
7537            input_scales,
7538        } => {
7539            if term.shape != ShapeConstraint::None {
7540                crate::bail_invalid_basis!(
7541                    "ShapeConstraint::{:?} for term '{}' is not supported on measure-jet smooths",
7542                    term.shape,
7543                    term.name
7544                );
7545            }
7546            let mut x = select_columns(data, feature_cols)?;
7547            // Matern-style per-axis standardization; the realized σ vector is
7548            // persisted into the metadata for predict-time replay.
7549            //
7550            // Length-scale round-trip contract (owning statement; the freeze
7551            // and frozen-validation arms reference it): `input_scales: Some`
7552            // marks the REPLAY path — the frozen length_scale is already the
7553            // realized post-standardization value and passes through
7554            // verbatim. Fresh path: an explicit user length_scale is in
7555            // ORIGINAL coordinates and gets the σ_geom compensation; the 0.0
7556            // auto sentinel passes through (auto-derivation runs inside the
7557            // builder, post-standardization).
7558            let (scales, length_scale_eff) = if let Some(s) = input_scales {
7559                apply_input_standardization(&mut x, s);
7560                (Some(s.clone()), spec.length_scale)
7561            } else if let Some(s) = compute_spatial_input_scales(x.view()) {
7562                apply_input_standardization(&mut x, &s);
7563                let l_eff = if spec.length_scale > 0.0 {
7564                    compensate_length_scale_for_standardization(spec.length_scale, &s)
7565                } else {
7566                    spec.length_scale
7567                };
7568                (Some(s), l_eff)
7569            } else {
7570                (None, spec.length_scale)
7571            };
7572            let mut spec_local = spec.clone();
7573            spec_local.length_scale = length_scale_eff;
7574            let mut result = build_measure_jet_basis(x.view(), &spec_local)?;
7575            if let BasisMetadata::MeasureJet {
7576                input_scales: ms, ..
7577            } = &mut result.metadata
7578            {
7579                *ms = scales;
7580            }
7581            result
7582        }
7583        SmoothBasisSpec::Matern {
7584            feature_cols,
7585            spec,
7586            input_scales,
7587        } => {
7588            if term.shape != ShapeConstraint::None {
7589                if feature_cols.len() != 1 {
7590                    crate::bail_invalid_basis!(
7591                        "ShapeConstraint::{:?} for term '{}' on Matern basis requires exactly 1 feature axis; found {}",
7592                        term.shape,
7593                        term.name,
7594                        feature_cols.len()
7595                    );
7596                }
7597                shape_axis_col = Some(feature_cols[0]);
7598            }
7599            let mut x = select_columns(data, feature_cols)?;
7600            // Auto-standardization (per-axis division by σ_a) reinterprets
7601            // the user's `length_scale` from original data coordinates
7602            // into post-standardization coordinates: for uniform σ_a = σ,
7603            // `kernel(‖x_std − c_std‖/L)` equals `kernel(‖x − c‖/(σ·L))`,
7604            // so the effective kernel range shrinks by σ. To keep
7605            // `length_scale` consistently expressed in *original* data
7606            // coordinates regardless of axis variances, we standardize
7607            // and divide L by σ_geom = (∏σ_a)^(1/d). For uniform σ this
7608            // recovers the user's kernel exactly; for anisotropic data
7609            // the resulting per-axis effective scales σ_a / σ_geom are
7610            // the standard Mahalanobis preconditioning and preserve the
7611            // geometric-mean kernel range. Storing the σ vector in
7612            // metadata.input_scales makes the same transformation
7613            // replayable at predict time.
7614            let (scales, length_scale_eff) = if let Some(s) = input_scales {
7615                apply_input_standardization(&mut x, s);
7616                (
7617                    Some(s.clone()),
7618                    compensate_length_scale_for_standardization(spec.length_scale, s),
7619                )
7620            } else if let Some(s) = compute_spatial_input_scales(x.view()) {
7621                apply_input_standardization(&mut x, &s);
7622                let l_eff = compensate_length_scale_for_standardization(spec.length_scale, &s);
7623                (Some(s), l_eff)
7624            } else {
7625                (None, spec.length_scale)
7626            };
7627            let mut spec_local = spec.clone();
7628            spec_local.length_scale = length_scale_eff;
7629            let mut result = build_matern_basiswithworkspace(x.view(), &spec_local, workspace)?;
7630            if let BasisMetadata::Matern {
7631                input_scales,
7632                length_scale,
7633                ..
7634            } = &mut result.metadata
7635            {
7636                *input_scales = scales;
7637                *length_scale = spec.length_scale;
7638            }
7639            result
7640        }
7641        SmoothBasisSpec::Duchon {
7642            feature_cols,
7643            spec,
7644            input_scales,
7645        } => {
7646            if term.shape != ShapeConstraint::None {
7647                if feature_cols.len() != 1 {
7648                    crate::bail_invalid_basis!(
7649                        "ShapeConstraint::{:?} for term '{}' on Duchon basis requires exactly 1 feature axis; found {}",
7650                        term.shape,
7651                        term.name,
7652                        feature_cols.len()
7653                    );
7654                }
7655                shape_axis_col = Some(feature_cols[0]);
7656            }
7657            let mut x = select_columns(data, feature_cols)?;
7658            // Hybrid Duchon (length_scale=Some) is governed by the same
7659            // standardization-vs-length-scale equivalence as Matérn: the
7660            // user's `length_scale` is interpreted in original data
7661            // coordinates, but auto-standardization (per-axis division by
7662            // σ_a) reinterprets it as σ_geom · L. Pre-multiply by 1/σ_geom
7663            // so kernel(‖x_std − c_std‖/L_eff) reproduces the user's
7664            // original-coord kernel exactly for uniform σ_a, and reduces
7665            // to standard Mahalanobis preconditioning for anisotropic σ.
7666            // Pure Duchon (length_scale=None) is scale-free and needs no
7667            // compensation.
7668            let (scales, length_scale_eff) = if let Some(s) = input_scales {
7669                apply_input_standardization(&mut x, s);
7670                (
7671                    Some(s.clone()),
7672                    compensate_optional_length_scale_for_standardization(spec.length_scale, s),
7673                )
7674            } else if let Some(s) = compute_spatial_input_scales(x.view()) {
7675                apply_input_standardization(&mut x, &s);
7676                let l_eff =
7677                    compensate_optional_length_scale_for_standardization(spec.length_scale, &s);
7678                (Some(s), l_eff)
7679            } else {
7680                (None, spec.length_scale)
7681            };
7682            let mut spec_local = spec.clone();
7683            spec_local.length_scale = length_scale_eff;
7684            // The Duchon input axis is standardized in place above (`x → x/σ`,
7685            // scale-only, no centering). A 1-D cyclic boundary `[start, end)`
7686            // declared in ORIGINAL covariate units must move into that same
7687            // standardized frame, or the modular wrap in
7688            // `build_cyclic_duchon_basis_1dwithworkspace` folds the standardized
7689            // coordinate against an original-unit period: the seam never closes
7690            // and the basis silently degrades to non-periodic (#1074:
7691            // `duchon(x, periodic=true)` predictions diverged across the wrap,
7692            // f(0) ≠ f(2π)). Rescale by the same 1/σ applied to the data so
7693            // training and predict share one periodic geometry.
7694            if let (Some(s), crate::basis::OneDimensionalBoundary::Cyclic { start, end }) =
7695                (scales.as_ref(), spec_local.boundary.clone())
7696                && s.len() == 1
7697                && s[0] > 0.0
7698            {
7699                spec_local.boundary = crate::basis::OneDimensionalBoundary::Cyclic {
7700                    start: start / s[0],
7701                    end: end / s[0],
7702                };
7703            }
7704            if matches!(
7705                spec_local.identifiability,
7706                SpatialIdentifiability::OrthogonalToParametric
7707            ) {
7708                spec_local.identifiability = SpatialIdentifiability::None;
7709            }
7710            let mut result = build_duchon_basiswithworkspace(x.view(), &spec_local, workspace)?;
7711            if let BasisMetadata::Duchon {
7712                input_scales,
7713                length_scale,
7714                ..
7715            } = &mut result.metadata
7716            {
7717                *input_scales = scales;
7718                *length_scale = spec.length_scale;
7719            }
7720            result
7721        }
7722        SmoothBasisSpec::Pca {
7723            feature_cols,
7724            basis_matrix,
7725            centered,
7726            smooth_penalty,
7727            center_mean,
7728            pca_basis_path,
7729            chunk_size,
7730        } => {
7731            if term.shape != ShapeConstraint::None {
7732                crate::bail_invalid_basis!(
7733                    "ShapeConstraint::{:?} for term '{}' is not supported on Pca basis",
7734                    term.shape,
7735                    term.name
7736                );
7737            }
7738            build_pca_smooth_basis(
7739                data,
7740                feature_cols,
7741                basis_matrix,
7742                *centered,
7743                *smooth_penalty,
7744                center_mean.as_ref(),
7745                pca_basis_path.as_ref(),
7746                *chunk_size,
7747            )?
7748        }
7749        SmoothBasisSpec::TensorBSpline { feature_cols, spec } => {
7750            build_tensor_bspline_basis(data, feature_cols, spec)?
7751        }
7752        SmoothBasisSpec::ByVariable { .. } => {
7753            crate::bail_invalid_basis!(
7754                "internal: ByVariable smooths must return before inner basis dispatch"
7755            );
7756        }
7757        SmoothBasisSpec::BySmooth { .. } => {
7758            crate::bail_invalid_basis!("internal: BySmooth smooths must be lowered to ByVariable before inner basis dispatch"
7759                    .to_string(),);
7760        }
7761        SmoothBasisSpec::FactorSmooth { spec } => {
7762            if term.shape != ShapeConstraint::None {
7763                crate::bail_invalid_basis!(
7764                    "ShapeConstraint::{:?} is unsupported for factor smooth term '{}'",
7765                    term.shape,
7766                    term.name
7767                );
7768            }
7769            return build_factor_smooth(data, spec, &term.name, workspace);
7770        }
7771    };
7772
7773    // The Matérn design ALWAYS uses the operator-collocation {mass, tension,
7774    // stiffness} penalty triplet, overriding whatever penalty
7775    // `build_matern_basis_seeded` produced for the `double_penalty` flag.
7776    //
7777    // #1074 investigated swapping this for the genuine RKHS kernel penalty
7778    // `β' K_CC β` (mgcv `bs="gp"` / fields kriging) on the theory that the
7779    // operator triplet under-smooths the rougher half-integer kernels. MSI
7780    // truth-recovery measurement REFUTED that: the kernel penalty did NOT
7781    // improve ν=3/2 recovery (`matern(x,nu=1.5)` RMSE-vs-truth stayed 0.0554)
7782    // and it REGRESSED the high-frequency-init guard — `matern(x,nu≥5/2)` on
7783    // sin(2π·8·x) collapsed (span 0.53, RMSE 0.70) because the single RKHS
7784    // norm over-smooths a high-frequency truth where the Sobolev-order operator
7785    // dials do not. The operator triplet is therefore retained as the Matérn
7786    // penalty, and the κ-optimizer re-key / ψ-derivative paths route through the
7787    // same triplet builder so the block count stays ψ-stable (#1270).
7788    if let SmoothBasisSpec::Matern { .. } = &term.basis {
7789        let (penalties, nullspace_dims, penaltyinfo) =
7790            matern_operator_penalty_triplet_from_metadata(&built.metadata)?;
7791        built.penalties = penalties;
7792        built.nullspace_dims = nullspace_dims;
7793        built.penaltyinfo = penaltyinfo;
7794    }
7795
7796    let p_local = built.design.ncols();
7797    let mut metadata = built.metadata.clone();
7798    // Extract factored Kronecker representation before consuming fields.
7799    // Invalidate it if shape transforms will be applied (they break structure).
7800    let kron_factored = if term.shape == ShapeConstraint::None {
7801        built.kronecker_factored
7802    } else {
7803        None
7804    };
7805    let mut design_t = built.design;
7806    let mut penalties_t: Vec<Array2<f64>> = built.penalties;
7807    // Ops vector parallel to `penalties_t`. Survives unchanged through the
7808    // identity path; nulled element-wise when `T^T S T` reparametrization
7809    // is applied (operator no longer bit-equivalent to the transformed
7810    // matrix); wrapped in `ScaledPenaltyOp` after Frobenius normalization.
7811    let mut ops_t: Vec<Option<std::sync::Arc<dyn crate::analytic_penalties::PenaltyOp>>> =
7812        built.ops;
7813    if matches!(
7814        spatial_identifiability_policy(term),
7815        Some(SpatialIdentifiability::OrthogonalToParametric)
7816    ) {
7817        metadata = freeze_raw_spatial_metadata(metadata, design_t.ncols());
7818    }
7819
7820    let active_penaltyinfo_t = built
7821        .penaltyinfo
7822        .iter()
7823        .filter(|info| info.active)
7824        .cloned()
7825        .collect::<Vec<_>>();
7826    let pre_dropped_penaltyinfo_t = built
7827        .penaltyinfo
7828        .iter()
7829        .filter(|info| !info.active)
7830        .cloned()
7831        .collect::<Vec<_>>();
7832    let use_box_reparam =
7833        term.shape != ShapeConstraint::None && shape_uses_box_reparameterization(&term.basis);
7834    if let Some((order, sign)) = shape_order_and_sign(term.shape)
7835        && use_box_reparam
7836    {
7837        // Order 1 (monotone): the plain first-difference cone θ_{i+1}−θ_i ≥ 0 is
7838        // the control-polygon monotonicity criterion, which is independent of
7839        // Greville-abscissa spacing (it only fixes the *sign* of consecutive
7840        // control-point gaps), so the integer-difference transform is exact.
7841        //
7842        // Order 2 (convex/concave): the plain second-difference cone is only
7843        // correct for evenly spaced Greville abscissae. gam's B-splines are
7844        // clamped (and may use quantile knots), so the abscissae are not
7845        // uniform and the geometrically-correct cone is the second *divided*
7846        // difference. Build the Greville-scaled transform so γ_{≥2} ≥ 0
7847        // certifies convexity of the function, not of the raw coefficient
7848        // index. Periodic B-splines use uniform interior knots (uniform
7849        // abscissae), where the divided differences coincide with the integer
7850        // differences up to scale, so the plain path stays exact there.
7851        let t = if order == 2 {
7852            let bspline_meta = match &metadata {
7853                BasisMetadata::BSpline1D {
7854                    knots,
7855                    degree,
7856                    periodic,
7857                    ..
7858                } if periodic.is_none() => Some((knots.clone(), degree.unwrap_or(0))),
7859                _ => None,
7860            };
7861            match bspline_meta {
7862                Some((knots, degree)) if degree >= 1 => {
7863                    let greville = crate::basis::compute_greville_abscissae(&knots, degree)?;
7864                    if greville.len() != p_local {
7865                        crate::bail_invalid_basis!(
7866                            "shape-constraint Greville abscissae count {} does not match basis dim {} for term '{}'",
7867                            greville.len(),
7868                            p_local,
7869                            term.name
7870                        );
7871                    }
7872                    convex_divided_difference_transform_matrix(&greville, sign)?
7873                }
7874                _ => cumulative_sum_transform_matrix(p_local, order, sign),
7875            }
7876        } else {
7877            cumulative_sum_transform_matrix(p_local, order, sign)
7878        };
7879        // Coefficient-side transform: wrap the design in an operator that
7880        // applies T on the coefficient side, preserving sparsity/operator
7881        // structure of the inner design.
7882        let inner_dense = match design_t {
7883            DesignMatrix::Dense(d) => d,
7884            DesignMatrix::Sparse(sp) => gam_linalg::matrix::DenseDesignMatrix::from(
7885                sp.try_to_dense_arc("shape-constrained coefficient transform")
7886                    .map_err(BasisError::InvalidInput)?,
7887            ),
7888        };
7889        let coeff_op = gam_linalg::matrix::CoefficientTransformOperator::new(inner_dense, t.clone())
7890            .map_err(|e| BasisError::InvalidInput(format!("CoefficientTransformOperator: {e}")))?;
7891        design_t = DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(Arc::new(coeff_op)));
7892        if penalties_t.len() != active_penaltyinfo_t.len() {
7893            crate::bail_invalid_basis!(
7894                "internal box-reparam penalty/info mismatch for term '{}': penalties={}, infos={}",
7895                term.name,
7896                penalties_t.len(),
7897                active_penaltyinfo_t.len()
7898            );
7899        }
7900        // Wiggliness penalties undergo the exact congruence `S → TᵀST` (PSD
7901        // preserving). The double-penalty *nullspace shrinkage* ridge must NOT:
7902        // it is a unit-eigenvalue projector `ZZᵀ` onto null(S_wiggle) in the
7903        // β (B-spline coefficient) coordinates, and the congruence
7904        // `Tᵀ(ZZᵀ)T = (TᵀZ)(TᵀZ)ᵀ` is no longer a projector — its eigenvalues
7905        // blow up by the conditioning of the cumulative-sum `T` (cond(T) grows
7906        // with the basis dim), concentrating an enormous penalty on the leading
7907        // γ₀ "level" coordinate. REML then drives the shared λ to its ceiling
7908        // and the smooth collapses to a flat constant (#509, the over-smoothing
7909        // face). The principled fix keeps mgcv's double-penalty semantics in the
7910        // *reparametrized* space: rebuild the ridge as the unit-eigenvalue
7911        // nullspace projector of the transformed wiggliness penalty `TᵀST`, so
7912        // the double penalty shrinks exactly the unpenalized polynomial
7913        // directions of the γ-space smooth with eigenvalue 1, identical in
7914        // conditioning to the unconstrained fit.
7915        let transformed_wiggliness = penalties_t
7916            .iter()
7917            .zip(active_penaltyinfo_t.iter())
7918            .find(|(_, info)| !matches!(info.source, PenaltySource::DoublePenaltyNullspace))
7919            .map(|(s_local, _)| {
7920                let tt_s = fast_atb(&t, s_local);
7921                fast_ab(&tt_s, &t)
7922            });
7923        let mut rebuilt = Vec::with_capacity(penalties_t.len());
7924        for (s_local, info) in penalties_t.iter().zip(active_penaltyinfo_t.iter()) {
7925            if matches!(info.source, PenaltySource::DoublePenaltyNullspace) {
7926                // #1654: the double-penalty nullspace ridge under the box
7927                // reparameterization.
7928                //
7929                // For the CURVATURE constraints (order == 2, convex/concave) the
7930                // box transform `T` is the Greville-scaled second *divided*
7931                // difference map (`convex_divided_difference_transform_matrix`).
7932                // Rebuilding the ridge from scratch as the orthonormal null-space
7933                // projector of `TᵀST` (the #509-era monotone fix below) yields a
7934                // γ-space ridge `Z_γ Z_γᵀ` whose null subspace is the affine
7935                // (level γ₀ + slope γ₁) face — the SAME subspace targeted in
7936                // β-space, but measured in the γ inner product rather than the
7937                // β one. A reparameterization `β = Tγ` must leave the penalized
7938                // REML fit invariant, which requires every penalty block to
7939                // transform by the SAME congruence `S ↦ TᵀST`; the from-scratch
7940                // projector rebuild is NOT that congruence, so it silently
7941                // re-weights the level/slope shrinkage relative to the wiggliness
7942                // penalty (each block is independently Frobenius-normalized just
7943                // below, decoupling their scales). The distorted REML λ landscape
7944                // then drives the convex/concave smooth into the flat linear
7945                // corner (curvature pinned ≈ 0, EDF ≈ 1.5) for a
7946                // seed/basis-dimension–specific subset of fits, even though an
7947                // unconstrained `s(x)` on the same data recovers the convex truth
7948                // at EDF ≈ 4. Restoring the exact congruence `Tᵀ R_β T` for the
7949                // ridge keeps the box reparameterization a true invertible
7950                // change of coordinates, so the curvature-constrained fit tracks
7951                // the unconstrained smoothing instead of over-smoothing
7952                // (verified: seed-7/k-20 truth-RMSE 0.31 → 0.045).
7953                //
7954                // For MONOTONE (order == 1) `T` is the cumulative-sum transform
7955                // whose conditioning grows fast with the basis dimension; there
7956                // the congruence concentrates an enormous penalty on the leading
7957                // γ₀ level coordinate and over-smooths to a flat constant (#509),
7958                // which the from-scratch unit-eigenvalue projector rebuild was
7959                // introduced to cure. Keep that path for monotone.
7960                if order == 2 {
7961                    let tt_s = fast_atb(&t, s_local);
7962                    rebuilt.push(fast_ab(&tt_s, &t));
7963                } else {
7964                    let s_wiggle_t = transformed_wiggliness.as_ref().ok_or_else(|| {
7965                        BasisError::InvalidInput(format!(
7966                            "box-reparam term '{}' has a double-penalty ridge but no primary wiggliness penalty to derive its nullspace from",
7967                            term.name
7968                        ))
7969                    })?;
7970                    let ridge = crate::basis::build_nullspace_shrinkage_penalty(s_wiggle_t)?
7971                        .map(|shrink| shrink.sym_penalty)
7972                        .unwrap_or_else(|| Array2::<f64>::zeros((p_local, p_local)));
7973                    rebuilt.push(ridge);
7974                }
7975            } else {
7976                let tt_s = fast_atb(&t, s_local);
7977                rebuilt.push(fast_ab(&tt_s, &t));
7978            }
7979        }
7980        penalties_t = rebuilt;
7981        // T^T S T (and the rebuilt γ-space ridge) invalidate op-form
7982        // bit-equivalence; drop ops here.
7983        ops_t = vec![None; penalties_t.len()];
7984    }
7985    if penalties_t.len() != active_penaltyinfo_t.len() {
7986        crate::bail_invalid_basis!(
7987            "internal penalty metadata mismatch for term '{}': active penalties={}, active infos={}",
7988            term.name,
7989            penalties_t.len(),
7990            active_penaltyinfo_t.len()
7991        );
7992    }
7993    if ops_t.len() != penalties_t.len() {
7994        ops_t = vec![None; penalties_t.len()];
7995    }
7996    let penalty_candidates = penalties_t
7997        .into_iter()
7998        .zip(active_penaltyinfo_t.into_iter())
7999        .zip(ops_t.into_iter())
8000        .map(
8001            |((matrix, info), op_in)| -> Result<PenaltyCandidate, BasisError> {
8002                let (matrix, c_new) = normalize_penalty_in_constrained_space(&matrix);
8003                let normalization_scale = info.normalization_scale * c_new;
8004                let op_scale = 1.0 / c_new;
8005                let kronecker_scale = 1.0 / c_new;
8006                // Frobenius rescale: wrap inner op in `ScaledPenaltyOp(1/c_new)`
8007                // so `op.as_dense() == matrix` post-normalization.
8008                let scaled_op = if op_scale > 0.0 && op_scale.is_finite() {
8009                    op_in.map(|op| {
8010                        std::sync::Arc::new(crate::analytic_penalties::ScaledPenaltyOp::new(
8011                            op, op_scale,
8012                        ))
8013                            as std::sync::Arc<dyn crate::analytic_penalties::PenaltyOp>
8014                    })
8015                } else {
8016                    None
8017                };
8018                let kronecker_factors = info.kronecker_factors.map(|mut factors| {
8019                    if let Some(first) = factors.first_mut() {
8020                        first.mapv_inplace(|v| v * kronecker_scale);
8021                    }
8022                    factors
8023                });
8024                Ok(PenaltyCandidate {
8025                    nullspace_dim_hint: info.nullspace_dim_hint,
8026                    matrix,
8027                    source: info.source,
8028                    normalization_scale,
8029                    kronecker_factors,
8030                    op: scaled_op,
8031                })
8032            },
8033        )
8034        .collect::<Result<Vec<_>, _>>()?;
8035    let (penalties_t, nullspaces_t, penaltyinfo_t, null_eigenvectors_t, ops_t) =
8036        crate::basis::filter_active_penalty_candidates_with_ops(penalty_candidates)?;
8037    let shape_linear_constraints = if term.shape != ShapeConstraint::None && !use_box_reparam {
8038        let axis = shape_axis_col.ok_or_else(|| {
8039            BasisError::InvalidInput(format!(
8040                "internal shape-constraint axis missing for term '{}'",
8041                term.name
8042            ))
8043        })?;
8044        let (x_shape_eval, design_shape_eval) =
8045            build_shape_constraint_design_1d(data, term, &metadata, axis)?;
8046        build_shape_linear_constraints_1d(
8047            x_shape_eval.view(),
8048            design_shape_eval.view(),
8049            term.shape,
8050        )?
8051    } else {
8052        None
8053    };
8054    let linear_constraints_local = merge_linear_constraints_global(shape_linear_constraints, None);
8055
8056    // Joint-null absorption rotation. Fresh fit specs compute Q from the final
8057    // per-smooth penalty set (after all in-smooth reparameterizations have
8058    // already been applied). Frozen specs already carry the complete realized
8059    // coefficient chart in their `FrozenTransform`; recomputing Q there would
8060    // rotate an already-frozen chart a second time and desynchronize value
8061    // rebuilds from derivative operators.
8062    //
8063    // Kronecker-factored smooths (tensor B-splines under `TensorBSplineIdentifiability::None`)
8064    // carry their joint penalty as `Σ_d S_d` with `S_d = I ⊗ … ⊗ S_d^{1D} ⊗ … ⊗ I`.
8065    // The joint null space is the tensor of marginal nulls and is handled directly
8066    // by the REML runtime's `kronecker_penalty_system` path (see
8067    // `runtime.rs:8334-8344`). Applying a dense (p × p) Q here would densify
8068    // `X_raw = mx ⊗ my` into `X_raw · Q`, destroying the Kronecker product
8069    // structure that the runtime relies on for fast log-det/derivative
8070    // assembly — and the rotation block at the wrapper site also unconditionally
8071    // wipes `kronecker_factored`, leaving the runtime to fall back to the
8072    // dense per-block log-det. Skip the rotation for Kronecker-factored terms
8073    // so the factored representation survives end-to-end.
8074    let joint_null_rotation = match term.joint_null_rotation.clone() {
8075        Some(persisted) => Some(persisted),
8076        None if smooth_has_frozen_identifiability(term) => None,
8077        None if kron_factored.is_some() => None,
8078        None => crate::basis::compute_joint_null_rotation(&penalties_t)?,
8079    };
8080
8081    Ok(LocalSmoothTermBuild {
8082        dim: p_local,
8083        design: design_t,
8084        penalties: penalties_t,
8085        ops: ops_t,
8086        nullspaces: nullspaces_t,
8087        null_eigenvectors: null_eigenvectors_t,
8088        joint_null_rotation,
8089        penaltyinfo: penaltyinfo_t,
8090        pre_dropped_penaltyinfo: pre_dropped_penaltyinfo_t,
8091        metadata,
8092        linear_constraints: linear_constraints_local,
8093        box_reparam: use_box_reparam,
8094        kronecker_factored: kron_factored,
8095    })
8096}
8097
8098pub fn build_smooth_design(
8099    data: ArrayView2<'_, f64>,
8100    terms: &[SmoothTermSpec],
8101) -> Result<RawSmoothDesign, BasisError> {
8102    let mut ws = crate::basis::BasisWorkspace::new();
8103    build_smooth_design_withworkspace(data, terms, &mut ws)
8104}
8105
8106/// Like `build_smooth_design`, but honors the caller workspace policy while
8107/// building each planned smooth term with an independent per-term workspace.
8108///
8109/// Independent workspaces avoid shared mutable distance-cache state during the
8110/// parallel term build; the final design, penalties, and metadata are assembled
8111/// in the original smooth-term order.
8112pub fn build_smooth_design_withworkspace(
8113    data: ArrayView2<'_, f64>,
8114    terms: &[SmoothTermSpec],
8115    workspace: &mut crate::basis::BasisWorkspace,
8116) -> Result<RawSmoothDesign, BasisError> {
8117    validate_smooth_terms_finite_inputs(data, terms)?;
8118    build_smooth_design_withworkspace_unvalidated(data, terms, workspace)
8119}
8120
8121pub fn build_smooth_design_withworkspace_unvalidated(
8122    data: ArrayView2<'_, f64>,
8123    terms: &[SmoothTermSpec],
8124    workspace: &mut crate::basis::BasisWorkspace,
8125) -> Result<RawSmoothDesign, BasisError> {
8126    let mut planned_blocks = plan_joint_spatial_centers_for_term_blocks(data, &[terms.to_vec()])?;
8127    let planned_terms = planned_blocks.pop().ok_or_else(|| {
8128        BasisError::InvalidInput(
8129            "joint spatial center planner returned no smooth blocks".to_string(),
8130        )
8131    })?;
8132    let policy = workspace.policy().clone();
8133    let local_builds: Vec<LocalSmoothTermBuild> = {
8134        use rayon::iter::{IntoParallelIterator, ParallelIterator};
8135        planned_terms
8136            .into_par_iter()
8137            .map(|term| {
8138                let mut term_workspace = crate::basis::BasisWorkspace::with_policy(policy.clone());
8139                build_single_local_smooth_term(data, &term, &mut term_workspace)
8140            })
8141            .collect::<Result<Vec<_>, _>>()?
8142    };
8143
8144    let total_p: usize = local_builds.iter().map(|built| built.dim).sum();
8145
8146    let mut local_designs: Vec<DesignMatrix> = Vec::with_capacity(local_builds.len());
8147    let mut terms_out = Vec::<SmoothTerm>::with_capacity(terms.len());
8148    let mut penalties_global = Vec::<BlockwisePenalty>::new();
8149    let mut nullspace_dims_global = Vec::<usize>::new();
8150    let mut penaltyinfo_global = Vec::<PenaltyBlockInfo>::new();
8151    let mut dropped_penaltyinfo_global = Vec::<DroppedPenaltyBlockInfo>::new();
8152    let mut coefficient_lower_bounds = Array1::<f64>::from_elem(total_p, f64::NEG_INFINITY);
8153    let mut any_bounds = false;
8154    // Each linear-constraint row only touches the current term's column slice.
8155    // Track `(col_start, col_end, local_row_values)` and assemble the final
8156    // dense `Array2` in one pass, avoiding per-row `Array1::zeros(total_p)`
8157    // allocation plus a row-by-row copy at the end.
8158    let mut linear_constraintsrows: Vec<(usize, usize, Array1<f64>)> = Vec::new();
8159    let mut linear_constraints_b: Vec<f64> = Vec::new();
8160
8161    let mut col_start = 0usize;
8162    for (term, mut built) in terms.iter().zip(local_builds.into_iter()) {
8163        let p_local = built.dim;
8164        let col_end = col_start + p_local;
8165        let lb_local = if built.box_reparam {
8166            shape_lower_bounds_local(term.shape, p_local)
8167        } else {
8168            None
8169        };
8170
8171        // Stage-2 joint-null absorption rotation. Fired *before* the
8172        // penalty / design / global aggregation loops below so that every
8173        // subsequent reference to `built.penalties`, `built.design`, and
8174        // `built.ops` sees the post-rotation values.
8175        //
8176        // The math: when the smooth's joint penalty `Σ_k S_k` has a
8177        // non-trivial null space, eigh selects `Q = [U_range | U_null]`
8178        // with null columns at the tail. Setting `β_raw = Q · γ` and
8179        // applying:
8180        //     design        ← X · Q
8181        //     penalties[k]  ← Qᵀ · S_k · Q   (block-diag, zero null tail)
8182        // yields a model whose fitted γ is invariant to the rotation
8183        // (since likelihood depends only on `X · β_raw = X · Q · γ`), but
8184        // whose penalty is full-rank on the range columns. The large-scale
8185        // failing case (cert refusal in the joint-Newton inner solve)
8186        // resolves because `H_pen = H_loglik + S` becomes full rank on
8187        // the smooth's range columns.
8188        //
8189        // Rotation is suppressed when the smooth carries coordinate-wise
8190        // shape constraints (`lb_local` or `built.linear_constraints`):
8191        // those encode a cone in the original coordinate system and a
8192        // general orthogonal rotation breaks the cone geometry. Smooths
8193        // with shape constraints typically have full-rank joint penalty
8194        // (their structural shape comes from the cone, not from null
8195        // directions in the penalty), so suppression is rarely a loss.
8196        //
8197        // `applied_rotation` carries the Q that was applied (or `None`
8198        // if no rotation fired). It is persisted onto `SmoothTerm` below
8199        // so prediction-side `X_new_raw · Q` replay can reproduce the
8200        // exact rotation. Persistence through the saved-model artifact
8201        // is a follow-up — see the doc on `SmoothTerm.joint_null_rotation`.
8202        let applied_rotation: Option<crate::basis::JointNullRotation> = match (
8203            built.joint_null_rotation.take(),
8204            lb_local.is_some(),
8205            built.linear_constraints.is_some(),
8206        ) {
8207            (Some(rot), false, false) => {
8208                let q = &rot.rotation;
8209                let dense = built
8210                    .design
8211                    .try_to_dense_by_chunks("joint-null absorption rotation")
8212                    .map_err(BasisError::InvalidInput)?;
8213                let rotated = gam_linalg::faer_ndarray::fast_ab(&dense, q);
8214                built.design = DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(rotated));
8215                built.penalties = built
8216                    .penalties
8217                    .into_iter()
8218                    .map(|s_local| {
8219                        let qt_s = gam_linalg::faer_ndarray::fast_atb(q, &s_local);
8220                        gam_linalg::faer_ndarray::fast_ab(&qt_s, q)
8221                    })
8222                    .collect();
8223                built.ops = vec![None; built.penalties.len()];
8224                built.kronecker_factored = None;
8225                Some(rot)
8226            }
8227            (Some(_), _, _) => None,
8228            (None, _, _) => None,
8229        };
8230
8231        let activeinfos = built
8232            .penaltyinfo
8233            .iter()
8234            .filter(|info| info.active)
8235            .collect::<Vec<_>>();
8236        if activeinfos.len() != built.penalties.len() {
8237            crate::bail_invalid_basis!(
8238                "internal penalty info mismatch for term '{}': activeinfos={}, penalties={}",
8239                term.name,
8240                activeinfos.len(),
8241                built.penalties.len()
8242            );
8243        }
8244        for (((s_local, &ns), info), op_local) in built
8245            .penalties
8246            .iter()
8247            .zip(built.nullspaces.iter())
8248            .zip(activeinfos.into_iter())
8249            .zip(built.ops.iter())
8250        {
8251            let global_index = penalties_global.len();
8252            penalties_global.push(
8253                BlockwisePenalty::new(col_start..col_end, s_local.clone())
8254                    .with_op(op_local.clone()),
8255            );
8256            nullspace_dims_global.push(ns);
8257            let mut penalty = info.clone();
8258            penalty.nullspace_dim_hint = ns;
8259            penaltyinfo_global.push(PenaltyBlockInfo {
8260                global_index,
8261                termname: Some(term.name.clone()),
8262                penalty,
8263            });
8264        }
8265        for info in built.penaltyinfo.iter().filter(|info| !info.active) {
8266            dropped_penaltyinfo_global.push(DroppedPenaltyBlockInfo {
8267                termname: Some(term.name.clone()),
8268                penalty: info.clone(),
8269            });
8270        }
8271        for info in &built.pre_dropped_penaltyinfo {
8272            dropped_penaltyinfo_global.push(DroppedPenaltyBlockInfo {
8273                termname: Some(term.name.clone()),
8274                penalty: info.clone(),
8275            });
8276        }
8277
8278        if let Some(lin_local) = &built.linear_constraints {
8279            for r in 0..lin_local.a.nrows() {
8280                linear_constraintsrows.push((col_start, col_end, lin_local.a.row(r).to_owned()));
8281                linear_constraints_b.push(lin_local.b[r]);
8282            }
8283        }
8284        if let Some(lb_local) = &lb_local {
8285            coefficient_lower_bounds
8286                .slice_mut(s![col_start..col_end])
8287                .assign(lb_local);
8288            any_bounds = true;
8289        }
8290
8291        // Move the per-term design out of `built` rather than cloning it.
8292        local_designs.push(built.design);
8293
8294        terms_out.push(SmoothTerm {
8295            name: term.name.clone(),
8296            coeff_range: col_start..col_end,
8297            shape: term.shape,
8298            penalties_local: built.penalties,
8299            nullspace_dims: built.nullspaces,
8300            penaltyinfo_local: built.penaltyinfo,
8301            metadata: built.metadata,
8302            lower_bounds_local: lb_local,
8303            linear_constraints_local: built.linear_constraints,
8304            kronecker_factored: built.kronecker_factored.take(),
8305            joint_null_rotation: applied_rotation,
8306            unabsorbed_global_orthogonality: None,
8307        });
8308
8309        col_start = col_end;
8310    }
8311
8312    assert_eq!(
8313        penalties_global.len(),
8314        nullspace_dims_global.len(),
8315        "global smooth penalty/nullspace bookkeeping diverged"
8316    );
8317    assert_eq!(
8318        penalties_global.len(),
8319        penaltyinfo_global.len(),
8320        "global smooth penalty metadata bookkeeping diverged"
8321    );
8322
8323    Ok(RawSmoothDesign {
8324        term_designs: local_designs,
8325        penalties: penalties_global,
8326        nullspace_dims: nullspace_dims_global,
8327        penaltyinfo: penaltyinfo_global,
8328        dropped_penaltyinfo: dropped_penaltyinfo_global,
8329        terms: terms_out,
8330        coefficient_lower_bounds: if any_bounds {
8331            Some(coefficient_lower_bounds)
8332        } else {
8333            None
8334        },
8335        linear_constraints: if linear_constraintsrows.is_empty() {
8336            None
8337        } else {
8338            let mut a = Array2::<f64>::zeros((linear_constraintsrows.len(), total_p));
8339            for (i, (cs, ce, values)) in linear_constraintsrows.iter().enumerate() {
8340                a.row_mut(i).slice_mut(s![*cs..*ce]).assign(values);
8341            }
8342            Some(LinearInequalityConstraints {
8343                a,
8344                b: Array1::from_vec(linear_constraints_b),
8345            })
8346        },
8347    })
8348}