Skip to main content

RandomVariable

Struct RandomVariable 

Source
pub struct RandomVariable<S = f64, const M: usize = 2>
where S: Weighable,
{ /* private fields */ }
Expand description

A simple collector for statistical data, inspired by SLX’s random_variable and generalized to numerical data points of an arbitrary type.

This implementation uses numerically stable online algorithms using sums of differences to the estimated mean described in Pébaÿ et al. rather than SLX’s naïve approach of using sums directly. It supports the sampling of weighted values and the subsequent estimation of up to four statistical moments (arithmetic mean, variance, skew, and kurtosis). The user can select the number of statistical moments they want to estimate by supplying a compile-time constant between 0 (only estimating the minimum and maximum) and 4 (additionally estimating mean, variance, skew, and kurtosis).

All four moments are useful to describe the kind of distribution:

  • mean tells us the location of the distribution by giving us its center,
  • variance measures the size of the distribution by giving us the location-independent distance from the mean,
  • skew describes the asymmetry of the distribution by giving us a location and variance-independent measure for the balance of values left and right of the arithmetic mean, and
  • kurtosis is a measure for the number and severity of outliers by weighing samples higher the further from the mean they get.

To sum it up: the former two statistical moments talk about the location and size, and the latter two statistical moments talk about the shape of the distribution. Since we usually cannot sample the whole population of values, efforts have been made to numerically correct the sampling bias inherent in sampling only a subset of a population.

Implementations§

Source§

impl<S> RandomVariable<S>
where S: Weighable,

Source

pub const fn new<const M: usize>() -> RandomVariable<S, M>

Creates a new, empty random variable.

Source§

impl<S, const M: usize> RandomVariable<S, M>
where S: Weighable,

Source

pub fn reset(&self)

Resets the stored statistical moments.

Source

pub fn tabulate(&self, value: S)
where CentralMoments<M>: Univariate,

Adds another value to the statistical collection.

Source

pub fn try_tabulate(&self, value: S) -> Result<(), <S as Weighable>::Error>
where CentralMoments<M>: Univariate,

Adds a value to the statistical collection and returns an error if it fails.

Source

pub fn join(self, other: RandomVariable<S, M>) -> RandomVariable<S, M>
where CentralMoments<M>: Univariate,

Combines another equally typed random variable with this one, consuming both and returning a new random variable representing the aggregated statistical collection.

Source

pub fn count(&self) -> usize

Returns the number of tabulated observations.

Source

pub fn min(&self) -> Option<<S as Weighable>::Sample>

Returns the smallest tabulated observation or None if no observations have occurred.

Source

pub fn max(&self) -> Option<<S as Weighable>::Sample>

Returns the largest tabulated observation or None if no observations have occurred.

Source

pub fn weight(&self) -> f64

Returns the sum of the weights of the observations.

Source

pub fn sum(&self) -> <S as Weighable>::Sample
where CentralMoments<M>: Mean,

Returns the weighted sum of the observations.

Source

pub fn mean(&self) -> f64
where CentralMoments<M>: Mean,

Returns the estimated arithmetic mean of the observations.

Source

pub fn variance(&self) -> f64
where CentralMoments<M>: Variance,

Returns the unbiased sample variance of the observations under the assumption that the samples were taken from an infinite population.

§Note

If you know that you have sampled the whole population, i.e., all possible samples, use Self::population_variance to determine the real (not estimated) variance.

Source

pub fn std_dev(&self) -> f64
where CentralMoments<M>: Variance,

Available on crate features std or libm only.

Returns the corrected sample standard deviation of the observations under the assumption that the samples were taken from an infinite population.

§Note

If you know that you have sampled the whole population, i.e., all possible samples, use Self::population_std_dev to determine the real (not estimated) standard deviation.

Source

pub fn skew(&self) -> f64
where CentralMoments<M>: Skewness,

Available on crate features std or libm only.

Returns the skewness of the observations under the assumption that the samples were taken from an infinite population.

§Note

If you know that you have sampled the whole population, i.e., all possible samples, use Self::population_skew to determine the real (not estimated) skewness.

Source

pub fn kurtosis(&self) -> f64
where CentralMoments<M>: Kurtosis,

Returns the sample kurtosis of the observations under the assumption that the samples were taken from an infinite population.

§Note

If you know that you have sampled the whole population, i.e., all possible samples, use Self::population_kurtosis to determine the real (not estimated) kurtosis.

Source

pub fn excess_kurtosis(&self) -> f64
where CentralMoments<M>: Kurtosis,

Returns the excess kurtosis of the observations under the assumption that the samples were taken from an infinite population.

§Note

If you know that you have sampled the whole population, i.e., all possible samples, use Self::population_excess_kurtosis to determine the real (not estimated) kurtosis.

Source

pub fn population_variance(&self) -> f64
where CentralMoments<M>: Variance,

Returns the population variance of the observations under the assumption that the whole population has been sampled.

§Note

This is likely not what you want since you will usually only be able to sample a limited subset of all possible observations and will therefore want to use Self::variance instead. However, both values will eventually converge to the same number.

Source

pub fn population_std_dev(&self) -> f64
where CentralMoments<M>: Variance,

Available on crate features std or libm only.

Returns the population standard deviation of the observations under the assumption that the whole population has been sampled.

§Note

This is likely not what you want since you will usually only be able to sample a limited subset of all possible observations and will therefore want to use Self::std_dev instead. However, both values will eventually converge to the same number.

