Exponential

Struct Exponential 

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

§The Exponential Distribution

§Description:

Density, distribution function, quantile function and random generation for the exponential distribution with rate ‘rate’ (i.e., mean ‘1/rate’).

§Arguments:

  • rate.
  • scale: 1 / rate

§Details:

If ‘rate’ is not specified, it assumes the default value of ‘1’.

The exponential distribution with rate lambda has density

$ f(x) = lambda {e}^{- lambda x} $

for $x >= 0$.

§Density Plot

let exp = ExponentialBuilder::new().build();
let x = <[f64]>::sequence(-0.5, 4.0, 1000);
let y = x
    .iter()
    .map(|x| exp.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) = - \text{log}(1 - F(t))$ is ‘-pexp(t, r, lower = FALSE, log = TRUE)’.

§Source:

‘dexp’, ‘pexp’ and ‘qexp’ are all calculated from numerically stable versions of the definitions.

‘rexp’ uses

Ahrens, J. H. and Dieter, U. (1972). Computer methods for sampling from the exponential and normal distributions. Communications of the ACM, 15, 873-882.

§References:

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

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

§See Also:

‘exp’ for the exponential function.

Distributions for other standard distributions, including ‘dgamma’ for the gamma distribution and ‘dweibull’ for the Weibull distribution, both of which generalize the exponential.

§Examples:

println!("{}", (-1.0_f64).exp());
println!("{}", exp.density(1));

A fast way to generate sorted U[0,1] random numbers

let rsunif = |n| {
    let mut rng = MersenneTwister::new();
    rng.set_seed(1);
    let exp = ExponentialBuilder::new().build();
    let ce = (0..n)
        .map(|_| exp.random_sample(&mut rng).unwrap())
        .collect::<Vec<_>>()
        .cumsum();
    let ce_max = ce[n - 1];
    ce.into_iter().map(|ce| ce / ce_max).collect::<Vec<_>>()
};

let x = (0..1000).map(|x| x as f64).collect::<Vec<_>>();
let y = rsunif(1000);

let root = SVGBackend::new("rsunif.svg", (1024, 768)).into_drawing_area();
Plot::new()
    .with_options(PlotOptions {
        x_axis_label: "index".to_string(),
        y_axis_label: "rsunif".to_string(),
        ..Default::default()
    })
    .with_plottable(Line {
        x: vec![0.0, 1000.0],
        y: vec![0.0, 1.0],
        color: GREY_500,
        ..Default::default()
    })
    .with_plottable(Points {
        x,
        y,
        color: BLACK,
        ..Default::default()
    })
    .plot(&root)
    .unwrap();

Trait Implementations§

Source§

impl Distribution for Exponential

Source§

fn density<R: Into<Real64>>(&self, x: R) -> Real64

The density of the values at a given point
Source§

fn log_density<R: Into<Real64>>(&self, x: R) -> Real64

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

fn probability<R: Into<Real64>>(&self, q: R, lower_tail: bool) -> Probability64

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

fn log_probability<R: Into<Real64>>( &self, q: R, lower_tail: bool, ) -> LogProbability64

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

fn quantile<P: Into<Probability64>>(&self, p: P, lower_tail: bool) -> Real64

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

fn log_quantile<LP: Into<LogProbability64>>( &self, p: LP, lower_tail: bool, ) -> Real64

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

fn random_sample<R: RNG>(&self, rng: &mut R) -> Real64

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.