Struct probability::prelude::Gaussian
source · pub struct Gaussian { /* private fields */ }
Expand description
A Gaussian distribution.
Implementations§
source§impl Gaussian
impl Gaussian
sourcepub fn new(mu: f64, sigma: f64) -> Self
pub fn new(mu: f64, sigma: f64) -> Self
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
Trait Implementations§
source§impl Continuous for Gaussian
impl Continuous for Gaussian
source§impl Distribution for Gaussian
impl Distribution for Gaussian
source§impl FromIterator<f64> for Gaussian
impl FromIterator<f64> for Gaussian
source§fn from_iter<T: IntoIterator<Item = f64>>(iterator: T) -> Self
fn from_iter<T: IntoIterator<Item = f64>>(iterator: T) -> Self
Infer the distribution from an iterator.
source§impl Inverse for Gaussian
impl Inverse for Gaussian
source§fn inverse(&self, p: f64) -> f64
fn inverse(&self, p: f64) -> f64
Compute the inverse of the cumulative distribution function.
References
-
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.
-
http://people.sc.fsu.edu/~jburkardt/c_src/asa241/asa241.html