Stock Market Regime Detection using Hidden Markov Models

David Cruz
4 min readOct 12, 2022

--

Inspired by The Great Wave off Kanagawa by Hokusai

Stick to the models — Future Hendrix

Wid bby, its your favorite masters graduate and professional data scientist, Dave. Sorry for the hiatus, alot has happened since the story I posted (see previous sentence) but we’re back at it again. Today’s story is going to be about hidden markov models. When you inevitably research enough about machine learning in finance, you’ll stumble across hidden Markov models. I will be testing it out today and seeing if we can validate the results to add to the trading algorithm from before. If you need to refresh on the algorithm or don’t know what I’m referencing, click below.

I will try to keep this article self-contained by explaining what is a hidden markov model. I’ll explain what is a markov chain then bulid our way to the hidden markov model to find out what its hiding.

Markov Property

First, lets introduce a stocastic process or random process. A random process is a indexed set of random variables. In math, there are a lot of rabbit holes we can go into involving stochastic processes for now, we will consider one specific type stochastic process, the markov process. What makes a Markov process is a stochastic process that has the Markov property. A stochastic process has this Markov property if the conditional probability distribution of the next steps of the process only rely on the current step. In other words, the past steps are not involved so if you look it up enough you will see the word memoryless everywhere because the reliance only on the current step and not the past steps.

Hidden Markov Model

Now, a Markov Model is a stochastic state space model involving random transitions between states where the probability of the jump is only dependent upon the current state, rather than any of the previous states. These states for example in our area of interest would be bear, bull and sideways markets. What makes a hidden markov model is that the probabilities between the states and the underlying states themselves. Other markov models have know some or all of these underlying states. So that what’s it hiding, the sauce. Keeping with our previous example the market tends to be up down or sideways market so this model will make these markets states with probabilities of staying in the current state and jumping to different states. Each of which have their own probabilities for staying and jumping to another state.

Reasons to keep researching

The model as an indicator should indicate whether if we are entering and leaving an underlying state. Leaving a bear market and entering a sideways market would be crucial knowledge since we can modify our strategy to the correct market sooner than later.

Since we are predicting stock returns, this doesn’t seem like a good idea at first because momentum does seem to play a factor in stock prices. However, daily returns at times behaves like a normal distribution. In my first article I plotted as a visual to confirm. So if you want a visual check out this story.

lol but yeah a normal distrubtion. This lets us assume the properties needed to use the stock price time series in this way. The Black Scholes equation regarded as one of the best ways for pricing options and uses this same assumption. However, we should atleast test out the model and see if its feasible.

Testing

In the colab below, I got spy prices from yahoo finance and the hidden markov model. The states have been color coated to show which prices fall into which market. Green being bull would look correct, yellow being sideways and red being bear.

Colab link:

Based on the spy plot of the model it seems to depict the state of the market well enough.

Conclusions and future works

While this indicator doesn't tell us too much by itself but we can add it to the algorithm where it would hopefully add to its predictive power.

It would behoove us to have unrelated indicators as much as possible since we do not know which factor is best but there are Machine Learning Algorithms that can optimally manage these as features in a model.

No indicator will be perfect and we shouldn’t seek perfection because the market is stochastic in nature. It would be naive to think we can perfectly trading and never lose money. However, it is very possible to make profits that far exceed our losses which is my overall goal with this algorithm and these indicators we feed it.

My next article topic will be housing market prediction. Please stay tuned if this you below.

--

--

David Cruz

Professional Data Scientist/ 2 degrees in Mathematics focusing in financial math:Tall and Brown