pub struct SpearmanCorrelation { /* private fields */ }Expand description
Rolling Spearman rank correlation between two synchronised series.
Each update receives one (x, y) pair. Over the trailing window of
period pairs, the values in each channel are replaced by their ranks
(mid-ranks for ties), and the Pearson correlation of those ranks is
reported:
rx = rank(x_i) with mid-rank tie handling
ry = rank(y_i) with mid-rank tie handling
Spearman = Pearson( rx, ry )Spearman is the non-linear, monotone analogue of
crate::PearsonCorrelation: +1 means the two series move in the
same direction (any monotone relationship, not just linear); −1
means they move in opposite directions; 0 means no monotone
relationship. Because ranks throw away magnitude, Spearman is robust
to outliers and to non-linear (but monotone) transformations — the
canonical example is two assets that move together but with very
different volatility profiles.
Each update is O(period²) in the naïve implementation; Wickra uses
an O(period log period) sort-and-pair approach: the window is copied
into a scratch buffer, sorted twice (once per channel) to derive the
ranks, then Pearson is computed on the rank arrays via the same O(n)
rolling sums as crate::PearsonCorrelation.
A window in which one channel is constant has no rank dispersion and
the correlation is undefined; the indicator returns 0 rather than
NaN. The output is clamped to [−1, +1] to absorb tiny
floating-point overshoots.
§Example
use wickra_core::{Indicator, SpearmanCorrelation};
let mut indicator = SpearmanCorrelation::new(10).unwrap();
let mut last = None;
for i in 1..20 {
// Strictly monotone — Spearman should be +1.
last = indicator.update((f64::from(i), (f64::from(i)).powi(3)));
}
assert!((last.unwrap() - 1.0).abs() < 1e-9);Implementations§
Trait Implementations§
Source§impl Clone for SpearmanCorrelation
impl Clone for SpearmanCorrelation
Source§fn clone(&self) -> SpearmanCorrelation
fn clone(&self) -> SpearmanCorrelation
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 SpearmanCorrelation
impl Debug for SpearmanCorrelation
Source§impl Indicator for SpearmanCorrelation
impl Indicator for SpearmanCorrelation
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 SpearmanCorrelation
impl RefUnwindSafe for SpearmanCorrelation
impl Send for SpearmanCorrelation
impl Sync for SpearmanCorrelation
impl Unpin for SpearmanCorrelation
impl UnsafeUnpin for SpearmanCorrelation
impl UnwindSafe for SpearmanCorrelation
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