rand_distr/
inverse_gaussian.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
//! The inverse Gaussian distribution `IG(μ, λ)`.

use crate::{Distribution, StandardNormal, StandardUniform};
use core::fmt;
use num_traits::Float;
use rand::Rng;

/// Error type returned from [`InverseGaussian::new`]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
    /// `mean <= 0` or `nan`.
    MeanNegativeOrNull,
    /// `shape <= 0` or `nan`.
    ShapeNegativeOrNull,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Error::MeanNegativeOrNull => "mean <= 0 or is NaN in inverse Gaussian distribution",
            Error::ShapeNegativeOrNull => "shape <= 0 or is NaN in inverse Gaussian distribution",
        })
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

/// The [inverse Gaussian distribution](https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution) `IG(μ, λ)`.
///
/// This is a continuous probability distribution with mean parameter `μ` (`mu`)
/// and shape parameter `λ` (`lambda`), defined for `x > 0`.
/// It is also known as the Wald distribution.
///
/// # Plot
///
/// The following plot shows the inverse Gaussian distribution
/// with various values of `μ` and `λ`.
///
/// ![Inverse Gaussian distribution](https://raw.githubusercontent.com/rust-random/charts/main/charts/inverse_gaussian.svg)
///
/// # Example
/// ```
/// use rand_distr::{InverseGaussian, Distribution};
///
/// let inv_gauss = InverseGaussian::new(1.0, 2.0).unwrap();
/// let v = inv_gauss.sample(&mut rand::rng());
/// println!("{} is from a inverse Gaussian(1, 2) distribution", v);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct InverseGaussian<F>
where
    F: Float,
    StandardNormal: Distribution<F>,
    StandardUniform: Distribution<F>,
{
    mean: F,
    shape: F,
}

impl<F> InverseGaussian<F>
where
    F: Float,
    StandardNormal: Distribution<F>,
    StandardUniform: Distribution<F>,
{
    /// Construct a new `InverseGaussian` distribution with the given mean and
    /// shape.
    pub fn new(mean: F, shape: F) -> Result<InverseGaussian<F>, Error> {
        let zero = F::zero();
        if !(mean > zero) {
            return Err(Error::MeanNegativeOrNull);
        }

        if !(shape > zero) {
            return Err(Error::ShapeNegativeOrNull);
        }

        Ok(Self { mean, shape })
    }
}

impl<F> Distribution<F> for InverseGaussian<F>
where
    F: Float,
    StandardNormal: Distribution<F>,
    StandardUniform: Distribution<F>,
{
    #[allow(clippy::many_single_char_names)]
    fn sample<R>(&self, rng: &mut R) -> F
    where
        R: Rng + ?Sized,
    {
        let mu = self.mean;
        let l = self.shape;

        let v: F = rng.sample(StandardNormal);
        let y = mu * v * v;

        let mu_2l = mu / (F::from(2.).unwrap() * l);

        let x = mu + mu_2l * (y - (F::from(4.).unwrap() * l * y + y * y).sqrt());

        let u: F = rng.random();

        if u <= mu / (mu + x) {
            return x;
        }

        mu * mu / x
    }
}

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

    #[test]
    fn test_inverse_gaussian() {
        let inv_gauss = InverseGaussian::new(1.0, 1.0).unwrap();
        let mut rng = crate::test::rng(210);
        for _ in 0..1000 {
            inv_gauss.sample(&mut rng);
        }
    }

    #[test]
    fn test_inverse_gaussian_invalid_param() {
        assert!(InverseGaussian::new(-1.0, 1.0).is_err());
        assert!(InverseGaussian::new(-1.0, -1.0).is_err());
        assert!(InverseGaussian::new(1.0, -1.0).is_err());
        assert!(InverseGaussian::new(1.0, 1.0).is_ok());
    }

    #[test]
    fn inverse_gaussian_distributions_can_be_compared() {
        assert_eq!(
            InverseGaussian::new(1.0, 2.0),
            InverseGaussian::new(1.0, 2.0)
        );
    }
}