Skip to main content

gam_models/gamlss/gaussian/
binomial_locscale_decl.rs

1// Real concern-organized submodule of the gamlss family stack.
2// Cross-module items are re-exported flat through the parent (`gamlss.rs`),
3// so `use super::*;` makes the sibling-concern symbols this module references
4// resolve through the parent namespace.
5use super::*;
6
7#[derive(Clone)]
8pub struct BinomialLocationScaleFamily {
9    pub y: Array1<f64>,
10    pub weights: Array1<f64>,
11    pub link_kind: InverseLink,
12    pub threshold_design: Option<DesignMatrix>,
13    pub log_sigma_design: Option<DesignMatrix>,
14    /// Resource policy threaded into PsiDesignMap construction (and any other
15    /// per-call materialization decision) made during exact-Newton joint psi
16    /// derivative evaluation. Defaults to `ResourcePolicy::default_library()`
17    /// when the family is built without an explicit policy.
18    pub policy: gam_runtime::resource::ResourcePolicy,
19}
20
21/// Both Binomial location-scale families plug into the unified
22/// [`LocationScaleJointPsiFamily`] trait with byte-identical thin delegations
23/// to inherent methods, differing only in the implementing type and its
24/// `LABEL` fragment; generate them from one template. The Binomial families do
25/// not thread the outer-row subsample (they run the full-data exact ψ path), so
26/// the trait's `subsample` argument is accepted and ignored here.
27macro_rules! impl_binomial_location_scale_joint_psi_family {
28    ($family:ty, $label:literal) => {
29        impl LocationScaleJointPsiFamily for $family {
30            type Direction = LocationScaleJointPsiDirection;
31            const LABEL: &'static str = $label;
32
33            fn ws_policy(&self) -> &gam_runtime::resource::ResourcePolicy {
34                &self.policy
35            }
36
37            fn ws_exact_joint_dense_block_designs<'a>(
38                &'a self,
39                specs: Option<&'a [ParameterBlockSpec]>,
40            ) -> Result<Option<(Cow<'a, Array2<f64>>, Cow<'a, Array2<f64>>)>, String> {
41                self.exact_joint_dense_block_designs(specs)
42            }
43
44            fn ws_psi_direction(
45                &self,
46                block_states: &[ParameterBlockState],
47                derivative_blocks: &[Vec<crate::custom_family::CustomFamilyBlockPsiDerivative>],
48                psi_index: usize,
49                design_loc: &Array2<f64>,
50                design_scale: &Array2<f64>,
51                policy: &gam_runtime::resource::ResourcePolicy,
52            ) -> Result<Option<LocationScaleJointPsiDirection>, String> {
53                self.exact_newton_joint_psi_direction(
54                    block_states,
55                    derivative_blocks,
56                    psi_index,
57                    design_loc,
58                    design_scale,
59                    policy,
60                )
61            }
62
63            fn ws_psi_second_order_terms_from_parts(
64                &self,
65                block_states: &[ParameterBlockState],
66                derivative_blocks: &[Vec<crate::custom_family::CustomFamilyBlockPsiDerivative>],
67                psi_a: &LocationScaleJointPsiDirection,
68                psi_b: &LocationScaleJointPsiDirection,
69                design_loc: &Array2<f64>,
70                design_scale: &Array2<f64>,
71                subsample: Option<&[crate::outer_subsample::WeightedOuterRow]>,
72            ) -> Result<ExactNewtonJointPsiSecondOrderTerms, String> {
73                assert!(subsample.is_none());
74                self.exact_newton_joint_psisecond_order_terms_from_parts(
75                    block_states,
76                    derivative_blocks,
77                    psi_a,
78                    psi_b,
79                    design_loc,
80                    design_scale,
81                )
82            }
83
84            fn ws_psi_hessian_directional_from_parts(
85                &self,
86                block_states: &[ParameterBlockState],
87                psi_dir: &LocationScaleJointPsiDirection,
88                d_beta_flat: &Array1<f64>,
89                design_loc: &Array2<f64>,
90                design_scale: &Array2<f64>,
91                subsample: Option<&[crate::outer_subsample::WeightedOuterRow]>,
92            ) -> Result<Array2<f64>, String> {
93                assert!(subsample.is_none());
94                self.exact_newton_joint_psihessian_directional_derivative_from_parts(
95                    block_states,
96                    psi_dir,
97                    d_beta_flat,
98                    design_loc,
99                    design_scale,
100                )
101            }
102        }
103    };
104}
105
106impl_binomial_location_scale_joint_psi_family!(
107    BinomialLocationScaleFamily,
108    "BinomialLocationScaleFamily"
109);
110
111impl_binomial_location_scale_joint_psi_family!(
112    BinomialLocationScaleWiggleFamily,
113    "BinomialLocationScaleWiggleFamily"
114);
115
116pub(crate) type BinomialLocationScaleExactNewtonJointPsiWorkspace =
117    LocationScaleJointPsiWorkspace<BinomialLocationScaleFamily>;
118
119pub(crate) type BinomialLocationScaleWiggleExactNewtonJointPsiWorkspace =
120    LocationScaleJointPsiWorkspace<BinomialLocationScaleWiggleFamily>;