Struct rand_distr::Normal
source · 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::thread_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 = thread_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);
Trait Implementations§
source§impl<'de, F> Deserialize<'de> for Normal<F>
impl<'de, F> Deserialize<'de> for Normal<F>
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<F> Distribution<F> for Normal<F>
impl<F> Distribution<F> for Normal<F>
impl<F> Copy for Normal<F>
impl<F> StructuralPartialEq for Normal<F>
Auto Trait Implementations§
impl<F> Freeze for Normal<F>where
F: Freeze,
impl<F> RefUnwindSafe for Normal<F>where
F: RefUnwindSafe,
impl<F> Send for Normal<F>where
F: Send,
impl<F> Sync for Normal<F>where
F: Sync,
impl<F> Unpin for Normal<F>where
F: Unpin,
impl<F> UnwindSafe for Normal<F>where
F: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)