pub struct RollingCorrelation { /* private fields */ }Expand description
Rolling correlation of the returns of two synchronised series.
Where crate::PearsonCorrelation correlates the raw levels (x, y),
this indicator first differences each channel into a one-step return and
correlates those returns over the trailing window:
rxₜ = xₜ − xₜ₋₁ ryₜ = yₜ − yₜ₋₁
corr = cov(rx, ry) / √(var(rx) · var(ry))Return correlation is the quantity that matters for hedging and portfolio
risk: two assets can trend together (high level correlation) while their
day-to-day moves are nearly independent (low return correlation). The output
is in [−1, +1]; a flat return channel makes the ratio undefined and the
indicator reports 0 rather than NaN. The value is clamped to [−1, +1]
to absorb tiny floating-point overshoots near the boundaries.
Each update is O(1): the five running sums (Σrx, Σry, Σrx², Σry²,
Σrxry) are maintained as the window of returns slides. The first level in
each channel produces no return, so a period-pair correlation needs
period + 1 updates of warmup.
§Example
use wickra_core::{Indicator, RollingCorrelation};
let mut rc = RollingCorrelation::new(10).unwrap();
let mut last = None;
for i in 0..40 {
// A varying path where y always moves with x ⇒ return correlation +1.
let x = (f64::from(i) * 0.5).sin() * 10.0;
last = rc.update((x, 2.0 * x));
}
assert!((last.unwrap() - 1.0).abs() < 1e-9);Implementations§
Trait Implementations§
Source§impl Clone for RollingCorrelation
impl Clone for RollingCorrelation
Source§fn clone(&self) -> RollingCorrelation
fn clone(&self) -> RollingCorrelation
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 RollingCorrelation
impl Debug for RollingCorrelation
Source§impl Indicator for RollingCorrelation
impl Indicator for RollingCorrelation
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 RollingCorrelation
impl RefUnwindSafe for RollingCorrelation
impl Send for RollingCorrelation
impl Sync for RollingCorrelation
impl Unpin for RollingCorrelation
impl UnsafeUnpin for RollingCorrelation
impl UnwindSafe for RollingCorrelation
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