ndarray_glm/response/
linear.rs1#[cfg(feature = "stats")]
4use crate::response::Response;
5use crate::{
6 error::{RegressionError, RegressionResult},
7 glm::{DispersionType, Glm},
8 link::Link,
9 num::Float,
10 response::Yval,
11};
12use num_traits::ToPrimitive;
13#[cfg(feature = "stats")]
14use statrs::distribution::Normal;
15use std::marker::PhantomData;
16
17pub struct Linear<L = link::Id>
19where
20 L: Link<Linear<L>>,
21{
22 _link: PhantomData<L>,
23}
24
25impl<Y, L> Yval<Linear<L>> for Y
27where
28 Y: Float + ToPrimitive + ToString,
29 L: Link<Linear<L>>,
30{
31 fn into_float<F: Float>(self) -> RegressionResult<F, F> {
32 F::from(self).ok_or_else(|| RegressionError::InvalidY(self.to_string()))
33 }
34}
35
36#[cfg(feature = "stats")]
37impl<L> Response for Linear<L>
38where
39 L: Link<Linear<L>>,
40{
41 type DistributionType = Normal;
42
43 fn get_distribution(mu: f64, phi: f64) -> Self::DistributionType {
44 let sigma = phi.max(f64::MIN_POSITIVE).sqrt();
50 Normal::new(mu, sigma).unwrap()
51 }
52}
53
54impl<L> Glm for Linear<L>
55where
56 L: Link<Linear<L>>,
57{
58 type Link = L;
59 const DISPERSED: DispersionType = DispersionType::FreeDispersion;
60
61 fn log_partition<F: Float>(nat_par: F) -> F {
64 let half = F::from(0.5).unwrap();
65 half * nat_par * nat_par
66 }
67
68 fn variance<F: Float>(_mean: F) -> F {
70 F::one()
71 }
72
73 fn log_like_sat<F: Float>(y: F) -> F {
77 Self::log_partition(y)
79 }
80}
81
82pub(crate) mod link {
83 use super::*;
85 use crate::link::{Canonical, Link};
86
87 pub struct Id;
89 impl Canonical for Id {}
91 impl Link<Linear> for Id {
92 #[inline]
93 fn func<F: Float>(y: F) -> F {
94 y
95 }
96 #[inline]
97 fn func_inv<F: Float>(lin_pred: F) -> F {
98 lin_pred
99 }
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use super::Linear;
106 use crate::{error::RegressionResult, model::ModelBuilder};
107 use approx::assert_abs_diff_eq;
108 use ndarray::array;
109
110 #[test]
111 fn id_closure() {
114 use crate::link::TestLink;
115 let x = crate::array![
118 -1e5, -100., -13., -2.0, -1.0, -0.025, -0.001, 0., 0.001, 0.04, 1.0, 2.5, 17., 128.,
119 1e5
120 ];
121 super::link::Id::check_closure(&x);
122 super::link::Id::check_closure_y(&x);
123 }
124
125 #[test]
126 fn lin_reg() -> RegressionResult<(), f64> {
127 let beta = array![0.3, 1.2, -0.5];
128 let data_x = array![[-0.1, 0.2], [0.7, 0.5], [3.2, 0.1]];
129 let data_y = array![
131 beta[0] + beta[1] * data_x[[0, 0]] + beta[2] * data_x[[0, 1]],
132 beta[0] + beta[1] * data_x[[1, 0]] + beta[2] * data_x[[1, 1]],
133 beta[0] + beta[1] * data_x[[2, 0]] + beta[2] * data_x[[2, 1]],
134 ];
135 let model = ModelBuilder::<Linear>::data(&data_y, &data_x).build()?;
136 let fit = model.fit_options().max_iter(10).fit()?;
137 assert_abs_diff_eq!(beta, fit.result, epsilon = 64.0 * f64::EPSILON);
139 let _lr: f64 = fit.lr_test();
140 Ok(())
141 }
142}