pub struct Moving<T> { /* private fields */ }
Expand description
Moving<T>
provides an ergonomic way to compute the moving average, mode, and count
for a sequence of values of type T
. It supports both signed and unsigned numeric types,
and can enforce a threshold to stop accepting new values when the mean reaches or exceeds it.
Internally, it tracks:
- The number of values added (
count
) - The current mean (
mean
) - The frequency of each value for mode calculation (
mode
) - An optional threshold (
threshold
)
§Type Parameters
T
: The numeric type of the values (e.g.,usize
,i32
,f64
).
§Examples
use moving_average::Moving;
let moving = Moving::new();
moving.add(10);
moving.add(10);
moving.add(10);
moving.add(20);
assert_eq!(moving.mean(), 12.5);
assert_eq!(moving.count(), 4);
assert_eq!(moving.mode(), 10.0);
Implementations§
Source§impl<T> Moving<T>where
T: Sign + ToPrimitive,
impl<T> Moving<T>where
T: Sign + ToPrimitive,
Sourcepub fn new_with_threshold(threshold: f64) -> Self
pub fn new_with_threshold(threshold: f64) -> Self
Creates a new Moving<T>
instance with a specified threshold.
This method initializes the count
to 0, mean
to 0.0, is_error
to false
,
and threshold
to the provided value.
§Parameters
threshold
: The threshold value to be used for the new instance.
§Returns
A new instance of Moving<T>
with the specified threshold.
Values can be added to this instance to calculate the moving average.
When values are greater than or equal to the threshold, the [MovingResults::ThresholdReached
] variant is returned and no further values are added.
Sourcepub fn add_with_result(&self, value: T) -> Result<f64, MovingError>
pub fn add_with_result(&self, value: T) -> Result<f64, MovingError>
Adds a value to the current statistical collection, updating the mean accordingly.
This function converts the input value to an f64
and then updates the mean of the collection
based on the new value.
§Returns
If the mean is less than the threshold, the [MovingResults::Value
] variant is returned with the new mean.
§Panics
Panics if the type T
is unsigned and a negative value is attempted to be added. This is because
negative values are not allowed for unsigned types. If negative values are needed, it is recommended
to use signed types instead.
Sourcepub fn add(&self, value: T)
pub fn add(&self, value: T)
Adds a value to the current statistical collection, ignoring the result.
This method calls the add
method and ignores any errors that occur.
Sourcepub fn mode(&self) -> f64
pub fn mode(&self) -> f64
Returns the statistical mode of the values added so far.
The mode is the value that appears most frequently in the data set.
§Behavior
- If no values have been added, returns
0.0
. - If all values are unique (no repeats), returns the current mean.
- If one value occurs more frequently than any other, returns that value as the mode.
- If multiple values are tied for the highest frequency (i.e., a multi-modal distribution), returns the value among the tied modes that is closest to the mean.
§Examples
use moving_average::Moving;
let moving = Moving::new();
moving.add(10);
moving.add(20);
moving.add(10);
assert_eq!(moving.mode(), 10.0); // 10 appears most frequently
let moving = Moving::new();
moving.add(1);
moving.add(2);
moving.add(3);
assert_eq!(moving.mode(), moving.mean()); // all unique, returns mean
let moving = Moving::new();
moving.add(10);
moving.add(20);
moving.add(10);
moving.add(20);
// Ensure the mean is lowered
moving.add(1);
// Both 10 and 20 appear twice, closest to mean is returned
let mode = moving.mode();
assert_eq!(mode, 10.0); // 10 is closer to the mean than 20
Trait Implementations§
Source§impl AddAssign<f32> for Moving<f32>
impl AddAssign<f32> for Moving<f32>
Source§fn add_assign(&mut self, other: f32)
fn add_assign(&mut self, other: f32)
+=
operation. Read moreSource§impl AddAssign<f64> for Moving<f64>
impl AddAssign<f64> for Moving<f64>
Source§fn add_assign(&mut self, other: f64)
fn add_assign(&mut self, other: f64)
+=
operation. Read moreSource§impl AddAssign<i128> for Moving<i128>
impl AddAssign<i128> for Moving<i128>
Source§fn add_assign(&mut self, other: i128)
fn add_assign(&mut self, other: i128)
+=
operation. Read moreSource§impl AddAssign<i16> for Moving<i16>
impl AddAssign<i16> for Moving<i16>
Source§fn add_assign(&mut self, other: i16)
fn add_assign(&mut self, other: i16)
+=
operation. Read moreSource§impl AddAssign<i32> for Moving<i32>
impl AddAssign<i32> for Moving<i32>
Source§fn add_assign(&mut self, other: i32)
fn add_assign(&mut self, other: i32)
+=
operation. Read moreSource§impl AddAssign<i64> for Moving<i64>
impl AddAssign<i64> for Moving<i64>
Source§fn add_assign(&mut self, other: i64)
fn add_assign(&mut self, other: i64)
+=
operation. Read moreSource§impl AddAssign<i8> for Moving<i8>
impl AddAssign<i8> for Moving<i8>
Source§fn add_assign(&mut self, other: i8)
fn add_assign(&mut self, other: i8)
+=
operation. Read moreSource§impl AddAssign<u128> for Moving<u128>
impl AddAssign<u128> for Moving<u128>
Source§fn add_assign(&mut self, other: u128)
fn add_assign(&mut self, other: u128)
+=
operation. Read moreSource§impl AddAssign<u16> for Moving<u16>
impl AddAssign<u16> for Moving<u16>
Source§fn add_assign(&mut self, other: u16)
fn add_assign(&mut self, other: u16)
+=
operation. Read moreSource§impl AddAssign<u32> for Moving<u32>
impl AddAssign<u32> for Moving<u32>
Source§fn add_assign(&mut self, other: u32)
fn add_assign(&mut self, other: u32)
+=
operation. Read moreSource§impl AddAssign<u64> for Moving<u64>
impl AddAssign<u64> for Moving<u64>
Source§fn add_assign(&mut self, other: u64)
fn add_assign(&mut self, other: u64)
+=
operation. Read moreSource§impl AddAssign<u8> for Moving<u8>
impl AddAssign<u8> for Moving<u8>
Source§fn add_assign(&mut self, other: u8)
fn add_assign(&mut self, other: u8)
+=
operation. Read moreSource§impl AddAssign<usize> for Moving<usize>
impl AddAssign<usize> for Moving<usize>
Source§fn add_assign(&mut self, other: usize)
fn add_assign(&mut self, other: usize)
+=
operation. Read more