pub struct RollingCovariance { /* private fields */ }Expand description
Rolling covariance of the returns of two synchronised series.
Each update takes one (x, y) level pair, differences each channel into a
one-step return, and reports the population covariance of those returns over
the trailing window of period return pairs:
rxₜ = xₜ − xₜ₋₁ ryₜ = yₜ − yₜ₋₁
cov = (1/n) · Σ rx·ry − r̄x · r̄yUnlike crate::RollingCorrelation the result is not normalised to
[−1, 1]: it carries the units of the two return streams multiplied
together, so it scales with volatility. It is the raw building block behind
correlation, beta and portfolio variance — positive when the two return
streams tend to move the same way, negative when they offset.
Each update is O(1): three running sums (Σrx, Σry, Σrxry) are
maintained as the window slides. The first level in each channel produces no
return, so a period-pair covariance needs period + 1 updates of warmup.
§Example
use wickra_core::{Indicator, RollingCovariance};
let mut rc = RollingCovariance::new(5).unwrap();
let mut last = None;
for i in 0..20 {
let x = f64::from(i);
last = rc.update((x, 3.0 * x)); // y's return is 3× x's return
}
// cov(rx, ry) = cov(1, 3) over constant unit returns = 3 · var(rx) = 0
// for a constant return; use a varying path in practice. Here returns are
// constant (1 and 3) ⇒ covariance 0.
assert!(last.unwrap().abs() < 1e-9);Implementations§
Trait Implementations§
Source§impl Clone for RollingCovariance
impl Clone for RollingCovariance
Source§fn clone(&self) -> RollingCovariance
fn clone(&self) -> RollingCovariance
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 RollingCovariance
impl Debug for RollingCovariance
Source§impl Indicator for RollingCovariance
impl Indicator for RollingCovariance
Source§type Input = (f64, f64)
type Input = (f64, f64)
f64 for a price, or Candle / Tick).Source§fn update(&mut self, input: (f64, f64)) -> Option<f64>
fn update(&mut self, input: (f64, 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 RollingCovariance
impl RefUnwindSafe for RollingCovariance
impl Send for RollingCovariance
impl Sync for RollingCovariance
impl Unpin for RollingCovariance
impl UnsafeUnpin for RollingCovariance
impl UnwindSafe for RollingCovariance
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> 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