pub struct Probability { /* private fields */ }Expand description
A validated probability value in the closed interval [0, 1].
Implementations§
Source§impl Probability
impl Probability
Sourcepub const fn try_new(value: f64) -> Result<Self, ProbabilityError>
pub const fn try_new(value: f64) -> Result<Self, ProbabilityError>
Creates a probability from a finite value in [0, 1].
§Errors
Returns ProbabilityError::NonFiniteProbability when value is
NaN or infinite, and ProbabilityError::ProbabilityOutOfRange when
value is outside [0, 1].
§Examples
use use_probability::{Probability, ProbabilityError};
let probability = Probability::try_new(0.25)?;
assert_eq!(probability, Probability::new(0.25));
assert!(matches!(
Probability::try_new(1.5),
Err(ProbabilityError::ProbabilityOutOfRange(1.5))
));Examples found in repository?
3fn main() -> Result<(), use_probability::ProbabilityError> {
4 let rain = Probability::from_fraction(1, 4)?;
5 let traffic = Probability::try_new(0.5)?;
6 let either_delay = independent_union(rain, traffic);
7 let commute = Bernoulli::new(rain);
8
9 assert!((either_delay.value() - 0.625).abs() < 1.0e-12);
10 assert_eq!(commute.failure_probability(), Probability::try_new(0.75)?);
11 assert!((commute.variance() - 0.1875).abs() < 1.0e-12);
12
13 Ok(())
14}Sourcepub fn from_fraction(part: u64, total: u64) -> Result<Self, ProbabilityError>
pub fn from_fraction(part: u64, total: u64) -> Result<Self, ProbabilityError>
Creates a probability from part / total.
§Errors
Returns ProbabilityError::ZeroTotal when total == 0, and
ProbabilityError::PartExceedsTotal when part > total.
§Examples
use use_probability::Probability;
let probability = Probability::from_fraction(1, 4)?;
assert!((probability.value() - 0.25).abs() < 1.0e-12);Examples found in repository?
3fn main() -> Result<(), use_probability::ProbabilityError> {
4 let rain = Probability::from_fraction(1, 4)?;
5 let traffic = Probability::try_new(0.5)?;
6 let either_delay = independent_union(rain, traffic);
7 let commute = Bernoulli::new(rain);
8
9 assert!((either_delay.value() - 0.625).abs() < 1.0e-12);
10 assert_eq!(commute.failure_probability(), Probability::try_new(0.75)?);
11 assert!((commute.variance() - 0.1875).abs() < 1.0e-12);
12
13 Ok(())
14}Sourcepub const fn validate(self) -> Result<Self, ProbabilityError>
pub const fn validate(self) -> Result<Self, ProbabilityError>
Validates that an existing probability remains normalized.
§Errors
Returns the same error variants as Self::try_new.
Sourcepub const fn value(&self) -> f64
pub const fn value(&self) -> f64
Returns the stored probability value.
Examples found in repository?
3fn main() -> Result<(), use_probability::ProbabilityError> {
4 let rain = Probability::from_fraction(1, 4)?;
5 let traffic = Probability::try_new(0.5)?;
6 let either_delay = independent_union(rain, traffic);
7 let commute = Bernoulli::new(rain);
8
9 assert!((either_delay.value() - 0.625).abs() < 1.0e-12);
10 assert_eq!(commute.failure_probability(), Probability::try_new(0.75)?);
11 assert!((commute.variance() - 0.1875).abs() < 1.0e-12);
12
13 Ok(())
14}Sourcepub const fn as_percentage(&self) -> f64
pub const fn as_percentage(&self) -> f64
Returns the stored probability as a percentage.
Sourcepub const fn complement(self) -> Self
pub const fn complement(self) -> Self
Returns the complementary probability 1 - p.
Sourcepub const fn impossible() -> Self
pub const fn impossible() -> Self
Returns the impossible event probability 0.
Sourcepub const fn intersection_independent(self, other: Self) -> Self
pub const fn intersection_independent(self, other: Self) -> Self
Returns the probability of two independent events both happening.
Sourcepub fn union_independent(self, other: Self) -> Self
pub fn union_independent(self, other: Self) -> Self
Returns the probability of at least one of two independent events happening.
Trait Implementations§
Source§impl Clone for Probability
impl Clone for Probability
Source§fn clone(&self) -> Probability
fn clone(&self) -> Probability
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Probability
impl Debug for Probability
Source§impl Default for Probability
impl Default for Probability
Source§fn default() -> Probability
fn default() -> Probability
Source§impl Display for Probability
impl Display for Probability
Source§impl From<Probability> for f64
impl From<Probability> for f64
Source§fn from(value: Probability) -> Self
fn from(value: Probability) -> Self
Source§impl PartialEq for Probability
impl PartialEq for Probability
Source§fn eq(&self, other: &Probability) -> bool
fn eq(&self, other: &Probability) -> bool
self and other values to be equal, and is used by ==.