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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#![forbid(unsafe_code)]
#![warn(
missing_docs,
rust_2018_idioms,
trivial_casts,
unused_lifetimes,
unused_qualifications,
missing_copy_implementations,
missing_debug_implementations,
clippy::cognitive_complexity,
clippy::missing_const_for_fn,
clippy::needless_borrow
)]
use std::iter::FromIterator;
use statrs::distribution::{ContinuousCDF, Normal, StudentsT};
#[derive(Copy, Clone, Debug)]
pub struct Difference {
pub effect: f64,
pub effect_size: f64,
pub critical_value: f64,
pub p_value: f64,
pub alpha: f64,
pub beta: f64,
}
impl Difference {
pub fn is_significant(&self) -> bool {
self.effect > self.critical_value
}
}
#[derive(Copy, Clone, Debug)]
pub struct Summary {
pub n: f64,
pub mean: f64,
pub variance: f64,
}
impl<'a> FromIterator<&'a f64> for Summary {
fn from_iter<T: IntoIterator<Item = &'a f64>>(iter: T) -> Self {
let (mut mean, mut s, mut n) = (0.0, 0.0, 0.0);
for x in iter {
n += 1.0;
let delta = x - mean;
mean += delta / n;
s += delta * (x - mean);
}
let variance = s / (n - 1.0);
Summary { n, mean, variance }
}
}
impl Summary {
pub fn std_dev(&self) -> f64 {
self.variance.sqrt()
}
pub fn std_err(&self) -> f64 {
self.std_dev() / self.n.sqrt()
}
pub fn compare(&self, other: &Summary, confidence: f64) -> Difference {
assert!(0.0 < confidence && confidence < 100.0, "confidence must be (0,100)");
let (a, b) = (self, other);
let alpha = 1.0 - (confidence / 100.0);
let nu = (a.variance / a.n + b.variance / b.n).powf(2.0)
/ ((a.variance).powf(2.0) / ((a.n).powf(2.0) * (a.n - 1.0))
+ (b.variance).powf(2.0) / ((b.n).powf(2.0) * (b.n - 1.0)));
let dist_st = StudentsT::new(0.0, 1.0, nu).unwrap();
let t_hyp = dist_st.inverse_cdf(1.0 - (alpha / TAILS));
let effect = (a.mean - b.mean).abs();
let std_err = (a.variance / a.n + b.variance / b.n).sqrt();
let t_exp = effect / std_err;
let p_value = dist_st.cdf(-t_exp) * TAILS;
let critical_value = t_hyp * std_err;
let std_dev = ((a.variance + b.variance) / 2.0).sqrt();
let effect_size = effect / std_dev;
let z = effect / (std_dev * (1.0 / a.n + 1.0 / b.n).sqrt());
let dist_norm = Normal::new(0.0, 1.0).unwrap();
let za = dist_norm.inverse_cdf(1.0 - alpha / TAILS);
let beta = dist_norm.cdf(z - za) - dist_norm.cdf(-z - za);
Difference { effect, effect_size, critical_value, p_value, alpha, beta }
}
}
const TAILS: f64 = 2.0;
#[cfg(test)]
mod test {
use approx::assert_relative_eq;
use super::*;
#[test]
fn summarize_odd() {
let s: Summary = vec![1.0, 2.0, 3.0].iter().collect();
assert_relative_eq!(s.n, 3.0);
assert_relative_eq!(s.mean, 2.0);
assert_relative_eq!(s.variance, 1.0);
}
#[test]
fn summarize_even() {
let s: Summary = vec![1.0, 2.0, 3.0, 4.0].iter().collect();
assert_relative_eq!(s.n, 4.0);
assert_relative_eq!(s.mean, 2.5);
assert_relative_eq!(s.variance, 1.6666666666666667);
}
#[test]
fn compare_similar_data() {
let a: Summary = vec![1.0, 2.0, 3.0, 4.0].iter().collect();
let b: Summary = vec![1.0, 2.0, 3.0, 4.0].iter().collect();
let diff = a.compare(&b, 80.0);
assert_relative_eq!(diff.effect, 0.0);
assert_relative_eq!(diff.effect_size, 0.0);
assert_relative_eq!(diff.critical_value, 1.3143111667913936);
assert_relative_eq!(diff.p_value, 1.0);
assert_relative_eq!(diff.alpha, 0.19999999999999996);
assert_relative_eq!(diff.beta, 0.0);
assert_eq!(diff.is_significant(), false);
}
#[test]
fn compare_different_data() {
let a: Summary = vec![1.0, 2.0, 3.0, 4.0].iter().collect();
let b: Summary = vec![10.0, 20.0, 30.0, 40.0].iter().collect();
let diff = a.compare(&b, 80.0);
assert_relative_eq!(diff.effect, 22.5);
assert_relative_eq!(diff.effect_size, 2.452519415855564);
assert_relative_eq!(diff.critical_value, 10.568344341563591);
assert_relative_eq!(diff.p_value, 0.03916791618893325);
assert_relative_eq!(diff.alpha, 0.19999999999999996);
assert_relative_eq!(diff.beta, 0.985621684277956);
assert_eq!(diff.is_significant(), true);
}
}