Skip to main content

gam_models/
gamlss.rs

1//! Multi-parameter GAMLSS family stack, organized into real concern modules.
2//!
3//! The historical single-file module was split textually with `include!`; this
4//! is the genuine module decomposition. The fitting math is one tightly-coupled
5//! namespace (families share layouts, the location-scale joint-ψ machinery, and
6//! the numeric foundation), so the concern submodules each open with
7//! `use super::*;` and this parent re-exports their cross-concern items flat via
8//! `pub(crate) use <module>::*`. That keeps every intra-stack reference resolving
9//! without a web of explicit per-symbol imports, while each file now holds a
10//! single concern:
11//!
12//! - [`errors`]    — `GamlssError`, module-wide numeric constants, the low-level
13//!                   row-map / design-resolution / joint-ψ-direction helpers, and
14//!                   the `ParameterLink` link-identifier enum (the shared
15//!                   foundation every family builds on).
16//! - [`builders`]  — block/penalty construction, term specs, fit-result types,
17//!                   the `LocationScaleFamilyBuilder` strategy + its four builders,
18//!                   and the public `fit_*` entry points (construction &
19//!                   orchestration).
20//! - [`gaussian`]  — the Gaussian location-scale family (+ its wiggle variant),
21//!                   the shared location-scale joint-ψ trait/workspace, the
22//!                   Gaussian joint-Hessian assembly, the matrix-free
23//!                   `RowCoeffOperator`, and the Poisson/Gamma log-link families.
24//! - [`binomial`]  — the binomial location-scale numeric kernel (q-algebra,
25//!                   towers, directional coefficients) and the binomial
26//!                   location-scale / wiggle / mean-wiggle families plus their
27//!                   exact-Newton Hessian workspaces.
28//!
29//! Pre-existing leaf concerns keep their own modules: [`dispersion_family`],
30//! [`binomial_q_derivs`], [`binomial_q_coeffs`], [`validation`],
31//! [`weighted_design_products`], [`row_linalg`], and [`joint_packing`].
32
33use gam_terms::basis::{BasisOptions, PenaltyInfo, PenaltySource};
34use gam_problem::MIN_WEIGHT;
35
36use crate::custom_family::{
37    AdditiveBlockJacobian, BlockEffectiveJacobian, BlockWorkingSet, BlockwiseFitOptions,
38    CustomFamily, CustomFamilyBlockPsiDerivative, CustomFamilyJointDesignChannel,
39    CustomFamilyJointDesignPairContribution, CustomFamilyJointPsiOperator,
40    CustomFamilyPsiDesignAction, CustomFamilyPsiLinearMapRef, CustomFamilyPsiSecondDesignAction,
41    CustomFamilyWarmStart, ExactNewtonJointGradientEvaluation, ExactNewtonJointHessianWorkspace,
42    ExactNewtonJointPsiDirectCache, ExactNewtonJointPsiSecondOrderTerms,
43    ExactNewtonJointPsiWorkspace, FamilyChannelHessian, FamilyEvaluation, ParameterBlockSpec,
44    ParameterBlockState, PenaltyMatrix, PsiDesignMap, evaluate_custom_family_joint_hyper,
45    evaluate_custom_family_joint_hyper_efs, fit_custom_family, fit_custom_family_fixed_log_lambdas,
46    resolve_custom_family_x_psi_map, resolve_custom_family_x_psi_psi_map, second_psi_linear_map,
47    shared_dense_arc, weighted_crossprod_psi_maps,
48};
49
50use crate::model_types::UnifiedFitResult;
51
52use gam_linalg::faer_ndarray::{fast_ab, fast_atv, fast_av, fast_joint_hessian_2x2};
53
54use crate::block_layout::block_count::validate_block_count;
55
56use crate::location_scale_engine::build_location_scale_exact_joint_setup;
57
58use crate::parameter_block::ParameterBlockInput;
59
60use crate::scale_design::{
61    build_scale_deviation_operator, build_scale_deviation_transform_design,
62};
63
64use crate::sigma_link::{
65    LOGB_SIGMA_FLOOR, SigmaJet1, exp_sigma_derivs_up_to_fourth_scalar,
66    exp_sigma_derivs_up_to_third, exp_sigma_from_eta_scalar, exp_sigma_jet1_scalar,
67    logb_sigma_from_eta_scalar, logb_sigma_jet1_scalar, safe_exp,
68};
69
70use crate::spatial_psi_bridge::build_block_spatial_psi_derivatives;
71
72// The monotone-wiggle helpers live in the neutral `families::wiggle` module
73// (decoupling refactor); this block imports only the ones gamlss's own non-test
74// code uses. Symbols used solely by this module's `#[cfg(test)]` block
75// (`initializewiggle_knots_from_seed`, `monotone_wiggle_internal_degree`,
76// `split_wiggle_penalty_orders`) are imported inside that block instead, so they
77// are not flagged unused in a non-test `--lib` build; downstream consumers import
78// from `families::wiggle` directly.
79use crate::wiggle::{
80    SelectedWiggleBasis, WiggleBlockConfig, buildwiggle_block_input_from_knots,
81    initializewiggle_knots_from_seed, monotone_wiggle_basis_with_derivative_order,
82    monotone_wiggle_nonnegative_constraints, project_monotone_wiggle_beta_nonnegative,
83    select_wiggle_basis_from_seed, validate_monotone_wiggle_beta_nonnegative,
84};
85
86use crate::inference::generative::{CustomFamilyGenerative, GenerativeSpec, NoiseModel};
87
88use gam_linalg::matrix::SymmetricMatrix;
89
90use gam_linalg::matrix::{DenseDesignMatrix, DenseDesignOperator, DesignMatrix};
91
92use gam_solve::mixture_link::{inverse_link_jet_for_inverse_link, inverse_link_mu_d1_for_inverse_link};
93
94use gam_solve::pirls::LinearInequalityConstraints;
95
96use crate::probability::{normal_logcdf, normal_logsf, standard_normal_quantile};
97
98use gam_terms::smooth::{
99    BlockwisePenalty, PenaltyBlockInfo, SpatialLengthScaleOptimizationOptions, SpatialLogKappaCoords,
100    TermCollectionDesign, TermCollectionSpec,
101};
102use crate::fit_orchestration::drivers::{
103    ExactJointHyperSetup, freeze_term_collection_from_design,
104    optimize_spatial_length_scale_exact_joint, spatial_dims_per_term,
105    spatial_length_scale_term_indices,
106};
107// #1521: relocated DOWN into gam_terms::smooth (was drivers::build_term_collection_design).
108use gam_terms::smooth::build_term_collection_design;
109
110use crate::model_types::validate_all_finite_estimation;
111
112// #1521: relocated DOWN into gam-model-kernels (was gamlss::builders). The
113// penalized-WLS projection carries no gamlss type, so the family stack consumes
114// it from the base kernel crate; this re-export keeps the gamlss-internal
115// (`builders`/`dispersion_family`) call sites resolving unchanged.
116pub(crate) use crate::penalized_projection::solve_penalizedweighted_projection;
117
118use gam_problem::{InverseLink, StandardLink};
119
120use ndarray::{Array1, Array2, ArrayView1, ArrayView2, Axis, s};
121
122use rayon::prelude::*;
123
124use std::borrow::Cow;
125
126use std::collections::{HashMap, hash_map::DefaultHasher};
127
128use std::hash::{Hash, Hasher};
129
130use std::sync::atomic::AtomicUsize;
131
132use std::sync::{Arc, Mutex};
133
134// ---------------------------------------------------------------------------
135// Leaf concern modules (pre-existing real modules; their public surface is
136// folded into the flat gamlss namespace here so the family code can name them
137// unqualified, exactly as it did under the prior `include!` layout).
138// ---------------------------------------------------------------------------
139
140mod dispersion_family;
141pub use dispersion_family::{
142    DispersionFamilyKind, DispersionGlmLocationScaleTermSpec, FAMILY_BETA_LOCATION_SCALE,
143    FAMILY_GAMMA_LOCATION_SCALE, FAMILY_NEGBIN_LOCATION_SCALE, FAMILY_TWEEDIE_LOCATION_SCALE,
144    fit_dispersion_glm_location_scale_terms,
145};
146
147mod binomial_q_derivs;
148use binomial_q_derivs::{
149    binomial_neglog_q_derivatives_dispatch, binomial_neglog_q_fourth_derivative_dispatch,
150};
151
152mod binomial_q_coeffs;
153use binomial_q_coeffs::{
154    directionalhessian_coeff_fromobjective_q_terms, hessian_coeff_fromobjective_q_terms,
155    second_directionalhessian_coeff_fromobjective_q_terms,
156};
157
158mod validation;
159use validation::{
160    minimum_monotone_wiggle_knot_count, validate_binomial_location_scale_termspec,
161    validate_binomial_location_scalewiggle_termspec, validate_binomial_response,
162    validate_blockrows, validate_gaussian_location_scale_termspec,
163    validate_gaussian_location_scalewiggle_termspec, validate_len_match, validate_term_weights,
164    validateweights,
165};
166
167mod weighted_design_products;
168use weighted_design_products::{
169    mirror_upper_to_lower, scaled_outer_add, signedwith_floor, xt_diag_x_dense, xt_diag_x_design,
170    xt_diag_y_dense, xt_diag_y_design,
171};
172
173mod row_linalg;
174use row_linalg::{psd_clamp_2x2, scale_matrix_rows};
175
176mod joint_packing;
177use joint_packing::{
178    binomial_pack_mean_wiggle_joint_score, binomial_pack_mean_wiggle_joint_symmetrichessian,
179    gaussian_pack_joint_score, gaussian_pack_joint_symmetrichessian,
180    gaussian_pack_wiggle_joint_score, gaussian_pack_wiggle_joint_symmetrichessian,
181};
182
183// ---------------------------------------------------------------------------
184// Concern modules of the family stack itself. Each opens with `use super::*;`
185// and exposes its cross-concern items as `pub(crate)`; re-exporting them flat
186// here lets the stack reference any concern's symbols unqualified.
187// ---------------------------------------------------------------------------
188
189// `pub use … ::*` re-exports each concern's items at their own visibility:
190// `pub` family/spec/result types stay crate-public (the binary crate reaches
191// them via `gam::gamlss::…`), while `pub(crate)` helpers stay crate-internal.
192mod errors;
193pub use errors::*;
194
195mod builders;
196pub use builders::*;
197
198mod gaussian;
199pub use gaussian::*;
200
201mod binomial;
202pub use binomial::*;
203
204#[cfg(test)]
205mod test_support;
206
207#[cfg(test)]
208mod tests;