pub struct Gaussian { /* private fields */ }
Expand description

A Gaussian distribution.

Implementations§

Create a Gaussian distribution with mean mu and standard deviation sigma.

It should hold that sigma > 0.

Examples found in repository?
src/distribution/gaussian.rs (line 48)
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
    fn default() -> Self {
        Gaussian::new(0.0, 1.0)
    }
}

impl distribution::Continuous for Gaussian {
    fn density(&self, x: f64) -> f64 {
        (-(x - self.mu).powi(2) / (2.0 * self.sigma * self.sigma)).exp() / self.norm
    }
}

impl distribution::Distribution for Gaussian {
    type Value = f64;

    fn distribution(&self, x: f64) -> f64 {
        use core::f64::consts::SQRT_2;
        use special::Error;
        (1.0 + ((x - self.mu) / (self.sigma * SQRT_2)).error()) / 2.0
    }
}

impl distribution::Entropy for Gaussian {
    #[inline]
    fn entropy(&self) -> f64 {
        use core::f64::consts::{E, PI};
        0.5 * (2.0 * PI * E * self.sigma * self.sigma).ln()
    }
}

impl distribution::Inverse for Gaussian {
    /// Compute the inverse of the cumulative distribution function.
    ///
    /// ## References
    ///
    /// 1. M. J. Wichura, “Algorithm as 241: The percentage points of the normal
    ///    distribution,” Journal of the Royal Statistical Society. Series C
    ///    (Applied Statistics), vol. 37, no. 3, pp. pp. 477–484, 1988.
    ///
    /// 2. <http://people.sc.fsu.edu/~jburkardt/c_src/asa241/asa241.html>
    #[inline(always)]
    fn inverse(&self, p: f64) -> f64 {
        self.mu + self.sigma * inverse(p)
    }
}

impl distribution::Kurtosis for Gaussian {
    #[inline]
    fn kurtosis(&self) -> f64 {
        0.0
    }
}

impl distribution::Mean for Gaussian {
    #[inline]
    fn mean(&self) -> f64 {
        self.mu
    }
}

impl distribution::Median for Gaussian {
    #[inline]
    fn median(&self) -> f64 {
        self.mu
    }
}

impl distribution::Modes for Gaussian {
    #[inline]
    fn modes(&self) -> Vec<f64> {
        vec![self.mu]
    }
}

impl distribution::Sample for Gaussian {
    /// Draw a sample.
    ///
    /// ## References
    ///
    /// 1. G. Marsaglia and W. W. Tsang, “The ziggurat method for generating
    ///    random variables,” Journal of Statistical Software, vol. 5, no. 8,
    ///    pp. 1–7, 10 2000.
    ///
    /// 2. D. Eddelbuettel, “Ziggurat Revisited,” 2014.
    #[inline]
    fn sample<S>(&self, source: &mut S) -> f64
    where
        S: Source,
    {
        self.sigma * sample(source) + self.mu
    }
}

impl distribution::Skewness for Gaussian {
    #[inline]
    fn skewness(&self) -> f64 {
        0.0
    }
}

impl distribution::Variance for Gaussian {
    #[inline]
    fn variance(&self) -> f64 {
        self.sigma * self.sigma
    }

    #[inline]
    fn deviation(&self) -> f64 {
        self.sigma
    }
}

impl core::iter::FromIterator<f64> for Gaussian {
    /// Infer the distribution from an iterator.
    fn from_iter<T: IntoIterator<Item = f64>>(iterator: T) -> Self {
        let samples: Vec<f64> = iterator.into_iter().collect();
        let mu = samples.iter().fold(0.0, |a, b| a + b) / samples.len() as f64;
        let sigma = f64::sqrt(
            samples
                .iter()
                .fold(0.0, |a, b| a + f64::powf(b - mu as f64, 2.0))
                / (samples.len() - 1) as f64,
        );
        Gaussian::new(mu, sigma)
    }
More examples
Hide additional examples
src/distribution/lognormal.rs (line 26)
21
22
23
24
25
26
27
28
    pub fn new(mu: f64, sigma: f64) -> Self {
        should!(sigma > 0.0);
        Lognormal {
            mu,
            sigma,
            gaussian: Gaussian::new(mu, sigma),
        }
    }
src/distribution/cauchy.rs (line 111)
107
108
109
110
111
112
113
114
115
    fn sample<S>(&self, source: &mut S) -> f64
    where
        S: Source,
    {
        let gaussian = distribution::Gaussian::new(0.0, 1.0);
        let a = gaussian.sample(source);
        let b = gaussian.sample(source);
        self.x_0() + self.gamma() * a / (b.abs() + f64::MIN_POSITIVE)
    }

Return the mean.

Return the standard deviation.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Compute the probability density function.
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The type of outcomes.
Compute the cumulative distribution function.
Compute the differential entropy. Read more

Infer the distribution from an iterator.

Compute the inverse of the cumulative distribution function.

References
  1. M. J. Wichura, “Algorithm as 241: The percentage points of the normal distribution,” Journal of the Royal Statistical Society. Series C (Applied Statistics), vol. 37, no. 3, pp. pp. 477–484, 1988.

  2. http://people.sc.fsu.edu/~jburkardt/c_src/asa241/asa241.html

Compute the excess kurtosis.
Compute the expected value.
Compute the median.
Compute the modes.

Draw a sample.

References
  1. G. Marsaglia and W. W. Tsang, “The ziggurat method for generating random variables,” Journal of Statistical Software, vol. 5, no. 8, pp. 1–7, 10 2000.

  2. D. Eddelbuettel, “Ziggurat Revisited,” 2014.

Compute the skewness.
Compute the variance.
Compute the standard deviation.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.