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);
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<Normal<F>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Normal<F>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl<F> Distribution<F> for Normal<F>
impl<F> Distribution<F> for Normal<F>
Source§impl<T> From<Normal<T>> for TruncatedNormal<T>
impl<T> From<Normal<T>> for TruncatedNormal<T>
Source§fn from(normal: Normal<T>) -> TruncatedNormal<T>
fn from(normal: Normal<T>) -> TruncatedNormal<T>
Source§impl<F> Serialize for Normal<F>
impl<F> Serialize for Normal<F>
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
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> CallInPlace<T> for Twhere
T: CallInto<T>,
impl<T> CallInPlace<T> for Twhere
T: CallInto<T>,
Source§fn call_inplace<F>(&mut self, f: F) -> <T as CallInto<T>>::Output
fn call_inplace<F>(&mut self, f: F) -> <T as CallInto<T>>::Output
call_on_mut
method allows an object to be passed onto a function that takes a mutable reference
to the object. This is useful for cases where you want to perform an operation on
an object and mutate it in the process.Source§impl<T> CallInto<T> for T
impl<T> CallInto<T> for T
Source§impl<T> CallOn<T> for Twhere
T: CallInto<T>,
impl<T> CallOn<T> for Twhere
T: CallInto<T>,
Source§fn call_on<F>(&self, f: F) -> <T as CallInto<T>>::Output
fn call_on<F>(&self, f: F) -> <T as CallInto<T>>::Output
call_on
method allows an object to be passed onto a function that takes a reference
to the object. This is useful for cases where you want to perform an operation on
an object without needing to extract it from a container or context.