Skip to main content

quantwave_core/regimes/
tar.rs

1//! Threshold Autoregressive (TAR / SETAR) Models
2//! 
3//! Source: Tong (1983) "Non-Linear Time Series: A Dynamical System Approach"
4//! 
5//! TAR models allow regime shifts to be triggered by an observable signal 
6//! (e.g., volatility or price relative to a threshold).
7
8use crate::traits::Next;
9use crate::regimes::MarketRegime;
10use serde::{Deserialize, Serialize};
11
12/// A Threshold Autoregressive model with multiple thresholds.
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct TAR {
15    /// Sorted thresholds [t1, t2, ..., tn]
16    pub thresholds: Vec<f64>,
17}
18
19impl TAR {
20    /// Creates a new TAR model with a single threshold.
21    pub fn new(threshold: f64) -> Self {
22        Self { thresholds: vec![threshold] }
23    }
24
25    /// Creates a multi-threshold TAR model (SETAR).
26    pub fn multi(thresholds: Vec<f64>) -> Self {
27        let mut t = thresholds;
28        t.sort_by(|a, b| a.partial_cmp(b).unwrap());
29        Self { thresholds: t }
30    }
31}
32
33impl Next<f64> for TAR {
34    type Output = MarketRegime;
35
36    fn next(&mut self, signal: f64) -> Self::Output {
37        // Find which threshold bin the signal falls into
38        match self.thresholds.binary_search_by(|t| t.partial_cmp(&signal).unwrap()) {
39            Ok(idx) => MarketRegime::Cluster(idx as u8 + 1), // Exact match
40            Err(idx) => {
41                if idx == 0 {
42                    MarketRegime::Steady // Below first threshold
43                } else if idx >= self.thresholds.len() {
44                    MarketRegime::Crisis // Above last threshold
45                } else {
46                    MarketRegime::Cluster(idx as u8)
47                }
48            }
49        }
50    }
51}