pub struct HMMRegimeDetector { /* private fields */ }Expand description
Hidden Markov Model for regime detection.
Uses a 3-state HMM (by default) to model market regimes:
- State 0: Bull market (positive returns, low volatility)
- State 1: Bear market (negative returns, medium volatility)
- State 2: High volatility (any direction, high volatility)
The model uses the forward algorithm for online filtering and periodically re-estimates parameters using the Baum-Welch algorithm.
§Example
use indicators::{HMMRegimeDetector, HMMConfig, MarketRegime};
let mut detector = HMMRegimeDetector::crypto_optimized();
// Feed close prices
for i in 0..200 {
let price = 100.0 * (1.0 + 0.001 * i as f64); // gentle uptrend
let result = detector.update(price);
if detector.is_ready() {
println!("HMM regime: {} (conf: {:.0}%)", result.regime, result.confidence * 100.0);
}
}Implementations§
Source§impl HMMRegimeDetector
impl HMMRegimeDetector
Sourcepub fn default_config() -> Self
pub fn default_config() -> Self
Create with default config
Sourcepub fn crypto_optimized() -> Self
pub fn crypto_optimized() -> Self
Create optimized for crypto
Sourcepub fn conservative() -> Self
pub fn conservative() -> Self
Create with conservative config
Sourcepub fn update(&mut self, close: f64) -> RegimeConfidence
pub fn update(&mut self, close: f64) -> RegimeConfidence
Update with new close price and get regime.
Calculates log return from the previous close, then runs the forward algorithm step and optional parameter updates.
Sourcepub fn update_ohlc(
&mut self,
_high: f64,
_low: f64,
close: f64,
) -> RegimeConfidence
pub fn update_ohlc( &mut self, _high: f64, _low: f64, close: f64, ) -> RegimeConfidence
Update with OHLC data (uses close price for HMM)
Sourcepub fn get_regime_confidence(&self) -> RegimeConfidence
pub fn get_regime_confidence(&self) -> RegimeConfidence
Get current regime with confidence
Sourcepub fn state_probabilities(&self) -> &[f64]
pub fn state_probabilities(&self) -> &[f64]
Get state probabilities
Sourcepub fn state_parameters(&self) -> Vec<(f64, f64)>
pub fn state_parameters(&self) -> Vec<(f64, f64)>
Get state parameters (mean, variance) for inspection
Sourcepub fn transition_matrix(&self) -> &[Vec<f64>]
pub fn transition_matrix(&self) -> &[Vec<f64>]
Get transition matrix
Sourcepub fn current_state_index(&self) -> usize
pub fn current_state_index(&self) -> usize
Get current state index
Sourcepub fn expected_regime_duration(&self, state: usize) -> f64
pub fn expected_regime_duration(&self, state: usize) -> f64
Get expected regime duration (from transition matrix).
Expected duration = 1 / (1 - P(stay in state))
Sourcepub fn predict_next_state(&self) -> (usize, f64)
pub fn predict_next_state(&self) -> (usize, f64)
Predict most likely next state
Sourcepub fn n_observations(&self) -> usize
pub fn n_observations(&self) -> usize
Get the total number of observations processed
Sourcepub fn current_confidence(&self) -> f64
pub fn current_confidence(&self) -> f64
Get the current confidence score