Skip to main content

HMMRegimeDetector

Struct HMMRegimeDetector 

Source
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

Source

pub fn new(config: HMMConfig) -> Self

Create a new HMM detector with the given configuration

Source

pub fn default_config() -> Self

Create with default config

Source

pub fn crypto_optimized() -> Self

Create optimized for crypto

Source

pub fn conservative() -> Self

Create with conservative config

Source

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.

Source

pub fn update_ohlc( &mut self, _high: f64, _low: f64, close: f64, ) -> RegimeConfidence

Update with OHLC data (uses close price for HMM)

Source

pub fn get_regime_confidence(&self) -> RegimeConfidence

Get current regime with confidence

Source

pub fn state_probabilities(&self) -> &[f64]

Get state probabilities

Source

pub fn state_parameters(&self) -> Vec<(f64, f64)>

Get state parameters (mean, variance) for inspection

Source

pub fn transition_matrix(&self) -> &[Vec<f64>]

Get transition matrix

Source

pub fn current_state_index(&self) -> usize

Get current state index

Source

pub fn is_ready(&self) -> bool

Check if model is warmed up (has enough observations)

Source

pub fn expected_regime_duration(&self, state: usize) -> f64

Get expected regime duration (from transition matrix).

Expected duration = 1 / (1 - P(stay in state))

Source

pub fn predict_next_state(&self) -> (usize, f64)

Predict most likely next state

Source

pub fn n_observations(&self) -> usize

Get the total number of observations processed

Source

pub fn current_confidence(&self) -> f64

Get the current confidence score

Source

pub fn config(&self) -> &HMMConfig

Get the configuration

Trait Implementations§

Source§

impl Debug for HMMRegimeDetector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.