The Curious Case of the Bull vs Bear Market: Hidden Markov Models in R
Image Source: The Wolf of Wall Street (2013)

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.

No alt text provided for this image

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.

No alt text provided for this image
No alt text provided for this image

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.

No alt text provided for this image

4) Plotting - Other plotting options are available, but this is easy and concise to use.

No alt text provided for this image
No alt text provided for this image

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.

要查看或添加评论,请登录

Colin Berry的更多文章

  • Bottling It, Or Bad Math?

    Bottling It, Or Bad Math?

    The shift of the 2024 World Series to Yankee Stadium for Game 3 did nothing to impede the momentum of the Los Angeles…

    3 条评论
  • Machiavelli's Blade

    Machiavelli's Blade

    Let’s explore a fresh analysis of the Tory leadership contest, with a focus on Stages 2, 3, and 4, using mathematical…

  • Professor Lugo

    Professor Lugo

    In his first start as a Kansas City Royal, Seth Lugo delivered six scoreless innings. In his second outing, he pitched…

  • The Garrett Crochet Effect

    The Garrett Crochet Effect

    The "Garett Crochet Effect" is unmistakably influencing the upcoming clash between the White Sox and the Dodgers on…

  • London Calling to the Faraway Towns

    London Calling to the Faraway Towns

    The historic rivalry between the New York Mets and the Philadelphia Phillies has been filled with memorable moments and…

  • Things Can Only Get Slugger!

    Things Can Only Get Slugger!

    Juan Soto showcased his power at the plate, belting two home runs, including a crucial go-ahead shot in the ninth…

  • Yu Darvish: Unveiling the Pitching Maestro of the San Diego Padres

    Yu Darvish: Unveiling the Pitching Maestro of the San Diego Padres

    Yu Darvish, who hadn't allowed a run in 25 consecutive innings and had only surrendered two home runs all season—none…

  • Was Shohei Ohtani's $700 Million Deal Worth It for the LA Dodgers?

    Was Shohei Ohtani's $700 Million Deal Worth It for the LA Dodgers?

    The signing of Shohei Ohtani by the Los Angeles Dodgers for a historic $700 million contract has sparked immense…

    4 条评论
  • Game Theory and 'The Worst Team in Major League Baseball'

    Game Theory and 'The Worst Team in Major League Baseball'

    During a recent trip to Chicago, I had the opportunity to attend my first baseball game. As someone intrigued by the…

    3 条评论
  • Thomas Tuchel's High Flying Blues? Using R To Predict Chelsea in the UCL.

    Thomas Tuchel's High Flying Blues? Using R To Predict Chelsea in the UCL.

    Okay, okay, Tuchel's Chelsea might not be the most entertaining, but they're certainly effective. 13 games undefeated…

    1 条评论

社区洞察

其他会员也浏览了