pub struct Beta { /* private fields */ }Expand description
Rolling Beta of an asset series relative to a benchmark series.
Each update receives one (asset, benchmark) pair. Over the trailing
window of period pairs:
cov_ab = (1/n) · Σ a·b − ā·b̄
var_b = (1/n) · Σ b² − b̄²
Beta = cov_ab / var_bBeta measures how much the asset moves for a unit move in the
benchmark. A reading of 1.0 means the two move together one-for-one;
2.0 means the asset typically doubles the benchmark’s moves;
0.5 means it moves only half as much; 0.0 means moves are
uncorrelated; negative Betas signal a hedge. It is the slope of the
OLS regression of the asset on the benchmark and the foundation of the
CAPM. Unlike crate::PearsonCorrelation, Beta is not unit-free —
it carries the ratio of standard deviations.
Each update is O(1): four running sums (Σa, Σb, Σb², Σa·b)
are maintained as the window slides. A flat benchmark window has zero
variance and Beta is undefined; the indicator returns 0 in that
case rather than producing NaN.
Conventionally Beta is computed on returns (typically log-returns) rather than raw prices; feed the indicator pre-computed returns if that is your convention. The pure rolling OLS slope is the same either way.
§Example
use wickra_core::{Beta, Indicator};
let mut indicator = Beta::new(20).unwrap();
let mut last = None;
for i in 0..40 {
// Asset doubles every benchmark move.
last = indicator.update((2.0 * f64::from(i), f64::from(i)));
}
assert!((last.unwrap() - 2.0).abs() < 1e-9);Implementations§
Trait Implementations§
Source§impl Indicator for Beta
impl Indicator for Beta
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 Beta
impl RefUnwindSafe for Beta
impl Send for Beta
impl Sync for Beta
impl Unpin for Beta
impl UnsafeUnpin for Beta
impl UnwindSafe for Beta
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