Welcome to{fHMM}, an R package for modeling financialtime series data with hidden Markov models (HMMs). This introductionmotivates the approach, gives anoverview of the packagefunctionality and the included vignettes, andplaces the approach in the existingliterature.
Earning money with stock trading is simple: one “only” needs to buyand sell stocks at the right moment. In general, stock traders seek toinvest at the beginning of upward trends (hereon termed as bullishmarkets) and repel their stocks just in time before the prices fallagain (hereon termed as bearish markets). As stock prices depend on avariety of environmental factors(Humpe and Macmillan 2009;Cohen, Diether, and Malloy 2013), chancecertainly plays a fundamental role in hitting those exact moments.However, investigating market behavior can lead to a betterunderstanding of how trends alternate and thereby increases the chanceof making profitable investment decisions.
The{fHMM} package aims at contributing to thoseinvestigations by applying HMMs to detect bearish and bullish markets infinancial time series. It also implements the hierarchical modelextension presented inOelschläger and Adam (2021), which improves the model’scapability for distinguishing between short- and long-term trends andallows to interpret market dynamics at multiple time scales.
The functionality of the{fHMM} package can beclassified into functions for data preparation, model estimation, andmodel evaluation. The following flowchart visualizes theirdependencies:
The tasksdata preparation,model estimation, andmodel evaluation as well as their corresponding functions andclasses are explained in detail in separate vignettes:
The vignetteModeldefinition defines the HMM and its hierarchicalextension.
The vignetteControlsintroduces theset_controls() function which is used formodel specifications.
The vignetteDatamanagement explains how to prepare or simulate data andintroduces thedownload_data() function that can downloadfinancial data directly fromhttps://finance.yahoo.com/.
The vignetteModelestimation defines the likelihood function and explains thetask of its numerical maximization via thefit_model()function.
The vignetteStatedecoding and prediction introduces the Viterbi algorithm thatis used for decoding the most likely underlying state sequence andsubsequently for forecasting.
The vignetteModelchecking explains the task of checking a fitted model viacomputing (pseudo-) residuals, which is implemented in thecompute_residuals() function.
The vignetteModelselection discusses the task of selecting the (in some sense)best model among a set of competing models via thecompare_models() function.
Over the last decades, various HMM-type models have emerged aspopular tools for modeling financial time series that are subject tostate-switching over time(Schaller and Van Norden 1997;Dias, Vermunt, and Ramos 2010;Ang and Timmermann 2012;DeAngelis and Viroli 2017).Rydén,Teräsvirta, and Åsbrink (1998),Bulla and Bulla (2006),andNystrup, Madsen, and Lindström (2015), e.g., used HMMs to derive stylizedfacts of stock returns, whileHassan and Nath (2005) andNystrup,Madsen, and Lindström (2017)demonstrated that HMMs can prove useful for economic forecasting. Morerecently,Lihn (2017) applied HMMs to the Standard andPoor’s 500, where HMMs were used to identify different levels of marketvolatility, aiming at providing evidence for the conjecture that returnsexhibit negative correlation with volatility. Another application to theS&P 500 can be found inNguyen (2018), where HMMs were used to predictmonthly closing prices to derive an optimal trading strategy, which wasshown to outperform the conventional buy-and-hold strategy. Furtherapplications, which involve HMM-type models for asset allocation andportfolio optimization, can be found inBekaertand Ang (2002),Bulla et al. (2011),Nystrup, Madsen, and Lindström (2015) andNystrup,Madsen, and Lindström (2018), to namebut a few examples. All these applications demonstrate that HMMsconstitute a versatile class of time series models that naturallyaccounts for the dynamics typically exhibited by financial timeseries.