// Copyright 2021 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
//! The Skew Normal distribution.
usecrate::{Distribution, StandardNormal}; use core::fmt; use num_traits::Float; use rand::Rng;
/// The [skew normal distribution] `SN(location, scale, shape)`. /// /// The skew normal distribution is a generalization of the /// [`Normal`] distribution to allow for non-zero skewness. /// /// It has the density function, for `scale > 0`, /// `f(x) = 2 / scale * phi((x - location) / scale) * Phi(alpha * (x - location) / scale)` /// where `phi` and `Phi` are the density and distribution of a standard normal variable. /// /// # Example /// /// ``` /// use rand_distr::{SkewNormal, Distribution}; /// /// // location 2, scale 3, shape 1 /// let skew_normal = SkewNormal::new(2.0, 3.0, 1.0).unwrap(); /// let v = skew_normal.sample(&mut rand::thread_rng()); /// println!("{} is from a SN(2, 3, 1) distribution", v) /// ``` /// /// # Implementation details /// /// We are using the algorithm from [A Method to Simulate the Skew Normal Distribution]. /// /// [skew normal distribution]: https://en.wikipedia.org/wiki/Skew_normal_distribution /// [`Normal`]: struct.Normal.html /// [A Method to Simulate the Skew Normal Distribution]: https://dx.doi.org/10.4236/am.2014.513201 #[derive(Clone, Copy, Debug)] #[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pubstruct SkewNormal<F> where
F: Float,
StandardNormal: Distribution<F>,
{
location: F,
scale: F,
shape: F,
}
/// Error type returned from `SkewNormal::new`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pubenum Error { /// The scale parameter is not finite or it is less or equal to zero.
ScaleTooSmall, /// The shape parameter is not finite.
BadShape,
}
impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(matchself {
Error::ScaleTooSmall => { "scale parameter is either non-finite or it is less or equal to zero in skew normal distribution"
}
Error::BadShape => "shape parameter is non-finite in skew normal distribution",
})
}
}
#[test] fn skew_normal_value_location_nan() { let skew_normal = SkewNormal::new(core::f64::NAN, 1.0, 0.0).unwrap(); letmut rng = crate::test::rng(213); letmut buf = [0.0; 4]; for x in &mut buf {
*x = rng.sample(&skew_normal);
} for value in buf.iter() {
assert!(value.is_nan());
}
}
}
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.18Bemerkung:
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.