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,
impl<S> RandomVariable<S>where
S: Weighable,
Sourcepub const fn new<const M: usize>() -> RandomVariable<S, M>
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,
impl<S, const M: usize> RandomVariable<S, M>where
S: Weighable,
Sourcepub fn tabulate(&self, value: S)where
CentralMoments<M>: Univariate,
pub fn tabulate(&self, value: S)where
CentralMoments<M>: Univariate,
Adds another value to the statistical collection.
Sourcepub fn try_tabulate(&self, value: S) -> Result<(), <S as Weighable>::Error>where
CentralMoments<M>: Univariate,
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.
Sourcepub fn join(self, other: RandomVariable<S, M>) -> RandomVariable<S, M>where
CentralMoments<M>: Univariate,
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.
Sourcepub fn min(&self) -> Option<<S as Weighable>::Sample>
pub fn min(&self) -> Option<<S as Weighable>::Sample>
Returns the smallest tabulated observation or None if no observations
have occurred.
Sourcepub fn max(&self) -> Option<<S as Weighable>::Sample>
pub fn max(&self) -> Option<<S as Weighable>::Sample>
Returns the largest tabulated observation or None if no observations
have occurred.
Sourcepub fn sum(&self) -> <S as Weighable>::Samplewhere
CentralMoments<M>: Mean,
pub fn sum(&self) -> <S as Weighable>::Samplewhere
CentralMoments<M>: Mean,
Returns the weighted sum of the observations.
Sourcepub fn mean(&self) -> f64where
CentralMoments<M>: Mean,
pub fn mean(&self) -> f64where
CentralMoments<M>: Mean,
Returns the estimated arithmetic mean of the observations.
Sourcepub fn variance(&self) -> f64where
CentralMoments<M>: Variance,
pub fn variance(&self) -> f64where
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.
Sourcepub fn std_dev(&self) -> f64where
CentralMoments<M>: Variance,
Available on crate features std or libm only.
pub fn std_dev(&self) -> f64where
CentralMoments<M>: Variance,
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.
Sourcepub fn skew(&self) -> f64where
CentralMoments<M>: Skewness,
Available on crate features std or libm only.
pub fn skew(&self) -> f64where
CentralMoments<M>: Skewness,
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.
Sourcepub fn kurtosis(&self) -> f64where
CentralMoments<M>: Kurtosis,
pub fn kurtosis(&self) -> f64where
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.
Sourcepub fn excess_kurtosis(&self) -> f64where
CentralMoments<M>: Kurtosis,
pub fn excess_kurtosis(&self) -> f64where
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.
Sourcepub fn population_variance(&self) -> f64where
CentralMoments<M>: Variance,
pub fn population_variance(&self) -> f64where
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.
Sourcepub fn population_std_dev(&self) -> f64where
CentralMoments<M>: Variance,
Available on crate features std or libm only.
pub fn population_std_dev(&self) -> f64where
CentralMoments<M>: Variance,
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.
Sourcepub fn population_skew(&self) -> f64where
CentralMoments<M>: Skewness,
Available on crate features std or libm only.
pub fn population_skew(&self) -> f64where
CentralMoments<M>: Skewness,
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.
Sourcepub fn population_kurtosis(&self) -> f64where
CentralMoments<M>: Kurtosis,
pub fn population_kurtosis(&self) -> f64where
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.
Sourcepub fn population_excess_kurtosis(&self) -> f64where
CentralMoments<M>: Kurtosis,
pub fn population_excess_kurtosis(&self) -> f64where
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>
impl<T, S, const M: usize> RandomVariable<Utilized<T, S>, M>
Sourcepub fn tracked(&self) -> Option<S>
pub fn tracked(&self) -> Option<S>
Returns the currently tracked value within the most recent interval.
Sourcepub fn update(&self, time: T)where
CentralMoments<M>: Univariate,
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.
Sourcepub fn try_update(&self, time: T) -> Result<(), Causality<T>>where
CentralMoments<M>: Univariate,
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>
impl<D, U, V, const M: usize> RandomVariable<Quantity<D, U, V>, M>
Sourcepub fn display<N>(
&self,
unit: N,
) -> RandomQuantity<&RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
pub fn display<N>( &self, unit: N, ) -> RandomQuantity<&RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
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.
Sourcepub fn displayed<N>(
self,
unit: N,
) -> RandomQuantity<RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
pub fn displayed<N>( self, unit: N, ) -> RandomQuantity<RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
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.
Sourcepub fn display_full<N>(
&self,
unit: N,
) -> RandomQuantity<&RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
pub fn display_full<N>( &self, unit: N, ) -> RandomQuantity<&RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
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.
Sourcepub fn displayed_full<N>(
self,
unit: N,
) -> RandomQuantity<RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
pub fn displayed_full<N>( self, unit: N, ) -> RandomQuantity<RandomVariable<Quantity<D, U, V>, M>, D, U, V, N, M>
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,
impl<S, const M: usize> Clone for RandomVariable<S, M>where
S: Weighable,
Source§fn clone(&self) -> RandomVariable<S, M>
fn clone(&self) -> RandomVariable<S, M>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<S> Debug for RandomVariable<S>
impl<S> Debug for RandomVariable<S>
Source§impl<S> Debug for RandomVariable<S, 0>
impl<S> Debug for RandomVariable<S, 0>
Source§impl<S> Debug for RandomVariable<S, 1>
impl<S> Debug for RandomVariable<S, 1>
Source§impl<S> Debug for RandomVariable<S, 3>
impl<S> Debug for RandomVariable<S, 3>
Source§impl<S> Debug for RandomVariable<S, 4>
impl<S> Debug for RandomVariable<S, 4>
Source§impl<S, const M: usize> Default for RandomVariable<S, M>where
S: Weighable,
impl<S, const M: usize> Default for RandomVariable<S, M>where
S: Weighable,
Source§fn default() -> RandomVariable<S, M>
fn default() -> RandomVariable<S, M>
Source§impl<S1, S2> PartialEq<RandomVariable<S2>> for RandomVariable<S1>
impl<S1, S2> PartialEq<RandomVariable<S2>> for RandomVariable<S1>
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>
impl<S = f64, const M: usize = 2> !Sync for RandomVariable<S, M>
impl<S, const M: usize> Unpin for RandomVariable<S, M>
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> 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§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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