pub struct SpreadAr1Coefficient { /* private fields */ }Expand description
First-order autoregression coefficient ρ of the spread a − b.
Each update takes one (a, b) price pair and forms the spread
sₜ = aₜ − bₜ. Over the trailing window of period spreads the indicator
fits the discrete AR(1) model by ordinary least squares of the level on its
own lag:
sₜ = ρ · sₜ₋₁ + c + εₜ
ρ = cov(sₜ₋₁, sₜ) / var(sₜ₋₁)ρ is the direct measure of cointegration / mean-reversion strength of the
pair:
ρnear0— the spread snaps back to its mean almost instantly (very strong mean reversion).ρnear1— the spread behaves like a random walk (a unit root: no reliable reversion, the pair is not cointegrated).ρ > 1— the spread is explosive (diverging).
This is the complement of OuHalfLife: the OU half-life
is −ln(2) / ln(ρ) for 0 < ρ < 1, but ρ itself is the raw, unbounded
stationarity statistic many pairs-trading screens threshold on directly
(e.g. “trade only pairs with ρ < 0.9”). When the spread is flat over the
window (var(sₜ₋₁) = 0) the regression slope is undefined and the indicator
returns 0.
Each update is O(period): the OLS slope is recomputed from the window’s
running geometry.
§Example
use wickra_core::{Indicator, SpreadAr1Coefficient};
let mut ar1 = SpreadAr1Coefficient::new(40).unwrap();
let mut last = None;
for t in 0..120 {
let b = 100.0 + f64::from(t);
// `a` hugs `b` with a fast mean-reverting wobble ⇒ ρ well below 1.
let a = b + 2.0 * (f64::from(t) * 0.9).sin();
last = ar1.update((a, b));
}
let rho = last.unwrap();
assert!(rho > 0.0 && rho < 1.0);Implementations§
Source§impl SpreadAr1Coefficient
impl SpreadAr1Coefficient
Trait Implementations§
Source§impl Clone for SpreadAr1Coefficient
impl Clone for SpreadAr1Coefficient
Source§fn clone(&self) -> SpreadAr1Coefficient
fn clone(&self) -> SpreadAr1Coefficient
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 SpreadAr1Coefficient
impl Debug for SpreadAr1Coefficient
Source§impl Indicator for SpreadAr1Coefficient
impl Indicator for SpreadAr1Coefficient
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 SpreadAr1Coefficient
impl RefUnwindSafe for SpreadAr1Coefficient
impl Send for SpreadAr1Coefficient
impl Sync for SpreadAr1Coefficient
impl Unpin for SpreadAr1Coefficient
impl UnsafeUnpin for SpreadAr1Coefficient
impl UnwindSafe for SpreadAr1Coefficient
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