1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::{
error::{RegressionError, RegressionResult},
glm::{DispersionType, Glm},
link::Link,
math::prod_log,
num::Float,
response::Response,
};
use ndarray::Array1;
use std::marker::PhantomData;
pub struct Logistic<L = link::Logit>
where
L: Link<Logistic<L>>,
{
_link: PhantomData<L>,
}
impl<L> Response<Logistic<L>> for bool
where
L: Link<Logistic<L>>,
{
fn into_float<F: Float>(self) -> RegressionResult<F> {
Ok(if self { F::one() } else { F::zero() })
}
}
impl<L> Response<Logistic<L>> for f32
where
L: Link<Logistic<L>>,
{
fn into_float<F: Float>(self) -> RegressionResult<F> {
if !(0.0..=1.0).contains(&self) {
return Err(RegressionError::InvalidY(self.to_string()));
}
F::from(self).ok_or_else(|| RegressionError::InvalidY(self.to_string()))
}
}
impl<L> Response<Logistic<L>> for f64
where
L: Link<Logistic<L>>,
{
fn into_float<F: Float>(self) -> RegressionResult<F> {
if !(0.0..=1.0).contains(&self) {
return Err(RegressionError::InvalidY(self.to_string()));
}
F::from(self).ok_or_else(|| RegressionError::InvalidY(self.to_string()))
}
}
impl<L> Glm for Logistic<L>
where
L: Link<Logistic<L>>,
{
type Link = L;
const DISPERSED: DispersionType = DispersionType::NoDispersion;
fn log_partition<F: Float>(nat_par: F) -> F {
num_traits::Float::exp(nat_par).ln_1p()
}
fn variance<F: Float>(mean: F) -> F {
mean * (F::one() - mean)
}
fn log_like_natural<F>(y: F, logit_p: F) -> F
where
F: Float,
{
let (yt, xt) = if logit_p < F::zero() {
(y, logit_p)
} else {
(F::one() - y, -logit_p)
};
yt * xt - num_traits::Float::exp(xt).ln_1p()
}
fn log_like_sat<F: Float>(y: F) -> F {
prod_log(y) + prod_log(F::one() - y)
}
}
pub mod link {
use super::*;
use crate::link::{Canonical, Link, Transform};
use crate::num::Float;
pub struct Logit {}
impl Canonical for Logit {}
impl Link<Logistic<Logit>> for Logit {
fn func<F: Float>(y: F) -> F {
num_traits::Float::ln(y / (F::one() - y))
}
fn func_inv<F: Float>(lin_pred: F) -> F {
(F::one() + num_traits::Float::exp(-lin_pred)).recip()
}
}
pub struct Cloglog {}
impl Link<Logistic<Cloglog>> for Cloglog {
fn func<F: Float>(y: F) -> F {
num_traits::Float::ln(-F::ln_1p(-y))
}
fn func_inv<F: Float>(lin_pred: F) -> F {
-F::exp_m1(-num_traits::Float::exp(lin_pred))
}
}
impl Transform for Cloglog {
fn nat_param<F: Float>(lin_pred: Array1<F>) -> Array1<F> {
lin_pred.mapv(|x| num_traits::Float::ln(num_traits::Float::exp(x).exp_m1()))
}
fn d_nat_param<F: Float>(lin_pred: &Array1<F>) -> Array1<F> {
let neg_exp_lin = -lin_pred.mapv(num_traits::Float::exp);
&neg_exp_lin / &neg_exp_lin.mapv(F::exp_m1)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{error::RegressionResult, model::ModelBuilder};
use approx::assert_abs_diff_eq;
use ndarray::array;
#[test]
fn log_reg() -> RegressionResult<()> {
let beta = array![0., 1.0];
let ln2 = f64::ln(2.);
let data_x = array![[0.], [0.], [ln2], [ln2], [ln2]];
let data_y = array![true, false, true, true, false];
let model = ModelBuilder::<Logistic>::data(&data_y, &data_x).build()?;
let fit = model.fit()?;
assert_abs_diff_eq!(beta, fit.result, epsilon = 0.05 * f32::EPSILON as f64);
Ok(())
}
#[test]
fn cloglog_closure() {
use link::Cloglog;
let mu_test_vals = array![1e-8, 0.01, 0.1, 0.3, 0.5, 0.7, 0.9, 0.99, 0.9999999];
assert_abs_diff_eq!(
mu_test_vals,
mu_test_vals.mapv(|mu| Cloglog::func_inv(Cloglog::func(mu)))
);
let lin_test_vals = array![-10., -2., -0.1, 0.0, 0.1, 1., 2.];
assert_abs_diff_eq!(
lin_test_vals,
lin_test_vals.mapv(|lin| Cloglog::func(Cloglog::func_inv(lin))),
epsilon = 1e-3 * f32::EPSILON as f64
);
}
}