PROTECTED SOURCE SCRIPT

Gaussian Hidden Markov Model

59
A Hidden Markov Model (HMM) is a statistical model that assumes an underlying process is a Markov process with unobservable (hidden) states. In the context of financial data analysis, a HMM can be particularly useful because it allows for the modeling of time series data where the state of the market at a given time depends on its state in the previous time period, but these states are not directly observable from the market data. When we say that a state is "unobservable" or "hidden," we mean that the true state of the process generating the observations at any time is not directly visible or measurable. Instead, what is observed is a set of data points that are influenced by these hidden states.

snapshot

The HMM uses a set of observed data to infer the sequence of hidden states of the model (in our case a model with 3 states and Gaussian emissions). It comprises three main components: the initial probabilities, the state transition probabilities, and the emission probabilities. The initial probabilities describe the likelihood of starting in a particular state. The state transition probabilities describe the likelihood of moving from one state to another, while the emission probabilities (in our case emitted from Gaussian probability density functions, in the image red yellow and green Laplace probability densitty functions) describe the likelihood of the observed data given a particular state.

MODEL FIT

Posterior


snapshot

By default, the indicator displays the posterior distribution as fitted by training a 3-state Gaussian HMM. The posterior refers to the probability distribution of the hidden states given the observed data. In the case of your Gaussian HMM with three states, the posterior represents the probabilities that the model assigns to each of these three states at each time point, after observing the data. The term "posterior" comes from Bayes' theorem, where it represents the updated belief about the model's states after considering the evidence (the observed data).

In the indicator, the posterior is visualized as the probability of the stock market being in a particular volatility state (high vol, medium vol, low vol) at any given time in the time series. Each day, the probabilities of the three states sum to 1, with the plot showing color-coded bands to reflect these state probabilities over time. It is important to note that the posterior distribution of the model fit tells you about the performance of the model on past data. The model calculates the probabilities of observations for all states by taking into account the relationship between observations and their past and future counterparts in the dataset. This is achieved using the forward-backward algorithm, which enables us to train the HMM.

Conditional Mean


snapshot

The conditional mean is the expected value of the observed data given the current state of the model. For a Gaussian HMM, this would be the mean of the Gaussian distribution associated with the current state. It’s "conditional" because it depends on the probabilities of the different states the model is in at a given time. This connects back to the posterior probability, which assigns a probability to the model being in a particular state at a given time.

Conditional Standard Deviation Bands


snapshot

The conditional standard deviation is a measure of the variability of the observed data given the current state of the model. In a Gaussian HMM, each state has its own emission probability, defined by a Gaussian distribution with a specific mean and standard deviation. The standard deviation represents how spread out the data is around the mean for each state. These bands directly relate to the emission probabilities of the HMM, as they describe the likelihood of the observed values given the current state. Narrow bands suggest a lower standard deviation, indicating the model is more confident about the data's expected range when in that state, while wider bands indicate higher uncertainty and variability.

Transition Matrix

snapshot

The transition matrix in a HMM is a key component that characterizes the model. It's a square matrix representing the probabilities of transitioning from one hidden state to another. Each row of the transition matrix must sum up to 1 since the probabilities of moving from a given state to all possible subsequent states (including staying in the same state) must encompass all possible outcomes.

For example, we can see the following transition probabilities in our model:

Going from state X: to X (0.98), to Y (0.02), to Z (0)
Going from state Y: to X (0.03), to Y (0.96), to Z (0.01)
Going from state Z: to X (0), to Y (0.11), to Z (0.89)

MODEL TEST

snapshot

When the "Test Out of Sample” option is enabled, the indicator plots models out-of-sample predictions. This is particularly useful for real-time identification of market regimes, ensuring that the model's predictive capability is rigorously tested on unseen data. The indicator displays the out of sample posterior probabilities which are calculated using the forward algorithm. Higher probability for a particular state indicate that the model is predicted a higher likelihood that the market is currently in that state. Evaluating the models performance on unseen data is crucial in understanding how well the model explains data that are not included in its training process.

Aviso legal

As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.