Skip to main content

DecimalStats

Trait DecimalStats 

Source
pub trait DecimalStats {
    // Required methods
    fn mean(&self) -> Result<Decimal, DecimalError>;
    fn std_dev(&self) -> Result<Decimal, DecimalError>;
}
Expand description

Defines statistical operations for collections of decimal values.

This trait provides methods to calculate common statistical measures for sequences or collections of Decimal values. It allows implementing types to offer standardized statistical analysis capabilities.

§Key Features

  • Basic statistical calculations for Decimal collections
  • Consistent interface for various collection types
  • Precision-preserving operations using the Decimal type

§Available Statistics

  • mean: Calculates the arithmetic mean (average) of the values
  • std_dev: Calculates the standard deviation, measuring the dispersion from the mean

§Example

use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use optionstratlib_core::error::DecimalError;
use optionstratlib_core::model::decimal::DecimalStats;

struct DecimalSeries(Vec<Decimal>);

impl DecimalStats for DecimalSeries {
    fn mean(&self) -> Result<Decimal, DecimalError> {
        if self.0.is_empty() {
            return Ok(dec!(0));
        }
        let sum: Decimal = self.0.iter().sum();
        Ok(sum / Decimal::from(self.0.len()))
    }

    fn std_dev(&self) -> Result<Decimal, DecimalError> {
        // Implementation of standard deviation calculation
        // ...
        Ok(dec!(0)) // Placeholder return
    }
}

Required Methods§

Source

fn mean(&self) -> Result<Decimal, DecimalError>

Calculates the arithmetic mean (average) of the collection.

The mean is the sum of all values divided by the count of values. This method should handle empty collections appropriately.

§Errors

Implementations return DecimalError when the running sum or the division by the count leaves the representable Decimal range.

Source

fn std_dev(&self) -> Result<Decimal, DecimalError>

Calculates the standard deviation of the collection.

The standard deviation measures the amount of variation or dispersion from the mean. A low standard deviation indicates that values tend to be close to the mean, while a high standard deviation indicates values are spread out over a wider range.

§Errors

Implementations return DecimalError when a centred deviation, its square, the sum of squares, or the final division leaves the representable Decimal range.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl DecimalStats for Vec<Decimal>

Source§

fn mean(&self) -> Result<Decimal, DecimalError>

§Errors

Returns DecimalError::Overflow when the sum of the values leaves the representable range — vec![Decimal::MAX; 2] is enough. The previous signature had nowhere to put that, and iter().sum() aborts with Addition overflowed.

Source§

fn std_dev(&self) -> Result<Decimal, DecimalError>

§Errors

Returns DecimalError::Overflow when the mean, a centred deviation, its square or the sum of squares leaves the representable range, and DecimalError::ArithmeticError if the sample variance were negative. The squaring was .powd(Decimal::TWO), which aborts with Pow overflowed.

Implementors§