Struct distimate::Triangular

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

Represents a Triangular distribution.

The Triangular distribution is defined by its minimum, most likely (mode), and maximum values.

§Examples

use distimate::prelude::*;
use distimate::Triangular;
use approx::assert_relative_eq;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_eq!(triangular.min(), 1.0);
assert_eq!(triangular.mode(), 2.0);
assert_eq!(triangular.max(), 3.0);
assert_relative_eq!(triangular.mean().unwrap(), 2.0, epsilon = 1e-4);

Implementations§

source§

impl Triangular

source

pub fn new(min: f64, likely: f64, max: f64) -> Result<Self>

Creates a new Triangular distribution with the given minimum, likely, and maximum values.

§Arguments
  • min - The minimum value of the distribution
  • likely - The most likely value (mode) of the distribution
  • max - The maximum value of the distribution
§Returns

Returns a Result containing the new Triangular instance if the parameters are valid, or an Error if the parameters are invalid.

§Errors

Returns an error if:

  • min is greater than or equal to max
  • likely is less than min or greater than max
§Examples
use distimate::prelude::*;
use distimate::Triangular;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_eq!(triangular.min(), 1.0);
assert_eq!(triangular.mode(), 2.0);
assert_eq!(triangular.max(), 3.0);

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

Implementation of the Continuous trait for the Triangular distribution.

This implementation provides methods to calculate the probability density function (PDF) and its natural logarithm for the Triangular distribution.

source§

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

Calculates the probability density function (PDF) of the Triangular distribution at a given point.

§Arguments
  • x - The point at which to calculate the PDF
§Returns

The value of the PDF at the given point as an f64.

§Examples
use distimate::prelude::*;
use distimate::Triangular;
use approx::assert_relative_eq;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_relative_eq!(triangular.pdf(2.0), 1.0, epsilon = 1e-6);
assert_eq!(triangular.pdf(0.0), 0.0); // Outside the distribution's range
source§

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

Calculates the natural logarithm of the probability density function (PDF) of the Triangular distribution at a given point.

§Arguments
  • x - The point at which to calculate the log PDF
§Returns

The natural logarithm of the PDF at the given point as an f64.

§Examples
use distimate::prelude::*;
use distimate::Triangular;
use approx::assert_relative_eq;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_relative_eq!(triangular.ln_pdf(2.0), 0.0, epsilon = 1e-6);
assert_eq!(triangular.ln_pdf(0.0), f64::NEG_INFINITY); // Outside the distribution's range
source§

impl ContinuousCDF<f64, f64> for Triangular

Implementation of the ContinuousCDF trait for the Triangular distribution.

source§

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

Calculates the cumulative distribution function (CDF) of the Triangular distribution at a given point.

§Arguments
  • x - The point at which to calculate the CDF
§Returns

The value of the CDF at the given point as an f64, in the range [0, 1].

§Examples
use distimate::prelude::*;
use distimate::Triangular;
use approx::assert_relative_eq;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_relative_eq!(triangular.cdf(2.0), 0.5, epsilon = 1e-6);
assert_eq!(triangular.cdf(1.0), 0.0);
assert_eq!(triangular.cdf(3.0), 1.0);
source§

fn inverse_cdf(&self, p: f64) -> f64

Calculates the inverse of the cumulative distribution function (inverse CDF) of the Triangular distribution for a given probability.

§Arguments
  • p - The probability for which to calculate the inverse CDF, must be in the range [0, 1]
§Returns

The value x for which the CDF of the distribution equals the given probability.

§Examples
use distimate::prelude::*;
use distimate::Triangular;
use approx::assert_relative_eq;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_relative_eq!(triangular.inverse_cdf(0.5), 2.0, epsilon = 1e-4);
assert_relative_eq!(triangular.inverse_cdf(0.0), 1.0, epsilon = 1e-4);
assert_relative_eq!(triangular.inverse_cdf(1.0), 3.0, epsilon = 1e-4);
source§

fn sf(&self, x: K) -> T

Returns the survival function calculated at x for a given distribution. May panic depending on the implementor. Read more
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

Implementation of the Distribution trait for the Triangular distribution.

This implementation provides methods to calculate various statistical properties of the Triangular distribution.

source§

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

Calculates the mean of the Triangular distribution.

§Returns

The mean of the distribution as an Option<f64>. Always returns Some(value) as the Triangular distribution always has a defined mean.

§Examples
use distimate::prelude::*;
use distimate::Triangular;
use approx::assert_relative_eq;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_relative_eq!(triangular.mean().unwrap(), 2.0, epsilon = 1e-6);
source§

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

Calculates the variance of the Triangular distribution.

§Returns

The variance of the distribution as an Option<f64>. Always returns Some(value) as the Triangular distribution always has a defined variance.

§Examples
use distimate::prelude::*;
use distimate::Triangular;
use approx::assert_relative_eq;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_relative_eq!(triangular.variance().unwrap(), 0.1666667, epsilon = 1e-6);
source§

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

Calculates the skewness of the Triangular distribution.

§Returns

The skewness of the distribution as an Option<f64>. Always returns Some(value) as the Triangular distribution always has a defined skewness.

