Hierarchical Hidden Markov Model

The hierarchical nature of HHMMs is facilitated through the concept of termination probabilities. A termination probability is the probability that a given state will stop emitting observations and transition control back to its parent state. This mechanism allows the model to dynamically switch between different levels of the hierarchy, thereby modeling the nested structure effectively. Beside the transition, emission and initial probabilities that generally define a HMM, termination probabilities distinguish HHMMs from HMMs because they define when the process in a sub-state concludes, allowing the model to transition back to the higher-level state and potentially move to a different branch of the hierarchy.
In financial markets, HHMMs can be applied similiarly to HMMs to model latent market regimes such as high volatility, low volatility, or neutral, along with their respective sub-regimes. By identifying the most likely market regime and sub-regime, traders and analysts can make informed decisions based on a more granular probabilistic assessment of market conditions. For instance, during a high volatility regime, the model might detect sub-regimes that indicate different types of price movements, helping traders to adapt their strategies accordingly.
MODEL FIT:
By default, the indicator displays the posterior probabilities, which represent the likelihood that the market is in a specific hidden state at any given time, based on the observed data and the model fit. These posterior probabilities strictly represent the model fit, reflecting how well the model explains the historical data it was trained on. This model fit is inherently different from out-of-sample predictions, which are generated using data that was not included in the training process. The posterior probabilities from the model fit provide a probabilistic assessment of the state the market was in at a particular time based on the data that came before and after it in the training sequence. Out-of-sample predictions, on the other hand, offer a forward-looking evaluation to test the model's predictive capability.
MODEL TESTING:
When the "Test Out of Sample" option is enabled, the indicator plots the selected display settings based on models' out-of-sample predictions. The display settings for out-of-sample testing include several options:
State Probability option displays the probability of each state at a given time for segments of data points not included in the training process. This is particularly useful for real-time identification of market regimes, ensuring that the model's predictive capability is tested on unseen data. These probabilities are calculated using the forward algorithm, which efficiently computes the likelihood of the observed sequence given the model parameters. Higher probabilities for a particular state suggest that the market is currently in that state. Traders can use this information to adjust their strategies according to the identified market regime and their statistical features.
Confidence Interval Bands option plots the upper, lower, and median confidence interval bands for predicted values. These bands provide a range within which future values are expected to lie with a certain confidence level. The width of the interval is determined by the current probability of different states in the model and the distribution of data within these states. The confidence level can be specified in the Confidence Interval setting.
Omega Ratio option displays a risk-adjusted performance measure that offers a more comprehensive view of potential returns compared to traditional metrics like the Sharpe ratio. It takes into account all moments of the returns distribution, providing a nuanced perspective on the risk-return tradeoff in the context of the HHMM's identified market regimes. The minimum acceptable return (MAR) used for the calculation of the omega can be specified in the settings of the indicator. The plot displays both the current Omega ratio and a forecasted "N day Omega" ratio. A higher Omega ratio suggests better risk-adjusted performance, essentially comparing the probability of gains versus the probability of losses relative to the minimum acceptable return. The Omega ratio plot is color-coded, green indicates that the long-term forecasted Omega is higher than the current Omega (suggesting improving risk-adjusted returns over time), while red indicates the opposite. Traders can use omega ratio to assess the risk-adjusted forecast of the model, under current market conditions with a specific target return requirement (MAR). By leveraging the HHMM's ability to identify different market states, the Omega ratio provides a forward-looking risk assessment tool, helping traders make more informed decisions about position sizing, risk management, and strategy selection.
Model Complexity option shows the complexity of the model, as well as complexity of individual states if the “complexity components” option is enabled. Model complexity is measured in terms of the entropy expressed through transition probabilities. The used complexity metric can be related to the models entropy rate and is calculated as the sum of the p*log(p) for every transition probability of a given state. Complexity in this context informs us on how complex the models transitions are. A model that might transition between states more often would be characterised by higher complexity, while a model that tends to transition less often would have lower complexity. High complexity can also suggest the model captures noise rather than the underlying market structure also known as overfitting, whereas lower complexity might indicate underfitting, where the model is too simplistic to capture important market dynamics. It is useful to assess the stability of the model complexity as well as understand where changes come from when a shift happens. A model with irregular complexity values can be strong sign of overfitting, as it suggests that the process that the model is capturing changes siginficantly over time.
Akaike/Bayesian Information Criterion option plots the AIC or BIC values for the model on both the training and out-of-sample data. These criteria are used for model selection, helping to balance model fit and complexity, as they take into account both the goodness of fit (likelihood) and the number of parameters in the model. The metric therefore provides a value we can use to compare different models with different number of parameters. Lower values generally indicate a better model. AIC is considered more liberal while BIC is considered a more conservative criterion which penalizes the likelihood more. Beside comparing different models, we can also assess how much the AIC and BIC differ between the training sets and test sets. A test set metric, which is consistently significantly higher than the training set metric can point to a drift in the models parameters, a strong drift of model parameters might again indicate overfitting or underfitting the sampled data.
Indicator settings:
- Source : Data source which is used to fit the model
- Training Period : Adjust based on the amount of historical data available. Longer periods can capture more trends but might be computationally intensive.
- EM Iterations : Balance between computational efficiency and model fit. More iterations can improve the model but at the cost of speed.
- Test Out of Sample : turn on predict the test data out of sample, based on the model that is retrained every N bars
- Out of Sample Display: A selection of metrics to evaluate out of sample. Pick among State probability, confidence interval, model complexity and AIC/BIC.
- Test Model on N Bars : set the number of bars we perform out of sample testing on.
- Retrain Model on N Bars: Set based on how often you want to retrain the model when testing out of sample segments
- Confidence Interval : When confidence interval is selected in the out of sample display you can adjust the percentage to reflect the desired confidence level for predictions.
- Omega forecast: Specifies the number of days ahead the omega ratio will be forecasted to get a long run measure.
- Minimum Acceptable Return : Specifies the target minimum acceptable return for the omega ratio calculation
- Complexity Components : When model complexity is selected in the out of sample display, this option will display the complexity of each individual state.
-Bayesian Information Criterion : When AIC/BIC is selected, turning this on this will ensure BIC is calculated instead of AIC.
Invite-only script
Only users approved by the author can access this script. You'll need to request and get permission to use it. This is typically granted after payment. For more details, follow the author's instructions below or contact Motgench directly.
Note that this private, invite-only script has not been reviewed by script moderators, and its compliance with House Rules is undetermined. TradingView does NOT recommend paying for or using a script unless you fully trust its author and understand how it works. You may also find free, open-source alternatives in our community scripts.
Author's instructions
Disclaimer
Invite-only script
Only users approved by the author can access this script. You'll need to request and get permission to use it. This is typically granted after payment. For more details, follow the author's instructions below or contact Motgench directly.
Note that this private, invite-only script has not been reviewed by script moderators, and its compliance with House Rules is undetermined. TradingView does NOT recommend paying for or using a script unless you fully trust its author and understand how it works. You may also find free, open-source alternatives in our community scripts.