gam_problem/dispersion.rs
1//! Dispersion/scale contract used by covariance and reference-distribution code.
2
3use serde::{Deserialize, Serialize};
4
5/// Dispersion contract used by inferential covariance and reference distributions.
6///
7/// `Known(phi)` is used for fixed-scale exponential-family fits such as
8/// Poisson and Binomial (`phi = 1`). `Estimated(phi)` is used when the
9/// residual/likelihood scale is estimated from the data, e.g. Gaussian
10/// (`phi = sigma^2`) and Gamma (`phi = 1 / shape`). Stored covariance
11/// matrices are scaled by this `phi`.
12#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
13pub enum Dispersion {
14 Known(f64),
15 Estimated(f64),
16}
17
18impl Dispersion {
19 #[inline]
20 pub const fn phi(self) -> f64 {
21 match self {
22 Self::Known(phi) | Self::Estimated(phi) => phi,
23 }
24 }
25
26 #[inline]
27 pub const fn is_estimated(self) -> bool {
28 matches!(self, Self::Estimated(_))
29 }
30}
31
32impl Default for Dispersion {
33 fn default() -> Self {
34 Self::Known(1.0)
35 }
36}