Learn R Programming

⚠️There's a newer version (1.4.2) of this package.Take me there.

{fHMM}: Fitting HMMs to financial time series

With {fHMM} you can detect and characterize financial market regimes in financial time series by applying hidden Markov Models (HMMs). The functionality and the model is documented in detail here. Below, you can find a first application to the German stock index DAX.

Installation

You can install the released version of {fHMM} from CRAN with:

install.packages("fHMM")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("loelschlaeger/fHMM")

Contributing

We welcome contributions! Please submit bug reports and feature requests as issues and extensions as merge requests via a branch forked from “master”.

Example: Fitting an HMM to the DAX

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).

The package has a build-in function to download financial data from Yahoo Finance:

dax <- download_data(symbol = "^GDAXI", file = NULL, verbose = FALSE)

We first need to define the model:

controls <- list(
  states = 3,
  sdds   = "t",
  data   = list(file        = dax,
                date_column = "Date",
                data_column = "Close",
                logreturns  = TRUE,
                from        = "2000-01-01",
                to          = "2022-12-31")
)
controls <- set_controls(controls)

The function prepare_data() then prepares the data for estimation:

data <- prepare_data(controls)

The summary() method gives an overview:

summary(data)
#> Summary of fHMM empirical data
#> * number of observations: 5882 
#> * data source: data.frame 
#> * date column: Date 
#> * log returns: TRUE

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)
#> Summary of fHMM model
#> 
#>   simulated hierarchy       LL       AIC       BIC
#> 1     FALSE     FALSE 17649.52 -35269.03 -35168.84
#> 
#> State-dependent distributions:
#> t() 
#> 
#> Estimates:
#>                   lb   estimate        ub
#> Gamma_2.1  1.286e-02  2.007e-02 3.113e-02
#> Gamma_3.1  1.208e-06  1.198e-06 1.180e-06
#> Gamma_1.2  1.557e-02  2.489e-02 3.959e-02
#> Gamma_3.2  1.036e-02  1.877e-02 3.378e-02
#> Gamma_1.3  4.119e-07  4.080e-07 4.019e-07
#> Gamma_2.3  2.935e-03  5.275e-03 9.422e-03
#> mu_1       9.655e-04  1.271e-03 1.576e-03
#> mu_2      -8.483e-04 -3.102e-04 2.278e-04
#> mu_3      -3.813e-03 -1.760e-03 2.932e-04
#> sigma_1    5.417e-03  5.853e-03 6.324e-03
#> sigma_2    1.278e-02  1.330e-02 1.384e-02
#> sigma_3    2.348e-02  2.579e-02 2.832e-02
#> df_1       3.957e+00  5.198e+00 6.828e+00
#> df_2       3.870e+08  3.870e+08 3.870e+08
#> df_3       5.549e+00  1.078e+01 2.095e+01
#> 
#> States:
#> decoded
#>    1    2    3 
#> 2278 2900  704 
#> 
#> Residuals:
#>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
#> -3.519694 -0.658831  0.009613 -0.002206  0.669598  3.905726

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")

Copy Link

Version

Install

install.packages('fHMM')

Monthly Downloads

358

Version

1.1.0

License

GPL-3

Maintainer

Lennart Oelschläger

Last Published

February 14th, 2023

Functions in fHMM (1.1.0)

find_closest_year

Find the closest year to a given date
dax_model_3t

DAX 3-state HMM with t-distributions
gammasUncon2gammasCon

This function constrains non-diagonal elements gammasUncon of a transition probability matrix.
gammasUncon2Gamma

This function builds a transition probability matrix from un-constrained non-diagonal elements gammasUncon.
dax_model_2n

DAX 2-state HMM with normal distributions
download_data

Download financial data from Yahoo Finance
fHMM_events

Checking events
fHMM_sdds

Define state-dependent distributions
fHMM_parameters

Set and check model parameters
fHMM_model

Constructor of a model object
dfCon2dfUncon

This function un-constrains the constrained degrees of freedom dfCon.
par2parCon

This function transforms an object of class fHMM_parameters into an object of class parCon.
dfUncon2dfCon

This function constrains the un-constrained degrees of freedom dfUncon.
par2parUncon

This function transforms an object of class fHMM_parameters into an object of class parUncon.
match_all

Best-possible match of two numeric vectors
muCon2muUncon

This function un-constrains the constrained expected values muCon.
muUncon2muCon

This function constrains the un-constrained expected values muUncon.
parCon2parUncon

This function transforms an object of class parCon into an object of class parUncon.
parCon2par

This function transforms an object of class parCon into an object of class fHMM_parameters.
nLL_hhmm

Negative log-likelihood function of an HHMM
prepare_data

Prepare data
fit_model

Model fitting
plot.fHMM_model

Plot method for an object of class fHMM_model
plot_ll

Visualization of log-likelihood values
read_data

Read data
parUncon2par

This function transforms an object of class parUncon into an object of class fHMM_parameters.
parUncon2parCon

This function transforms an object of class parUncon into an object of class parCon.
sigmaCon2sigmaUncon

This function un-constrains the constrained standard deviations sigmaCon.
simulate_data

Simulate data
simulate_markov_chain

Simulate Markov chain
plot_pr

Visualize pseudo residuals
set_controls

Set and validate controls
npar

Number of model parameters
nLL_hmm

Negative log-likelihood function of an HMM
unemp

Unemployment rate data USA
plot_sdds

Visualization of estimated state-dependent distributions
sample_tpm

Sample transition probability matrices
gammasCon2gammasUncon

This function un-constrains the constrained non-diagonal elements gammasCon of a transition probability matrix of dimension dim.
sim_model_2gamma

Simulated 2-state HMM with gamma distributions
sigmaUncon2sigmaCon

This function constrains the un-constrained standard deviations sigmaUncon.
gammasCon2Gamma

This function builds a transition probability matrix of dimension dim from constrained non-diagonal elements gammasCon.
fHMM

fHMM: Fitting Hidden Markov Models to Financial Data
sim_model_4lnorm

Simulated 4-state HMM with log-normal distributions
residuals.fHMM_model

Residuals
predict.fHMM_model

Prediction
reorder_states

Reorder estimated states
is_number

Check for integers
plot_ts

Visualize time series
vw

Volkswagen AG (VW) stock data
unemp_spx_model_3_2

Unemployment rate and S&P 500 hierarchical HMM
simulate_observations

Simulate state-dependent observations
is_tpm

Check for transition probability matrix
plot.fHMM_data

Plot method for an object of class fHMM_data
parameter_labels

Create labels for estimated parameters
spx

Standard & Poor’s 500 (S&P 500) index data
coef.fHMM_model

Model coefficients
dax

Deutscher Aktienindex (DAX) index data
compute_residuals

Compute (pseudo-) residuals
compare_models

Compare multiple models
compute_ci

Compute confidence intervals
compute_T_star

Compute lengths of fine-scale chunks
Gamma2gammasUncon

This function un-constrains the non-diagonal matrix elements of a transition probability matrix Gamma.
Gamma2delta

This function computes the stationary distribution of a transition probability matrix Gamma.
Gamma2gammasCon

This function constrains the non-diagonal matrix elements of a transition probability matrix Gamma.
check_date

Check date format
decode_states

Decode the underlying hidden state sequence
fHMM_colors

Set color scheme for visualizations
dax_vw_model

DAX/VW hierarchical HMM with t-distributions
fHMM_data

Constructor of an fHMM_data object