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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Invere Gaussian distribution over x in (0, ∞)
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};

use once_cell::sync::OnceCell;
use rand::Rng;
use rand_distr::Normal;
use std::fmt;

use crate::consts::*;
use crate::data::InvGaussianSuffStat;
use crate::traits::*;
use crate::{clone_cache_f64, impl_display};

/// [Inverse Gaussian distribution](https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution),
/// N<sup>-1</sup>(μ, λ) over real values.
#[derive(Debug)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct InvGaussian {
    /// Mean
    mu: f64,
    /// Shape
    lambda: f64,
    /// Cached log(lambda)
    #[cfg_attr(feature = "serde1", serde(skip))]
    ln_lambda: OnceCell<f64>,
}

impl Clone for InvGaussian {
    fn clone(&self) -> Self {
        Self {
            mu: self.mu,
            lambda: self.lambda,
            ln_lambda: clone_cache_f64!(self, ln_lambda),
        }
    }
}

impl PartialEq for InvGaussian {
    fn eq(&self, other: &InvGaussian) -> bool {
        self.mu == other.mu && self.lambda == other.lambda
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub enum InvGaussianError {
    /// The mu parameter is infinite or NaN
    MuNotFinite { mu: f64 },
    /// The mu parameter is less than or equal to zero
    MuTooLow { mu: f64 },
    /// The lambda parameter is less than or equal to zero
    LambdaTooLow { lambda: f64 },
    /// The lambda parameter is infinite or NaN
    LambdaNotFinite { lambda: f64 },
}

impl InvGaussian {
    /// Create a new Inverse Gaussian distribution
    ///
    /// # Aruments
    /// - mu: mean > 0
    /// - lambda: shape > 0
    ///
    /// ```
    /// use rv::dist::InvGaussian;
    /// let invgauss = InvGaussian::new(1.0, 3.0).unwrap();
    /// ```
    ///
    /// Mu and lambda must be finite and greater than 0.
    /// ```
    /// # use rv::dist::InvGaussian;
    /// use std::f64::{NAN, INFINITY};
    /// assert!(InvGaussian::new(0.0, 3.0).is_err());
    /// assert!(InvGaussian::new(NAN, 3.0).is_err());
    /// assert!(InvGaussian::new(INFINITY, 3.0).is_err());
    ///
    /// assert!(InvGaussian::new(1.0, 0.0).is_err());
    /// assert!(InvGaussian::new(1.0, NAN).is_err());
    /// assert!(InvGaussian::new(1.0, INFINITY).is_err());
    /// ```
    pub fn new(mu: f64, lambda: f64) -> Result<Self, InvGaussianError> {
        if !mu.is_finite() {
            Err(InvGaussianError::MuNotFinite { mu })
        } else if mu <= 0.0 {
            Err(InvGaussianError::MuTooLow { mu })
        } else if lambda <= 0.0 {
            Err(InvGaussianError::LambdaTooLow { lambda })
        } else if !lambda.is_finite() {
            Err(InvGaussianError::LambdaNotFinite { lambda })
        } else {
            Ok(InvGaussian {
                mu,
                lambda,
                ln_lambda: OnceCell::new(),
            })
        }
    }

    /// Creates a new InvGaussian without checking whether the parameters are
    /// valid.
    #[inline]
    pub fn new_unchecked(mu: f64, lambda: f64) -> Self {
        InvGaussian {
            mu,
            lambda,
            ln_lambda: OnceCell::new(),
        }
    }

    /// Get mu parameter
    ///
    /// # Example
    ///
    /// ```rust
    /// # use rv::dist::InvGaussian;
    /// let ig = InvGaussian::new(2.0, 1.5).unwrap();
    ///
    /// assert_eq!(ig.mu(), 2.0);
    /// ```
    #[inline]
    pub fn mu(&self) -> f64 {
        self.mu
    }

    /// Set the value of mu
    ///
    /// # Example
    ///
    /// ```rust
    /// # use rv::dist::InvGaussian;
    /// let mut ig = InvGaussian::new(2.0, 1.5).unwrap();
    /// assert_eq!(ig.mu(), 2.0);
    ///
    /// ig.set_mu(1.3).unwrap();
    /// assert_eq!(ig.mu(), 1.3);
    /// ```
    ///
    /// Will error for invalid values
    ///
    /// ```rust
    /// # use rv::dist::InvGaussian;
    /// # let mut ig = InvGaussian::new(2.0, 1.5).unwrap();
    /// assert!(ig.set_mu(1.3).is_ok());
    /// assert!(ig.set_mu(0.0).is_err());
    /// assert!(ig.set_mu(-1.0).is_err());
    /// assert!(ig.set_mu(std::f64::NEG_INFINITY).is_err());
    /// assert!(ig.set_mu(std::f64::INFINITY).is_err());
    /// assert!(ig.set_mu(std::f64::NAN).is_err());
    /// ```
    #[inline]
    pub fn set_mu(&mut self, mu: f64) -> Result<(), InvGaussianError> {
        if !mu.is_finite() {
            Err(InvGaussianError::MuNotFinite { mu })
        } else if mu <= 0.0 {
            Err(InvGaussianError::MuTooLow { mu })
        } else {
            self.set_mu_unchecked(mu);
            Ok(())
        }
    }

    /// Set the value of mu without input validation
    #[inline]
    pub fn set_mu_unchecked(&mut self, mu: f64) {
        self.mu = mu;
    }

    /// Get lambda parameter
    ///
    /// # Example
    ///
    /// ```rust
    /// # use rv::dist::InvGaussian;
    /// let ig = InvGaussian::new(2.0, 1.5).unwrap();
    ///
    /// assert_eq!(ig.lambda(), 1.5);
    /// ```
    #[inline]
    pub fn lambda(&self) -> f64 {
        self.lambda
    }

    /// Set the value of lambda
    ///
    /// # Example
    ///
    /// ```rust
    /// # use rv::dist::InvGaussian;
    /// let mut ig = InvGaussian::new(1.0, 2.0).unwrap();
    /// assert_eq!(ig.lambda(), 2.0);
    ///
    /// ig.set_lambda(2.3).unwrap();
    /// assert_eq!(ig.lambda(), 2.3);
    /// ```
    ///
    /// Will error for invalid values
    ///
    /// ```rust
    /// # use rv::dist::InvGaussian;
    /// # let mut ig = InvGaussian::new(1.0, 2.0).unwrap();
    /// assert!(ig.set_lambda(2.3).is_ok());
    /// assert!(ig.set_lambda(0.0).is_err());
    /// assert!(ig.set_lambda(-1.0).is_err());
    /// assert!(ig.set_lambda(std::f64::INFINITY).is_err());
    /// assert!(ig.set_lambda(std::f64::NEG_INFINITY).is_err());
    /// assert!(ig.set_lambda(std::f64::NAN).is_err());
    /// ```
    #[inline]
    pub fn set_lambda(&mut self, lambda: f64) -> Result<(), InvGaussianError> {
        if lambda <= 0.0 {
            Err(InvGaussianError::LambdaTooLow { lambda })
        } else if !lambda.is_finite() {
            Err(InvGaussianError::LambdaNotFinite { lambda })
        } else {
            self.set_lambda_unchecked(lambda);
            Ok(())
        }
    }

    /// Set the value of lambda without input validation
    #[inline]
    pub fn set_lambda_unchecked(&mut self, lambda: f64) {
        self.ln_lambda = OnceCell::new();
        self.lambda = lambda;
    }

    /// Return (mu, lambda)
    #[inline]
    pub fn params(&self) -> (f64, f64) {
        (self.mu, self.lambda)
    }

    #[inline]
    fn ln_lambda(&self) -> f64 {
        *self.ln_lambda.get_or_init(|| self.lambda.ln())
    }
}

impl From<&InvGaussian> for String {
    fn from(ig: &InvGaussian) -> String {
        format!("N⁻¹(μ: {}, λ: {})", ig.mu, ig.lambda)
    }
}

impl_display!(InvGaussian);

macro_rules! impl_traits {
    ($kind:ty) => {
        impl Rv<$kind> for InvGaussian {
            fn ln_f(&self, x: &$kind) -> f64 {
                let (mu, lambda) = self.params();
                let xf = f64::from(*x);
                let z = self.ln_lambda() - xf.ln().mul_add(3.0, LN_2PI);
                let term = lambda * (xf - mu).powi(2) / (2.0 * mu * mu * xf);
                z.mul_add(0.5, -term)
            }

            // https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution#Sampling_from_an_inverse-Gaussian_distribution
            fn draw<R: Rng>(&self, rng: &mut R) -> $kind {
                let (mu, lambda) = self.params();
                let g = Normal::new(0.0, 1.0).unwrap();
                let v: f64 = rng.sample(g);
                let y = v * v;
                let mu2 = mu * mu;
                let x = mu
                    + 0.5
                        * (mu2 * y / lambda
                            - mu / lambda
                                * (4.0 * mu * lambda * y + mu2 * y * y).sqrt());
                let z: f64 = rng.gen();

                if z <= mu / (mu + x) {
                    x as $kind
                } else {
                    (mu2 / x) as $kind
                }
            }
        }

        impl ContinuousDistr<$kind> for InvGaussian {}

        impl Support<$kind> for InvGaussian {
            fn supports(&self, x: &$kind) -> bool {
                x.is_finite()
            }
        }

        impl Cdf<$kind> for InvGaussian {
            fn cdf(&self, x: &$kind) -> f64 {
                let xf = f64::from(*x);
                let (mu, lambda) = self.params();
                let gauss = crate::dist::Gaussian::standard();
                let z = (lambda / xf).sqrt();
                let a = z * (xf / mu - 1.0);
                let b = -z * (xf / mu + 1.0);
                gauss.cdf(&a) + (2.0 * lambda / mu).exp() * gauss.cdf(&b)
            }
        }
        impl Mean<$kind> for InvGaussian {
            fn mean(&self) -> Option<$kind> {
                Some(self.mu as $kind)
            }
        }

        impl Mode<$kind> for InvGaussian {
            fn mode(&self) -> Option<$kind> {
                let (mu, lambda) = self.params();
                let a = (1.0 + 0.25 * 9.0 * mu.powi(2) / lambda.powi(2)).sqrt();
                let b = 0.5 * 3.0 * mu / lambda;
                let mode = mu * (a - b);
                Some(mode as $kind)
            }
        }

        impl HasSuffStat<$kind> for InvGaussian {
            type Stat = InvGaussianSuffStat;
            fn empty_suffstat(&self) -> Self::Stat {
                InvGaussianSuffStat::new()
            }
        }
    };
}

impl Variance<f64> for InvGaussian {
    fn variance(&self) -> Option<f64> {
        Some(self.mu.powi(3) / self.lambda)
    }
}

impl Skewness for InvGaussian {
    fn skewness(&self) -> Option<f64> {
        Some(2.0 * (self.mu / self.lambda).sqrt())
    }
}

impl Kurtosis for InvGaussian {
    fn kurtosis(&self) -> Option<f64> {
        Some(15.0 * self.mu / self.lambda)
    }
}

impl_traits!(f32);
impl_traits!(f64);

impl std::error::Error for InvGaussianError {}

impl fmt::Display for InvGaussianError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::MuNotFinite { mu } => write!(f, "non-finite mu: {}", mu),
            Self::MuTooLow { mu } => {
                write!(f, "mu ({}) must be greater than zero", mu)
            }
            Self::LambdaTooLow { lambda } => {
                write!(f, "lambda ({}) must be greater than zero", lambda)
            }
            Self::LambdaNotFinite { lambda } => {
                write!(f, "non-finite lambda: {}", lambda)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::misc::ks_test;

    const N_TRIES: usize = 10;
    const KS_PVAL: f64 = 0.2;

    #[test]
    fn mode_is_higest_point() {
        let mut rng = rand::thread_rng();
        let mu_prior = crate::dist::InvGamma::new_unchecked(2.0, 2.0);
        let lambda_prior = crate::dist::InvGamma::new_unchecked(2.0, 2.0);
        for _ in 0..100 {
            let mu: f64 = mu_prior.draw(&mut rng);
            let lambda: f64 = lambda_prior.draw(&mut rng);
            let ig = InvGaussian::new(mu, lambda).unwrap();
            let mode: f64 = ig.mode().unwrap();
            let ln_f_mode = ig.ln_f(&mode);
            let ln_f_plus = ig.ln_f(&(mode + 1e-4));
            let ln_f_minus = ig.ln_f(&(mode - 1e-4));

            assert!(ln_f_mode > ln_f_plus);
            assert!(ln_f_mode > ln_f_minus);
        }
    }

    #[test]
    fn quad_on_pdf_agrees_with_cdf_x() {
        use crate::misc::quad_eps;
        let ig = InvGaussian::new(1.1, 2.5).unwrap();
        // use pdf to hit `supports(x)` first
        let pdf = |x: f64| ig.pdf(&x);
        let mut rng = rand::thread_rng();
        for _ in 0..100 {
            let x: f64 = ig.draw(&mut rng);
            let res = quad_eps(pdf, 1e-16, x, Some(1e-10));
            let cdf = ig.cdf(&x);
            assert::close(res, cdf, 1e-9);
        }
    }

    #[test]
    fn draw_vs_kl() {
        let mut rng = rand::thread_rng();
        let ig = InvGaussian::new(1.2, 3.4).unwrap();
        let cdf = |x: f64| ig.cdf(&x);

        // test is flaky, try a few times
        let passes = (0..N_TRIES).fold(0, |acc, _| {
            let xs: Vec<f64> = ig.sample(1000, &mut rng);
            let (_, p) = ks_test(&xs, cdf);
            if p > KS_PVAL {
                acc + 1
            } else {
                acc
            }
        });

        assert!(passes > 0);
    }
}