pub struct GrangerCausality { /* private fields */ }Expand description
Granger causality of b on a over a rolling window, as an F-statistic.
Each update takes one (a, b) pair. Over the trailing window of period
observations the indicator fits two autoregressions of a and compares them
with an F-test:
restricted: aₜ = c + Σ φᵢ·aₜ₋ᵢ (a's own lags only)
unrestricted: aₜ = c + Σ φᵢ·aₜ₋ᵢ + Σ ψᵢ·bₜ₋ᵢ (+ b's lags)
F = ((RSSᵣ − RSSᵤ) / lag) / (RSSᵤ / (n − 2·lag − 1))If adding b’s lags significantly reduces the residual sum of squares, b
Granger-causes a: past values of b carry information about the future
of a beyond what a’s own past holds. A larger F means stronger
predictive causality (lead–lag structure a stat-arb model can trade); a
value near 0 means b adds nothing. Note Granger causality is purely
predictive — it is not structural cause and effect.
The statistic is 0 when a regression is degenerate — a collinear or flat
window makes the normal equations singular. The output is always ≥ 0.
Each update is O(period · lag² + lag³), bounded by the fixed parameters.
§Example
use wickra_core::{GrangerCausality, Indicator};
let mut g = GrangerCausality::new(60, 1).unwrap();
let mut last = None;
for t in 0..120 {
let drive = (f64::from(t) * 0.3).sin();
// a echoes b's previous value plus noise ⇒ b Granger-causes a.
let b = drive;
let a = 0.5 * (f64::from(t.max(1) - 1) * 0.3).sin() + 0.1 * (f64::from(t) * 0.9).cos();
last = g.update((a, b));
}
assert!(last.unwrap() >= 0.0);Implementations§
Source§impl GrangerCausality
impl GrangerCausality
Sourcepub fn new(period: usize, lag: usize) -> Result<Self>
pub fn new(period: usize, lag: usize) -> Result<Self>
Construct a new Granger causality test.
period is the look-back window; lag is the autoregressive order
(number of own/cross lags in each model).
§Errors
Returns Error::InvalidPeriod if lag < 1 or if period < 3·lag + 2
(the smallest window that leaves the unrestricted regression at least one
residual degree of freedom).
Trait Implementations§
Source§impl Clone for GrangerCausality
impl Clone for GrangerCausality
Source§fn clone(&self) -> GrangerCausality
fn clone(&self) -> GrangerCausality
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 GrangerCausality
impl Debug for GrangerCausality
Source§impl Indicator for GrangerCausality
impl Indicator for GrangerCausality
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 GrangerCausality
impl RefUnwindSafe for GrangerCausality
impl Send for GrangerCausality
impl Sync for GrangerCausality
impl Unpin for GrangerCausality
impl UnsafeUnpin for GrangerCausality
impl UnwindSafe for GrangerCausality
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