pub struct PairwiseBeta { /* private fields */ }Expand description
Rolling Beta of asset a’s log-returns on asset b’s log-returns.
Each update receives one (a, b) pair of raw prices. Internally the
indicator differences consecutive prices into log-returns
rₜ = ln(pₜ / pₜ₋₁) and runs a rolling ordinary-least-squares regression of
a’s returns on b’s returns over the trailing window of period return
pairs:
cov_ab = (1/n) · Σ rₐ·r_b − r̄ₐ·r̄_b
var_b = (1/n) · Σ r_b² − r̄_b²
Beta = cov_ab / var_bThis is the slope of the OLS line and measures how much asset a moves, in
return space, for a unit return of asset b. A reading of 1.0 means the
two move together one-for-one; 2.0 means a typically doubles b’s
moves; negative readings signal an inverse relationship and the basis for a
hedge.
This differs from crate::Beta, which regresses the raw inputs it is
fed. PairwiseBeta always works in return space: feed it raw price levels
and it computes the returns for you, which is the conventional way to
measure cross-asset Beta (a Beta on price levels is dominated by the
shared trend and rarely what you want).
Each update is O(1): four running sums (Σrₐ, Σr_b, Σr_b²,
Σrₐ·r_b) are maintained as the window of returns slides. A flat b
window has zero return variance and Beta is undefined; the indicator
returns 0 in that case rather than producing NaN.
Prices must be strictly positive and finite for the log-return to be defined. A non-positive or non-finite price breaks the return chain: that sample is dropped and the next valid price re-seeds the previous-price reference, exactly as a real feed would resume after a bad tick.
§Example
use wickra_core::{Indicator, PairwiseBeta};
let mut indicator = PairwiseBeta::new(10).unwrap();
let mut last = None;
for i in 0..30 {
// A varying (non-constant-return) positive price path.
let b = 100.0 + 10.0 * (f64::from(i) * 0.5).sin();
// `a = b²`, so a's log-returns are exactly twice b's.
last = indicator.update((b * b, b));
}
assert!((last.unwrap() - 2.0).abs() < 1e-9);Implementations§
Source§impl PairwiseBeta
impl PairwiseBeta
Trait Implementations§
Source§impl Clone for PairwiseBeta
impl Clone for PairwiseBeta
Source§fn clone(&self) -> PairwiseBeta
fn clone(&self) -> PairwiseBeta
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 PairwiseBeta
impl Debug for PairwiseBeta
Source§impl Indicator for PairwiseBeta
impl Indicator for PairwiseBeta
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 PairwiseBeta
impl RefUnwindSafe for PairwiseBeta
impl Send for PairwiseBeta
impl Sync for PairwiseBeta
impl Unpin for PairwiseBeta
impl UnsafeUnpin for PairwiseBeta
impl UnwindSafe for PairwiseBeta
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