pub struct Cointegration { /* private fields */ }Expand description
Rolling cointegration test for a pair of assets (Engle–Granger two-step).
Each update receives one (a, b) pair (price levels, or log-levels if you
prefer). Over the trailing window of period pairs the indicator:
- fits the hedge ratio
β(and interceptα) by ordinary least squares ofaonb, and forms the spreadeₜ = aₜ − (α + β·bₜ); - runs an augmented Dickey–Fuller test (no constant, no trend, with
adf_lagslagged differences) on the spread series and reports itst-statistic.
A strongly negative ADF statistic means the spread reverts to its mean — the
pair is cointegrated and the spread is tradeable. A statistic near zero
means the spread wanders like a random walk (no cointegration). This is the
classic pairs-trading screen: β tells you the hedge size, the spread is
what you trade, and the ADF statistic tells you whether it is worth trading.
Each update is O(period + adf_lags³): the hedge ratio is maintained from
running sums, while the spread series and the small ADF regression are
recomputed over the window — both bounded by the fixed parameters, not the
series length.
§Example
use wickra_core::{Cointegration, Indicator};
let mut c = Cointegration::new(30, 1).unwrap();
let mut last = None;
for t in 0..60 {
let b = 100.0 + f64::from(t);
// `a` tracks 2·b with a small mean-reverting wobble ⇒ cointegrated.
let a = 2.0 * b + 5.0 + 0.5 * (f64::from(t) * 0.7).sin();
last = c.update((a, b));
}
let out = last.unwrap();
assert!((out.hedge_ratio - 2.0).abs() < 0.1);
assert!(out.adf_stat < 0.0); // mean-reverting spreadImplementations§
Source§impl Cointegration
impl Cointegration
Sourcepub fn new(period: usize, adf_lags: usize) -> Result<Self>
pub fn new(period: usize, adf_lags: usize) -> Result<Self>
Construct a new rolling cointegration test.
period is the look-back window; adf_lags is the number of lagged
differences in the augmented Dickey–Fuller regression (0 is the plain
Dickey–Fuller test).
§Errors
Returns Error::InvalidPeriod if period < 2·adf_lags + 4, which is
the smallest window that leaves the ADF regression at least one degree
of freedom.
Trait Implementations§
Source§impl Clone for Cointegration
impl Clone for Cointegration
Source§fn clone(&self) -> Cointegration
fn clone(&self) -> Cointegration
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 Cointegration
impl Debug for Cointegration
Source§impl Indicator for Cointegration
impl Indicator for Cointegration
Source§type Output = CointegrationOutput
type Output = CointegrationOutput
Source§fn update(&mut self, input: (f64, f64)) -> Option<CointegrationOutput>
fn update(&mut self, input: (f64, f64)) -> Option<CointegrationOutput>
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 Cointegration
impl RefUnwindSafe for Cointegration
impl Send for Cointegration
impl Sync for Cointegration
impl Unpin for Cointegration
impl UnsafeUnpin for Cointegration
impl UnwindSafe for Cointegration
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