knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-" ) set.seed(1) library("fHMM") model <- fHMM::dax_model_3t data <- model$data
The {fHMM}
R package allows for the detection and characterization of financial market regimes in time series data by applying hidden Markov Models (HMMs). The vignettes outline the package functionality and the model formulation.
For a reference on the method, see:
Oelschläger, L., and Adam, T. 2021. "Detecting Bearish and Bullish Markets in Financial Time Series Using Hierarchical Hidden Markov Models." Statistical Modelling. https://doi.org/10.1177/1471082X211034048
A user guide is provided by the accompanying software paper:
Oelschläger, L., Adam, T., and Michels, R. 2024. "fHMM: Hidden Markov Models for Financial Time Series in R". Journal of Statistical Software. https://doi.org/10.18637/jss.v109.i09
Below, we illustrate an application to the German stock index DAX. We also show how to use the package to simulate HMM data, compute the model likelihood, and decode the hidden states using the Viterbi algorithm.
You can install the released package version from CRAN with:
install.packages("fHMM")
We are open to contributions and would appreciate your input:
If you encounter any issues, please submit bug reports as issues.
If you have any ideas for new features, please submit them as feature requests.
If you would like to add extensions to the package, please fork the master
branch and submit a merge request.
We fit a 3-state HMM with state-dependent t-distributions to the DAX log-returns from 2000 to 2022. The states can be interpreted as proxies for bearish (green below) and bullish markets (red) and an "in-between" market state (yellow).
library("fHMM")
The package has a build-in function to download financial data from Yahoo Finance:
dax <- download_data(symbol = "^GDAXI")
We first need to define the model:
controls <- set_controls( states = 3, sdds = "t", file = dax, date_column = "Date", data_column = "Close", logreturns = TRUE, from = "2000-01-01", to = "2022-12-31" )
The function prepare_data()
then prepares the data for estimation:
data <- prepare_data(controls)
The summary()
method gives an overview:
summary(data)
We fit the model and subsequently decode the hidden states and compute (pseudo-) residuals:
model <- fit_model(data) model <- decode_states(model) model <- compute_residuals(model)
The summary()
method gives an overview of the model fit:
summary(model)
Having estimated the model, we can visualize the state-dependent distributions and the decoded time series:
events <- fHMM_events( list(dates = c("2001-09-11", "2008-09-15", "2020-01-27"), labels = c("9/11 terrorist attack", "Bankruptcy Lehman Brothers", "First COVID-19 case Germany")) ) plot(model, plot_type = c("sdds","ts"), events = events)
The (pseudo-) residuals help to evaluate the model fit:
plot(model, plot_type = "pr")
The {fHMM}
package supports data simulation from an HMM and access to the model likelihood function for model fitting and the Viterbi algorithm for state decoding.
controls <- set_controls( states = 2, sdds = "gamma", horizon = 1000 )
fHMM_parameters()
function (unspecified parameters would be set at random).par <- fHMM_parameters( controls = controls, Gamma = matrix(c(0.95, 0.05, 0.05, 0.95), 2, 2), mu = c(1, 3), sigma = c(1, 3) )
simulate_hmm()
function.sim <- simulate_hmm( controls = controls, true_parameters = par ) plot(sim$data, col = sim$markov_chain, type = "b")
ll_hmm()
is evaluated at the identified and unconstrained parameter values, they can be derived via the par2parUncon()
function.(parUncon <- par2parUncon(par, controls))
Note that this transformation takes care of the restrictions, that Gamma
must be a transition probability matrix (which we can ensure via the logit link) and that mu
and sigma
must be positive (an assumption for the Gamma distribution, which we can ensure via the exponential link).
ll_hmm(parUncon, sim$data, controls) ll_hmm(parUncon, sim$data, controls, negative = TRUE)
ll_hmm()
over parUncon
(or rather minimize the negative log-likelihood).optimization <- nlm( f = ll_hmm, p = parUncon, observations = sim$data, controls = controls, negative = TRUE ) (estimate <- optimization$estimate)
parUncon2par()
function. The state-labeling is not identified.class(estimate) <- "parUncon" estimate <- parUncon2par(estimate, controls) par$Gamma estimate$Gamma par$mu estimate$mu par$sigma estimate$sigma
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.