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
impl Triangular
sourcepub fn new(min: f64, likely: f64, max: f64) -> Result<Self>
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 distributionlikely- The most likely value (mode) of the distributionmax- 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:
minis greater than or equal tomaxlikelyis less thanminor greater thanmax
§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
impl Clone for Triangular
source§fn clone(&self) -> Triangular
fn clone(&self) -> Triangular
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Continuous<f64, f64> for Triangular
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
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 rangesource§fn ln_pdf(&self, x: f64) -> f64
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 rangesource§impl ContinuousCDF<f64, f64> for Triangular
impl ContinuousCDF<f64, f64> for Triangular
Implementation of the ContinuousCDF trait for the Triangular distribution.
source§fn cdf(&self, x: f64) -> f64
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
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§impl Debug for Triangular
impl Debug for Triangular
source§impl Distribution<f64> for Triangular
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>
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>
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>
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>
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§impl Distribution<f64> for Triangular
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
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 therand::Rngtrait
§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>
fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
T, using rng as
the source of randomness. Read moresource§impl EstimationDistribution for Triangular
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 EstimationDistributionsource§fn percentile_estimate(&self, p: f64) -> Result<f64>
fn percentile_estimate(&self, p: f64) -> Result<f64>
source§fn optimistic_estimate(&self) -> f64
fn optimistic_estimate(&self) -> f64
source§fn pessimistic_estimate(&self) -> f64
fn pessimistic_estimate(&self) -> f64
source§fn most_likely_estimate(&self) -> f64
fn most_likely_estimate(&self) -> f64
source§fn expected_value(&self) -> f64
fn expected_value(&self) -> f64
source§fn uncertainty(&self) -> f64
fn uncertainty(&self) -> f64
source§fn probability_of_completion(&self, estimate: f64) -> f64
fn probability_of_completion(&self, estimate: f64) -> f64
source§fn risk_of_overrun(&self, estimate: f64) -> f64
fn risk_of_overrun(&self, estimate: f64) -> f64
source§fn confidence_interval(&self, confidence_level: f64) -> (f64, f64)
fn confidence_interval(&self, confidence_level: f64) -> (f64, f64)
source§fn evaluate_fit_quality(&self, data: &[f64]) -> Result<EstimationFitQuality>
fn evaluate_fit_quality(&self, data: &[f64]) -> Result<EstimationFitQuality>
source§impl Max<f64> for Triangular
impl Max<f64> for Triangular
Implementation of the Max trait for the Triangular distribution.
source§impl Median<f64> for Triangular
impl Median<f64> for Triangular
Implementation of the Median trait for the Triangular distribution.
source§fn median(&self) -> f64
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
impl Min<f64> for Triangular
Implementation of the Min trait for the Triangular distribution.
source§impl Mode<f64> for Triangular
impl Mode<f64> for Triangular
Implementation of the Mode trait for the Triangular distribution.
source§fn mode(&self) -> f64
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§
impl Freeze for Triangular
impl RefUnwindSafe for Triangular
impl Send for Triangular
impl Sync for Triangular
impl Unpin for Triangular
impl UnwindSafe for Triangular
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moresource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.