Skip to main content

wickra_core/indicators/
beta.rs

1//! Rolling Beta — sensitivity of an asset to a benchmark.
2
3use std::collections::VecDeque;
4
5use crate::error::{Error, Result};
6use crate::traits::Indicator;
7
8/// Rolling Beta of an `asset` series relative to a `benchmark` series.
9///
10/// Each `update` receives one `(asset, benchmark)` pair. Over the trailing
11/// window of `period` pairs:
12///
13/// ```text
14/// cov_ab = (1/n) · Σ a·b − ā·b̄
15/// var_b  = (1/n) · Σ b² − b̄²
16/// Beta   = cov_ab / var_b
17/// ```
18///
19/// Beta measures how much the asset moves for a unit move in the
20/// benchmark. A reading of `1.0` means the two move together one-for-one;
21/// `2.0` means the asset typically doubles the benchmark's moves;
22/// `0.5` means it moves only half as much; `0.0` means moves are
23/// uncorrelated; negative Betas signal a hedge. It is the slope of the
24/// OLS regression of the asset on the benchmark and the foundation of the
25/// CAPM. Unlike [`crate::PearsonCorrelation`], Beta is *not* unit-free —
26/// it carries the ratio of standard deviations.
27///
28/// Each `update` is O(1): four running sums (`Σa`, `Σb`, `Σb²`, `Σa·b`)
29/// are maintained as the window slides. A flat benchmark window has zero
30/// variance and Beta is undefined; the indicator returns `0` in that
31/// case rather than producing `NaN`.
32///
33/// Conventionally Beta is computed on **returns** (typically log-returns)
34/// rather than raw prices; feed the indicator pre-computed returns if
35/// that is your convention. The pure rolling OLS slope is the same
36/// either way.
37///
38/// # Example
39///
40/// ```
41/// use wickra_core::{Beta, Indicator};
42///
43/// let mut indicator = Beta::new(20).unwrap();
44/// let mut last = None;
45/// for i in 0..40 {
46///     // Asset doubles every benchmark move.
47///     last = indicator.update((2.0 * f64::from(i), f64::from(i)));
48/// }
49/// assert!((last.unwrap() - 2.0).abs() < 1e-9);
50/// ```
51#[derive(Debug, Clone)]
52pub struct Beta {
53    period: usize,
54    window: VecDeque<(f64, f64)>,
55    sum_a: f64,
56    sum_b: f64,
57    sum_bb: f64,
58    sum_ab: f64,
59}
60
61impl Beta {
62    /// Construct a new rolling Beta.
63    ///
64    /// # Errors
65    /// Returns [`Error::InvalidPeriod`] if `period < 2`.
66    pub fn new(period: usize) -> Result<Self> {
67        if period < 2 {
68            return Err(Error::InvalidPeriod {
69                message: "beta needs period >= 2",
70            });
71        }
72        Ok(Self {
73            period,
74            window: VecDeque::with_capacity(period),
75            sum_a: 0.0,
76            sum_b: 0.0,
77            sum_bb: 0.0,
78            sum_ab: 0.0,
79        })
80    }
81
82    /// Configured period.
83    pub const fn period(&self) -> usize {
84        self.period
85    }
86}
87
88impl Indicator for Beta {
89    /// `(asset, benchmark)` pair.
90    type Input = (f64, f64);
91    type Output = f64;
92
93    fn update(&mut self, input: (f64, f64)) -> Option<f64> {
94        let (a, b) = input;
95        if !a.is_finite() || !b.is_finite() {
96            return None;
97        }
98        if self.window.len() == self.period {
99            let (oa, ob) = self.window.pop_front().expect("non-empty");
100            self.sum_a -= oa;
101            self.sum_b -= ob;
102            self.sum_bb -= ob * ob;
103            self.sum_ab -= oa * ob;
104        }
105        self.window.push_back((a, b));
106        self.sum_a += a;
107        self.sum_b += b;
108        self.sum_bb += b * b;
109        self.sum_ab += a * b;
110        if self.window.len() < self.period {
111            return None;
112        }
113        let n = self.period as f64;
114        let mean_a = self.sum_a / n;
115        let mean_b = self.sum_b / n;
116        let var_b = (self.sum_bb / n - mean_b * mean_b).max(0.0);
117        let cov = self.sum_ab / n - mean_a * mean_b;
118        if var_b == 0.0 {
119            // A flat benchmark has no defined beta.
120            return Some(0.0);
121        }
122        Some(cov / var_b)
123    }
124
125    fn reset(&mut self) {
126        self.window.clear();
127        self.sum_a = 0.0;
128        self.sum_b = 0.0;
129        self.sum_bb = 0.0;
130        self.sum_ab = 0.0;
131    }
132
133    fn warmup_period(&self) -> usize {
134        self.period
135    }
136
137    fn is_ready(&self) -> bool {
138        self.window.len() == self.period
139    }
140
141    fn name(&self) -> &'static str {
142        "Beta"
143    }
144}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149    use crate::traits::BatchExt;
150    use approx::assert_relative_eq;
151
152    #[test]
153    fn rejects_period_below_two() {
154        assert!(Beta::new(0).is_err());
155        assert!(Beta::new(1).is_err());
156        assert!(Beta::new(2).is_ok());
157    }
158
159    #[test]
160    fn accessors_and_metadata() {
161        let b = Beta::new(14).unwrap();
162        assert_eq!(b.period(), 14);
163        assert_eq!(b.warmup_period(), 14);
164        assert_eq!(b.name(), "Beta");
165    }
166
167    #[test]
168    fn perfect_two_to_one_relationship() {
169        let pairs: Vec<(f64, f64)> = (0..10)
170            .map(|i| (2.0 * f64::from(i), f64::from(i)))
171            .collect();
172        let last = Beta::new(5)
173            .unwrap()
174            .batch(&pairs)
175            .into_iter()
176            .flatten()
177            .last()
178            .unwrap();
179        assert_relative_eq!(last, 2.0, epsilon = 1e-9);
180    }
181
182    #[test]
183    fn perfect_negative_one() {
184        let pairs: Vec<(f64, f64)> = (0..10).map(|i| (-f64::from(i), f64::from(i))).collect();
185        let last = Beta::new(5)
186            .unwrap()
187            .batch(&pairs)
188            .into_iter()
189            .flatten()
190            .last()
191            .unwrap();
192        assert_relative_eq!(last, -1.0, epsilon = 1e-9);
193    }
194
195    #[test]
196    fn constant_benchmark_yields_zero() {
197        let pairs: Vec<(f64, f64)> = (0..10).map(|i| (f64::from(i), 7.0)).collect();
198        let last = Beta::new(5)
199            .unwrap()
200            .batch(&pairs)
201            .into_iter()
202            .flatten()
203            .last()
204            .unwrap();
205        assert_relative_eq!(last, 0.0, epsilon = 1e-12);
206    }
207
208    #[test]
209    fn reset_clears_state() {
210        let mut b = Beta::new(5).unwrap();
211        b.batch(&[(1.0, 2.0), (2.0, 4.0), (3.0, 6.0), (4.0, 8.0), (5.0, 10.0)]);
212        assert!(b.is_ready());
213        b.reset();
214        assert!(!b.is_ready());
215        assert_eq!(b.update((1.0, 1.0)), None);
216    }
217
218    #[test]
219    fn batch_equals_streaming() {
220        let pairs: Vec<(f64, f64)> = (0..60)
221            .map(|i| {
222                let t = f64::from(i);
223                (t.sin() * 2.0 + 0.3 * t.cos(), t.sin())
224            })
225            .collect();
226        let batch = Beta::new(14).unwrap().batch(&pairs);
227        let mut b = Beta::new(14).unwrap();
228        let streamed: Vec<_> = pairs.iter().map(|p| b.update(*p)).collect();
229        assert_eq!(batch, streamed);
230    }
231
232    #[test]
233    fn non_finite_input_returns_none() {
234        let mut b = Beta::new(3).unwrap();
235        assert_eq!(b.update((f64::NAN, 1.0)), None);
236        assert_eq!(b.update((1.0, f64::INFINITY)), None);
237        // The rejected ticks leave no trace: a fresh window still warms up.
238        assert_eq!(b.update((1.0, 2.0)), None);
239        assert_eq!(b.update((2.0, 5.0)), None);
240        assert!(b.update((3.0, 7.0)).is_some());
241    }
242}