pub struct Normal<F>{ /* private fields */ }
Expand description
The Normal distribution N(μ, σ²)
.
The Normal distribution, also known as the Gaussian distribution or
bell curve, is a continuous probability distribution with mean
μ
(mu
) and standard deviation σ
(sigma
).
It is used to model continuous data that tend to cluster around a mean.
The Normal distribution is symmetric and characterized by its bell-shaped curve.
See StandardNormal
for an
optimised implementation for μ = 0
and σ = 1
.
§Density function
f(x) = (1 / sqrt(2π σ²)) * exp(-((x - μ)² / (2σ²)))
§Plot
The following diagram shows the Normal distribution with various values of μ
and σ
.
The blue curve is the StandardNormal
distribution, N(0, 1)
.
§Example
use rand_distr::{Normal, Distribution};
// mean 2, standard deviation 3
let normal = Normal::new(2.0, 3.0).unwrap();
let v = normal.sample(&mut rand::rng());
println!("{} is from a N(2, 9) distribution", v)
§Notes
Implemented via the ZIGNOR variant1 of the Ziggurat method.
Jurgen A. Doornik (2005). An Improved Ziggurat Method to Generate Normal Random Samples. Nuffield College, Oxford ↩
Implementations§
Source§impl<F> Normal<F>
impl<F> Normal<F>
Sourcepub fn new(mean: F, std_dev: F) -> Result<Normal<F>, Error>
pub fn new(mean: F, std_dev: F) -> Result<Normal<F>, Error>
Construct, from mean and standard deviation
Parameters:
- mean (
μ
, unrestricted) - standard deviation (
σ
, must be finite)
Sourcepub fn from_mean_cv(mean: F, cv: F) -> Result<Normal<F>, Error>
pub fn from_mean_cv(mean: F, cv: F) -> Result<Normal<F>, Error>
Construct, from mean and coefficient of variation
Parameters:
- mean (
μ
, unrestricted) - coefficient of variation (
cv = abs(σ / μ)
)
Sourcepub fn from_zscore(&self, zscore: F) -> F
pub fn from_zscore(&self, zscore: F) -> F
Sample from a z-score
This may be useful for generating correlated samples x1
and x2
from two different distributions, as follows.
let mut rng = rand::rng();
let z = StandardNormal.sample(&mut rng);
let x1 = Normal::new(0.0, 1.0).unwrap().from_zscore(z);
let x2 = Normal::new(2.0, -3.0).unwrap().from_zscore(z);