1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! Hurst exponent of the spread of two series, for pairs-trading regime detection.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::traits::Indicator;
/// Hurst exponent of the spread `a − b` over a rolling window.
///
/// Each `update` takes one `(a, b)` price pair and forms the spread
/// `sₜ = aₜ − bₜ`. Over the trailing window of `period` spreads the indicator
/// estimates the Hurst exponent `H` from how the variance of `τ`-lagged
/// differences grows with the lag `τ`:
///
/// ```text
/// V(τ) = mean_t (s_{t+τ} − s_t)² ∝ τ^(2H)
/// H = slope of log V(τ) on log τ, divided by two
/// ```
///
/// `H` classifies the spread's regime:
///
/// * `H < 0.5` — **mean-reverting** (anti-persistent): the spread snaps back,
/// the regime pairs traders want.
/// * `H ≈ 0.5` — a **random walk**: no exploitable structure.
/// * `H > 0.5` — **trending** (persistent): the spread keeps diverging.
///
/// The fit uses lags `1..=period/4` (at least two). When the spread is flat —
/// every lagged difference is zero, so the log-regression has fewer than two
/// usable points — the indicator returns the neutral `0.5`. The output is
/// clamped to `[0, 1]`.
///
/// Each `update` is `O(period · period/4)`, bounded by the fixed window.
///
/// # Example
///
/// ```
/// use wickra_core::{Indicator, SpreadHurst};
///
/// let mut h = SpreadHurst::new(60).unwrap();
/// let mut last = None;
/// for t in 0..200 {
/// let b = 100.0 + f64::from(t);
/// // A tight oscillating spread is anti-persistent ⇒ H < 0.5.
/// let a = b + 3.0 * (f64::from(t) * 0.8).sin();
/// last = h.update((a, b));
/// }
/// assert!(last.unwrap() < 0.5);
/// ```
#[derive(Debug, Clone)]
pub struct SpreadHurst {
period: usize,
max_lag: usize,
window: VecDeque<f64>,
}
impl SpreadHurst {
/// Construct a new spread Hurst estimator.
///
/// # Errors
/// Returns [`Error::InvalidPeriod`] if `period < 8` — fewer than eight
/// spreads cannot support a two-lag log–log regression.
pub fn new(period: usize) -> Result<Self> {
if period < 8 {
return Err(Error::InvalidPeriod {
message: "spread Hurst needs period >= 8",
});
}
Ok(Self {
period,
max_lag: (period / 4).max(2),
window: VecDeque::with_capacity(period),
})
}
/// Configured look-back window of spreads.
pub const fn period(&self) -> usize {
self.period
}
}
impl Indicator for SpreadHurst {
type Input = (f64, f64);
type Output = f64;
fn update(&mut self, input: (f64, f64)) -> Option<f64> {
let (a, b) = input;
if self.window.len() == self.period {
self.window.pop_front();
}
self.window.push_back(a - b);
if self.window.len() < self.period {
return None;
}
let spreads: Vec<f64> = self.window.iter().copied().collect();
// Collect (log τ, log V(τ)) for every lag whose variance is positive.
let mut log_lag = Vec::with_capacity(self.max_lag);
let mut log_var = Vec::with_capacity(self.max_lag);
for lag in 1..=self.max_lag {
let mut sum_sq = 0.0;
let mut count = 0.0;
for pair in spreads.windows(lag + 1) {
let diff = pair[lag] - pair[0];
sum_sq += diff * diff;
count += 1.0;
}
let var = sum_sq / count;
if var > 0.0 {
log_lag.push((lag as f64).ln());
log_var.push(var.ln());
}
}
if log_lag.len() < 2 {
// Degenerate (flat) spread: report the random-walk midpoint.
return Some(0.5);
}
let n = log_lag.len() as f64;
let mean_lag = log_lag.iter().sum::<f64>() / n;
let mean_var = log_var.iter().sum::<f64>() / n;
let mut cov = 0.0;
let mut var_lag = 0.0;
for (lx, lv) in log_lag.iter().zip(&log_var) {
cov += (lx - mean_lag) * (lv - mean_var);
var_lag += (lx - mean_lag) * (lx - mean_lag);
}
// `log_lag` holds at least two *distinct* lag logarithms, so the lag
// variance is strictly positive — no degenerate-slope guard is needed.
let slope = cov / var_lag;
Some((slope / 2.0).clamp(0.0, 1.0))
}
fn reset(&mut self) {
self.window.clear();
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.window.len() == self.period
}
fn name(&self) -> &'static str {
"SpreadHurst"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
#[test]
fn rejects_period_below_eight() {
assert!(SpreadHurst::new(7).is_err());
assert!(SpreadHurst::new(8).is_ok());
}
#[test]
fn accessors_and_metadata() {
let h = SpreadHurst::new(40).unwrap();
assert_eq!(h.period(), 40);
assert_eq!(h.warmup_period(), 40);
assert_eq!(h.name(), "SpreadHurst");
assert!(!h.is_ready());
}
#[test]
fn warmup_returns_none() {
let mut h = SpreadHurst::new(8).unwrap();
for t in 0..7 {
assert_eq!(h.update((f64::from(t), 0.0)), None);
}
assert!(h.update((7.0, 0.0)).is_some());
assert!(h.is_ready());
}
#[test]
fn oscillating_spread_is_anti_persistent() {
let pairs: Vec<(f64, f64)> = (0..200)
.map(|t| {
let b = 100.0 + f64::from(t);
(b + 3.0 * (f64::from(t) * 0.8).sin(), b)
})
.collect();
let last = SpreadHurst::new(60)
.unwrap()
.batch(&pairs)
.into_iter()
.flatten()
.last()
.unwrap();
assert!(last < 0.5, "H {last}");
}
#[test]
fn linear_trend_spread_is_persistent() {
// Spread = a − b = t ⇒ τ-lagged differences are all τ ⇒ V(τ) = τ² ⇒ H = 1.
let pairs: Vec<(f64, f64)> = (0..40)
.map(|t| (2.0 * f64::from(t), f64::from(t)))
.collect();
let last = SpreadHurst::new(20)
.unwrap()
.batch(&pairs)
.into_iter()
.flatten()
.last()
.unwrap();
assert_relative_eq!(last, 1.0, epsilon = 1e-9);
}
#[test]
fn flat_spread_returns_midpoint() {
// a − b is constant ⇒ all lagged differences zero ⇒ neutral 0.5.
let pairs: Vec<(f64, f64)> = (0..30)
.map(|t| (5.0 + f64::from(t), f64::from(t)))
.collect();
let last = SpreadHurst::new(16)
.unwrap()
.batch(&pairs)
.into_iter()
.flatten()
.last()
.unwrap();
assert_relative_eq!(last, 0.5, epsilon = 1e-12);
}
#[test]
fn output_in_unit_range() {
let pairs: Vec<(f64, f64)> = (0..150)
.map(|t| {
let b = 50.0 + 0.3 * f64::from(t);
(
b + (f64::from(t) * 0.5).sin() * 2.0 + (f64::from(t) * 0.13).cos(),
b,
)
})
.collect();
let mut h = SpreadHurst::new(48).unwrap();
for v in h.batch(&pairs).into_iter().flatten() {
assert!((0.0..=1.0).contains(&v));
}
}
#[test]
fn reset_clears_state() {
let mut h = SpreadHurst::new(8).unwrap();
for t in 0..12 {
h.update((f64::from(t) + (f64::from(t) * 0.7).sin(), f64::from(t)));
}
assert!(h.is_ready());
h.reset();
assert!(!h.is_ready());
assert_eq!(h.update((1.0, 0.0)), None);
}
#[test]
fn batch_equals_streaming() {
let pairs: Vec<(f64, f64)> = (0..100)
.map(|t| {
let b = 30.0 + 0.7 * f64::from(t);
(b + (f64::from(t) * 0.4).sin() * 1.5, b)
})
.collect();
let batch = SpreadHurst::new(32).unwrap().batch(&pairs);
let mut h = SpreadHurst::new(32).unwrap();
let streamed: Vec<_> = pairs.iter().map(|p| h.update(*p)).collect();
assert_eq!(batch, streamed);
}
}