Struct statrs::distribution::Triangular

source ·
pub struct Triangular { /* private fields */ }
Expand description

Implements the Triangular distribution

§Examples

use statrs::distribution::{Triangular, Continuous};
use statrs::statistics::Distribution;

let n = Triangular::new(0.0, 5.0, 2.5).unwrap();
assert_eq!(n.mean().unwrap(), 7.5 / 3.0);
assert_eq!(n.pdf(2.5), 5.0 / 12.5);

Implementations§

source§

impl Triangular

source

pub fn new(min: f64, max: f64, mode: f64) -> Result<Triangular>

Constructs a new triangular distribution with a minimum of min, maximum of max, and a mode of mode.

§Errors

Returns an error if min, max, or mode are NaN or ±INF. Returns an error if max < mode, mode < min, or max == min.

§Examples
use statrs::distribution::Triangular;

let mut result = Triangular::new(0.0, 5.0, 2.5);
assert!(result.is_ok());

result = Triangular::new(2.5, 1.5, 0.0);
assert!(result.is_err());

Trait Implementations§

source§

impl Clone for Triangular

source§

fn clone(&self) -> Triangular

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Continuous<f64, f64> for Triangular

source§

fn pdf(&self, x: f64) -> f64

Calculates the probability density function for the triangular distribution at x

§Formula
if x < min {
    0
} else if min <= x <= mode {
    2 * (x - min) / ((max - min) * (mode - min))
} else if mode < x <= max {
    2 * (max - x) / ((max - min) * (max - mode))
} else {
    0
}
source§

fn ln_pdf(&self, x: f64) -> f64

Calculates the log probability density function for the triangular distribution at x

§Formula
ln( if x < min {
    0
} else if min <= x <= mode {
    2 * (x - min) / ((max - min) * (mode - min))
} else if mode < x <= max {
    2 * (max - x) / ((max - min) * (max - mode))
} else {
    0
} )
source§

impl ContinuousCDF<f64, f64> for Triangular

source§

fn cdf(&self, x: f64) -> f64

Calculates the cumulative distribution function for the triangular distribution at x

§Formula
if x == min {
    0
} if min < x <= mode {
    (x - min)^2 / ((max - min) * (mode - min))
} else if mode < x < max {
    1 - (max - min)^2 / ((max - min) * (max - mode))
} else {
    1
}
source§

fn sf(&self, x: f64) -> f64

Calculates the survival function for the triangular distribution at x

§Formula
if x == min {
    1
} if min < x <= mode {
    1 - (x - min)^2 / ((max - min) * (mode - min))
} else if mode < x < max {
    (max - min)^2 / ((max - min) * (max - mode))
} else {
    0
}
source§

fn inverse_cdf(&self, p: T) -> K

Due to issues with rounding and floating-point accuracy the default implementation may be ill-behaved. Specialized inverse cdfs should be used whenever possible. Performs a binary search on the domain of cdf to obtain an approximation of F^-1(p) := inf { x | F(x) >= p }. Needless to say, performance may may be lacking.
source§

impl Debug for Triangular

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Distribution<f64> for Triangular

source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64

Generate a random value of T, using rng as the source of randomness.
source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
source§

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more
source§

impl Distribution<f64> for Triangular

source§

fn mean(&self) -> Option<f64>

Returns the mean of the triangular distribution

§Formula
(min + max + mode) / 3
source§

fn variance(&self) -> Option<f64>

Returns the variance of the triangular distribution

§Formula
(min^2 + max^2 + mode^2 - min * max - min * mode - max * mode) / 18
source§

fn entropy(&self) -> Option<f64>

Returns the entropy of the triangular distribution

§Formula
1 / 2 + ln((max - min) / 2)
source§

fn skewness(&self) -> Option<f64>

Returns the skewness of the triangular distribution

§Formula
(sqrt(2) * (min + max - 2 * mode) * (2 * min - max - mode) * (min - 2 *
max + mode)) /
( 5 * (min^2 + max^2 + mode^2 - min * max - min * mode - max * mode)^(3
/ 2))
source§

fn std_dev(&self) -> Option<T>

Returns the standard deviation, if it exists. Read more
source§

impl Max<f64> for Triangular

source§

fn max(&self) -> f64

Returns the maximum value in the domain of the triangular distribution representable by a double precision float

§Remarks

The return value is the same max used to construct the distribution

source§

impl Median<f64> for Triangular

source§

fn median(&self) -> f64

Returns the median of the triangular distribution

§Formula
if mode >= (min + max) / 2 {
    min + sqrt((max - min) * (mode - min) / 2)
} else {
    max - sqrt((max - min) * (max - mode) / 2)
}
source§

impl Min<f64> for Triangular

source§

fn min(&self) -> f64

Returns the minimum value in the domain of the triangular distribution representable by a double precision float

§Remarks

The return value is the same min used to construct the distribution

source§

impl Mode<Option<f64>> for Triangular

source§

fn mode(&self) -> Option<f64>

Returns the mode of the triangular distribution

§Formula
mode
source§

impl PartialEq for Triangular

source§

fn eq(&self, other: &Triangular) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Triangular

source§

impl StructuralPartialEq for Triangular

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> Same for T

§

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,