Skip to main content

MinMaxNormalizer

Struct MinMaxNormalizer 

Source
pub struct MinMaxNormalizer { /* private fields */ }
Expand description

Rolling min-max normalizer over a sliding window of f64 observations.

§Example

use fin_stream::norm::MinMaxNormalizer;

let mut norm = MinMaxNormalizer::new(4);
norm.update(10.0);
norm.update(20.0);
norm.update(30.0);
norm.update(40.0);

// 40.0 is the current max; 10.0 is the current min
let v = norm.normalize(40.0).unwrap();
assert!((v - 1.0).abs() < 1e-10);

Implementations§

Source§

impl MinMaxNormalizer

Source

pub fn new(window_size: usize) -> Self

Create a new normalizer with the given rolling window size.

window_size must be at least 1. Passing 0 is a programming error; this function will panic in debug mode and is flagged by the clippy::panic lint configured in Cargo.toml.

§Panics

Panics if window_size == 0.

Source

pub fn update(&mut self, value: f64)

Add a new observation to the rolling window.

If the window is full, the oldest value is evicted. After the call, the internal min/max cache is marked dirty and will be recomputed lazily on the next call to normalize or min_max.

§Complexity: O(1) amortized
Source

pub fn min_max(&mut self) -> Option<(f64, f64)>

Return the current (min, max) of the window.

Returns None if the window is empty.

§Complexity: O(1) when the cache is clean; O(W) after an eviction.
Source

pub fn normalize(&mut self, value: f64) -> Result<f64, StreamError>

Normalize value into [0.0, 1.0] using the current window.

The value is clamped so that even if value falls outside the window range the result is always in [0.0, 1.0].

§Errors

Returns NormalizationError if the window is empty (no observations have been fed yet).

§Complexity: O(1) when cache is clean; O(W) after an eviction.
Source

pub fn reset(&mut self)

Reset the normalizer, clearing all observations and the cache.

Source

pub fn len(&self) -> usize

Number of observations currently in the window.

Source

pub fn is_empty(&self) -> bool

Returns true if no observations have been added since construction or the last reset.

Source

pub fn window_size(&self) -> usize

The configured window size.

Auto Trait Implementations§

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