Struct RollingMoments

Source
pub struct RollingMoments<T> { /* private fields */ }
Expand description

This module provides functionality for calculating rolling statistical moments over a time series.

Rolling moments are essential statistics that help analyze the characteristics of data over a moving window. These include measures like mean, variance, skewness, and kurtosis, which provide insights into the distribution and behavior of financial time series data.

The implementation uses Kahan-Babuska-Neumaier summation algorithm for numerical stability when computing these statistics over potentially large datasets with floating-point values.

Implementations§

Source§

impl<T: Float + Default> RollingMoments<T>

Source

pub fn new(period: usize) -> Self

Creates a new RollingMoments instance with the specified period.

§Arguments
  • period - The period of the statistics
§Returns
  • Self - The statistics object
Source

pub const fn ddof(&self) -> bool

Returns the Delta Degrees of Freedom

§Returns
  • bool - The Delta Degrees of Freedom
Source

pub const fn set_ddof(&mut self, ddof: bool) -> &mut Self

Sets the Delta Degrees of Freedom

§Arguments
  • ddof - The Delta Degrees of Freedom
§Returns
  • &mut Self - The statistics object
Source

pub fn reset(&mut self) -> &mut Self

Resets the rolling moments

§Returns
  • &mut Self - The rolling moments object
Source

pub fn next(&mut self, value: T) -> &mut Self

Updates the value to the rolling moments

§Arguments
  • value - The value to update the rolling moments with
§Returns
  • &mut Self - The rolling moments object
Source

pub fn recompute(&mut self)

Recomputes the rolling statistics, could be called to avoid prolonged compounding of floating rounding errors

§Returns
  • &mut Self - The rolling moments object
Source

pub const fn popped(&self) -> Option<T>

Returns the value that was removed from the window

§Returns
  • Option<T> - The value that was removed from the window
Source

pub const fn value(&self) -> Option<T>

Returns the value that was added to the window

§Returns
  • Option<T> - The value that was added to the window
Source

pub fn max(&self) -> Option<T>

Returns the maximum value in the ring buffer

Source

pub fn min(&self) -> Option<T>

Returns the minimum value in the ring buffer

Source

pub const fn period(&self) -> usize

Returns the window period

§Returns
  • usize - The window period
Source

pub const fn is_ready(&self) -> bool

Returns true of the calculation was ready

§Returns
  • bool - True if the calculation was ready
Source

pub const fn count(&self) -> usize

Returns the number of elements in the buffer

§Returns
  • usize - The number of elements in the buffer
Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Returns an iterator over the elements in the ring buffer

Source

pub fn as_slice(&self) -> &[T]

Returns a slice of the elements in the buffer

Source

pub fn sum(&self) -> Option<T>

Returns the sum of all values in the rolling window

§Returns
  • Option<T> - The sum of all values if the window is ready, None otherwise
Source

pub fn sum_sq(&self) -> Option<T>

Returns the sum of squares of all values in the rolling window

§Returns
  • Option<T> - The sum of squares of all values if the window is ready, None otherwise
Source

pub fn mean(&self) -> Option<T>

Returns the mean of all values in the rolling window

§Returns
  • Option<T> - The mean of all values if the window is ready, None otherwise
Source

pub fn mean_sq(&self) -> Option<T>

Returns the mean of squared values in the rolling window

§Returns
  • Option<T> - The mean of squared values if the window is ready, None otherwise
Source

pub fn variance(&self) -> Option<T>

Returns the variance of values in the rolling window

Variance quantifies the dispersion of data points around the mean, providing essential volatility measurement for financial time series:

  • Serves as the foundation for volatility-based position sizing
  • Enables normalization of returns for cross-asset comparison
  • Provides critical input for statistical arbitrage models
  • Forms the basis for numerous technical indicators like Bollinger Bands
§Returns
  • Option<T> - The variance, or None if the window is not full
Source

pub fn stddev(&self) -> Option<T>

Returns the standard deviation of values in the rolling window

Standard deviation quantifies the dispersion of data points around the mean, providing essential volatility measurement for financial time series:

  • Serves as the foundation for volatility-based position sizing
  • Enables normalization of returns for cross-asset comparison
  • Provides critical input for statistical arbitrage models
  • Forms the basis for numerous technical indicators like Bollinger Bands
§Returns
  • Option<T> - The standard deviation, or None if the window is not full
Source

pub fn zscore(&self) -> Option<T>

Returns the Zscore of the most recent value

Zscore measures the number of standard deviations a value is from the mean.

§Returns
  • Option<T> - The Zscore if the window is ready and standard deviation is positive, None otherwise
Source

pub fn skew(&self) -> Option<T>

Returns the skewness of values in the rolling window

Skewness measures the asymmetry of the distribution of values.

  • Positive values indicate a right-skewed distribution (long tail on the right)
  • Negative values indicate a left-skewed distribution (long tail on the left)
  • A normal distribution has a skewness of 0

When ddof is true, applies a bias correction for sample skewness.

§Returns
  • Option<T> - The skewness if the window is ready and variance is positive, None otherwise
Source

pub fn kurt(&self) -> Option<T>

Returns the excess kurtosis of values in the rolling window

Excess kurtosis measures the “tailedness” of a distribution compared to a normal distribution.

  • Positive values indicate a distribution with heavier tails (more outliers)
  • Negative values indicate a distribution with lighter tails (fewer outliers)
  • A normal distribution has an excess kurtosis of 0

When ddof is true, applies a bias correction for sample kurtosis.

§Returns
  • Option<T> - The excess kurtosis if the window is ready and variance is positive, None otherwise

Trait Implementations§

Source§

impl<T: Clone> Clone for RollingMoments<T>

Source§

fn clone(&self) -> RollingMoments<T>

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<T: Debug> Debug for RollingMoments<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RollingMoments<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for RollingMoments<T>
where T: RefUnwindSafe,

§

impl<T> Send for RollingMoments<T>
where T: Send,

§

impl<T> Sync for RollingMoments<T>
where T: Sync,

§

impl<T> Unpin for RollingMoments<T>
where T: Unpin,

§

impl<T> UnwindSafe for RollingMoments<T>
where T: UnwindSafe,

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> 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.