The Curious Case of the Bull vs Bear Market: Hidden Markov Models in R
As a grumpy, nearly 30 something, I'm old enough to remember the financial crisis, chin-stroking tweed-wearing Keynesian lecturers and the emergence of Randy Marsh as one of the main characters in South Park. Bull vs Bear Markets used to dominate the financial press in the early 10's and have made somewhat of a comeback. GameStop? Sound familiar? I've generally avoided media coverage but wanted to run some Hidden Markov Models in R.
"The Hidden Markov Model (HMM) is a relatively simple way to model sequential data. A hidden Markov model implies that the Markov Model underlying the data is hidden or unknown to you. More specifically, you only know observational data and not information about the states."
With five minutes, basic code and statistics; you can build a model to help understand the market a little better. You could use your HMM to analyse the market or economy for your product or sector. This is not a substitute for an investment professional and I would be hesitant toward investing without professional advice.
1) Load the libraries. "depmixS4" for the Hidden Markov Model and "quantmod" for ease of data access.
2) getSymbols generates the dataset we wish to use. 'AAPL' would provide you with Apple stock data and countless others are available. Quandl and other sites have APIs that are useful too. Be careful to ensure that data is an xts object.
3) Using the depmixS4 package we build out the Hidden Markov Model and fit it to our problem. The distribution is gaussian and the number of states is two: Bull & Bear. If you wish to expand on the content in this article, please read Expectation–maximization algorithm. The "depmixS4" package cuts out a lot of the hard miles although this is no substitute for understanding Markov models.
4) Plotting - Other plotting options are available, but this is easy and concise to use.
After running the script, we can see market behaviour change and the classification with it too. Volatility spikes show that recent events have created an uncertain market, with classification changing frequently between bull and bear markets on the SAP 500. 2021 forecasts are wide-ranging and while short and quick returns may be available, the probability of a loss is high too. Be careful!
Currently available for contract roles using R and Python in the data analytics and science space across the UK. If you're interested in learning what this can bring to your business or client, contact me on [email protected] for more information.