pub struct PearsonCorrelation { /* private fields */ }Expand description
Rolling Pearson correlation between two synchronised series.
Each update receives one (x, y) pair (e.g. the latest close of the
asset and of the benchmark). Over the trailing window of period
pairs:
cov_xy = (1/n) · Σ x·y − x̄·ȳ
var_x = (1/n) · Σ x² − x̄²
var_y = (1/n) · Σ y² − ȳ²
Pearson = cov_xy / √(var_x · var_y)Output is in [−1, +1]. +1 means a perfect positive linear
relationship; −1 is a perfect inverse one; 0 means no linear
relationship. It is the same statistic SciPy / NumPy report as
pearsonr and the standardised relative of crate::Beta — Beta
scales Pearson by the ratio of standard deviations.
Each update is O(1): five running sums (Σx, Σy, Σx², Σy²,
Σxy) are maintained as the window slides. A flat series in either
channel gives an undefined ratio; the indicator returns 0 in that
case rather than producing NaN. The output is clamped to [−1, +1]
to absorb tiny floating-point overshoots near the boundaries.
§Example
use wickra_core::{Indicator, PearsonCorrelation};
let mut indicator = PearsonCorrelation::new(20).unwrap();
let mut last = None;
for i in 0..40 {
last = indicator.update((f64::from(i), 2.0 * f64::from(i) + 1.0));
}
// A perfectly linear pair → +1.
assert!((last.unwrap() - 1.0).abs() < 1e-9);Implementations§
Trait Implementations§
Source§impl Clone for PearsonCorrelation
impl Clone for PearsonCorrelation
Source§fn clone(&self) -> PearsonCorrelation
fn clone(&self) -> PearsonCorrelation
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 PearsonCorrelation
impl Debug for PearsonCorrelation
Source§impl Indicator for PearsonCorrelation
impl Indicator for PearsonCorrelation
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 PearsonCorrelation
impl RefUnwindSafe for PearsonCorrelation
impl Send for PearsonCorrelation
impl Sync for PearsonCorrelation
impl Unpin for PearsonCorrelation
impl UnsafeUnpin for PearsonCorrelation
impl UnwindSafe for PearsonCorrelation
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