forked from statrs-dev/statrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirac.rs
324 lines (292 loc) · 7.65 KB
/
dirac.rs
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use crate::distribution::{Continuous, Univariate};
use crate::statistics::*;
use crate::{Result, StatsError};
use rand::distributions::Distribution;
use rand::Rng;
use std::f64;
/// Implements the [Dirac Delta](https://en.wikipedia.org/wiki/Dirac_delta_function#As_a_distribution)
/// distribution
///
/// # Examples
///
/// ```
/// use statrs::distribution::{Dirac};
///
/// let n = Dirac::new(3.0).unwrap();
/// assert_eq!(n.mean(), 3.0);
/// assert_eq!(n.pdf(1.0), 0.0);
/// ```
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Dirac(f64);
impl Dirac {
/// Constructs a new dirac distribution function at value `v`.
///
/// # Errors
///
/// Returns an error if `v` is not-a-number.
///
/// # Examples
///
/// ```
/// use statrs::distribution::Dirac;
///
/// let mut result = Dirac::new(0.0);
/// assert!(result.is_ok());
///
/// result = Dirac::new(f64::NAN);
/// assert!(result.is_err());
/// ```
pub fn new(v: f64) -> Result<Self> {
if v.is_nan() {
Err(StatsError::BadParams)
} else {
Ok(Dirac(v))
}
}
}
impl Distribution<f64> for Dirac {
fn sample<R: Rng + ?Sized>(&self, _: &mut R) -> f64 {
self.0
}
}
impl Univariate<f64, f64> for Dirac {
/// Calculates the cumulative distribution function for the
/// dirac distribution at `x`
///
/// Where the value is 1 if x > `v`, 0 otherwise.
///
fn cdf(&self, x: f64) -> f64 {
if x < self.0 { 0.0 } else { 1.0 }
}
}
impl Min<f64> for Dirac {
/// Returns the minimum value in the domain of the
/// dirac distribution representable by a double precision float
///
/// # Formula
///
/// ```ignore
/// v
/// ```
fn min(&self) -> f64 { self.0 }
}
impl Max<f64> for Dirac {
/// Returns the maximum value in the domain of the
/// dirac distribution representable by a double precision float
///
/// # Formula
///
/// ```ignore
/// v
/// ```
fn max(&self) -> f64 { self.0 }
}
impl Mean<f64> for Dirac {
/// Returns the mean of the dirac distribution
///
/// # Remarks
///
/// Since the only value that can be produced by this distribution is `v` with probability
/// 1, it is just `v`.
fn mean(&self) -> f64 { self.0 }
}
impl Variance<f64> for Dirac {
/// Returns the variance of the normal distribution
///
/// # Formula
///
/// ```ignore
/// 0
/// ```
///
/// Since only one value can be produced there is no variance.
fn variance(&self) -> f64 { 0.0 }
/// Returns the standard deviation of the normal distribution
///
/// # Remarks
///
/// Since there is no variance in draws from this distribution the standard deviation is
/// also 0.
fn std_dev(&self) -> f64 { 0.0 }
}
impl Entropy<f64> for Dirac {
/// Returns the entropy of the dirac distribution
///
/// # Formula
///
/// ```ignore
/// 0
/// ```
///
/// Since this distribution has full certainty, it encodes no information
fn entropy(&self) -> f64 { 0.0 }
}
impl Skewness<f64> for Dirac {
/// Returns the skewness of the dirac distribution
///
/// # Formula
///
/// ```ignore
/// 0
/// ```
fn skewness(&self) -> f64 { 0.0 }
}
impl Median<f64> for Dirac {
/// Returns the median of the dirac distribution
///
/// # Formula
///
/// ```ignore
/// v
/// ```
///
/// where `v` is the point of the dirac distribution
fn median(&self) -> f64 {
self.0
}
}
impl Mode<f64> for Dirac {
/// Returns the mode of the normal distribution
///
/// # Formula
///
/// ```ignore
/// v
/// ```
///
/// where `v` is the point of the dirac distribution
fn mode(&self) -> f64 {
self.0
}
}
impl Continuous<f64, f64> for Dirac {
/// Calculates the probability density function for the dirac distribution
/// at `x`
///
/// # Formula
///
/// ```ignore
/// 1 if x = v, 0 otherwise
/// ```
///
/// where `v` is point of this dirac distribution
fn pdf(&self, x: f64) -> f64 {
if x == self.0 { 1.0 } else { 0.0 }
}
/// Calculates the log probability density function for the normal
/// distribution
/// at `x`
///
/// # Formula
///
/// ```ignore
/// ln(1 if x = v, 0 otherwise)
/// ```
///
/// where `v` is the point of this dirac distribution
///
/// # Remarks
/// This distribution is usually negative infinity everywhere except at `v`.
fn ln_pdf(&self, x: f64) -> f64 {
if self.0 == x { 0.0 } else { f64::NEG_INFINITY }
}
}
#[cfg_attr(rustfmt, rustfmt_skip)]
#[cfg(test)]
mod test {
use std::f64;
use crate::statistics::*;
use crate::distribution::{Univariate, Continuous, Dirac};
use crate::distribution::internal::*;
fn try_create(v: f64) -> Dirac {
let d = Dirac::new(v);
assert!(d.is_ok());
d.unwrap()
}
fn create_case(v: f64) {
let d = try_create(v);
assert_eq!(v, d.mean());
}
fn bad_create_case(v: f64) {
let d = Dirac::new(v);
assert!(d.is_err());
}
fn test_case<F>(v: f64, expected: f64, eval: F)
where F: Fn(Dirac) -> f64
{
let x = eval(try_create(v));
assert_eq!(expected, x);
}
#[test]
fn test_create() {
create_case(10.0);
create_case(-5.0);
create_case(10.0);
create_case(100.0);
create_case(f64::INFINITY);
}
#[test]
fn test_bad_create() {
bad_create_case(f64::NAN);
}
#[test]
fn test_variance() {
test_case(0.0, 0.0, |x| x.variance());
test_case(-5.0, 0.0, |x| x.variance());
test_case(f64::INFINITY, 0.0, |x| x.variance());
}
#[test]
fn test_entropy() {
test_case(0.0, 0.0, |x| x.entropy());
test_case(f64::INFINITY, 0.0, |x| x.entropy());
}
#[test]
fn test_skewness() {
test_case(0.0, 0.0, |x| x.skewness());
test_case(4.0, 0.0, |x| x.skewness());
test_case(0.3, 0.0, |x| x.skewness());
test_case(f64::INFINITY, 0.0, |x| x.skewness());
}
#[test]
fn test_mode() {
test_case(0.0, 0.0, |x| x.mode());
test_case(3.0, 3.0, |x| x.mode());
test_case(f64::INFINITY, f64::INFINITY, |x| x.mode());
}
#[test]
fn test_median() {
test_case(0.0, 0.0, |x| x.median());
test_case(3.0, 3.0, |x| x.median());
test_case(f64::INFINITY, f64::INFINITY, |x| x.median());
}
#[test]
fn test_min_max() {
test_case(0.0, 0.0, |x| x.min());
test_case(3.0, 3.0, |x| x.min());
test_case(f64::INFINITY, f64::INFINITY, |x| x.min());
test_case(0.0, 0.0, |x| x.max());
test_case(3.0, 3.0, |x| x.max());
test_case(f64::NEG_INFINITY, f64::NEG_INFINITY, |x| x.max());
}
#[test]
fn test_pdf() {
test_case(0.0, 0.0, |x| x.pdf(1.0));
test_case(3.0, 1.0, |x| x.pdf(3.0));
test_case(f64::NEG_INFINITY, 0.0, |x| x.pdf(1.0));
test_case(f64::NEG_INFINITY, 1.0, |x| x.pdf(f64::NEG_INFINITY));
}
#[test]
fn test_ln_pdf() {
test_case(0.0, 0.0, |x| x.ln_pdf(0.0));
test_case(3.0, 0.0, |x| x.ln_pdf(3.0));
test_case(f64::INFINITY, f64::NEG_INFINITY, |x| x.ln_pdf(1.0));
test_case(f64::INFINITY, 0.0, |x| x.ln_pdf(f64::INFINITY));
}
#[test]
fn test_cdf() {
test_case(0.0, 1.0, |x| x.cdf(0.0));
test_case(3.0, 1.0, |x| x.cdf(3.0));
test_case(f64::INFINITY, 0.0, |x| x.cdf(1.0));
test_case(f64::INFINITY, 1.0, |x| x.cdf(f64::INFINITY));
}
}