Struct Weibull

Source
pub struct Weibull { /* private fields */ }
Expand description

§The Weibull Distribution

§Description

Density, distribution function, quantile function and random generation for the Weibull distribution with parameters shape and scale.

§Arguments

  • shape, scale: shape and scale parameters, the latter defaulting to 1.

§Details

The Weibull distribution with shape parameter a and scale parameter b has density given by

$ f(x) = (a/b) (x/b)^{a-1} exp(- (x/b)^a) $

for x > 0. The cumulative distribution function is $F(x) = 1 - exp(- (x/b)^a)$ on x > 0, the mean is $E(X) = b \Gamma(1 + 1/a)$, and the $Var(X) = b^2 * (\Gamma(1 + 2/a) - (\Gamma(1 + 1/a))^2)$.

§Density Plot

let weibull = WeibullBuilder::new().build();
let x = <[f64]>::sequence(-1.0, 5.0, 1000);
let y = x
    .iter()
    .map(|x| weibull.density(x).unwrap())
    .collect::<Vec<_>>();

let root = SVGBackend::new("density.svg", (1024, 768)).into_drawing_area();
Plot::new()
    .with_options(PlotOptions {
        x_axis_label: "x".to_string(),
        y_axis_label: "density".to_string(),
        ..Default::default()
    })
    .with_plottable(Line {
        x,
        y,
        color: BLACK,
        ..Default::default()
    })
    .plot(&root)
    .unwrap();

§Note

The cumulative hazard $H(t) = - log(1 - F(t))$ is

-pweibull(t, a, b, lower = FALSE, log = TRUE) which is just $H(t) = (t/b)^a$.

§Source

[dpq]weibull are calculated directly from the definitions. rweibull uses inversion.

References Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 21. Wiley, New York.

§See Also

Distributions for other standard distributions, including the Exponential which is a special case of the Weibull distribution.

§Examples

Using this x for everything below

let mut rng = MersenneTwister::new();
rng.set_seed(1);

let lnorm = LogNormalBuilder::new().build();

let mut x = (0..50)
    .map(|_| lnorm.random_sample(&mut rng).unwrap())
    .collect::<Vec<_>>();
x.insert(0, 0.0);
let weibull = WeibullBuilder::new().with_shape(1).build();
let r1 = x
    .iter()
    .map(|x| weibull.density(x).unwrap())
    .collect::<Vec<_>>();

let exp = ExponentialBuilder::new().build();
let r2 = x
    .iter()
    .map(|x| exp.density(x).unwrap())
    .collect::<Vec<_>>();

println!("{r1:?}");
println!("{r2:?}");
let weibull = WeibullBuilder::new()
    .with_shape(1)
    .with_scale(f64::PI())
    .build();
let r1 = x
    .iter()
    .map(|x| weibull.probability(x, true).unwrap())
    .collect::<Vec<_>>();

let exp = ExponentialBuilder::new().with_rate(1.0 / f64::PI()).build();
let r2 = x
    .iter()
    .map(|x| exp.probability(x, true).unwrap())
    .collect::<Vec<_>>();

println!("{r1:?}");
println!("{r2:?}");

Cumulative hazard H()

let weibull = WeibullBuilder::new()
    .with_shape(2.5)
    .with_scale(f64::PI())
    .build();
let r1 = x
    .iter()
    .map(|x| weibull.log_probability(x, false).unwrap())
    .collect::<Vec<_>>();

let r2 = x
    .iter()
    .map(|x| -(x / f64::PI()).powf(2.5))
    .collect::<Vec<_>>();

println!("{r1:?}");
println!("{r2:?}");
let weibull = WeibullBuilder::new()
    .with_shape(1)
    .with_scale(f64::PI())
    .build();
let r1 = x
    .iter()
    .map(|x| weibull.quantile(x / 11.0, true).unwrap())
    .collect::<Vec<_>>();

let exp = ExponentialBuilder::new().with_rate(1.0 / f64::PI()).build();
let r2 = x
    .iter()
    .map(|x| exp.quantile(x / 11.0, true).unwrap())
    .collect::<Vec<_>>();

println!("{r1:?}");
println!("{r2:?}");

Trait Implementations§

Source§

impl Distribution for Weibull

Source§

fn density<R>(&self, x: R) -> NonNan<f64>
where R: Into<NonNan<f64>>,

The density of the values at a given point
Source§

fn log_density<R>(&self, x: R) -> NonNan<f64>
where R: Into<NonNan<f64>>,

The logarithmic density of the values at a given point
Source§

fn probability<R>( &self, q: R, lower_tail: bool, ) -> GreaterThanEqualZero<LessThanEqualOne<NonNan<f64>>>
where R: Into<NonNan<f64>>,

PDF; The probability that a value is found in a distribution (inverse of quantile)
Source§

fn log_probability<R>( &self, q: R, lower_tail: bool, ) -> LessThanEqualZero<NonNan<f64>>
where R: Into<NonNan<f64>>,

log(PDF); The logarithmic probability that a value is found in a distribution (inverse of quantile)
Source§

fn quantile<P>(&self, p: P, lower_tail: bool) -> NonNan<f64>

The value in the distribution that is associated with a probability (inverse of probability)
Source§

fn log_quantile<LP>(&self, p: LP, lower_tail: bool) -> NonNan<f64>

The logarithmic value in the distribution that is associated with a probability (inverse of probability)
Source§

fn random_sample<R>(&self, rng: &mut R) -> NonNan<f64>
where R: RNG,

Generates a random sample from the distribution

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.