gam_models/fit_orchestration/request.rs
1use super::*;
2
3#[derive(Clone, Debug)]
4pub struct LinkWiggleConfig {
5 pub degree: usize,
6 pub num_internal_knots: usize,
7 pub penalty_orders: Vec<usize>,
8 pub double_penalty: bool,
9}
10
11/// Configuration for the second-stage binomial-mean wiggle fit appended to a
12/// standard pilot. The blockwise refit options live inside this struct so the
13/// pilot config (`link_kind` + `wiggle`) and its required `refit_options` can
14/// never disagree: either the whole standard-wiggle request is `Some`, or it
15/// is `None`. The previous shape had two sibling `Option` fields on
16/// `StandardFitRequest`, which allowed the materialize path to construct an
17/// inconsistent state (#320: linkwiggle config without blockwise options).
18#[derive(Clone)]
19pub struct StandardBinomialWiggleConfig {
20 pub link_kind: InverseLink,
21 pub wiggle: LinkWiggleConfig,
22 pub refit_options: BlockwiseFitOptions,
23}
24
25pub struct StandardFitRequest<'a> {
26 pub data: Array2<f64>,
27 pub y: Array1<f64>,
28 pub weights: Array1<f64>,
29 pub offset: Array1<f64>,
30 pub spec: TermCollectionSpec,
31 pub family: LikelihoodSpec,
32 pub options: FitOptions,
33 pub kappa_options: SpatialLengthScaleOptimizationOptions,
34 pub wiggle: Option<StandardBinomialWiggleConfig>,
35 pub coefficient_groups: Vec<CoefficientGroupSpec>,
36 pub penalty_block_gamma_priors: Vec<(String, f64, f64)>,
37 pub latent_coord: Option<StandardLatentCoordConfig>,
38 #[doc(hidden)]
39 pub _marker: std::marker::PhantomData<&'a ()>,
40}
41
42pub struct GaussianLocationScaleFitRequest<'a> {
43 pub data: ArrayView2<'a, f64>,
44 pub spec: GaussianLocationScaleTermSpec,
45 pub wiggle: Option<LinkWiggleConfig>,
46 pub options: BlockwiseFitOptions,
47 pub kappa_options: SpatialLengthScaleOptimizationOptions,
48}
49
50pub struct BinomialLocationScaleFitRequest<'a> {
51 pub data: ArrayView2<'a, f64>,
52 pub spec: BinomialLocationScaleTermSpec,
53 pub wiggle: Option<LinkWiggleConfig>,
54 pub options: BlockwiseFitOptions,
55 pub kappa_options: SpatialLengthScaleOptimizationOptions,
56}
57
58pub struct DispersionLocationScaleFitRequest<'a> {
59 pub data: ArrayView2<'a, f64>,
60 pub spec: DispersionGlmLocationScaleTermSpec,
61 pub options: BlockwiseFitOptions,
62 pub kappa_options: SpatialLengthScaleOptimizationOptions,
63}
64
65pub struct SurvivalLocationScaleFitRequest<'a> {
66 pub data: ArrayView2<'a, f64>,
67 pub spec: SurvivalLocationScaleTermSpec,
68 pub wiggle: Option<LinkWiggleConfig>,
69 pub kappa_options: SpatialLengthScaleOptimizationOptions,
70 pub optimize_inverse_link: bool,
71 /// See [`gam_custom_family::BlockwiseFitOptions::cache_session`].
72 /// Threaded into the internally constructed `BlockwiseFitOptions` by
73 /// `fit_survival_location_scale_model`.
74 pub cache_session: Option<std::sync::Arc<gam_runtime::warm_start::Session>>,
75}
76
77pub struct SurvivalTransformationFitRequest<'a> {
78 pub data: ArrayView2<'a, f64>,
79 pub spec: SurvivalTransformationTermSpec,
80 /// See [`gam_custom_family::BlockwiseFitOptions::cache_session`].
81 /// Threaded into the internally constructed `BlockwiseFitOptions` by
82 /// `fit_survival_transformation_model`.
83 pub cache_session: Option<std::sync::Arc<gam_runtime::warm_start::Session>>,
84}
85
86#[derive(Clone)]
87pub struct SurvivalTransformationTermSpec {
88 pub age_entry: Array1<f64>,
89 pub age_exit: Array1<f64>,
90 pub event_target: Array1<u8>,
91 pub weights: Array1<f64>,
92 pub covariate_spec: TermCollectionSpec,
93 pub covariate_offset: Array1<f64>,
94 pub baseline_cfg: crate::survival::SurvivalBaselineConfig,
95 pub likelihood_mode: crate::survival::SurvivalLikelihoodMode,
96 pub time_anchor: f64,
97 pub time_build: crate::survival::SurvivalTimeBuildOutput,
98 pub timewiggle: Option<LinkWiggleFormulaSpec>,
99 pub weibull_seed: Option<(f64, f64)>,
100 pub ridge_lambda: f64,
101 pub penalty_block_gamma_priors: Vec<(String, f64, f64)>,
102}
103pub struct BernoulliMarginalSlopeFitRequest<'a> {
104 pub data: ArrayView2<'a, f64>,
105 pub spec: BernoulliMarginalSlopeTermSpec,
106 pub options: BlockwiseFitOptions,
107 pub kappa_options: SpatialLengthScaleOptimizationOptions,
108 pub policy: gam_runtime::resource::ResourcePolicy,
109}
110
111pub struct SurvivalMarginalSlopeFitRequest<'a> {
112 pub data: ArrayView2<'a, f64>,
113 pub spec: SurvivalMarginalSlopeTermSpec,
114 pub options: BlockwiseFitOptions,
115 pub kappa_options: SpatialLengthScaleOptimizationOptions,
116}
117pub struct LatentSurvivalFitRequest<'a> {
118 pub data: ArrayView2<'a, f64>,
119 pub spec: LatentSurvivalTermSpec,
120 pub frailty: FrailtySpec,
121 pub options: BlockwiseFitOptions,
122}
123
124pub struct LatentBinaryFitRequest<'a> {
125 pub data: ArrayView2<'a, f64>,
126 pub spec: LatentBinaryTermSpec,
127 pub frailty: FrailtySpec,
128 pub options: BlockwiseFitOptions,
129}
130
131pub struct TransformationNormalFitRequest<'a> {
132 pub data: ArrayView2<'a, f64>,
133 pub response: Array1<f64>,
134 pub weights: Array1<f64>,
135 pub offset: Array1<f64>,
136 pub covariate_spec: TermCollectionSpec,
137 pub config: TransformationNormalConfig,
138 pub options: BlockwiseFitOptions,
139 pub kappa_options: SpatialLengthScaleOptimizationOptions,
140 pub warm_start: Option<TransformationWarmStart>,
141}
142pub enum FitRequest<'a> {
143 Standard(StandardFitRequest<'a>),
144 GaussianLocationScale(GaussianLocationScaleFitRequest<'a>),
145 BinomialLocationScale(BinomialLocationScaleFitRequest<'a>),
146 DispersionLocationScale(DispersionLocationScaleFitRequest<'a>),
147 SurvivalLocationScale(SurvivalLocationScaleFitRequest<'a>),
148 SurvivalTransformation(SurvivalTransformationFitRequest<'a>),
149 BernoulliMarginalSlope(BernoulliMarginalSlopeFitRequest<'a>),
150 SurvivalMarginalSlope(SurvivalMarginalSlopeFitRequest<'a>),
151 LatentSurvival(LatentSurvivalFitRequest<'a>),
152 LatentBinary(LatentBinaryFitRequest<'a>),
153 TransformationNormal(TransformationNormalFitRequest<'a>),
154}
155
156pub struct StandardFitResult {
157 pub fit: UnifiedFitResult,
158 pub design: TermCollectionDesign,
159 pub resolvedspec: TermCollectionSpec,
160 pub adaptive_diagnostics: Option<AdaptiveRegularizationDiagnostics>,
161 pub kappa_timing: Option<SpatialLengthScaleOptimizationTiming>,
162 pub saved_link_state: FittedLinkState,
163 pub wiggle_knots: Option<Array1<f64>>,
164 pub wiggle_degree: Option<usize>,
165 /// Standard-basis link-warp coefficients `β_w = Z·γ` for the saved-model
166 /// predict runtime when the frozen-basis de-aliasing engaged (#1596). The
167 /// fit's coefficients stay in the reduced `γ` coordinate; this lift is
168 /// persisted into the payload's `beta_link_wiggle`.
169 pub wiggle_saved_warp_beta: Option<Vec<f64>>,
170}
171
172pub struct SurvivalLocationScaleFitResult {
173 pub fit: SurvivalLocationScaleTermFitResult,
174 pub inverse_link: InverseLink,
175 pub wiggle_knots: Option<Array1<f64>>,
176 pub wiggle_degree: Option<usize>,
177}
178
179pub struct SurvivalTransformationFitResult {
180 pub fit: UnifiedFitResult,
181 pub resolvedspec: TermCollectionSpec,
182 pub baseline_cfg: crate::survival::SurvivalBaselineConfig,
183 pub likelihood_mode: crate::survival::SurvivalLikelihoodMode,
184 /// Persistable snapshot of the time basis used during the fit. Replaces
185 /// six previously flat fields (basisname / degree / knots / keep_cols /
186 /// smooth_lambda / anchor) so the FFI save path consumes a single
187 /// source-of-truth value rather than threading siblings independently.
188 pub time_basis: crate::survival::SavedSurvivalTimeBasis,
189 pub time_base_ncols: usize,
190 pub baseline_timewiggle: Option<TimeWiggleBlockInput>,
191}
192
193pub enum FitResult {
194 Standard(StandardFitResult),
195 GaussianLocationScale(GaussianLocationScaleFitResult),
196 BinomialLocationScale(BinomialLocationScaleFitResult),
197 DispersionLocationScale(DispersionLocationScaleFitResult),
198 SurvivalLocationScale(SurvivalLocationScaleFitResult),
199 SurvivalTransformation(SurvivalTransformationFitResult),
200 BernoulliMarginalSlope(BernoulliMarginalSlopeFitResult),
201 SurvivalMarginalSlope(SurvivalMarginalSlopeFitResult),
202 LatentSurvival(LatentSurvivalTermFitResult),
203 LatentBinary(LatentBinaryTermFitResult),
204 TransformationNormal(TransformationNormalFitResult),
205 /// Exact O(n) state-space cubic/linear/quintic smoothing-spline scan
206 /// (#1030/#1034). A scan-bearing model IS a Gaussian-identity model with a
207 /// different (exact) representation: rather than a dense design + coefficient
208 /// vector it carries the Durbin–Koopman smoother posterior directly (knots,
209 /// smoothed states, pointwise variances, σ², log λ, exact diffuse-REML EDF,
210 /// and an exact per-row `predict`). Library callers that want the fitted
211 /// posterior get it here without paying the dense O(n·k²)+O(k³) route; the
212 /// CLI/FFI save paths build the persistence payload from the same
213 /// `SplineScanFit` via `assemble_spline_scan_payload`.
214 SplineScan(gam_solve::spline_scan::SplineScanFit),
215 /// O(n log n) multiresolution residual-cascade smooth (#1032). UNLIKE the
216 /// 1-D scan, the cascade is NOT the same posterior as the Duchon/Matérn term
217 /// it stands in for (a different finite basis — the multilevel Wendland
218 /// frame), so it is never a silent swap: this variant is produced only when
219 /// the structural detector [`residual_cascade_fast_path`] fires on an
220 /// eligible scattered-low-d Gaussian fit past the dense-kernel cliff AND the
221 /// in-cascade quasi-uniformity guard certifies the metric; every other shape
222 /// (and a rejected metric) falls through to the dense `fit_model` path. The
223 /// cascade-bearing model carries the
224 /// [`ResidualCascadeFit`](gam_solve::residual_cascade::ResidualCascadeFit)
225 /// directly — knots-free nested geometry, coefficients, the factored
226 /// precision, and an exact per-row `predict`; the CLI/FFI save paths build
227 /// the persistence payload from its `to_state` snapshot.
228 ResidualCascade(gam_solve::residual_cascade::ResidualCascadeFit),
229}
230
231/// Result of a dispersion-channel GAMLSS location-scale fit (#913). Wraps the
232/// shared two-block [`BlockwiseTermFitResult`] (mean + log-precision designs
233/// and coefficients) plus the family kind so the save path can stamp the right
234/// likelihood. These families have no link-wiggle and no response
235/// standardization, so the result is a thin wrapper.
236pub struct DispersionLocationScaleFitResult {
237 pub fit: BlockwiseTermFitResult,
238 pub kind: DispersionFamilyKind,
239}
240
241/// Out-of-fold Stage-1 latent score and its score-influence Jacobian for a
242/// CTN → marginal-slope chain. `z_oof` (length n) replaces the in-sample `z`
243/// the Stage-2 model consumes; `jac_oof` (n × p₁) is fed to the Stage-2 spec's
244/// `score_influence_jacobian` so the joint solve absorbs the realized leakage
245/// directions `Z_infl = diag(s_f·β̂₀)·J`.
246pub struct CrossFitScoreCalibration {
247 pub z_oof: Array1<f64>,
248 pub jac_oof: Array2<f64>,
249}
250
251/// Internal recipe describing the CTN Stage-1 fit that produced a Stage-2 `z`
252/// column. This is in-process plumbing — never a CLI flag, env var, or feature
253/// gate. The orchestration layer populates [`FitConfig::ctn_stage1`] when (and
254/// only when) the marginal-slope `z` was generated by a transformation-normal
255/// Stage-1 fit; its presence is the sole auto-enable signal for cross-fitted
256/// orthogonalization (design §5). When absent, Stage-2 falls back to the free
257/// 1-D `score_warp` spline (which spans only the x-free leakage column).
258#[derive(Clone, Debug)]
259pub struct CtnStage1Recipe {
260 /// Stage-1 response column name (the `y` the CTN transforms).
261 pub response_column: String,
262 /// Stage-1 covariate-side formula right-hand side (e.g. `"s(pc1) + s(pc2)"`),
263 /// with no `~` and no response symbol. [`crossfit_score_calibration`] parses
264 /// it and builds the CTN covariate basis exactly as
265 /// `materialize_transformation_normal` does, then FREEZES that basis once on
266 /// the full data and reuses the frozen spec for every fold's refit — so the
267 /// rebuilt covariate design has an identical column geometry across folds,
268 /// keeping `J`'s `p₁ = p_resp · p_cov` columns aligned (design §3).
269 ///
270 /// The recipe carries the formula RHS (a primitive string) rather than a
271 /// resolved [`TermCollectionSpec`] because this struct is populated both via
272 /// [`CtnStage1Recipe::new`] (set on [`FitConfig::ctn_stage1`], then
273 /// [`fit_from_formula`]) and by the gamfit FFI marshaller
274 /// (`gamfit/_calibrated_slope.py`), which can only serialize primitives over
275 /// the JSON boundary — a `TermCollectionSpec` is not serializable. Freezing on
276 /// the full Stage-2 data is equivalent to
277 /// freezing on the Stage-1 data whenever the two stages share a frame (the
278 /// calibrated-chain contract), so the column geometry still matches Stage-1.
279 pub covariate_formula_rhs: String,
280 /// Stage-1 CTN config (response basis degree / knot count / penalties).
281 /// Its `response_num_internal_knots` is the FIXED response-basis size; the
282 /// cross-fit pins it across folds so `p_resp` (and hence `p₁`) is
283 /// fold-invariant (design §3).
284 pub config: TransformationNormalConfig,
285 /// Optional Stage-1 weight column name.
286 pub weight_column: Option<String>,
287 /// Optional Stage-1 offset column name.
288 pub offset_column: Option<String>,
289}
290
291impl CtnStage1Recipe {
292 /// Build a Stage-1 CTN recipe from the Stage-1 description. This is the public
293 /// way to populate [`FitConfig::ctn_stage1`] — set it on a marginal-slope
294 /// config and run [`fit_from_formula`] (the entry IS `fit_from_formula` with
295 /// `ctn_stage1` set; there is no separate combined entry function). The
296 /// materializer then cross-fits the CTN and installs the leakage-projection
297 /// block; supplying the recipe *is* the request for orthogonalization.
298 ///
299 /// `response` is the Stage-1 CTN response column; `covariates` is the
300 /// covariate-side formula right-hand side (e.g. `"s(pc1) + s(pc2)"` — no `~`,
301 /// no response symbol). Validates both are non-empty and that `covariates`
302 /// is an RHS only.
303 pub fn new(
304 response: &str,
305 covariates: &str,
306 config: TransformationNormalConfig,
307 weight_column: Option<&str>,
308 offset_column: Option<&str>,
309 ) -> Result<Self, String> {
310 let response_column = response.trim().to_string();
311 if response_column.is_empty() {
312 return Err("CtnStage1Recipe requires a non-empty Stage-1 response column".to_string());
313 }
314 let covariate_formula_rhs = covariates.trim().to_string();
315 if covariate_formula_rhs.is_empty() {
316 return Err(
317 "CtnStage1Recipe requires a non-empty Stage-1 covariate formula RHS".to_string(),
318 );
319 }
320 if covariate_formula_rhs.contains('~') {
321 return Err(
322 "CtnStage1Recipe covariates is a right-hand side only; pass 's(pc1) + s(pc2)', \
323 not 'score ~ s(pc1) + s(pc2)'"
324 .to_string(),
325 );
326 }
327 Ok(Self {
328 response_column,
329 covariate_formula_rhs,
330 config,
331 weight_column: weight_column
332 .map(str::to_string)
333 .filter(|s| !s.trim().is_empty()),
334 offset_column: offset_column
335 .map(str::to_string)
336 .filter(|s| !s.trim().is_empty()),
337 })
338 }
339}
340#[derive(Clone, Debug)]
341pub struct FitConfig {
342 /// Family: "gaussian", "binomial", "poisson", "negative-binomial",
343 /// "gamma", "tweedie" (alias "tw"; variance power fixed at p = 1.5), or
344 /// None for auto-detect.
345 pub family: Option<String>,
346 /// Fixed size/overdispersion parameter for `family="negative-binomial"`.
347 pub negative_binomial_theta: Option<f64>,
348 /// Link: "identity", "logit", "probit", "cloglog", "sas", "beta-logistic", or None.
349 pub link: Option<String>,
350 /// Whether to use flexible (wiggle-augmented) link.
351 pub flexible_link: bool,
352 /// Optional additive offset column for the primary linear predictor.
353 pub offset_column: Option<String>,
354 /// Optional additive offset column for the noise/log-scale predictor.
355 pub noise_offset_column: Option<String>,
356 /// Optional family-level frailty modifier.
357 pub frailty: Option<FrailtySpec>,
358
359 // Survival-specific
360 /// Baseline target: "linear", "weibull", "gompertz", "gompertz-makeham".
361 pub baseline_target: String,
362 pub baseline_scale: Option<f64>,
363 pub baseline_shape: Option<f64>,
364 pub baseline_rate: Option<f64>,
365 pub baseline_makeham: Option<f64>,
366 /// Time basis: "ispline" or "none".
367 pub time_basis: String,
368 pub time_degree: usize,
369 pub time_num_internal_knots: usize,
370 pub time_smooth_lambda: f64,
371 /// Survival likelihood mode: "location-scale", "transformation", "weibull",
372 /// "marginal-slope", "latent", or "latent-binary".
373 pub survival_likelihood: String,
374 /// Residual distribution: "gaussian", "logistic", "gumbel".
375 pub survival_distribution: String,
376 pub threshold_time_k: Option<usize>,
377 pub threshold_time_degree: usize,
378 pub sigma_time_k: Option<usize>,
379 pub sigma_time_degree: usize,
380
381 // Location-scale (GAMLSS)
382 /// If set, fit a location-scale model with this formula for the noise parameter.
383 pub noise_formula: Option<String>,
384
385 // Marginal-slope
386 /// Formula for the log-slope model (survival marginal-slope or Bernoulli marginal-slope).
387 pub logslope_formula: Option<String>,
388 /// Column name for the z (exposure/dose) variable in marginal-slope models.
389 pub z_column: Option<String>,
390 /// Optional non-negative per-row training weights column.
391 pub weight_column: Option<String>,
392 /// Expectile asymmetry `τ ∈ (0, 1)` for `family = "expectile"`.
393 ///
394 /// When `family` resolves to `"expectile"` the fit minimizes the
395 /// Newey–Powell asymmetric squared loss `Σ wᵢ(τ)·(yᵢ − μᵢ)²` with
396 /// `wᵢ(τ) = τ` if `yᵢ > μᵢ` else `1 − τ`, tracing the conditional
397 /// `τ`-expectile — the smooth analogue of the `τ`-quantile. `τ = 0.5`
398 /// reduces exactly to the Gaussian-identity mean fit. The whole penalized
399 /// smooth + REML `λ`-selection machinery is reused via a Least
400 /// Asymmetrically Weighted Squares (LAWS) outer loop. `None` defaults to
401 /// the median expectile `τ = 0.5` when the family is `"expectile"`; it is
402 /// ignored for every other family. The asymmetry may also be written inline
403 /// as `family = "expectile(0.9)"`, which fills this field at resolve time.
404 pub expectile_tau: Option<f64>,
405 /// Internal CTN Stage-1 provenance for the marginal-slope `z` column.
406 ///
407 /// When the marginal-slope `z` was generated by a transformation-normal
408 /// Stage-1 fit, the orchestration layer fills this with the Stage-1 recipe.
409 /// Its presence is the sole auto-enable signal for cross-fitted, Neyman-
410 /// orthogonal score calibration (#461): the materializer cross-fits the CTN
411 /// to produce out-of-fold `z` and the score-influence Jacobian `J`, replaces
412 /// the raw `z` with `z_oof`, and absorbs `J` as a leakage-projection block in
413 /// Stage-2. This is in-process plumbing only — there is no CLI flag, env var,
414 /// or feature gate. `None` ⇒ raw `z` with the free-warp `score_warp`
415 /// fallback. See [`CtnStage1Recipe`].
416 pub ctn_stage1: Option<CtnStage1Recipe>,
417
418 // Fitting options
419 pub scale_dimensions: bool,
420 /// Enable exact spatial adaptive regularization for standard formula fits.
421 /// `None` uses the quality-first automatic policy. The current automatic
422 /// policy leaves LAREG off unless explicitly requested because the
423 /// optimizer's REML-selected local weights can over-regularize small
424 /// high-yield spatial signals.
425 pub adaptive_regularization: Option<bool>,
426 pub ridge_lambda: f64,
427
428 /// Route the fit through the transformation-normal family. When set, the
429 /// formula terms are treated as the covariate side of the transformation
430 /// model and the response basis is built internally. Incompatible with
431 /// `noise_formula` and with `Surv(...)` responses.
432 pub transformation_normal: bool,
433
434 /// Enable Firth bias reduction for standard single-parameter families.
435 pub firth: bool,
436 /// Optional cap on the REML/LAML outer smoothing-parameter iterations for
437 /// standard formula fits. `None` uses the production default.
438 pub outer_max_iter: Option<usize>,
439 /// Optional wall-clock budget (seconds) for the outer smoothing search
440 /// (gam#979). Threaded to the survival marginal-slope fit, whose constrained
441 /// joint-Newton can fail to certify convergence and otherwise grind without
442 /// bound; with this set the fit returns its best-so-far iterate (or a
443 /// catchable error) within the budget instead of hanging. `None` keeps the
444 /// generous built-in default for that path and is unbounded elsewhere.
445 pub outer_wall_clock_budget_secs: Option<f64>,
446
447 /// GPU backend selection policy. `Auto` uses supported device kernels for
448 /// large workloads, `Off` pins execution to CPU kernels, and `Force` fails
449 /// loudly when a requested GPU kernel has no compiled backend.
450 pub gpu_policy: gam_gpu::GpuPolicy,
451 /// Optional override of the [`gam_runtime::resource::ResourcePolicy`] used when
452 /// planning spatial bases (TPS / Matern / Duchon) during term construction.
453 /// When `None`, the default-library policy is used.
454 pub resource_policy: Option<gam_runtime::resource::ResourcePolicy>,
455
456 /// Optional per-group metadata supplied by the caller. Fitting ignores this
457 /// field; saved-model builders pass it through so deployment consumers can
458 /// recover group provenance.
459 pub group_metadata: Option<BTreeMap<String, JsonValue>>,
460
461 /// Optional user-defined coefficient groups with separate precision
462 /// parameters. Group-local priors, including catalog-metadata-informed
463 /// Gamma precision hyperpriors, are resolved during design setup.
464 pub coefficient_groups: Vec<CoefficientGroupSpec>,
465
466 /// Optional per-existing-penalty-block Gamma(shape, rate) precision
467 /// hyperpriors keyed by penalty-block label. This is the
468 /// catalog-metadata-informed-prior hook for models that do not need a new
469 /// user-defined coefficient group.
470 pub penalty_block_gamma_priors: Vec<(String, f64, f64)>,
471
472 /// Python `gamfit.fit(..., latents={...})` configuration. This reaches
473 /// the standard formula workflow as an owned latent-coordinate block:
474 /// the named smooth's synthetic covariates are rebuilt from `t`, and
475 /// joint REML optimizes `[rho, vec(t)]` through latent design hyper-dirs.
476 pub latents: Option<JsonValue>,
477 /// Python `gamfit.fit(..., penalties=[...])` analytic-penalty descriptors,
478 /// validated against the declared latent-coordinate blocks before a
479 /// standard latent fit starts.
480 pub analytic_penalties: Option<JsonValue>,
481 /// Formula-path latent topology selector descriptor. The selector itself
482 /// fits candidates through the ordinary workflow; this slot lets callers
483 /// request and validate that path from the same config registry.
484 pub topology_auto_selector: Option<gam_solve::topology_selector::TopologyAutoSelector>,
485 /// `gamfit.fit(..., smooths={...})` Python kwarg routed through the FFI
486 /// bridge. JSON object keyed by formula symbol (single column name or
487 /// comma-joined tuple) → smooth descriptor (`{"kind": "duchon",
488 /// "centers": [[...], ...], ...}`). Applied as a post-processing step on
489 /// the [`TermCollectionSpec`] produced by the formula DSL: each smooth
490 /// term whose `feature_cols` match a registry key has its kind-specific
491 /// tunables (centers, knots, kernel hyperparameters) overridden with the
492 /// user-supplied values. The single canonical lowering path guarantees
493 /// `smooths={"x": Duchon(centers=K)}` (integer) produces a bit-identical
494 /// block spec to writing `duchon(x, centers=K)` in the formula; only
495 /// explicit array-valued `centers=` differs, routing through
496 /// `CenterStrategy::UserProvided` instead of `FarthestPoint`/`EqualMass`.
497 pub smooth_overrides: Option<JsonValue>,
498 /// Engage the cross-process ON-DISK persistent warm-start layer (#1082).
499 ///
500 /// Default `false`: only the always-on in-memory warm start runs, so a
501 /// single fit and throwaway/replicate/CI-coverage loops pay zero disk I/O
502 /// (no `WarmStartStore` dir/eviction scan, no record load/store). Set
503 /// `true` to engage cross-process / repeat-fit resume: the flag threads
504 /// `FitConfig → FitOptions → ExternalOptimOptions` down to the standard
505 /// `RemlState`, which then calls `enable_persistent_warm_start_disk()`.
506 pub persist_warm_start_disk: bool,
507}
508
509impl Default for FitConfig {
510 fn default() -> Self {
511 Self {
512 family: None,
513 negative_binomial_theta: None,
514 link: None,
515 flexible_link: false,
516 offset_column: None,
517 noise_offset_column: None,
518 frailty: None,
519 baseline_target: "linear".into(),
520 baseline_scale: None,
521 baseline_shape: None,
522 baseline_rate: None,
523 baseline_makeham: None,
524 time_basis: "ispline".into(),
525 time_degree: 3,
526 time_num_internal_knots: 8,
527 time_smooth_lambda: 1e-2,
528 survival_likelihood: "transformation".into(),
529 survival_distribution: "gaussian".into(),
530 threshold_time_k: None,
531 threshold_time_degree: 3,
532 sigma_time_k: None,
533 sigma_time_degree: 3,
534 noise_formula: None,
535 logslope_formula: None,
536 z_column: None,
537 weight_column: None,
538 expectile_tau: None,
539 ctn_stage1: None,
540 scale_dimensions: false,
541 adaptive_regularization: None,
542 ridge_lambda: 1e-6,
543 transformation_normal: false,
544 firth: false,
545 outer_max_iter: None,
546 outer_wall_clock_budget_secs: None,
547 gpu_policy: gam_gpu::GpuPolicy::Auto,
548 resource_policy: None,
549 group_metadata: None,
550 coefficient_groups: Vec::new(),
551 penalty_block_gamma_priors: Vec::new(),
552 latents: None,
553 analytic_penalties: None,
554 topology_auto_selector: None,
555 smooth_overrides: None,
556 persist_warm_start_disk: false,
557 }
558 }
559}
560/// The result of materializing a formula + config against a dataset.
561pub struct MaterializedModel<'a> {
562 pub request: FitRequest<'a>,
563 pub inference_notes: Vec<String>,
564}
565pub struct SplineScanInputs {
566 /// Abscissae of the single 1-D smooth (training rows of its feature column).
567 pub x: Vec<f64>,
568 /// Gaussian response.
569 pub y: Vec<f64>,
570 /// Observation weights (variance is `σ²/w`).
571 pub w: Vec<f64>,
572 /// Smoothing-spline order `m = penalty_order ∈ {1, 2, 3}`: `m = 1` the
573 /// random-walk/linear smoother (penalty `λ∫f′²`), `m = 2` the cubic
574 /// smoother (penalty `λ∫f″²`), `m = 3` the quintic smoother (penalty
575 /// `λ∫(f‴)²`).
576 pub order: usize,
577}
578pub struct ResidualCascadeInputs {
579 /// One slice per coordinate axis (2 or 3) of the single scattered smooth.
580 pub coords: Vec<Vec<f64>>,
581 /// Gaussian response.
582 pub y: Vec<f64>,
583 /// Observation weights (variance is `σ²/w`).
584 pub w: Vec<f64>,
585 /// Per-axis positive metric scaling `diag(metric)` of `z = diag(metric)·x`.
586 pub metric: Vec<f64>,
587 /// Sobolev smoothness order `s` of the multilevel Wendland-(3,1) prior,
588 /// clamped into the native-space window `(d/2, (d+3)/2]` (issue caveat 1).
589 pub sobolev_s: f64,
590}