Struct Probability

Source
pub struct Probability(/* private fields */);
Expand description

Type representing the probability an event.

This struct is just a wrapper around f64 that strictly enforces that the probability lies in \([0, 1]\).

Implementations§

Source§

impl Probability

Source

pub fn new(p: f64) -> Result<Self, InvalidProbabilityError>

Construct a probability, checking that the argument lies in \([0, 1]\).

§Examples
use rstat::Probability;

// Invalid probabilities:
assert!(Probability::new(1.5).is_err());
assert!(Probability::new(-1.0).is_err());

// Valid probabilities:
assert!(Probability::new(0.0).is_ok());
assert!(Probability::new(0.5).is_ok());
assert!(Probability::new(1.0).is_ok());
Source

pub fn new_unchecked(p: f64) -> Probability

Construct a probability without checking for validity.

Source

pub fn zero() -> Probability

Returns a new Probability with value 0.

Source

pub fn half() -> Probability

Returns a new Probability with value 0.5.

Source

pub fn one() -> Probability

Returns a new Probability with value 1.

Source

pub fn unwrap(self) -> f64

Unwrap the probability and return the internal f64.

§Examples
use rstat::Probability;

assert_eq!(Probability::half().unwrap(), 0.5);
Source

pub fn is_valid(&self) -> bool

Returns true if the probability lies in \([0, 1]\).

§Examples
use rstat::Probability;

assert!(Probability::new_unchecked(0.5).is_valid());
assert!(!Probability::new_unchecked(10.0).is_valid());
Source

pub fn ln(self) -> f64

Return the natural logarithm of the probability: \(\ln{p}\).

§Examples
use rstat::Probability;

assert_eq!(Probability::one().ln(), 0.0);
Source

pub fn log2(self) -> f64

Return the base-2 logarithm of the probability: \(\log_2{p}\).

§Examples
use rstat::Probability;

assert_eq!(Probability::half().log2(), -1.0);
Source

pub fn powf(self, e: f64) -> f64

Return the probability raised to the power e: \(p^e\).

§Examples
use rstat::Probability;

assert_eq!(Probability::half().powf(2.5), 0.1767766952966369);
Source

pub fn powi(self, e: i32) -> f64

Return the probability raised to the power e: \(p^e\).

§Examples
use rstat::Probability;

assert_eq!(Probability::half().powi(2), 0.25);

Trait Implementations§

Source§

impl<'a, 'b> Add<&'a Probability> for &'b Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the + operator.
Source§

