pub struct VarianceRatio { /* private fields */ }Expand description
Lo–MacKinlay variance ratio of the spread a − b at horizon q.
Each update takes one (a, b) price pair and forms the spread
sₜ = aₜ − bₜ. Over the trailing window of period spreads the indicator
compares the variance of q-step changes against q times the variance of
one-step changes:
rₜ = sₜ − sₜ₋₁ (one-step changes)
VR(q) = Var(Σ of q consecutive r) / (q · Var(r))Under a random walk the variance of returns grows linearly with the horizon,
so VR(q) = 1. Departures reveal autocorrelation structure:
VR(q) < 1— mean reversion (negatively autocorrelated changes): the spread’s moves partly cancel, the regime pairs traders exploit.VR(q) ≈ 1— a random walk: no exploitable structure.VR(q) > 1— momentum / trending (positively autocorrelated changes).
The estimator uses overlapping q-step windows. When the one-step changes
have zero variance (a flat spread) the ratio is undefined and the indicator
returns the null value 1. The output is always ≥ 0.
Each update is O(period), bounded by the fixed window.
§Example
use wickra_core::{Indicator, VarianceRatio};
let mut vr = VarianceRatio::new(60, 2).unwrap();
let mut last = None;
for t in 0..200 {
let b = 100.0 + f64::from(t);
// A fast, choppy spread mean-reverts (negatively autocorrelated
// changes) ⇒ VR(2) < 1.
let a = b + 2.0 * (f64::from(t) * 2.5).sin();
last = vr.update((a, b));
}
assert!(last.unwrap() < 1.0);Implementations§
Source§impl VarianceRatio
impl VarianceRatio
Sourcepub fn new(period: usize, q: usize) -> Result<Self>
pub fn new(period: usize, q: usize) -> Result<Self>
Construct a new variance-ratio test.
period is the look-back window of spreads; q is the aggregation
horizon (number of one-step changes summed per long-horizon change).
§Errors
Returns Error::InvalidPeriod if q < 2 or if period < q + 2
(which would leave fewer than two long-horizon observations).
Trait Implementations§
Source§impl Clone for VarianceRatio
impl Clone for VarianceRatio
Source§fn clone(&self) -> VarianceRatio
fn clone(&self) -> VarianceRatio
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 VarianceRatio
impl Debug for VarianceRatio
Source§impl Indicator for VarianceRatio
impl Indicator for VarianceRatio
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 VarianceRatio
impl RefUnwindSafe for VarianceRatio
impl Send for VarianceRatio
impl Sync for VarianceRatio
impl Unpin for VarianceRatio
impl UnsafeUnpin for VarianceRatio
impl UnwindSafe for VarianceRatio
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