§Examples
use distimate::prelude::*;
use distimate::Triangular;
use approx::assert_relative_eq;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_relative_eq!(triangular.skewness().unwrap(), 0.0, epsilon = 1e-6);
source§

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

Calculates the entropy of the Triangular distribution.

The entropy is a measure of the average amount of information contained in the distribution.

§Returns

The entropy of the distribution as an Option<f64>. Always returns Some(value) as the Triangular distribution always has a defined entropy.

§Examples
use distimate::prelude::*;
use distimate::Triangular;
use approx::assert_relative_eq;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_relative_eq!(triangular.entropy().unwrap(), 0.5, epsilon = 1e-6);
source§

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

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

impl Distribution<f64> for Triangular

Implementation of the rand::distributions::Distribution trait for the Triangular distribution.

This implementation allows for random sampling from the Triangular distribution using any random number generator that implements the rand::Rng trait.

source§

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

Generates a random sample from the Triangular distribution.

This method uses the inverse transform sampling technique: it generates a uniform random number between 0 and 1, then applies the inverse CDF to this number to produce a sample from the Triangular distribution.

§Arguments
  • rng - A random number generator that implements the rand::Rng trait
§Returns

A random sample from the Triangular distribution as an f64.

§Examples
use distimate::prelude::*;
use distimate::Triangular;
use rand::prelude::*;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
let mut rng = StdRng::seed_from_u64(42);  // For reproducibility

let sample = triangular.sample(&mut rng);
assert!(sample >= 1.0 && sample <= 3.0);
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 EstimationDistribution for Triangular

Implementation of the EstimationDistribution trait for the Triangular distribution.

This trait marks the Triangular distribution as suitable for use in estimation contexts. It doesn’t add any new methods, but signals that this distribution is appropriate for modeling uncertain estimates or durations.

§Examples

use distimate::prelude::*;
use distimate::Triangular;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
// The fact that we can create a Triangular instance demonstrates
// that it implements EstimationDistribution
source§

fn percentile_estimate(&self, p: f64) -> Result<f64>

Returns the estimate at a given percentile. Read more
source§

fn optimistic_estimate(&self) -> f64

Returns the optimistic (best-case) estimate, typically the 5th percentile. Read more
source§

fn pessimistic_estimate(&self) -> f64

Returns the pessimistic (worst-case) estimate, typically the 95th percentile. Read more
source§

fn most_likely_estimate(&self) -> f64

Returns the most likely estimate (mode). Read more
source§

fn expected_value(&self) -> f64

Returns the expected value (mean) of the distribution. Read more
source§

fn uncertainty(&self) -> f64

Returns the standard deviation of the distribution. Read more
source§

fn probability_of_completion(&self, estimate: f64) -> f64

Calculates the probability of completing a task or project by a given estimate. Read more
source§

fn risk_of_overrun(&self, estimate: f64) -> f64

Calculates the risk of exceeding a given estimate. Read more
source§

fn confidence_interval(&self, confidence_level: f64) -> (f64, f64)

Returns a confidence interval for the estimate. Read more
source§

fn evaluate_fit_quality(&self, data: &[f64]) -> Result<EstimationFitQuality>

Evaluates how well the distribution fits a given dataset in the context of estimation. Read more
source§

fn calculate_within_interval( &self, data: &[f64], lower_percentile: f64, upper_percentile: f64, ) -> Result<f64>

Helper method to calculate the proportion of data within a given interval. Read more
source§

impl Max<f64> for Triangular

Implementation of the Max trait for the Triangular distribution.

source§

fn max(&self) -> f64

Returns the maximum value of the Triangular distribution.

§Returns

The maximum value of the distribution as an f64.

§Examples
use distimate::prelude::*;
use distimate::Triangular;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_eq!(triangular.max(), 3.0);
source§

impl Median<f64> for Triangular

Implementation of the Median trait for the Triangular distribution.

source§

fn median(&self) -> f64

Calculates the median of the Triangular distribution.

The median is the value separating the higher half from the lower half of the distribution.

§Returns

The median of the distribution as an f64 value.

§Examples
use distimate::prelude::*;
use distimate::Triangular;
use approx::assert_relative_eq;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_relative_eq!(triangular.median(), 2.0, epsilon = 1e-6);
source§

impl Min<f64> for Triangular

Implementation of the Min trait for the Triangular distribution.

source§

fn min(&self) -> f64

Returns the minimum value of the Triangular distribution.

§Returns

The minimum value of the distribution as an f64.

§Examples
use distimate::prelude::*;
use distimate::Triangular;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_eq!(triangular.min(), 1.0);
source§

impl Mode<f64> for Triangular

Implementation of the Mode trait for the Triangular distribution.

source§

fn mode(&self) -> f64

Returns the mode of the Triangular distribution.

The mode is the most likely value of the distribution, which is one of the defining parameters of the Triangular distribution.

§Returns

The mode of the distribution as an f64 value.

§Examples
use distimate::prelude::*;
use distimate::Triangular;

let triangular = Triangular::new(1.0, 2.0, 3.0).unwrap();
assert_eq!(triangular.mode(), 2.0);

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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