fn add( self, other: &'a Probability, ) -> Result<Probability, InvalidProbabilityError>

Performs the + operation. Read more
Source§

impl<'a, 'b> Add<&'a Probability> for &'b f64

Source§

type Output = f64

The resulting type after applying the + operator.
Source§

fn add(self, other: &'a Probability) -> f64

Performs the + operation. Read more
Source§

impl<'a> Add<&'a Probability> for Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the + operator.
Source§

fn add( self, other: &'a Probability, ) -> Result<Probability, InvalidProbabilityError>

Performs the + operation. Read more
Source§

impl<'a> Add<&'a Probability> for f64

Source§

type Output = f64

The resulting type after applying the + operator.
Source§

fn add(self, other: &'a Probability) -> f64

Performs the + operation. Read more
Source§

impl<'a, 'b> Add<&'a f64> for &'b Probability

Source§

type Output = f64

The resulting type after applying the + operator.
Source§

fn add(self, other: &'a f64) -> f64

Performs the + operation. Read more
Source§

impl<'a> Add<&'a f64> for Probability

Source§

type Output = f64

The resulting type after applying the + operator.
Source§

fn add(self, other: &'a f64) -> f64

Performs the + operation. Read more
Source§

impl<'a> Add<Probability> for &'a Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the + operator.
Source§

fn add(self, other: Probability) -> Result<Probability, InvalidProbabilityError>

Performs the + operation. Read more
Source§

impl<'a> Add<Probability> for &'a f64

Source§

type Output = f64

The resulting type after applying the + operator.
Source§

fn add(self, other: Probability) -> f64

Performs the + operation. Read more
Source§

impl Add<Probability> for f64

Source§

type Output = f64

The resulting type after applying the + operator.
Source§

fn add(self, other: Probability) -> f64

Performs the + operation. Read more
Source§

impl<'a> Add<f64> for &'a Probability

Source§

type Output = f64

The resulting type after applying the + operator.
Source§

fn add(self, other: f64) -> f64

Performs the + operation. Read more
Source§

impl Add<f64> for Probability

Source§

type Output = f64

The resulting type after applying the + operator.
Source§

fn add(self, other: f64) -> f64

Performs the + operation. Read more
Source§

impl Add for Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the + operator.
Source§

fn add(self, other: Probability) -> Result<Probability, InvalidProbabilityError>

Performs the + operation. Read more
Source§

impl Clone for Probability

Source§

fn clone(&self) -> Probability

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 Debug for Probability

Source§

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

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

impl Default for Probability

Source§

fn default() -> Probability

Returns the “default value” for a type. Read more
Source§

impl Display for Probability

Source§

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

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

impl<'a, 'b> Div<&'a Probability> for &'b Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the / operator.
Source§

fn div( self, other: &'a Probability, ) -> Result<Probability, InvalidProbabilityError>

Performs the / operation. Read more
Source§

impl<'a, 'b> Div<&'a Probability> for &'b f64

Source§

type Output = f64

The resulting type after applying the / operator.
Source§

fn div(self, other: &'a Probability) -> f64

Performs the / operation. Read more
Source§

impl<'a> Div<&'a Probability> for Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the / operator.
Source§

fn div( self, other: &'a Probability, ) -> Result<Probability, InvalidProbabilityError>

Performs the / operation. Read more
Source§

impl<'a> Div<&'a Probability> for f64

Source§

type Output = f64

The resulting type after applying the / operator.
Source§

fn div(self, other: &'a Probability) -> f64

Performs the / operation. Read more
Source§

impl<'a, 'b> Div<&'a f64> for &'b Probability

Source§

type Output = f64

The resulting type after applying the / operator.
Source§

fn div(self, other: &'a f64) -> f64

Performs the / operation. Read more
Source§

impl<'a> Div<&'a f64> for Probability

Source§

type Output = f64

The resulting type after applying the / operator.
Source§

fn div(self, other: &'a f64) -> f64

Performs the / operation. Read more
Source§

impl<'a> Div<Probability> for &'a Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the / operator.
Source§

fn div(self, other: Probability) -> Result<Probability, InvalidProbabilityError>

Performs the / operation. Read more
Source§

impl<'a> Div<Probability> for &'a f64

Source§

type Output = f64

The resulting type after applying the / operator.
Source§

fn div(self, other: Probability) -> f64

Performs the / operation. Read more
Source§

impl Div<Probability> for f64

Source§

type Output = f64

The resulting type after applying the / operator.
Source§

fn div(self, other: Probability) -> f64

Performs the / operation. Read more
Source§

impl<'a> Div<f64> for &'a Probability

Source§

type Output = f64

The resulting type after applying the / operator.
Source§

fn div(self, other: f64) -> f64

Performs the / operation. Read more
Source§

impl Div<f64> for Probability

Source§

type Output = f64

The resulting type after applying the / operator.
Source§

fn div(self, other: f64) -> f64

Performs the / operation. Read more
Source§

impl Div for Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the / operator.
Source§

fn div(self, other: Probability) -> Result<Probability, InvalidProbabilityError>

Performs the / operation. Read more
Source§

impl From<Probability> for f64

Source§

fn from(p: Probability) -> f64

Converts to this type from the input type.
Source§

impl<'a, 'b> Mul<&'a Probability> for &'b Probability

Source§

type Output = Probability

The resulting type after applying the * operator.
Source§

fn mul(self, other: &'a Probability) -> Probability

Performs the * operation. Read more
Source§

impl<'a, 'b> Mul<&'a Probability> for &'b f64

Source§

type Output = f64

The resulting type after applying the * operator.
Source§

fn mul(self, other: &'a Probability) -> f64

Performs the * operation. Read more
Source§

impl<'a> Mul<&'a Probability> for Probability

Source§

type Output = Probability

The resulting type after applying the * operator.
Source§

fn mul(self, other: &'a Probability) -> Probability

Performs the * operation. Read more
Source§

impl<'a> Mul<&'a Probability> for f64

Source§

type Output = f64

The resulting type after applying the * operator.
Source§

fn mul(self, other: &'a Probability) -> f64

Performs the * operation. Read more
Source§

impl<'a, 'b> Mul<&'a f64> for &'b Probability

Source§

type Output = f64

The resulting type after applying the * operator.
Source§

fn mul(self, other: &'a f64) -> f64

Performs the * operation. Read more
Source§

impl<'a> Mul<&'a f64> for Probability

Source§

type Output = f64

The resulting type after applying the * operator.
Source§

fn mul(self, other: &'a f64) -> f64

Performs the * operation. Read more
Source§

impl<'a> Mul<Probability> for &'a Probability

Source§

type Output = Probability

The resulting type after applying the * operator.
Source§

fn mul(self, other: Probability) -> Probability

Performs the * operation. Read more
Source§

impl<'a> Mul<Probability> for &'a f64

Source§

type Output = f64

The resulting type after applying the * operator.
Source§

fn mul(self, other: Probability) -> f64

Performs the * operation. Read more
Source§

impl Mul<Probability> for f64

Source§

type Output = f64

The resulting type after applying the * operator.
Source§

fn mul(self, other: Probability) -> f64

Performs the * operation. Read more
Source§

impl<'a> Mul<f64> for &'a Probability

Source§

type Output = f64

The resulting type after applying the * operator.
Source§

fn mul(self, other: f64) -> f64

Performs the * operation. Read more
Source§

impl Mul<f64> for Probability

Source§

type Output = f64

The resulting type after applying the * operator.
Source§

fn mul(self, other: f64) -> f64

Performs the * operation. Read more
Source§

impl Mul for Probability

Source§

type Output = Probability

The resulting type after applying the * operator.
Source§

fn mul(self, other: Probability) -> Probability

Performs the * operation. Read more
Source§

impl Not for &Probability

Source§

type Output = Probability

The resulting type after applying the ! operator.
Source§

fn not(self) -> Probability

Performs the unary ! operation. Read more
Source§

impl Not for Probability

Source§

type Output = Probability

The resulting type after applying the ! operator.
Source§

fn not(self) -> Probability

Performs the unary ! operation. Read more
Source§

impl Param for Probability

Source§

type Value = f64

Source§

fn value(&self) -> &f64

Returns a reference to the parameter’s value. Read more
Source§

fn into_value(self) -> f64

Converts the parameter into it’s value.
Source§

fn constraints() -> Constraints<Self::Value>

Returns the constraints associated with this parameter type.
Source§

impl PartialEq<f64> for Probability

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for Probability

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Probability

Source§

fn partial_cmp(&self, other: &Probability) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, 'b> Rem<&'a Probability> for &'b Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the % operator.
Source§

fn rem( self, other: &'a Probability, ) -> Result<Probability, InvalidProbabilityError>

Performs the % operation. Read more
Source§

impl<'a, 'b> Rem<&'a Probability> for &'b f64

Source§

type Output = f64

The resulting type after applying the % operator.
Source§

fn rem(self, other: &'a Probability) -> f64

Performs the % operation. Read more
Source§

impl<'a> Rem<&'a Probability> for Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the % operator.
Source§

fn rem( self, other: &'a Probability, ) -> Result<Probability, InvalidProbabilityError>

Performs the % operation. Read more
Source§

impl<'a> Rem<&'a Probability> for f64

Source§

type Output = f64

The resulting type after applying the % operator.
Source§

fn rem(self, other: &'a Probability) -> f64

Performs the % operation. Read more
Source§

impl<'a, 'b> Rem<&'a f64> for &'b Probability

Source§

type Output = f64

The resulting type after applying the % operator.
Source§

fn rem(self, other: &'a f64) -> f64

Performs the % operation. Read more
Source§

impl<'a> Rem<&'a f64> for Probability

Source§

type Output = f64

The resulting type after applying the % operator.
Source§

fn rem(self, other: &'a f64) -> f64

Performs the % operation. Read more
Source§

impl<'a> Rem<Probability> for &'a Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the % operator.
Source§

fn rem(self, other: Probability) -> Result<Probability, InvalidProbabilityError>

Performs the % operation. Read more
Source§

impl<'a> Rem<Probability> for &'a f64

Source§

type Output = f64

The resulting type after applying the % operator.
Source§

fn rem(self, other: Probability) -> f64

Performs the % operation. Read more
Source§

impl Rem<Probability> for f64

Source§

type Output = f64

The resulting type after applying the % operator.
Source§

fn rem(self, other: Probability) -> f64

Performs the % operation. Read more
Source§

impl<'a> Rem<f64> for &'a Probability

Source§

type Output = f64

The resulting type after applying the % operator.
Source§

fn rem(self, other: f64) -> f64

Performs the % operation. Read more
Source§

impl Rem<f64> for Probability

Source§

type Output = f64

The resulting type after applying the % operator.
Source§

fn rem(self, other: f64) -> f64

Performs the % operation. Read more
Source§

impl Rem for Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the % operator.
Source§

fn rem(self, other: Probability) -> Result<Probability, InvalidProbabilityError>

Performs the % operation. Read more
Source§

impl<'a, 'b> Sub<&'a Probability> for &'b Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the - operator.
Source§

fn sub( self, other: &'a Probability, ) -> Result<Probability, InvalidProbabilityError>

Performs the - operation. Read more
Source§

impl<'a, 'b> Sub<&'a Probability> for &'b f64

Source§

type Output = f64

The resulting type after applying the - operator.
Source§

fn sub(self, other: &'a Probability) -> f64

Performs the - operation. Read more
Source§

impl<'a> Sub<&'a Probability> for Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the - operator.
Source§

fn sub( self, other: &'a Probability, ) -> Result<Probability, InvalidProbabilityError>

Performs the - operation. Read more
Source§

impl<'a> Sub<&'a Probability> for f64

Source§

type Output = f64

The resulting type after applying the - operator.
Source§

fn sub(self, other: &'a Probability) -> f64

Performs the - operation. Read more
Source§

impl<'a, 'b> Sub<&'a f64> for &'b Probability

Source§

type Output = f64

The resulting type after applying the - operator.
Source§

fn sub(self, other: &'a f64) -> f64

Performs the - operation. Read more
Source§

impl<'a> Sub<&'a f64> for Probability

Source§

type Output = f64

The resulting type after applying the - operator.
Source§

fn sub(self, other: &'a f64) -> f64

Performs the - operation. Read more
Source§

impl<'a> Sub<Probability> for &'a Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Probability) -> Result<Probability, InvalidProbabilityError>

Performs the - operation. Read more
Source§

impl<'a> Sub<Probability> for &'a f64

Source§

type Output = f64

The resulting type after applying the - operator.
Source§

fn sub(self, other: Probability) -> f64

Performs the - operation. Read more
Source§

impl Sub<Probability> for f64

Source§

type Output = f64

The resulting type after applying the - operator.
Source§

fn sub(self, other: Probability) -> f64

Performs the - operation. Read more
Source§

impl<'a> Sub<f64> for &'a Probability

Source§

type Output = f64

The resulting type after applying the - operator.
Source§

fn sub(self, other: f64) -> f64

Performs the - operation. Read more
Source§

impl Sub<f64> for Probability

Source§

type Output = f64

The resulting type after applying the - operator.
Source§

fn sub(self, other: f64) -> f64

Performs the - operation. Read more
Source§

impl Sub for Probability

Source§

type Output = Result<Probability, InvalidProbabilityError>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Probability) -> Result<Probability, InvalidProbabilityError>

Performs the - operation. Read more
Source§

impl<'a> Sum<&'a Probability> for f64

Source§

fn sum<I: Iterator<Item = &'a Probability>>(iter: I) -> f64

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl Sum<Probability> for f64

Source§

fn sum<I: Iterator<Item = Probability>>(iter: I) -> f64

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl TryFrom<f64> for Probability

Source§

type Error = InvalidProbabilityError

The type returned in the event of a conversion error.
Source§

fn try_from(p: f64) -> Result<Self, InvalidProbabilityError>

Performs the conversion.
Source§

impl Copy for Probability

Source§

impl StructuralPartialEq for Probability

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,

Source§

impl<T, Base> RefNum<Base> for T
where T: NumOps<Base, Base> + for<'r> NumOps<&'r Base, Base>,