Skip to main content

gam_models/fit_orchestration/
entry.rs

1use super::*;
2
3/// Request-specific inputs to the canonical standard-fit `FitOptions`.
4///
5/// Everything in here varies per call (the link state extracted from the
6/// formula/config, the linear constraints synthesized from `bounded()` /
7/// shape-constrained terms, the Firth / adaptive-regularization toggles read
8/// off the `FitConfig`). Every *policy* field of `FitOptions` — the ones that
9/// decide HOW the outer REML optimization behaves (`compute_inference`,
10/// `skip_rho_posterior_inference`, `tol`, the `max_iter` default, the penalty
11/// shrinkage floor) — is filled in by [`canonical_standard_fit_options`] and is
12/// NOT settable here, so the CLI binary and the Python/PyO3 path cannot resolve
13/// a different optimization policy for the same model (#1196). Before this seam
14/// existed the CLI hand-built `FitOptions` with `tol: 1e-6` /
15/// `skip_rho_posterior_inference: false` while the formula path used
16/// `tol: 1e-10` / `skip_rho_posterior_inference: true`, so the identical model
17/// fit *differently* depending on which entry point you called it from — the
18/// exact class of divergence #1191 surfaced.
19#[derive(Default)]
20pub struct StandardFitOptionsInputs {
21    pub latent_cloglog: Option<LatentCLogLogState>,
22    pub mixture_link: Option<MixtureLinkSpec>,
23    pub optimize_mixture: bool,
24    pub sas_link: Option<SasLinkSpec>,
25    pub optimize_sas: bool,
26    pub linear_constraints: Option<gam_solve::pirls::LinearInequalityConstraints>,
27    pub firth_bias_reduction: bool,
28    pub adaptive_regularization: Option<AdaptiveRegularizationOptions>,
29    /// `Some` only when a caller (the forced-Firth CLI branch) overrides the
30    /// canonical default. `None` keeps the single-source default `Some(1e-6)`.
31    pub penalty_shrinkage_floor_override: Option<Option<f64>>,
32    pub persist_warm_start_disk: bool,
33}
34
35/// The single source of truth for standard-fit `FitOptions` *policy*.
36///
37/// Both standard-fit entry points — `materialize_standard` (the formula /
38/// Python / PyO3 path) and the `gam` CLI's `run_fit` — construct their
39/// `StandardFitRequest` options through this function, so the outer REML
40/// optimization policy (`compute_inference`, `skip_rho_posterior_inference`,
41/// `tol`, `max_iter` default, `penalty_shrinkage_floor`) is identical by
42/// construction. New policy fields must be set HERE, never re-derived at a call
43/// site, which is what makes Python/CLI behavioral divergence structurally
44/// impossible rather than enforced by parallel-but-equal code (#1196).
45pub fn canonical_standard_fit_options(
46    config: &FitConfig,
47    inputs: StandardFitOptionsInputs,
48) -> FitOptions {
49    FitOptions {
50        latent_cloglog: inputs.latent_cloglog,
51        mixture_link: inputs.mixture_link,
52        optimize_mixture: inputs.optimize_mixture,
53        sas_link: inputs.sas_link,
54        optimize_sas: inputs.optimize_sas,
55        // Posterior covariance is always computed so `predict --uncertainty`
56        // works for every family (the `COV_MAX_P` diagonal fallback caps cost).
57        compute_inference: true,
58        // Formula/CLI fits are the interactive/default path: keep coefficient
59        // covariance and the smoothing correction, but do not run the optional
60        // live-rho posterior certificate/escalation, which can launch NUTS over
61        // rho and turn ordinary fits into sampler benchmarks. Lower-level
62        // callers that explicitly need the rho posterior opt in elsewhere.
63        skip_rho_posterior_inference: true,
64        max_iter: config.outer_max_iter.unwrap_or(200),
65        // Outer REML/LAML smoothing-selection tolerance. `1e-10` (effective
66        // projected-gradient threshold ≈ 1e-7) resolves λ̂ to optimiser
67        // precision and restores the `w=c ⇔ c-fold replication` invariance in
68        // smoothing selection (gam#893). The CLI previously used the stale
69        // `1e-6`, which over-smoothed relative to the formula path.
70        tol: 1e-10,
71        nullspace_dims: vec![],
72        linear_constraints: inputs.linear_constraints,
73        firth_bias_reduction: inputs.firth_bias_reduction,
74        adaptive_regularization: inputs.adaptive_regularization,
75        penalty_shrinkage_floor: inputs
76            .penalty_shrinkage_floor_override
77            .unwrap_or(Some(1e-6)),
78        rho_prior: Default::default(),
79        kronecker_penalty_system: None,
80        kronecker_factored: None,
81        persist_warm_start_disk: inputs.persist_warm_start_disk,
82    }
83}
84
85pub fn fit_model(request: FitRequest<'_>) -> Result<FitResult, WorkflowError> {
86    // Disk warm-start persistence is opt-in. The always-on in-memory warm start
87    // remains inside the fit engines, but the workflow dispatcher must not open
88    // the shared WarmStartStore for ordinary formula fits: refit-heavy quality
89    // tests get no cross-process reuse and previously paid cache lookup,
90    // checkpoint, and eviction scans on every replicate (#1082/#1114).
91    let request = request;
92    // Each `fit_*_model` helper still returns `Result<_, String>` internally;
93    // the boundary conversion happens here so the public API returns
94    // `WorkflowError::IntegrationFailed` carrying the underlying solver text.
95    let wrap_solver_err =
96        |reason: String| -> WorkflowError { WorkflowError::IntegrationFailed { reason } };
97    match request {
98        FitRequest::Standard(request) => fit_standard_model(request)
99            .map(FitResult::Standard)
100            .map_err(wrap_solver_err),
101        FitRequest::GaussianLocationScale(request) => fit_gaussian_location_scale_model(request)
102            .map(FitResult::GaussianLocationScale)
103            .map_err(wrap_solver_err),
104        FitRequest::BinomialLocationScale(request) => fit_binomial_location_scale_model(request)
105            .map(FitResult::BinomialLocationScale)
106            .map_err(wrap_solver_err),
107        FitRequest::DispersionLocationScale(request) => {
108            fit_dispersion_location_scale_model(request)
109                .map(FitResult::DispersionLocationScale)
110                .map_err(wrap_solver_err)
111        }
112        FitRequest::SurvivalLocationScale(request) => fit_survival_location_scale_model(request)
113            .map(FitResult::SurvivalLocationScale)
114            .map_err(wrap_solver_err),
115        FitRequest::SurvivalTransformation(request) => fit_survival_transformation_model(request)
116            .map(FitResult::SurvivalTransformation)
117            .map_err(wrap_solver_err),
118        FitRequest::BernoulliMarginalSlope(request) => fit_bernoulli_marginal_slope_model(request)
119            .map(FitResult::BernoulliMarginalSlope)
120            .map_err(wrap_solver_err),
121        FitRequest::SurvivalMarginalSlope(request) => fit_survival_marginal_slope_model(request)
122            .map(FitResult::SurvivalMarginalSlope)
123            .map_err(wrap_solver_err),
124        FitRequest::LatentSurvival(request) => fit_latent_survival_model(request)
125            .map(FitResult::LatentSurvival)
126            .map_err(wrap_solver_err),
127        FitRequest::LatentBinary(request) => fit_latent_binary_model(request)
128            .map(FitResult::LatentBinary)
129            .map_err(wrap_solver_err),
130        FitRequest::TransformationNormal(request) => fit_transformation_normal_model(request)
131            .map(FitResult::TransformationNormal)
132            .map_err(wrap_solver_err),
133    }
134}
135/// Resolve the [`gam_runtime::resource::ResourcePolicy`] backing term construction
136/// for a given [`FitConfig`] + dataset.
137///
138/// If the caller hasn't supplied an explicit policy override, derive one from
139/// the shape of the problem via
140/// [`gam_runtime::resource::ResourcePolicy::for_problem`]. At large scale (n_rows
141/// >= 100k or the marginal-slope large-scale path active) this returns
142/// > `analytic_operator_required` so that any silent dense materialization in
143/// > the term-construction layer fails fast rather than allocating tens of GiB;
144/// > at small scale it falls through to the permissive default-library policy
145/// > so that non-operator bases still build cleanly.
146///
147/// `p_estimate = 0` because the per-block coefficient count isn't known until
148/// the spec has been built; the n_rows and hints triggers are sufficient to
149/// flip strict mode for every shape that needs it.
150pub(crate) fn resolved_resource_policy(
151    config: &FitConfig,
152    data: &Dataset,
153    hints: gam_runtime::resource::ProblemHints,
154) -> gam_runtime::resource::ResourcePolicy {
155    if let Some(p) = config.resource_policy.clone() {
156        return p;
157    }
158    gam_runtime::resource::ResourcePolicy::for_problem(data.values.nrows(), 0, hints)
159}
160
161pub(crate) fn marginal_slope_hints(config: &FitConfig) -> gam_runtime::resource::ProblemHints {
162    gam_runtime::resource::ProblemHints {
163        marginal_slope_large_scale_active: config.logslope_formula.is_some()
164            || config.z_column.is_some(),
165    }
166}
167/// Parse, materialize, and fit a model in one call.
168/// Resolve the expectile asymmetry `τ` requested by `config`, if any.
169///
170/// Returns `Ok(Some(τ))` when `config.family` is `"expectile"` (optionally with
171/// an inline asymmetry, `"expectile(0.9)"`), `Ok(None)` for every other family,
172/// and `Err` when an expectile request carries an out-of-range `τ`. The inline
173/// form takes precedence over the explicit [`FitConfig::expectile_tau`] field
174/// only when both are present and disagree is rejected as a contradiction; when
175/// neither pins `τ`, the median expectile `τ = 0.5` (the ordinary mean fit) is
176/// the default.
177fn expectile_tau_for_config(config: &FitConfig) -> Result<Option<f64>, WorkflowError> {
178    let Some(raw) = config.family.as_deref() else {
179        return Ok(None);
180    };
181    let trimmed = raw.trim();
182    let lower = trimmed.to_ascii_lowercase();
183    if !(lower == "expectile" || lower.starts_with("expectile(")) {
184        return Ok(None);
185    }
186    let invalid = |reason: String| WorkflowError::InvalidConfig { reason };
187    // Optional inline asymmetry: `expectile(0.9)`.
188    let inline_tau = if let Some(rest) = lower.strip_prefix("expectile(") {
189        let inner = rest.strip_suffix(')').ok_or_else(|| {
190            invalid(format!(
191                "expectile family asymmetry must be written as `expectile(τ)`; got `{trimmed}`"
192            ))
193        })?;
194        let value: f64 = inner.trim().parse().map_err(|_| {
195            invalid(format!(
196                "expectile asymmetry `{}` is not a finite number",
197                inner.trim()
198            ))
199        })?;
200        Some(value)
201    } else {
202        None
203    };
204    let tau = match (inline_tau, config.expectile_tau) {
205        (Some(a), Some(b)) if (a - b).abs() > 0.0 => {
206            return Err(invalid(format!(
207                "expectile asymmetry given both inline (`expectile({a})`) and via expectile_tau \
208                 ({b}); supply exactly one"
209            )));
210        }
211        (Some(a), _) => a,
212        (None, Some(b)) => b,
213        (None, None) => 0.5,
214    };
215    if !(tau.is_finite() && tau > 0.0 && tau < 1.0) {
216        return Err(invalid(format!(
217            "expectile asymmetry τ must be finite and strictly in (0, 1); got {tau}"
218        )));
219    }
220    Ok(Some(tau))
221}
222
223/// Per-row asymmetric LAWS weight `wᵢ(τ) = τ` if `yᵢ > μᵢ` else `1 − τ`, scaled
224/// by the base prior weight. At the boundary `yᵢ = μᵢ` the two half-weights
225/// agree in the limit only at `τ = 0.5`; the convention `yᵢ > μᵢ ⇒ τ` (strict)
226/// matches Newey–Powell's lower-closed asymmetric loss and is what `expectreg`
227/// uses. The fixed point is independent of the tie convention because ties form
228/// a measure-zero set under any continuous response.
229fn expectile_row_weights(
230    y: ArrayView1<f64>,
231    mu: ArrayView1<f64>,
232    base: ArrayView1<f64>,
233    tau: f64,
234) -> Array1<f64> {
235    Array1::from_shape_fn(y.len(), |i| {
236        let asym = if y[i] > mu[i] { tau } else { 1.0 - tau };
237        base[i] * asym
238    })
239}
240
241fn constant_gaussian_standard_fit(
242    request: &StandardFitRequest<'_>,
243) -> Result<StandardFitResult, WorkflowError> {
244    if !request.family.is_gaussian_identity() || request.y.is_empty() {
245        return Err(WorkflowError::InvalidConfig {
246            reason: "constant Gaussian shortcut requires a non-empty Gaussian identity request"
247                .to_string(),
248        });
249    }
250    if request.y.iter().any(|value| !value.is_finite())
251        || request.offset.iter().any(|value| !value.is_finite())
252        || request
253            .weights
254            .iter()
255            .any(|value| !value.is_finite() || *value < 0.0)
256    {
257        return Err(WorkflowError::InvalidConfig {
258            reason: "constant Gaussian shortcut requires finite response, offset, and non-negative weights"
259                .to_string(),
260        });
261    }
262    let weight_sum = request.weights.sum();
263    if !(weight_sum.is_finite() && weight_sum > 0.0) {
264        return Err(WorkflowError::InvalidConfig {
265            reason: "constant Gaussian shortcut requires positive total weight".to_string(),
266        });
267    }
268    let mut centered_sum = 0.0_f64;
269    for i in 0..request.y.len() {
270        centered_sum += request.weights[i] * (request.y[i] - request.offset[i]);
271    }
272    let intercept = centered_sum / weight_sum;
273    let design =
274        build_term_collection_design(request.data.view(), &request.spec).map_err(|err| {
275            WorkflowError::InvalidConfig {
276                reason: format!("constant Gaussian shortcut could not rebuild design: {err}"),
277            }
278        })?;
279    let p = design.design.ncols();
280    let mut beta = Array1::<f64>::zeros(p);
281    for col in design.intercept_range.clone() {
282        if col < p {
283            beta[col] = intercept;
284        }
285    }
286    let lambdas = Array1::<f64>::ones(design.penalties.len());
287    let log_lambdas = Array1::<f64>::zeros(design.penalties.len());
288    let fit =
289        gam_solve::estimate::UnifiedFitResult::try_from_parts(gam_solve::estimate::UnifiedFitResultParts {
290            blocks: vec![gam_solve::estimate::FittedBlock {
291                beta: beta.clone(),
292                role: gam_problem::BlockRole::Mean,
293                edf: design.intercept_range.len() as f64,
294                lambdas: lambdas.clone(),
295            }],
296            log_lambdas,
297            lambdas,
298            likelihood_family: Some(request.family.clone()),
299            likelihood_scale: gam_problem::LikelihoodScaleMetadata::ProfiledGaussian,
300            log_likelihood_normalization: gam_problem::LogLikelihoodNormalization::UserProvided,
301            log_likelihood: 0.0,
302            deviance: 0.0,
303            reml_score: 0.0,
304            stable_penalty_term: 0.0,
305            penalized_objective: 0.0,
306            used_device: false,
307            outer_iterations: 0,
308            outer_converged: true,
309            outer_gradient_norm: Some(0.0),
310            standard_deviation: 0.0,
311            covariance_conditional: None,
312            covariance_corrected: None,
313            inference: None,
314            fitted_link: gam_solve::estimate::FittedLinkState::Standard(None),
315            geometry: None,
316            block_states: Vec::new(),
317            pirls_status: gam_solve::pirls::PirlsStatus::Converged,
318            max_abs_eta: intercept.abs(),
319            constraint_kkt: None,
320            artifacts: gam_solve::estimate::FitArtifacts {
321                pirls: None,
322                ..Default::default()
323            },
324            inner_cycles: 0,
325        })
326        .map_err(|err| WorkflowError::IntegrationFailed {
327            reason: format!("constant Gaussian shortcut produced invalid fit: {err}"),
328        })?;
329    let resolvedspec =
330        freeze_term_collection_from_design(&request.spec, &design).map_err(|err| {
331            WorkflowError::InvalidConfig {
332                reason: format!("constant Gaussian shortcut could not freeze design: {err}"),
333            }
334        })?;
335    Ok(StandardFitResult {
336        fit,
337        design,
338        resolvedspec,
339        adaptive_diagnostics: None,
340        kappa_timing: None,
341        saved_link_state: gam_solve::estimate::FittedLinkState::Standard(None),
342        wiggle_knots: None,
343        wiggle_degree: None,
344        wiggle_saved_warp_beta: None,
345    })
346}
347
348fn gaussian_response_is_constant(request: &StandardFitRequest<'_>) -> bool {
349    if !request.family.is_gaussian_identity()
350        || request.y.is_empty()
351        || request.y.iter().any(|value| !value.is_finite())
352    {
353        return false;
354    }
355    let (lo, hi) = request
356        .y
357        .iter()
358        .fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), &value| {
359            (lo.min(value), hi.max(value))
360        });
361    (hi - lo).abs() <= 1.0e-12 * hi.abs().max(1.0)
362}
363
364pub fn fit_from_formula(
365    formula: &str,
366    data: &Dataset,
367    config: &FitConfig,
368) -> Result<FitResult, WorkflowError> {
369    // Expectile regression (Newey–Powell asymmetric least squares): when the
370    // family resolves to "expectile", the τ-expectile of `y | x` is the
371    // minimizer of `Σ wᵢ(τ)·(yᵢ − μᵢ)²`, `wᵢ(τ) = τ` if `yᵢ > μᵢ` else `1 − τ`
372    // — the smooth analogue of the τ-quantile. The minimizer is a Least
373    // Asymmetrically Weighted Squares (LAWS) fixed point: iterate the penalized
374    // Gaussian-identity GAM with `wᵢ(τ)` recomputed from the current `μᵢ` until
375    // the residual-sign pattern stabilizes. REML λ-selection runs inside each
376    // inner Gaussian solve, so every gam smooth/tensor/spatial basis becomes a
377    // penalized expectile smooth with data-driven smoothing for free. This is a
378    // genuine estimator route, not a silent swap: it fires only on the explicit
379    // `family = "expectile"`. Every other family falls through unchanged.
380    if let Some(result) = fit_expectile_if_requested(formula, data, config)? {
381        return Ok(FitResult::Standard(result));
382    }
383    let mat = materialize(formula, data, config)?;
384    // Exact O(n) spline-scan fast path (#1030): when the materialized request
385    // is the single 1-D Gaussian-identity penalized-smooth shape the
386    // state-space scan solves exactly, route through it and return the
387    // scan-bearing model directly — the same penalized posterior at O(n) per
388    // λ-trial instead of the dense design/Gram route. Detection is structural
389    // and conservative (see `spline_scan_fast_path`); every other shape falls
390    // through to the dense `fit_model` path unchanged. Mirrors the CLI
391    // (main.rs run_fit) and FFI consumers, which build the persistence payload
392    // from this same `SplineScanFit`.
393    if let FitRequest::Standard(request) = &mat.request {
394        if gaussian_response_is_constant(request) {
395            return constant_gaussian_standard_fit(request).map(FitResult::Standard);
396        }
397        if let Some(inputs) = spline_scan_fast_path(request) {
398            let scan = gam_solve::spline_scan::fit_spline_scan(
399                &inputs.x,
400                &inputs.y,
401                &inputs.w,
402                inputs.order,
403            )
404            .map_err(|reason| WorkflowError::IntegrationFailed { reason })?;
405            return Ok(FitResult::SplineScan(scan));
406        }
407        // O(n log n) multiresolution residual-cascade fast path (#1032): a
408        // scattered low-d Gaussian-identity Duchon/Matérn smooth past the
409        // dense-kernel cliff. UNLIKE the scan, the cascade is a DIFFERENT
410        // posterior from the dense radial term, so it only ever fires as an
411        // explicit alternative estimator on the exact structural signature
412        // (`residual_cascade_fast_path`) AND when the in-cascade quasi-uniformity
413        // guard certifies the metric — a rejected metric or any ineligible shape
414        // falls through to the dense `fit_model` path (a genuine estimator
415        // choice, never a silent swap). The save paths build the persistence
416        // payload from this `ResidualCascadeFit`'s `to_state` snapshot.
417        if let Some(inputs) = residual_cascade_fast_path(request) {
418            let coord_refs: Vec<&[f64]> = inputs.coords.iter().map(Vec::as_slice).collect();
419            if let Ok(fit) = gam_solve::residual_cascade::fit_residual_cascade(
420                &coord_refs,
421                &inputs.y,
422                &inputs.w,
423                &inputs.metric,
424                inputs.sobolev_s,
425            ) {
426                return Ok(FitResult::ResidualCascade(fit));
427            }
428            // The quasi-uniformity guard (caveat 2) or any degenerate-design
429            // signal surfaces as a build/solve error; fall through to the dense
430            // kernel path rather than failing the fit outright.
431        }
432    }
433    // `fit_model` already returns `WorkflowError` end-to-end; propagate it
434    // directly instead of stringifying then re-wrapping.
435    fit_model(mat.request)
436}
437
438/// THE single dispatch seam for the expectile (Newey–Powell LAWS) family.
439///
440/// Returns `Ok(Some(result))` with the converged τ-expectile as an ordinary
441/// [`StandardFitResult`] when `config.family` selects the expectile family
442/// (`"expectile"` or `"expectile(τ)"`, optionally pinned by
443/// [`FitConfig::expectile_tau`]), `Ok(None)` for every other family — in which
444/// case the caller runs its normal materialize/`fit_model` path — and `Err` on a
445/// malformed expectile request or an inner-fit failure.
446///
447/// Every public entry point that resolves a family routes through this seam
448/// *before* materializing: the in-process [`fit_from_formula`], the Python FFI
449/// (`gam-pyffi`), and the `gam` CLI. Centralizing the dispatch here is what makes
450/// the estimator reachable from every interface instead of only the library
451/// call — and what prevents the class of bug where a newly-added outer estimator
452/// is wired into one entry point and silently bypassed by the others (#1777).
453/// The returned [`StandardFitResult`] carries the full design / resolved spec /
454/// fit, so each caller builds its persistence payload from it exactly as it does
455/// for any other standard fit.
456pub fn fit_expectile_if_requested(
457    formula: &str,
458    data: &Dataset,
459    config: &FitConfig,
460) -> Result<Option<StandardFitResult>, WorkflowError> {
461    match expectile_tau_for_config(config)? {
462        Some(tau) => Ok(Some(fit_expectile_laws(formula, data, config, tau)?)),
463        None => Ok(None),
464    }
465}
466
467/// Least Asymmetrically Weighted Squares (LAWS) driver for expectile GAMs.
468///
469/// The τ-expectile surface minimizes `Σ wᵢ(τ)·(yᵢ − μᵢ)²` with the residual-
470/// sign asymmetric weight `wᵢ(τ)`. Because that weight is piecewise-constant in
471/// `sign(yᵢ − μᵢ)`, the objective is the supremum of a finite family of
472/// weighted least-squares problems and its minimizer is the unique fixed point
473/// of: *solve the penalized WLS with weights frozen at the current sign
474/// pattern, then recompute the sign pattern from the new fit*. The asymmetric
475/// loss is strictly convex (weights bounded in `[min(τ,1−τ), max(τ,1−τ)] > 0`),
476/// so this monotone-descent iteration converges, and since the sign pattern
477/// takes finitely many values it stabilizes in finitely many steps (Schnabel &
478/// Eilers 2009; the same Newton/IRLS-for-expectiles `expectreg` runs).
479///
480/// Each inner solve is the FULL standard Gaussian-identity GAM: any basis,
481/// tensor, spatial smooth, by-variable, random effect, plus REML λ-selection on
482/// the current asymmetric weights. The returned fit is an ordinary
483/// [`FitResult::Standard`] whose coefficients ARE the penalized τ-expectile —
484/// every downstream consumer (predict, posterior bands, persistence) works
485/// unchanged. The reported scale is the asymmetric working variance, so
486/// expectile standard errors are the sandwich-free Gaussian-form bands of the
487/// converged weighted problem (a deliberate first-rung choice; see #1100).
488fn fit_expectile_laws(
489    formula: &str,
490    data: &Dataset,
491    config: &FitConfig,
492    tau: f64,
493) -> Result<StandardFitResult, WorkflowError> {
494    use gam_linalg::matrix::LinearOperator;
495
496    if config.frailty.as_ref().is_some_and(FrailtySpec::is_active) {
497        return Err(WorkflowError::InvalidConfig {
498            reason: "expectile regression does not support frailty; use a survival/frailty-aware family instead"
499                .to_string(),
500        });
501    }
502
503    // Inner fits are ordinary Gaussian-identity GAMs; the τ asymmetry lives
504    // entirely in the per-iteration prior weights this driver injects.
505    let gaussian_config = FitConfig {
506        family: Some("gaussian".to_string()),
507        link: Some("identity".to_string()),
508        expectile_tau: None,
509        // The inner Gaussian-identity design carries no frailty. Normalize the
510        // CLI/config-layer null value (`Some(FrailtySpec::None)`) to `None` so
511        // the expectile driver does not leak survival-only plumbing into the
512        // standard-family materializer, while the active-frailty guard above
513        // still rejects unsupported frailty requests explicitly.
514        frailty: None,
515        ..config.clone()
516    };
517
518    // Materialize once to capture the fixed training design, response, offset,
519    // and base prior weights. The design (basis, penalties, identifiability
520    // transforms) does not depend on the prior weights, so it is reused across
521    // every LAWS iteration; only the weight vector and the resulting β change.
522    let base_mat = materialize(formula, data, &gaussian_config)?;
523    let FitRequest::Standard(base_request) = base_mat.request else {
524        return Err(WorkflowError::InvalidConfig {
525            reason: "expectile regression is only defined for standard (non-survival, \
526                     non-location-scale) responses"
527                .to_string(),
528        });
529    };
530    let StandardFitRequest {
531        data: design_data,
532        y,
533        weights: base_weights,
534        offset,
535        spec,
536        family: materialized_family,
537        estimate_tweedie_p: _,
538        options,
539        kappa_options,
540        wiggle,
541        coefficient_groups,
542        penalty_block_gamma_priors,
543        latent_coord,
544        _marker,
545    } = base_request;
546    // The materializer already resolved the inner family to Gaussian-identity
547    // from `gaussian_config`; assert it so a future materializer change that
548    // silently picked a different family for `"gaussian"` is caught here rather
549    // than producing a non-expectile fit.
550    if !materialized_family.is_gaussian_identity() {
551        return Err(WorkflowError::InvalidConfig {
552            reason: format!(
553                "expectile LAWS requires a Gaussian-identity inner family; materializer produced {}",
554                materialized_family.name()
555            ),
556        });
557    }
558
559    if wiggle.is_some() || latent_coord.is_some() {
560        return Err(WorkflowError::InvalidConfig {
561            reason: "expectile regression does not support flexible-link wiggle or latent \
562                     coordinates"
563                .to_string(),
564        });
565    }
566
567    let n = y.len();
568    let gaussian_family = LikelihoodSpec::gaussian_identity();
569    // Cold start: τ = 0.5 (symmetric) weights ⇒ the first inner fit is the OLS
570    // mean GAM, the natural warm start for any τ.
571    let mut weights = base_weights.clone();
572    let mut last_sign: Option<Vec<bool>> = None;
573    let mut last_result: Option<StandardFitResult> = None;
574
575    // The sign pattern has 2ⁿ values but LAWS visits a monotone-descent subset;
576    // empirically a handful of iterations suffice. The cap is a safety guard:
577    // on the rare oscillation between two equal-objective sign patterns (only
578    // possible when rows sit exactly on the fitted surface) the last fit is a
579    // valid τ-expectile of the perturbation-stable problem, so returning it is
580    // correct rather than an error.
581    const MAX_LAWS_ITERS: usize = 50;
582
583    for _iter in 0..MAX_LAWS_ITERS {
584        let request = StandardFitRequest {
585            data: design_data.clone(),
586            y: y.clone(),
587            weights: weights.clone(),
588            offset: offset.clone(),
589            spec: spec.clone(),
590            family: gaussian_family.clone(),
591            // Expectile LAWS fits a Gaussian-identity inner family; no Tweedie
592            // power to estimate (#2026).
593            estimate_tweedie_p: false,
594            options: options.clone(),
595            kappa_options: kappa_options.clone(),
596            wiggle: None,
597            coefficient_groups: coefficient_groups.clone(),
598            penalty_block_gamma_priors: penalty_block_gamma_priors.clone(),
599            latent_coord: None,
600            _marker,
601        };
602        let result = fit_standard_model(request)
603            .map_err(|reason| WorkflowError::IntegrationFailed { reason })?;
604
605        // Training-scale fitted mean μ = X·β (identity link, zero-checked
606        // offset folded by the design path). The design columns match the
607        // combined coefficient vector exactly (the same contract `predict`
608        // and the safety tests rely on).
609        let mu = result.design.design.apply(&result.fit.beta);
610        if mu.len() != n {
611            return Err(WorkflowError::IntegrationFailed {
612                reason: format!(
613                    "expectile LAWS: fitted mean length {} disagrees with response length {n}",
614                    mu.len()
615                ),
616            });
617        }
618        let mut mu_off = mu;
619        mu_off += &offset;
620
621        let sign: Vec<bool> = (0..n).map(|i| y[i] > mu_off[i]).collect();
622        let converged = last_sign.as_ref().is_some_and(|prev| prev == &sign);
623        weights = expectile_row_weights(y.view(), mu_off.view(), base_weights.view(), tau);
624        last_sign = Some(sign);
625        last_result = Some(result);
626        if converged {
627            break;
628        }
629    }
630
631    let result = last_result.ok_or_else(|| WorkflowError::IntegrationFailed {
632        reason: "expectile LAWS produced no fit".to_string(),
633    })?;
634    Ok(result)
635}
636/// Detection seam for the exact O(n) cubic-smoothing-spline fast path.
637///
638/// This is the EARLIEST point in the standard workflow where a materialized
639/// fit request carries everything needed to prove the model is exactly the
640/// problem the scan solves: a Gaussian likelihood with identity link over
641/// `intercept + one 1-D cubic-class penalized smooth` — i.e. the penalized
642/// least-squares problem `min Σ w_i (y_i − f(x_i))² + λ∫f″²` with an
643/// unpenalized `{1, x}` null space. The Kalman/RTS scan computes that
644/// posterior (mean, pointwise variance, exact diffuse REML for λ) in O(n) per
645/// λ-trial instead of the dense design/Gram O(n·k²) + O(k³) route.
646///
647/// Returns `Some` only when ALL of the following hold; everything else falls
648/// through to the dense path:
649/// - family is Gaussian + identity link;
650/// - no link wiggle, no latent coordinates, no coefficient groups, no penalty
651///   hyperpriors, no linear/box constraints, no Firth, no adaptive
652///   regularization, no Kronecker systems, no externally injected null-space
653///   dims;
654/// - the term collection is exactly one smooth term — no linear terms, no
655///   random effects, no by-variables / factor interactions;
656/// - that smooth is a plain 1-D B-spline whose penalty order is compatible
657///   with the exact scan and whose null space is unshrunk
658///   (`double_penalty=false`). `double_penalty` (mgcv `select = TRUE`) on a free
659///   B-spline emits a second REML coordinate — the Marra & Wood (2011) null-space
660///   shrinkage block — that the scan cannot represent (its polynomial null space
661///   is an improper diffuse prior it can never shrink); routing such a fit
662///   through the scan would silently drop that penalty and select λ from the
663///   bending penalty alone, which is exactly the EDF inflation #1266 reports.
664///   Those fits fall through to the dense two-rho path, which owns both penalties
665///   jointly. Natural cubic regression (`bs="cr"`/`"cs"`) terms also fall
666///   through: their knot-value parameterization is a finite-rank regression
667///   spline, not the scan's full smoothing-spline state-space posterior;
668/// - the offset is identically zero and every weight is finite and positive;
669/// - at least 3 distinct finite abscissae (the scan's diffuse rank plus one).
670///
671/// λ-mapping note: the scan's penalty is exactly `λ∫f″²` (state-space
672/// `q = 1/λ` at unit σ²). The dense 1-D B-spline path penalizes the same
673/// cubic class through a reduced-rank discrete-difference Gram whose
674/// normalization differs by a basis-dependent constant, so a λ selected by
675/// one parameterization does not transfer numerically to the other. The scan
676/// therefore always re-selects λ by its own exact diffuse REML criterion
677/// (the optimizer of the same restricted likelihood, expressed in the scan's
678/// parameterization); user-pinned smoothing parameters are not representable
679/// at this seam (the formula DSL exposes none for this term class), so no
680/// pinned-λ mapping arises.
681///
682/// Identifiability transforms on the smooth (centering / linear-trend
683/// removal / orthogonality-to-intercept) are accepted as eligible: they only
684/// re-coordinate the unpenalized null space against the implicit intercept
685/// and do not change the fitted posterior of `E[y|x]`, which is what the
686/// scan returns directly.
687pub fn spline_scan_fast_path(request: &StandardFitRequest<'_>) -> Option<SplineScanInputs> {
688    if !request.family.is_gaussian_identity() {
689        return None;
690    }
691    if request.wiggle.is_some()
692        || request.latent_coord.is_some()
693        || !request.coefficient_groups.is_empty()
694        || !request.penalty_block_gamma_priors.is_empty()
695    {
696        return None;
697    }
698    let options = &request.options;
699    if options.latent_cloglog.is_some()
700        || options.mixture_link.is_some()
701        || options.sas_link.is_some()
702        || options.linear_constraints.is_some()
703        || options.adaptive_regularization.is_some()
704        || options.kronecker_penalty_system.is_some()
705        || options.kronecker_factored.is_some()
706        || options.firth_bias_reduction
707        || !options.nullspace_dims.is_empty()
708    {
709        return None;
710    }
711    let spec = &request.spec;
712    if !spec.linear_terms.is_empty()
713        || !spec.random_effect_terms.is_empty()
714        || spec.smooth_terms.len() != 1
715    {
716        return None;
717    }
718    let term = &spec.smooth_terms[0];
719    if !matches!(term.shape, gam_terms::smooth::ShapeConstraint::None)
720        || term.joint_null_rotation.is_some()
721    {
722        return None;
723    }
724    let gam_terms::smooth::SmoothBasisSpec::BSpline1D {
725        feature_col,
726        spec: bspec,
727    } = &term.basis
728    else {
729        return None;
730    };
731    // Smoothing-spline order m = penalty_order ∈ {1, 2, 3}. The exact scan
732    // integrates the order-m integrated-Wiener prior whose natural spline has
733    // degree 2m−1 (m=1 → linear, m=2 → cubic, m=3 → quintic), so require that
734    // degree to match user intent. The de Jong exact diffuse leading-block
735    // smoother (#1044) handles the m−1 partially-diffuse leading nodes for all
736    // m ≤ MAX_ORDER; m > MAX_ORDER falls through to the dense path.
737    let order = bspec.penalty_order;
738    // Double-penalty (mgcv `select = TRUE`) is NOT representable by the scan and
739    // must fall through to the dense two-rho path (#1266). On a free B-spline the
740    // double penalty emits a *second* REML coordinate — the Marra & Wood (2011)
741    // null-space shrinkage block `Z Zᵀ` (see `bspline_penalty_candidates`) —
742    // whose entire purpose is to let REML shrink the unpenalized `{1, x, …}`
743    // polynomial null space toward `EDF → 0` for an unsupported term. The scan,
744    // by construction, carries that null space as an *improper diffuse* prior it
745    // can never shrink (its EDF floor is the null-space dimension `order`), so
746    // routing a `double_penalty` fit through it silently DROPS the second penalty
747    // and selects λ from the single bending penalty alone. The scan's own exact
748    // diffuse REML then genuinely prefers a mildly wiggly fit at finite λ for
749    // some noise realizations (an interior REML optimum, EDF ≈ 3–4), which is the
750    // EDF inflation #1266 reports. The dense path owns both penalties jointly and
751    // its outer REML, seeded into the over-smoothing basin, drives the null space
752    // out (EDF → null-space dim) when the data are truly polynomial. Excluding
753    // `double_penalty` here keeps such a fit on the dense path; single-penalty
754    // and boundary-conditioned single-penalty B-splines keep the exact O(n) scan.
755    if !(1..=3).contains(&order)
756        || bspec.degree != 2 * order - 1
757        || bspec.double_penalty
758        || !bspec.boundary_conditions.is_free()
759        || !matches!(bspec.boundary, gam_terms::basis::OneDimensionalBoundary::Open)
760        || matches!(
761            bspec.knotspec,
762            gam_terms::basis::BSplineKnotSpec::PeriodicUniform { .. }
763                | gam_terms::basis::BSplineKnotSpec::NaturalCubicRegression { .. }
764        )
765        // mgcv `bs="cr"`/`"cs"` materialise a `NaturalCubicRegression` value-knot
766        // spec: a Lancaster–Salkauskas cubic-regression basis whose columns
767        // index `f(x*_i)` at `k` quantile knots — a genuinely DIFFERENT finite
768        // basis (and hence a different penalized posterior) from the free
769        // integrated-Wiener natural spline the exact scan solves on the raw data
770        // points. The scan builds its own knots from `x` and ignores this spec,
771        // so routing a cr fit through it would silently solve the wrong model and
772        // (per #1844) return a non-`Standard` `SplineScan` result the predict-time
773        // design replay cannot reconstruct. Keep cr/cs on the dense path.
774        || matches!(
775            bspec.knotspec,
776            gam_terms::basis::BSplineKnotSpec::NaturalCubicRegression { .. }
777        )
778    {
779        return None;
780    }
781    if request.offset.iter().any(|&v| v != 0.0) {
782        return None;
783    }
784    if request.weights.iter().any(|&v| !(v.is_finite() && v > 0.0)) {
785        return None;
786    }
787    if *feature_col >= request.data.ncols() || request.y.len() != request.data.nrows() {
788        return None;
789    }
790    let x: Vec<f64> = request.data.column(*feature_col).iter().copied().collect();
791    let y: Vec<f64> = request.y.iter().copied().collect();
792    let w: Vec<f64> = request.weights.iter().copied().collect();
793    if x.iter().any(|v| !v.is_finite()) || y.iter().any(|v| !v.is_finite()) {
794        return None;
795    }
796    // The diffuse polynomial null space consumes `order` innovations; the scan
797    // needs at least one proper innovation beyond them to profile σ².
798    let mut sorted = x.clone();
799    sorted.sort_by(f64::total_cmp);
800    sorted.dedup();
801    if sorted.len() < order + 1 {
802        return None;
803    }
804    Some(SplineScanInputs { x, y, w, order })
805}
806
807/// Formula-level entry for the exact O(n) cubic-smoothing-spline fast path.
808///
809/// Materializes the formula exactly like [`fit_from_formula`], then runs the
810/// [`spline_scan_fast_path`] detection on the resulting standard request.
811/// When detection fires the fit is routed through
812/// [`gam_solve::spline_scan::fit_spline_scan`] — the exact diffuse
813/// REML Kalman/RTS scan — and the full in-memory posterior
814/// ([`gam_solve::spline_scan::SplineScanFit`]: knots, smoothed
815/// states, pointwise variances, lag-one gains, σ², log λ, exact EDF, and an
816/// exact `predict`) is returned. `Ok(None)` means the model is not the
817/// scan-eligible shape and the caller should use the dense
818/// [`fit_from_formula`] path; this keeps every persistence-bearing consumer
819/// (model save, CLI, FFI) transparently on the dense fit, whose saved payload
820/// the scan does not yet have a schema for.
821pub fn fit_spline_scan_from_formula(
822    formula: &str,
823    data: &Dataset,
824    config: &FitConfig,
825) -> Result<Option<gam_solve::spline_scan::SplineScanFit>, WorkflowError> {
826    let mat = materialize(formula, data, config)?;
827    let FitRequest::Standard(request) = mat.request else {
828        return Ok(None);
829    };
830    let Some(inputs) = spline_scan_fast_path(&request) else {
831        return Ok(None);
832    };
833    gam_solve::spline_scan::fit_spline_scan(&inputs.x, &inputs.y, &inputs.w, inputs.order)
834        .map(Some)
835        .map_err(|reason| WorkflowError::IntegrationFailed { reason })
836}
837
838/// #1464 diagnostic entry point: evaluate the EXACT production fixed-κ
839/// profiled-REML criterion (`fixed_kappa_profiled_reml_score`, the same one the
840/// joint-fit κ-sign scan uses) at a list of pinned κ values for the first
841/// constant-curvature term of `formula`, materialised from `data`/`config`
842/// exactly like [`fit_from_formula`]. Returns `(κ, V_p(κ))` pairs.
843///
844/// This settles solver-vs-criterion for the railing bug: if `V_p(+κ) < V_p(−κ)`
845/// for a genuinely HYPERBOLIC dataset, the criterion itself prefers the collapsed
846/// +κ corner — the bug is in the constant-curvature REML/Occam term, not the
847/// optimiser. If `V_p(−κ) < V_p(+κ)` yet the full fit still returns +κ, the bug
848/// is in the solver/readback. The profiled fit pins κ and profiles only ρ
849/// (κ-optimisation disabled), so each returned score is the negative-log-evidence
850/// the outer loop minimises.
851pub fn constant_curvature_profiled_reml_scores(
852    formula: &str,
853    data: &Dataset,
854    config: &FitConfig,
855    kappas: &[f64],
856) -> Result<Vec<(f64, f64)>, WorkflowError> {
857    let mat = materialize(formula, data, config)?;
858    let FitRequest::Standard(request) = mat.request else {
859        return Err(WorkflowError::IntegrationFailed {
860            reason: "constant_curvature_profiled_reml_scores: formula did not materialise to a \
861                     standard fit request"
862                .to_string(),
863        });
864    };
865    let term_idx = *crate::fit_orchestration::drivers::constant_curvature_term_indices(&request.spec)
866        .first()
867        .ok_or_else(|| WorkflowError::IntegrationFailed {
868            reason: "constant_curvature_profiled_reml_scores: formula has no constant-curvature \
869                     curv() term"
870                .to_string(),
871        })?;
872    let mut out = Vec::with_capacity(kappas.len());
873    for &kappa in kappas {
874        let score = crate::fit_orchestration::drivers::fixed_kappa_profiled_reml_score(
875            request.data.view(),
876            request.y.view(),
877            request.weights.view(),
878            request.offset.view(),
879            &request.spec,
880            term_idx,
881            kappa,
882            request.family.clone(),
883            &request.options,
884        )
885        .map_err(|e| WorkflowError::IntegrationFailed {
886            reason: format!(
887                "constant_curvature_profiled_reml_scores: fixed-κ fit at κ={kappa} failed: {e}"
888            ),
889        })?;
890        out.push((kappa, score));
891    }
892    Ok(out)
893}
894
895/// Derived dense-kernel cliff: the cascade auto-route fires only once the dense
896/// radial basis the smooth would otherwise use has SATURATED at its center cap
897/// (`default_num_centers == K_MAX`), so the dense `O(n·K² + K³)` kernel solve
898/// can no longer grow resolution with `n` and the streaming cascade's
899/// `O(n·polylog)` is the only path that keeps improving. This is the structural
900/// "past the dense-kernel cliff" condition the issue names — derived from the
901/// dense sizing rule, NOT a magic n constant or a user flag.
902fn past_dense_kernel_cliff(n: usize, d: usize) -> bool {
903    // `default_num_centers` clamps to K_MAX = 2000; equality means the dense
904    // basis is pinned at the cap and cannot densify further with n.
905    const DENSE_CENTER_CAP: usize = 2000;
906    gam_terms::basis::default_num_centers(n, d) >= DENSE_CENTER_CAP
907}
908
909/// Map a Duchon/Matérn smoothness order onto the cascade's Sobolev order,
910/// clamped into the Wendland-(3,1) native window `(d/2, (d+3)/2]` (issue
911/// caveat 1: the multilevel frame can only represent up to `H^{(d+3)/2}`).
912fn cascade_sobolev_order(requested: f64, d: usize) -> f64 {
913    let lo = d as f64 / 2.0;
914    let hi = (d as f64 + 3.0) / 2.0;
915    // Nudge strictly inside the open lower bound when the request lands on it.
916    let eps = 1e-6 * (hi - lo);
917    requested.clamp(lo + eps, hi)
918}
919
920/// Detection seam for the O(n log n) multiresolution residual-cascade fast path
921/// (issue #1032).
922///
923/// This mirrors [`spline_scan_fast_path`] in shape but carries one CRITICAL
924/// difference dictated by the issue: the cascade is **not** the same posterior
925/// as the Duchon/Matérn term it stands in for (a different finite basis — the
926/// multilevel Wendland frame, not the reduced-rank radial kernel). So unlike
927/// the 1-D scan, which silently swaps an identical posterior, this path must
928/// only fire as an explicit alternative estimator on the structural signature
929/// the issue names, never as a transparent replacement. It returns `Some` only
930/// when ALL of the following hold:
931/// - family is Gaussian + identity link (the scattered low-d smooth the
932///   cascade solves);
933/// - none of the exotic-link / constraint / Firth / Kronecker / coefficient-
934///   group / hyperprior machinery is engaged;
935/// - the model is exactly one smooth term — no linear terms, no random
936///   effects, no by-variables;
937/// - that smooth is a scattered radial spatial smooth (`Duchon` or `Matern`)
938///   over `d ∈ {2, 3}` coordinates with no shape constraint;
939/// - the offset is identically zero and every weight is finite and positive;
940/// - `n` is past the derived dense-kernel cliff
941///   ([`past_dense_kernel_cliff`]) — below it the dense radial path is both
942///   exact-posterior and cheap, so there is no reason to change estimators.
943///
944/// The returned [`ResidualCascadeInputs`] carry a unit per-axis metric (the
945/// spec's isotropic radial distance); the quasi-uniformity guard inside
946/// [`gam_solve::residual_cascade::fit_residual_cascade`] (issue caveat 2)
947/// is the no-regression gate that refuses the iterative solve — and forces the
948/// caller back to the dense path — when a near-degenerate metric would break
949/// the BPX iteration bound.
950pub fn residual_cascade_fast_path(
951    request: &StandardFitRequest<'_>,
952) -> Option<ResidualCascadeInputs> {
953    if !request.family.is_gaussian_identity() {
954        return None;
955    }
956    if request.wiggle.is_some()
957        || request.latent_coord.is_some()
958        || !request.coefficient_groups.is_empty()
959        || !request.penalty_block_gamma_priors.is_empty()
960    {
961        return None;
962    }
963    let options = &request.options;
964    if options.latent_cloglog.is_some()
965        || options.mixture_link.is_some()
966        || options.sas_link.is_some()
967        || options.linear_constraints.is_some()
968        || options.adaptive_regularization.is_some()
969        || options.kronecker_penalty_system.is_some()
970        || options.kronecker_factored.is_some()
971        || options.firth_bias_reduction
972        || !options.nullspace_dims.is_empty()
973    {
974        return None;
975    }
976    let spec = &request.spec;
977    if !spec.linear_terms.is_empty()
978        || !spec.random_effect_terms.is_empty()
979        || spec.smooth_terms.len() != 1
980    {
981        return None;
982    }
983    let term = &spec.smooth_terms[0];
984    if !matches!(term.shape, gam_terms::smooth::ShapeConstraint::None)
985        || term.joint_null_rotation.is_some()
986    {
987        return None;
988    }
989    // Only scattered radial spatial smooths (Duchon / Matérn) over 2–3 axes.
990    // The Duchon spectral power `p + s` and the Matérn order set the requested
991    // Sobolev smoothness; both clamp into the Wendland native window.
992    let (feature_cols, requested_s) = match &term.basis {
993        gam_terms::smooth::SmoothBasisSpec::Duchon {
994            feature_cols, spec, ..
995        } => {
996            // Pure-Duchon native order is `p + s` (kernel exponent 2(p+s)−d);
997            // the multilevel frame targets the same continuum smoothness. `p`
998            // is the polynomial nullspace degree, `s` the spectral power.
999            let p = match spec.nullspace_order {
1000                gam_terms::basis::DuchonNullspaceOrder::Zero => 0.0,
1001                gam_terms::basis::DuchonNullspaceOrder::Linear => 1.0,
1002                gam_terms::basis::DuchonNullspaceOrder::Degree(k) => k as f64,
1003            };
1004            (feature_cols, spec.power + p)
1005        }
1006        gam_terms::smooth::SmoothBasisSpec::Matern {
1007            feature_cols, spec, ..
1008        } => {
1009            // Matérn smoothness ν sets native Sobolev order ν + d/2; the cascade
1010            // frame represents up to (d+3)/2, so the clamp below applies the
1011            // ceiling. (d is known just below from feature_cols.)
1012            let nu = spec.nu.half_integer_value();
1013            (feature_cols, nu + feature_cols.len() as f64 / 2.0)
1014        }
1015        _ => return None,
1016    };
1017    let d = feature_cols.len();
1018    if !(2..=3).contains(&d) {
1019        return None;
1020    }
1021    if request.offset.iter().any(|&v| v != 0.0) {
1022        return None;
1023    }
1024    if request.weights.iter().any(|&v| !(v.is_finite() && v > 0.0)) {
1025        return None;
1026    }
1027    let n = request.y.len();
1028    if n != request.data.nrows() || feature_cols.iter().any(|&c| c >= request.data.ncols()) {
1029        return None;
1030    }
1031    if !past_dense_kernel_cliff(n, d) {
1032        return None;
1033    }
1034    let coords: Vec<Vec<f64>> = feature_cols
1035        .iter()
1036        .map(|&c| request.data.column(c).iter().copied().collect())
1037        .collect();
1038    let y: Vec<f64> = request.y.iter().copied().collect();
1039    let w: Vec<f64> = request.weights.iter().copied().collect();
1040    if coords
1041        .iter()
1042        .any(|axis| axis.iter().any(|v| !v.is_finite()))
1043        || y.iter().any(|v| !v.is_finite())
1044    {
1045        return None;
1046    }
1047    let metric = vec![1.0_f64; d];
1048    let sobolev_s = cascade_sobolev_order(requested_s, d);
1049    Some(ResidualCascadeInputs {
1050        coords,
1051        y,
1052        w,
1053        metric,
1054        sobolev_s,
1055    })
1056}
1057
1058/// Formula-level library entry for the O(n log n) residual-cascade fast path
1059/// (issue #1032).
1060///
1061/// Materializes the formula exactly like [`fit_from_formula`], runs the
1062/// [`residual_cascade_fast_path`] detection, and — when it fires AND the
1063/// quasi-uniformity guard inside the cascade certifies the metric — returns the
1064/// certified [`ResidualCascadeFit`](gam_solve::residual_cascade::ResidualCascadeFit).
1065/// `Ok(None)` means EITHER the model is not the cascade-eligible shape OR the
1066/// quasi-uniformity guard rejected the metric; in both cases the caller falls
1067/// back to the dense [`fit_from_formula`] path (the cascade is a different
1068/// posterior, so the fallback is a genuine estimator choice, never a silent
1069/// swap). This keeps every persistence-bearing consumer on the dense fit until
1070/// the cascade payload schema lands.
1071pub fn fit_residual_cascade_from_formula(
1072    formula: &str,
1073    data: &Dataset,
1074    config: &FitConfig,
1075) -> Result<Option<gam_solve::residual_cascade::ResidualCascadeFit>, WorkflowError> {
1076    let mat = materialize(formula, data, config)?;
1077    let FitRequest::Standard(request) = mat.request else {
1078        return Ok(None);
1079    };
1080    let Some(inputs) = residual_cascade_fast_path(&request) else {
1081        return Ok(None);
1082    };
1083    let coord_refs: Vec<&[f64]> = inputs.coords.iter().map(Vec::as_slice).collect();
1084    match gam_solve::residual_cascade::fit_residual_cascade(
1085        &coord_refs,
1086        &inputs.y,
1087        &inputs.w,
1088        &inputs.metric,
1089        inputs.sobolev_s,
1090    ) {
1091        Ok(fit) => Ok(Some(fit)),
1092        // The quasi-uniformity guard (caveat 2) and any degenerate-design
1093        // signal both surface as a build/solve error; treat them as "not
1094        // cascade-eligible" so the caller falls back to the dense kernel path
1095        // rather than failing the fit outright.
1096        Err(_) => Ok(None),
1097    }
1098}
1099
1100/// Parse a formula, resolve it against a dataset, and produce a ready-to-fit `FitRequest`.
1101pub fn materialize<'a>(
1102    formula: &str,
1103    data: &'a Dataset,
1104    config: &FitConfig,
1105) -> Result<MaterializedModel<'a>, WorkflowError> {
1106    gam_gpu::configure_global_policy(config.gpu_policy);
1107    let parsed = parse_formula(formula)?;
1108    let col_map = data.column_map();
1109
1110    if let Some((left_col, right_col, event_col)) = parse_surv_interval_response(&parsed.response)?
1111    {
1112        if config.transformation_normal {
1113            return Err(WorkflowError::InvalidConfig {
1114                reason:
1115                    "transformation_normal cannot be combined with a SurvInterval(...) response"
1116                        .to_string(),
1117            });
1118        }
1119        // Interval censoring `T ∈ (L, R]` is only defined for the latent
1120        // hazard-window survival likelihood, whose kernel carries the
1121        // `log[S(L) − S(R)]` interval contribution. Route the left boundary `L`
1122        // through the standard exit channel and the right boundary `R` through
1123        // the dedicated interval-right channel; `event_col` distinguishes
1124        // bracketed (interval) rows from right-censored rows beyond the last
1125        // inspection (which carry an infinite/sentinel `R`).
1126        materialize_survival(
1127            &parsed,
1128            data,
1129            &col_map,
1130            config,
1131            None,
1132            &left_col,
1133            &event_col,
1134            Some(&right_col),
1135        )
1136    } else if let Some((entry_col, exit_col, event_col)) = parse_surv_response(&parsed.response)? {
1137        if config.transformation_normal {
1138            return Err(WorkflowError::InvalidConfig {
1139                reason: "transformation_normal cannot be combined with a Surv(...) response"
1140                    .to_string(),
1141            });
1142        }
1143        // `materialize_*` now return `WorkflowError` directly so the typed
1144        // `ColumnNotFound` payload (and any future variant-typed leaf
1145        // errors) survive the dispatcher hop instead of being flattened
1146        // into `IntegrationFailed { reason: String }`.
1147        materialize_survival(
1148            &parsed,
1149            data,
1150            &col_map,
1151            config,
1152            entry_col.as_deref(),
1153            &exit_col,
1154            &event_col,
1155            None,
1156        )
1157    } else {
1158        // Non-survival response: `timewiggle(...)` and `survmodel(...)` are
1159        // structurally meaningless (there is no baseline hazard / time axis to
1160        // wiggle and no survival likelihood to configure). They are parsed into
1161        // `ParsedFormula` but consumed *only* by `materialize_survival`; without
1162        // this guard every non-survival materializer below would silently drop
1163        // them, fitting an ordinary GAM while the user believes they requested a
1164        // time-varying / survival model (#371). Reject here — the single
1165        // chokepoint for all non-survival paths — mirroring the symmetric
1166        // auxiliary-formula rejection in `validate_auxiliary_formula_controls`.
1167        reject_survival_only_terms_for_nonsurvival(&parsed)?;
1168        // Symmetrically, the `config.survival_likelihood` *knob* selects a
1169        // survival likelihood mode read only by `materialize_survival`. On this
1170        // non-survival branch a non-default value (e.g. "weibull") would be
1171        // discarded and the fit would silently degrade to an ordinary GAM
1172        // (#1767). Reject it at the same chokepoint.
1173        reject_survival_likelihood_for_nonsurvival(config)?;
1174        if config.transformation_normal {
1175            // Issue #789A: a Bernoulli marginal-slope request with
1176            // `transformation_normal=true` used to dispatch as a CTN fit while
1177            // retaining marginal-slope controls, leaving the transformation path
1178            // in a non-advancing loop. CTN score calibration now uses the
1179            // explicit `ctn_stage1` recipe instead, so the legacy boolean is a
1180            // hard configuration error for marginal-slope requests.
1181            reject_marginal_slope_controls_for_transformation_normal(config)?;
1182            if config.noise_formula.is_some() {
1183                return Err(WorkflowError::InvalidConfig {
1184                    reason: "transformation_normal cannot be combined with noise_formula"
1185                        .to_string(),
1186                });
1187            }
1188            materialize_transformation_normal(&parsed, data, &col_map, config)
1189        } else if config.logslope_formula.is_some() || config.z_column.is_some() {
1190            materialize_bernoulli_marginal_slope(&parsed, data, &col_map, config)
1191        } else if config.noise_formula.is_some() {
1192            materialize_location_scale(&parsed, data, &col_map, config)
1193        } else {
1194            materialize_standard(&parsed, data, &col_map, config)
1195        }
1196    }
1197}
1198
1199#[cfg(test)]
1200mod sz_factor_smooth_recovery_tests {
1201    // `super::*` brings in `Dataset` (= gam_data::EncodedDataset), `FitConfig`,
1202    // `FitResult`, `StandardFitResult`, and `fit_from_formula`.
1203    use super::*;
1204
1205    const NOISE_SD: f64 = 0.20;
1206    const N: usize = 4000;
1207    const N_GROUPS: usize = 4;
1208
1209    /// A simple deterministic LCG so the dataset is reproducible without pulling
1210    /// an RNG dependency into the test.
1211    struct Lcg(u64);
1212    impl Lcg {
1213        fn next_u64(&mut self) -> u64 {
1214            // Numerical Recipes LCG constants.
1215            self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
1216            self.0
1217        }
1218        /// Uniform in [0, 1).
1219        fn unif(&mut self) -> f64 {
1220            (self.next_u64() >> 11) as f64 / (1u64 << 53) as f64
1221        }
1222        /// Standard normal via Box–Muller (one of the pair).
1223        fn normal(&mut self) -> f64 {
1224            let u1 = (self.unif()).max(1e-12);
1225            let u2 = self.unif();
1226            (-2.0 * u1.ln()).sqrt() * (std::f64::consts::TAU * u2).cos()
1227        }
1228    }
1229
1230    /// Data drawn from EXACTLY the `sz` model class: a shared smooth `f0(x)` plus
1231    /// zero-sum per-group deviations `d_g(x)` (phase-shifted sinusoids whose
1232    /// cross-group mean is removed at every `x`), plus observation noise. This
1233    /// mirrors the (blocked) Python bug-hunt test `tests/bug_hunt_sz_factor_
1234    /// smooth_underfits_own_model_class_test.py`.
1235    ///
1236    /// Written to a CSV and loaded through the real `load_dataset_projected`
1237    /// inferer so the grouping column `g` (string levels) is encoded as a genuine
1238    /// categorical exactly as production does — hand-built `EncodedDataset`s do
1239    /// not carry the categorical level map the factor-smooth level resolver needs.
1240    fn sz_class_dataset() -> (Dataset, tempfile::TempDir) {
1241        let mut rng = Lcg(0x5326_2026_0628_1605);
1242        let phases: Vec<f64> = (0..N_GROUPS)
1243            .map(|k| 1.2 * k as f64 / (N_GROUPS as f64 - 1.0))
1244            .collect();
1245        let deviations = |xi: f64| -> Vec<f64> {
1246            let vals: Vec<f64> = phases
1247                .iter()
1248                .map(|p| 0.6 * (std::f64::consts::TAU * xi + std::f64::consts::TAU * p).sin())
1249                .collect();
1250            let mean = vals.iter().sum::<f64>() / vals.len() as f64;
1251            vals.iter().map(|v| v - mean).collect()
1252        };
1253
1254        let mut csv = String::from("y,x,g\n");
1255        for _ in 0..N {
1256            let x = rng.unif();
1257            // Use the HIGH bits (via `unif`) for the group draw — an LCG's low
1258            // bits have a tiny period and would collapse `% N_GROUPS` to a near
1259            // constant.
1260            let g = ((rng.unif() * N_GROUPS as f64) as usize).min(N_GROUPS - 1);
1261            let f0 = (std::f64::consts::TAU * x).sin();
1262            let mu = f0 + deviations(x)[g];
1263            let y = mu + NOISE_SD * rng.normal();
1264            csv.push_str(&format!("{y},{x},g{g}\n"));
1265        }
1266        let td = tempfile::tempdir().expect("tempdir");
1267        let path = td.path().join("sz_class.csv");
1268        std::fs::write(&path, csv).expect("write sz-class csv");
1269        // Force `g` into a categorical role exactly as the formula intends so the
1270        // factor-smooth level resolver sees all `N_GROUPS` distinct levels.
1271        let mut roles = std::collections::HashSet::new();
1272        roles.insert("g");
1273        let data = gam_data::load_dataset_projected_with_categorical_roles(
1274            &path,
1275            &["y".to_string(), "x".to_string(), "g".to_string()],
1276            &roles,
1277        )
1278        .expect("load sz-class dataset");
1279        (data, td)
1280    }
1281
1282    fn gaussian_config() -> FitConfig {
1283        FitConfig { family: Some("gaussian".to_string()), ..FitConfig::default() }
1284    }
1285
1286    /// In-sample residual sd of a fitted standard GAM: `sd(y − Xβ̂)`.
1287    fn residual_sd(fit: &StandardFitResult, data: &Dataset) -> f64 {
1288        let beta = &fit.fit.beta;
1289        let design = &fit.design.design;
1290        let n = design.nrows();
1291        assert_eq!(design.ncols(), beta.len(), "design/beta width mismatch");
1292        let mut fitted = vec![0.0f64; n];
1293        // `try_row_chunk` materializes contiguous row blocks of whatever design
1294        // storage the fit used (dense or block-lazy) — robust to the storage kind.
1295        const CHUNK: usize = 512;
1296        let mut start = 0usize;
1297        while start < n {
1298            let end = (start + CHUNK).min(n);
1299            let block = design
1300                .try_row_chunk(start..end)
1301                .expect("materialize design row chunk");
1302            for (r, row) in block.rows().into_iter().enumerate() {
1303                let mut acc = 0.0;
1304                for (c, &xv) in row.iter().enumerate() {
1305                    acc += xv * beta[c];
1306                }
1307                fitted[start + r] = acc;
1308            }
1309            start = end;
1310        }
1311        let y = data.values.column(0);
1312        let resid: Vec<f64> = y.iter().zip(fitted.iter()).map(|(&yi, &fi)| yi - fi).collect();
1313        let mean = resid.iter().sum::<f64>() / resid.len() as f64;
1314        let var = resid.iter().map(|r| (r - mean).powi(2)).sum::<f64>() / resid.len() as f64;
1315        var.sqrt()
1316    }
1317
1318    fn fit_standard(formula: &str, data: &Dataset) -> StandardFitResult {
1319        match fit_from_formula(formula, data, &gaussian_config())
1320            .unwrap_or_else(|e| panic!("fit `{formula}` failed: {e:?}"))
1321        {
1322            FitResult::Standard(r) => r,
1323            other => panic!("expected Standard fit for `{formula}`, got a different variant: {}",
1324                std::any::type_name_of_val(&other)),
1325        }
1326    }
1327
1328    /// #1605 (gold standard, end-to-end REML fit): the sum-to-zero factor smooth
1329    /// `s(x) + s(g, x, bs="sz")` must RECOVER data drawn from its own model class
1330    /// to the observation-noise floor, exactly as the strictly-more-general
1331    /// `s(x, g, bs="fs")` superset provably does.
1332    ///
1333    /// The recovery gap (`sz` resid ≈ 0.43 ≈ 2.1× the 0.20 floor while `fs`
1334    /// reaches the floor) was closed by THREE mgcv-faithful corrections, each
1335    /// necessary, that this end-to-end fit jointly exercises:
1336    ///   1. marginal basis (baef17e): cr → curvature-capable B-spline, so a
1337    ///      deviation with non-zero boundary curvature is representable;
1338    ///   2. ownership/overlap residualization (b49bb5c): the `sz` deviation is
1339    ///      sum-to-zero ACROSS the grouping factor, hence orthogonal to a
1340    ///      factor-independent owner like the shared `s(x)`. Residualizing it
1341    ///      against `s(x)`'s realized span (the #978 chart) collapsed every
1342    ///      group's curve to a flat per-group contrast; skipping that ownership
1343    ///      (same family as the #1276 factor-`by` level gate) restores the curve
1344    ///      shape and stops REML railing the shared `s(x)` wiggliness λ;
1345    ///   3. null-space ridge (this change): the `sz` deviation blocks now carry
1346    ///      the per-null-dimension ridge structure of `fs`, mapped into the
1347    ///      zero-sum contrast space, so the {const, linear} null space is
1348    ///      shrinkable per dimension (the #700/#712/#713 partial-pooling form)
1349    ///      rather than left free — without breaking the zero-sum constraint.
1350    ///
1351    /// This is the gold-standard verification: it drives the real
1352    /// `fit_from_formula` REML λ-selection on data drawn from exactly the `sz`
1353    /// model class and asserts `sz` reaches the floor (and a `fs` control does
1354    /// too). It failed before the fixes and passes after.
1355    #[test]
1356    fn sz_factor_smooth_recovers_its_own_model_class_end_to_end() {
1357        let (data, _td) = sz_class_dataset();
1358
1359        // Control: bs="fs", a strict superset of the sz span, must reach the
1360        // noise floor — proves the data is well-posed and pins the floor.
1361        let fs_fit = fit_standard("y ~ s(x, g, bs='fs')", &data);
1362        let fs_resid = residual_sd(&fs_fit, &data);
1363        assert!(
1364            fs_resid < 1.2 * NOISE_SD,
1365            "control bs='fs' did not reach the noise floor: resid_sd={fs_resid:.4} \
1366             vs noise_sd={NOISE_SD} (data/floor sanity check)",
1367        );
1368
1369        // The documented sz idiom on data drawn from the sz model class.
1370        let sz_fit = fit_standard("y ~ s(x) + s(g, x, bs='sz')", &data);
1371        let sz_resid = residual_sd(&sz_fit, &data);
1372
1373        // A smoother whose span contains the truth, fit at large n, must explain
1374        // the systematic structure and leave ~only observation noise.
1375        assert!(
1376            sz_resid < 1.4 * NOISE_SD,
1377            "bs='sz' under-fits its own model class: resid_sd={sz_resid:.4} \
1378             ({:.2}x the noise floor {NOISE_SD}); the bs='fs' superset reached \
1379             {fs_resid:.4}. The sz fit leaves systematic signal in the residual.",
1380            sz_resid / NOISE_SD,
1381        );
1382
1383        // Comparative guard: sz must not be dramatically worse than the fs
1384        // superset that recovers the same data.
1385        assert!(
1386            sz_resid < 1.5 * fs_resid,
1387            "bs='sz' residual {sz_resid:.4} is {:.2}x the bs='fs' residual \
1388             {fs_resid:.4} on identical sz-class data",
1389            sz_resid / fs_resid,
1390        );
1391    }
1392}