pub struct SineWeightedMa { /* private fields */ }Expand description
Sine-Weighted Moving Average — a windowed average whose weights follow one half-cycle of a sine wave.
Over the last period inputs the weight of the value at position
i = 0, 1, …, period − 1 (oldest to newest) is
w_i = sin(π · (i + 1) / (period + 1))
SWMA = Σ (w_i · value_i) / Σ w_iThe window is symmetric: weights rise to a peak in the middle of the window
and fall off at both ends, so the central observations dominate while the
extremes are de-emphasised. Every weight is strictly positive because the
argument (i + 1) / (period + 1) lies in the open interval (0, 1), so the
normaliser is always non-zero.
Each update is O(period): the fixed weight vector is dotted with the
trailing window, mirroring the way Alma recomputes its
Gaussian weights. period == 1 collapses to a pass-through
(w_0 = sin(π/2) = 1).
§Example
use wickra_core::{Indicator, SineWeightedMa};
let mut indicator = SineWeightedMa::new(5).unwrap();
let mut last = None;
for i in 0..80 {
last = indicator.update(100.0 + f64::from(i));
}
assert!(last.is_some());Implementations§
Source§impl SineWeightedMa
impl SineWeightedMa
Sourcepub fn new(period: usize) -> Result<SineWeightedMa, Error>
pub fn new(period: usize) -> Result<SineWeightedMa, Error>
Construct a new sine-weighted moving average over period inputs.
§Errors
Returns Error::PeriodZero if period == 0.
Trait Implementations§
Source§impl Clone for SineWeightedMa
impl Clone for SineWeightedMa
Source§fn clone(&self) -> SineWeightedMa
fn clone(&self) -> SineWeightedMa
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SineWeightedMa
impl Debug for SineWeightedMa
Source§impl Indicator for SineWeightedMa
impl Indicator for SineWeightedMa
Source§fn update(&mut self, input: f64) -> Option<f64>
fn update(&mut self, input: f64) -> Option<f64>
None if the indicator is still warming up.Source§fn reset(&mut self)
fn reset(&mut self)
Source§fn warmup_period(&self) -> usize
fn warmup_period(&self) -> usize
None output can be produced.Auto Trait Implementations§
impl Freeze for SineWeightedMa
impl RefUnwindSafe for SineWeightedMa
impl Send for SineWeightedMa
impl Sync for SineWeightedMa
impl Unpin for SineWeightedMa
impl UnsafeUnpin for SineWeightedMa
impl UnwindSafe for SineWeightedMa
Blanket Implementations§
Source§impl<T> BatchExt for Twhere
T: Indicator,
impl<T> BatchExt for Twhere
T: Indicator,
Source§fn batch(&mut self, inputs: &[Self::Input]) -> Vec<Option<Self::Output>>
fn batch(&mut self, inputs: &[Self::Input]) -> Vec<Option<Self::Output>>
None during warmup) per input.Source§impl<T> BatchNanExt for T
impl<T> BatchNanExt for T
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> 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