pub struct EwmaVolatility { /* private fields */ }Expand description
EWMA Volatility — the RiskMetrics exponentially-weighted estimate of the
volatility of log returns.
r_t = ln(price_t / price_{t−1})
σ²_t = λ · σ²_{t−1} + (1 − λ) · r²_t
EWMA = √σ²_tUnlike HistoricalVolatility — an equally
weighted, mean-centred sample standard deviation over a fixed window — the
EWMA estimator weights recent squared returns geometrically by the decay
factor λ. The most recent return carries weight 1 − λ, the one before it
λ(1 − λ), and so on, so the estimate reacts to a volatility shock
immediately and then forgets it at rate λ. This is the J.P. Morgan
RiskMetrics one-parameter model; the standard daily decay is λ = 0.94
(monthly 0.97). No mean is subtracted: squared returns are the variance
contribution, which matches the RiskMetrics assumption of a zero conditional
mean over short horizons.
The recursion is seeded with the first squared return (σ²₁ = r²₁) and emits
from the first return onward, so the very first reading is a one-observation
estimate that the decay then refines. Each update is O(1).
Non-finite and non-positive prices are ignored (the log return would be undefined): the tick is dropped, state is left untouched, and the last value is returned.
§Example
use wickra_core::{EwmaVolatility, Indicator};
let mut indicator = EwmaVolatility::new(0.94).unwrap();
let mut last = None;
for i in 0..80 {
last = indicator.update(100.0 + (f64::from(i) * 0.3).sin() * 5.0);
}
assert!(last.is_some());Implementations§
Source§impl EwmaVolatility
impl EwmaVolatility
Sourcepub fn new(lambda: f64) -> Result<Self>
pub fn new(lambda: f64) -> Result<Self>
Construct a new EWMA-volatility indicator.
lambda is the decay factor, strictly between 0 and 1 (RiskMetrics
uses 0.94 for daily data). Larger lambda means a longer memory and a
smoother estimate.
§Errors
Returns Error::InvalidParameter if lambda is not finite or not in
the open interval (0, 1).
Trait Implementations§
Source§impl Clone for EwmaVolatility
impl Clone for EwmaVolatility
Source§impl Debug for EwmaVolatility
impl Debug for EwmaVolatility
Source§impl Indicator for EwmaVolatility
impl Indicator for EwmaVolatility
Source§fn update(&mut self, input: f64) -> Option<f64>
fn update(&mut self, input: f64) -> Option<f64>
None if there is no value for this input. Read moreSource§fn reset(&mut self)
fn reset(&mut self)
Source§fn warmup_period(&self) -> usize
fn warmup_period(&self) -> usize
None output can be produced.Source§fn is_ready(&self) -> bool
fn is_ready(&self) -> bool
Source§fn name(&self) -> &'static str
fn name(&self) -> &'static str
Source§fn batch_nan_into(&mut self, inputs: &[Self::Input], out: &mut [f64])
fn batch_nan_into(&mut self, inputs: &[Self::Input], out: &mut [f64])
Source§fn batch_fast_into(&mut self, inputs: &[Self::Input], out: &mut [f64])
fn batch_fast_into(&mut self, inputs: &[Self::Input], out: &mut [f64])
batch_nan_into, but
an indicator with a vectorised kernel may reassociate its arithmetic to
run it in SIMD lanes. Each value then agrees with the exact batch to within
the tolerance the indicator documents (a few units in the last place), not
bit for bit; warmup positions, NaN placement and the output length are
identical. The kernels are deterministic: the same input produces the same
bits on every platform, with or without SIMD hardware. Read moreAuto Trait Implementations§
impl Freeze for EwmaVolatility
impl RefUnwindSafe for EwmaVolatility
impl Send for EwmaVolatility
impl Sync for EwmaVolatility
impl Unpin for EwmaVolatility
impl UnsafeUnpin for EwmaVolatility
impl UnwindSafe for EwmaVolatility
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§fn batch_nan(&mut self, inputs: &[f64]) -> Vec<f64>
fn batch_nan(&mut self, inputs: &[f64]) -> Vec<f64>
f64 per input, warmup positions filled with NaN, bit-for-bit equal
to replaying update.Source§fn batch_fast(&mut self, inputs: &[f64]) -> Vec<f64>
fn batch_fast(&mut self, inputs: &[f64]) -> Vec<f64>
Indicator::batch_fast_into) into a fresh
vector: within the indicator’s documented tolerance of
batch_nan, deterministic across platforms.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