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§
Source§impl PearsonCorrelation
impl PearsonCorrelation
Sourcepub fn batch_pairs_into(&mut self, a: &[f64], b: &[f64], out: &mut [f64])
pub fn batch_pairs_into(&mut self, a: &[f64], b: &[f64], out: &mut [f64])
Exact batch over two columns: one output per pair (NaN during warmup),
bit for bit what replaying update gives, written into out.
§Panics
Panics if a, b and out differ in length.
Sourcepub fn batch_pairs_fast_into(&mut self, a: &[f64], b: &[f64], out: &mut [f64])
pub fn batch_pairs_fast_into(&mut self, a: &[f64], b: &[f64], out: &mut [f64])
Opt-in fast variant of batch_pairs_into:
the shifted sums of a, b, a², b² and a·b run as SIMD
prefix scans, re-centred every window like the exact accumulator, and
the correlation is finished lane-parallel. Every value
agrees with the exact batch to within a few units in the last place;
warmup NaNs and length are identical, and the result is the same on
every platform. Only a fresh indicator over finite values within
1e100, at least one window long, takes the kernel; anything else is
the exact batch. The correlation only remembers its last period pairs,
so afterwards the state is rebuilt exactly by replaying them.
§Panics
Panics if a, b and out differ in length.
Trait Implementations§
Source§impl Clone for PearsonCorrelation
impl Clone for PearsonCorrelation
Source§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 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 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