Source

pub fn population_skew(&self) -> f64
where CentralMoments<M>: Skewness,

Available on crate features std or libm only.

Returns the skewness of the observations under the assumption that the whole population has been sampled.

§Note

This is likely not what you want since you will usually only be able to sample a limited subset of all possible observations and will therefore want to use Self::skew instead. However, both values will eventually converge to the same number.

Source

pub fn population_kurtosis(&self) -> f64
where CentralMoments<M>: Kurtosis,

Returns the kurtosis of the observations under the assumption that the whole population has been sampled.

§Note

This is likely not what you want since you will usually only be able to sample a limited subset of all possible observations and will therefore want to use Self::kurtosis instead. However, both values will eventually converge to the same number.

Source

pub fn population_excess_kurtosis(&self) -> f64
where CentralMoments<M>: Kurtosis,

Returns the excess kurtosis of the observations under the assumption that the whole population has been sampled.

§Note

The excess kurtosis of a population is just the population kurtosis minus 3. This is usually not what you want since you will usually only be able to sample a limited subset of all possible observations and will therefore want to use Self::excess_kurtosis instead. However, both values will eventually converge to the same number.

Source§

impl<T, S, const M: usize> RandomVariable<Utilized<T, S>, M>
where T: Sample + Add<Output = T> + Sub<Output = T>, S: Sample,

Source

pub fn tracked(&self) -> Option<S>

Returns the currently tracked value within the most recent interval.

Source

pub fn update(&self, time: T)
where CentralMoments<M>: Univariate,

Updates the temporal random variable’s state to the current time without changing the tracked value and panics if the given time is before the previous tabulation.

Source

pub fn try_update(&self, time: T) -> Result<(), Causality<T>>
where CentralMoments<M>: Univariate,

Updates the temporal random variable’s state to the current time without changing the tracked value and returns an error if the given time is before the previous tabulation.

Source§

impl<D, U, V, const M: usize> RandomVariable<Quantity<D, U, V>, M>
where Quantity<D, U, V>: Sample, D: Dimension + ?Sized, U: Units<V> + ?Sized, V: Conversion<V> + Num,

Source

pub fn display<N>( &self, unit: N, ) -> RandomQuantity<&RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
where N: Compatible<D> + Conversion<V, T = <V as Conversion<V>>::T>,

Returns a borrowed displayable object that can be used as a format argument and displays the computed statistical moments with units of measurement in abbreviated form.

Source

pub fn displayed<N>( self, unit: N, ) -> RandomQuantity<RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
where N: Compatible<D> + Conversion<V, T = <V as Conversion<V>>::T>,

Returns an owning displayable object that can be used as a format argument and displays the computed statistical moments with units of measurement in abbreviated form.

Source

pub fn display_full<N>( &self, unit: N, ) -> RandomQuantity<&RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
where N: Compatible<D> + Conversion<V, T = <V as Conversion<V>>::T>,

Returns a borrowed displayable object that can be used as a format argument and displays the computed statistical moments with units of measurement in descriptive form.

Source

pub fn displayed_full<N>( self, unit: N, ) -> RandomQuantity<RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
where N: Compatible<D> + Conversion<V, T = <V as Conversion<V>>::T>,

Returns an owned displayable object that can be used as a format argument and displays the computed statistical moments with units of measurement in descriptive form.

Trait Implementations§

Source§

impl<S, const M: usize> Clone for RandomVariable<S, M>
where S: Weighable,

Source§

fn clone(&self) -> RandomVariable<S, M>

Returns a duplicate 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<S> Debug for RandomVariable<S>
where S: Weighable, <S as Weighable>::Sample: Debug,

Source§

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

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

impl<S> Debug for RandomVariable<S, 0>
where S: Weighable, <S as Weighable>::Sample: Debug,

Source§

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

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

impl<S> Debug for RandomVariable<S, 1>
where S: Weighable, <S as Weighable>::Sample: Debug,

Source§

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

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

impl<S> Debug for RandomVariable<S, 3>
where S: Weighable, <S as Weighable>::Sample: Debug,

Source§

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

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

impl<S> Debug for RandomVariable<S, 4>
where S: Weighable, <S as Weighable>::Sample: Debug,

Source§

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

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

impl<S, const M: usize> Default for RandomVariable<S, M>
where S: Weighable,

Source§

fn default() -> RandomVariable<S, M>

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

impl<S1, S2> PartialEq<RandomVariable<S2>> for RandomVariable<S1>
where S1: Weighable, S2: Weighable<Sample = <S1 as Weighable>::Sample>,

Source§

fn eq(&self, other: &RandomVariable<S2>) -> 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.

Auto Trait Implementations§

§

impl<S = f64, const M: usize = 2> !Freeze for RandomVariable<S, M>

§

impl<S = f64, const M: usize = 2> !RefUnwindSafe for RandomVariable<S, M>

§

impl<S, const M: usize> Send for RandomVariable<S, M>
where <S as Weighable>::Sample: Send, <S as Weighable>::Weight: Send,

§

impl<S = f64, const M: usize = 2> !Sync for RandomVariable<S, M>

§

impl<S, const M: usize> Unpin for RandomVariable<S, M>
where <S as Weighable>::Sample: Unpin, <S as Weighable>::Weight: Unpin,

§

impl<S, const M: usize> UnsafeUnpin for RandomVariable<S, M>

§

impl<S, const M: usize> UnwindSafe for RandomVariable<S, M>

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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, 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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more