INVITE-ONLY SCRIPT

Hidden Markov Model

69
Hidden Markov Models (HMMs) are a class of statistical models used to represent systems that follow a Markov process with hidden states. In such models, the system being modeled transitions between a finite number of states, with the probability of each transition dependent only on the current state. The hidden states are not directly observable; instead, we observe a sequence of emissions or outputs generated by these states. HMMs are widely used in various fields, including speech recognition, bioinformatics, and financial market analysis. In the context of financial markets, HMMs can be utilized to model the latent market regimes (e.g., bullish, bearish, or neutral) that influence the observed market data such as asset prices or returns. By estimating the posterior probabilities of these hidden states, traders and analysts can identify the most likely market regime and make informed decisions based on the probabilistic assessment of market conditions.

istantanea

The Hidden Markov Model (HMM) comprises several states that work together to model the hidden market dynamics. The states represent the unobservable market regimes such as bullish, bearish or neutral. The states are 'hidden' in nature because we need to infer them from the data and cannot directly observe them.

Model components:

Initial Probabilities: These denote the likelihood of starting in each hidden state. They can be related to long-run probabilities, reflecting the overall likelihood of each state across extended periods. In equilibrium, these initial probabilities may converge to the stationary distribution of the Markov chain.
Transition Probabilities: These capture the likelihood of moving between states, including the probability of remaining in the current state. They model how market regimes evolve over time, allowing the HMM to adapt to changing conditions.
Emission Probabilities: Also known as observation likelihoods, these represent the probability of observing specific market data (like returns) given each hidden state. Emission probabilities can be often represented by continuous probability distributions. In our case we are using a laplace distribution with its location parameter reflecting the central tendency of returns in each state and the scale reflecting the dispersion or the magnitude of the returns.

The power of HMMs in financial modeling lies in their ability to capture complex market dynamics probabilistically. By analyzing patterns in market, the model can estimate the likelihood of being in each state at any given time. This can reveal insights into market behavior and dynamics that might not be apparent from data alone.

MODEL FIT:

istantanea

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. It is crucial to understand that 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 sequeence. Out-of-sample predictions on the other hand offer a forward-looking evaluation to test the model's predictive capability.

MODEL TEST:

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 datapoints that were not included in the traning process. This is particularly useful for real-time identification of market regimes, ensuring that the model's predictive capability is rigorously 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 probability for a particular state indicate a higher likelihood 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.

istantanea

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.

istantanea

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.

istantanea

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 too low complexity might indicate underfitting, where the model is too simplistic to capture important market dynamics. It is also useful to assess the stability of the model complexity. A model with irregular complexity values can be sign of overfitting, as it suggests that the process that the model is capturing changes significantly over time.

istantanea

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.

istantanea

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.

Declinazione di responsabilità

Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.