| Title: | Fitting Hidden Markov Models to Financial Data |
|---|---|
| Description: | Fitting (hierarchical) hidden Markov models to financial data via maximum likelihood estimation. See Oelschläger, L. and Adam, T. "Detecting Bearish and Bullish Markets in Financial Time Series Using Hierarchical Hidden Markov Models" (2021, Statistical Modelling) <doi:10.1177/1471082X211034048> for a reference on the method. A user guide is provided by the accompanying software paper "fHMM: Hidden Markov Models for Financial Time Series in R", Oelschläger, L., Adam, T., and Michels, R. (2024, Journal of Statistical Software) <doi:10.18637/jss.v109.i09>. |
| Authors: | Lennart Oelschläger [aut, cre] (ORCID: <https://orcid.org/0000-0001-5421-9313>), Timo Adam [aut] (ORCID: <https://orcid.org/0000-0001-9079-3259>), Rouven Michels [aut] (ORCID: <https://orcid.org/0000-0002-5433-6197>) |
| Maintainer: | Lennart Oelschläger <[email protected]> |
| License: | GPL-3 |
| Version: | 1.4.3 |
| Built: | 2026-06-04 06:54:49 UTC |
| Source: | https://github.com/loelschlaeger/fhmm |
This function performs model comparison by comparing multiple
fHMM_model objects with respect to
the number of model parameters,
the log-likelihood value,
the AIC value,
the BIC value.
compare_models(...)compare_models(...)
... |
A list of one or more objects of class |
A data.frame with models in rows and comparison criteria in columns.
### 3-state HMM with t-distributions is preferred over 2-state HMM with ### normal distributions for the DAX data based on AIC and BIC compare_models(dax_model_2n, dax_model_3t)### 3-state HMM with t-distributions is preferred over 2-state HMM with ### normal distributions for the DAX data based on AIC and BIC compare_models(dax_model_2n, dax_model_3t)
This function computes (pseudo-) residuals of an fHMM_model
object.
compute_residuals(x, verbose = TRUE)compute_residuals(x, verbose = TRUE)
x |
[ |
verbose |
[ |
An object of class fHMM_model with residuals included.
compute_residuals(dax_model_3t) summary(residuals(dax_model_3t))compute_residuals(dax_model_3t) summary(residuals(dax_model_3t))
Deutscher Aktienindex (DAX) index data from 1988 to 2022 from Yahoo Finance.
daxdax
A data.frame with 9012 rows and the following 7 columns:
Date: The date.
Open: Opening price.
High: Highest price.
Low: Lowest price.
Close: Close price adjusted for splits.
Adj.Close: Close price adjusted for dividends and splits.
Volume: Trade volume.
The data was obtained via:
dax <- download_data( symbol = "^GDAXI", # DAX identifier on Yahoo Finance from = "1988-01-01", # first observation to = "2022-12-31" # last observation )
A pre-computed HMM on closing prices of the DAX from 2000 to 2022 with two hidden states and normal state-dependent distributions for demonstration purposes.
data("dax_model_2n")data("dax_model_2n")
An object of class fHMM_model.
The model was estimated via:
controls <- set_controls(
states = 2,
sdds = "normal",
data = list(
file = dax,
date_column = "Date",
data_column = "Close",
logreturns = TRUE,
from = "2000-01-03",
to = "2022-12-31"
),
fit = list("runs" = 10, "gradtol" = 1e-6, "steptol" = 1e-6)
)
dax_data <- prepare_data(controls)
dax_model_2n <- fit_model(dax_data, seed = 1)
dax_model_2n <- decode_states(dax_model_2n)
dax_model_2n <- compute_residuals(dax_model_2n)
summary(dax_model_2n)
A pre-computed HMM on closing prices of the DAX from 2000 to 2022 with three hidden states and state-dependent t-distributions for demonstration purposes.
data("dax_model_3t")data("dax_model_3t")
An object of class fHMM_model.
The model was estimated via:
controls <- set_controls(
states = 3,
sdds = "t",
data = list(
file = dax,
date_column = "Date",
data_column = "Close",
logreturns = TRUE,
from = "2000-01-03",
to = "2022-12-31"
),
fit = list(
runs = 100,
iterlim = 300,
gradtol = 1e-6,
steptol = 1e-6
)
)
dax_data <- prepare_data(controls)
dax_model_3t <- fit_model(dax_data, seed = 1, ncluster = 10)
dax_model_3t <- decode_states(dax_model_3t)
dax_model_3t <- compute_residuals(dax_model_3t)
summary(dax_model_3t)
A pre-computed HHMM with monthly averaged closing prices of the DAX from 2010 to 2022 on the coarse scale, Volkswagen AG stock data on the fine scale, two hidden fine-scale and coarse-scale states, respectively, and state-dependent t-distributions for demonstration purposes.
data("dax_vw_model")data("dax_vw_model")
An object of class fHMM_model.
The model was estimated via:
controls <- set_controls(
hierarchy = TRUE,
states = c(2, 2),
sdds = c("t", "t"),
period = "m",
data = list(
file = list(dax, vw),
from = "2010-01-01",
to = "2022-12-31",
logreturns = c(TRUE, TRUE)
),
fit = list(
runs = 200,
iterlim = 300,
gradtol = 1e-6,
steptol = 1e-6
)
)
dax_vw_data <- prepare_data(controls)
dax_vw_model <- fit_model(dax_vw_data, seed = 1, ncluster = 10)
dax_vw_model <- decode_states(dax_vw_model)
dax_vw_model <- compute_residuals(dax_vw_model)
summary(dax_vw_model)
This function decodes the (most likely) underlying hidden state sequence by applying the Viterbi algorithm for global decoding.
decode_states(x, verbose = TRUE) viterbi(observations, nstates, sdd, Gamma, mu, sigma = NULL, df = NULL)decode_states(x, verbose = TRUE) viterbi(observations, nstates, sdd, Gamma, mu, sigma = NULL, df = NULL)
x |
[ |
verbose |
[ |
observations |
[ |
nstates |
[ |
sdd |
[
|
Gamma |
[ |
mu |
[ For the gamma- or Poisson-distribution, |
sigma |
[ Not relevant in case of a state-dependent Poisson distribution. |
df |
[ Only relevant in case of a state-dependent t-distribution. |
An object of class fHMM_model with decoded state sequence
included.
https://en.wikipedia.org/wiki/Viterbi_algorithm
decode_states(dax_model_3t) plot(dax_model_3t, type = "ts") viterbi( observations = c(1, 1, 1, 10, 10, 10), nstates = 2, sdd = "poisson", Gamma = matrix(0.5, 2, 2), mu = c(1, 10) )decode_states(dax_model_3t) plot(dax_model_3t, type = "ts") viterbi( observations = c(1, 1, 1, 10, 10, 10), nstates = 2, sdd = "poisson", Gamma = matrix(0.5, 2, 2), mu = c(1, 10) )
This function downloads financial data from https://finance.yahoo.com/
and returns it as a data.frame.
download_data( symbol, from = "1902-01-01", to = Sys.Date(), fill_dates = FALSE, columns = c("Date", "Open", "High", "Low", "Close", "Adj.Close", "Volume") )download_data( symbol, from = "1902-01-01", to = Sys.Date(), fill_dates = FALSE, columns = c("Date", "Open", "High", "Low", "Close", "Adj.Close", "Volume") )
symbol |
[ It must match the identifier on https://finance.yahoo.com/. |
from |
[ Must not be earlier than |
to |
[ Default is the current date |
fill_dates |
[ By default, |
columns |
[ By default, all columns are returned. |
Yahoo Finance provides historical daily data for stocks or indices. The following data columns are available:
Date: The date.
Open: Opening price.
High: Highest price.
Low: Lowest price.
Close: Close price adjusted for splits.
Adj.Close: Close price adjusted for dividends and splits.
Volume: Trade volume.
A data.frame.
### 21st century DAX closing prices data <- download_data( symbol = "^GDAXI", from = "2000-01-01", columns = c("Date", "Close"), fill_dates = TRUE ) head(data)### 21st century DAX closing prices data <- download_data( symbol = "^GDAXI", from = "2000-01-01", columns = c("Date", "Close"), fill_dates = TRUE ) head(data)
fHMM_data objectThis function constructs an object of class fHMM_data, which contains
the financial data for modeling.
fHMM_data( dates, time_points, markov_chain, data, time_series, T_star, controls, true_parameters ) ## S3 method for class 'fHMM_data' print(x, ...) ## S3 method for class 'fHMM_data' summary(object, ...)fHMM_data( dates, time_points, markov_chain, data, time_series, T_star, controls, true_parameters ) ## S3 method for class 'fHMM_data' print(x, ...) ## S3 method for class 'fHMM_data' summary(object, ...)
dates |
[ |
time_points |
[ |
markov_chain |
[ |
data |
[ |
time_series |
[ |
T_star |
[ |
controls |
[ |
true_parameters |
[ |
x |
[ |
... |
Currently not used. |
object |
[ |
An object of class fHMM_data, which is a list containing
the following elements:
The matrix of the dates if simulated = FALSE and
controls$data$data_column is specified,
the matrix of the time_points if
simulated = TRUE
or controls$data$data_column is not specified,
the matrix of the simulated markov_chain if
simulated = TRUE,
the matrix of the simulated or empirical data used for
estimation,
the matrix time_series of empirical data before the
transformation to log-returns if simulated = FALSE,
the vector of fine-scale chunk sizes T_star if
controls$hierarchy = TRUE,
the input controls,
the true_parameters.
This function checks the input events.
fHMM_events(events) ## S3 method for class 'fHMM_events' print(x, ...)fHMM_events(events) ## S3 method for class 'fHMM_events' print(x, ...)
events |
[
|
x |
[ |
... |
Currently not used. |
An object of class fHMM_events.
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" ) ) events <- fHMM_events(events)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" ) ) events <- fHMM_events(events)
This function constructs an object of class fHMM_model, which
contains details about the fitted (hierarchical) Hidden Markov model.
fHMM_model( data, estimate, nlm_output, estimation_time, ll, lls, gradient, inverse_fisher, decoding ) ## S3 method for class 'fHMM_model' print(x, ...) ## S3 method for class 'fHMM_model' residuals(object, ...) ## S3 method for class 'fHMM_model' summary(object, alpha = 0.05, ...) ## S3 method for class 'fHMM_model' coef(object, alpha = 0.05, digits = 2, ...) ## S3 method for class 'fHMM_model' AIC(object, ..., k = 2) ## S3 method for class 'fHMM_model' BIC(object, ...) ## S3 method for class 'fHMM_model' nobs(object, ...) ## S3 method for class 'fHMM_model' logLik(object, ...) npar(object, ...) ## S3 method for class 'fHMM_model' npar(object, ...) ## S3 method for class 'fHMM_model' predict(object, ahead = 5, alpha = 0.05, ...)fHMM_model( data, estimate, nlm_output, estimation_time, ll, lls, gradient, inverse_fisher, decoding ) ## S3 method for class 'fHMM_model' print(x, ...) ## S3 method for class 'fHMM_model' residuals(object, ...) ## S3 method for class 'fHMM_model' summary(object, alpha = 0.05, ...) ## S3 method for class 'fHMM_model' coef(object, alpha = 0.05, digits = 2, ...) ## S3 method for class 'fHMM_model' AIC(object, ..., k = 2) ## S3 method for class 'fHMM_model' BIC(object, ...) ## S3 method for class 'fHMM_model' nobs(object, ...) ## S3 method for class 'fHMM_model' logLik(object, ...) npar(object, ...) ## S3 method for class 'fHMM_model' npar(object, ...) ## S3 method for class 'fHMM_model' predict(object, ahead = 5, alpha = 0.05, ...)
data |
[ |
estimate |
[ |
nlm_output |
[ |
estimation_time |
[ |
ll |
[ |
lls |
[ |
gradient |
[ |
inverse_fisher |
[ |
decoding |
[ |
x, object
|
[ |
... |
Currently not used. |
alpha |
[ |
digits |
[ |
k |
[ |
ahead |
[ |
An object of class fHMM_model.
This function sets and checks model parameters. Unspecified parameters are sampled.
fHMM_parameters( controls = list(), hierarchy = FALSE, states = if (!hierarchy) 2 else c(2, 2), sdds = if (!hierarchy) "normal" else c("normal", "normal"), Gamma = NULL, mu = NULL, sigma = NULL, df = NULL, Gamma_star = NULL, mu_star = NULL, sigma_star = NULL, df_star = NULL, scale_par = c(1, 1), seed = NULL, check_controls = TRUE ) ## S3 method for class 'fHMM_parameters' print(x, ...)fHMM_parameters( controls = list(), hierarchy = FALSE, states = if (!hierarchy) 2 else c(2, 2), sdds = if (!hierarchy) "normal" else c("normal", "normal"), Gamma = NULL, mu = NULL, sigma = NULL, df = NULL, Gamma_star = NULL, mu_star = NULL, sigma_star = NULL, df_star = NULL, scale_par = c(1, 1), seed = NULL, check_controls = TRUE ) ## S3 method for class 'fHMM_parameters' print(x, ...)
controls |
[ The
Either none, all, or selected elements can be specified. Unspecified parameters are set to their default values. Important: Specifications in |
hierarchy |
[ If By default, |
states |
[ If By default, |
sdds |
[
The distribution parameters, i.e. the
can be fixed via, e.g., If By default, |
Gamma, Gamma_star
|
[ It should have dimension
|
mu, mu_star
|
[ For the gamma- or Poisson-distribution, It should have length
|
sigma, sigma_star
|
[ It should have length
|
df, df_star
|
[ It should have length Only relevant in case of a state-dependent t-distribution.
|
scale_par |
[ The first entry is the scale for
|
seed |
[ |
check_controls |
[ |
x |
[ |
... |
Currently not used. |
See the package vignettes at https://loelschlaeger.de/fHMM/articles/ for details.
An object of class fHMM_parameters.
parameters <- fHMM_parameters(states = 2, sdds = "normal") parameters$Gammaparameters <- fHMM_parameters(states = 2, sdds = "normal") parameters$Gamma
This function fits a hidden Markov model via numerical likelihood maximization.
fit_model( data, controls = data[["controls"]], fit = list(), runs = 10, origin = FALSE, accept = 1:3, gradtol = 0.01, iterlim = 100, print.level = 0, steptol = 0.01, ncluster = 1, seed = NULL, verbose = TRUE, initial_estimate = NULL )fit_model( data, controls = data[["controls"]], fit = list(), runs = 10, origin = FALSE, accept = 1:3, gradtol = 0.01, iterlim = 100, print.level = 0, steptol = 0.01, ncluster = 1, seed = NULL, verbose = TRUE, initial_estimate = NULL )
data |
[ |
controls |
[ The
Either none, all, or selected elements can be specified. Unspecified parameters are set to their default values. Important: Specifications in |
fit |
[ The
Either none, all, or selected elements can be specified. Unspecified parameters are set to their default values, see below. Specifications in |
runs |
[ By default, |
origin |
[ In this case, a By default, |
accept |
[ By default, |
gradtol |
[ By default, |
iterlim |
[ By default, |
print.level |
[ By default, |
steptol |
[ By default, |
ncluster |
[ |
seed |
[ |
verbose |
[ |
initial_estimate |
[
|
Multiple optimization runs starting from different initial values are
computed in parallel if ncluster > 1.
An object of class fHMM_model.
### 2-state HMM with normal distributions # set specifications controls <- set_controls( states = 2, sdds = "normal", horizon = 100, runs = 10 ) # define parameters parameters <- fHMM_parameters(controls, mu = c(-1, 1), seed = 1) # sample data data <- prepare_data(controls, true_parameter = parameters, seed = 1) # fit model model <- fit_model(data, seed = 1) # inspect fit summary(model) plot(model, "sdds") # decode states model <- decode_states(model) plot(model, "ts") # predict predict(model, ahead = 5)### 2-state HMM with normal distributions # set specifications controls <- set_controls( states = 2, sdds = "normal", horizon = 100, runs = 10 ) # define parameters parameters <- fHMM_parameters(controls, mu = c(-1, 1), seed = 1) # sample data data <- prepare_data(controls, true_parameter = parameters, seed = 1) # fit model model <- fit_model(data, seed = 1) # inspect fit summary(model) plot(model, "sdds") # decode states model <- decode_states(model) plot(model, "ts") # predict predict(model, ahead = 5)
This function computes the log-likelihood value of a (hierarchical) hidden Markov model for given observations and parameter values.
ll_hmm( parUncon, observations, controls = list(), hierarchy = FALSE, states = if (!hierarchy) 2 else c(2, 2), sdds = if (!hierarchy) "normal" else c("normal", "normal"), negative = FALSE, check_controls = TRUE )ll_hmm( parUncon, observations, controls = list(), hierarchy = FALSE, states = if (!hierarchy) 2 else c(2, 2), sdds = if (!hierarchy) "normal" else c("normal", "normal"), negative = FALSE, check_controls = TRUE )
parUncon |
[
|
observations |
[ In the hierarchical case ( |
controls |
[ The
Either none, all, or selected elements can be specified. Unspecified parameters are set to their default values. Important: Specifications in |
hierarchy |
[ If By default, |
states |
[ If By default, |
sdds |
[
The distribution parameters, i.e. the
can be fixed via, e.g., If By default, |
negative |
[ |
check_controls |
[ |
The (negative) log-likelihood value.
### HMM log-likelihood controls <- set_controls(states = 2, sdds = "normal") parameters <- fHMM_parameters(controls) parUncon <- par2parUncon(parameters, controls) observations <- 1:10 ll_hmm(parUncon, observations, controls) ### HHMM log-likelihood controls <- set_controls( hierarchy = TRUE, states = c(2, 2), sdds = c("normal", "normal") ) parameters <- fHMM_parameters(controls) parUncon <- par2parUncon(parameters, controls) observations <- matrix(dnorm(110), ncol = 11, nrow = 10) ll_hmm(parUncon, observations, controls)### HMM log-likelihood controls <- set_controls(states = 2, sdds = "normal") parameters <- fHMM_parameters(controls) parUncon <- par2parUncon(parameters, controls) observations <- 1:10 ll_hmm(parUncon, observations, controls) ### HHMM log-likelihood controls <- set_controls( hierarchy = TRUE, states = c(2, 2), sdds = c("normal", "normal") ) parameters <- fHMM_parameters(controls) parUncon <- par2parUncon(parameters, controls) observations <- matrix(dnorm(110), ncol = 11, nrow = 10) ll_hmm(parUncon, observations, controls)
fHMM_data
This function is the plot method for an object of class fHMM_data.
## S3 method for class 'fHMM_data' plot(x, events = NULL, title = NULL, from = NULL, to = NULL, ...)## S3 method for class 'fHMM_data' plot(x, events = NULL, title = NULL, from = NULL, to = NULL, ...)
x |
[ |
events |
[ |
title |
[ |
from |
[ |
to |
[ |
... |
Currently not used. |
No return value. Draws a plot to the current device.
plot(dax_model_3t$data, title = "DAX time series")plot(dax_model_3t$data, title = "DAX time series")
fHMM_model
This function is the plot method for an object of class
fHMM_model.
## S3 method for class 'fHMM_model' plot( x, plot_type = "ts", events = NULL, colors = NULL, ll_relative = TRUE, title = NULL, from = NULL, to = NULL, ... )## S3 method for class 'fHMM_model' plot( x, plot_type = "ts", events = NULL, colors = NULL, ll_relative = TRUE, title = NULL, from = NULL, to = NULL, ... )
x |
[ |
plot_type |
[
|
events |
[ |
colors |
[ |
ll_relative |
[ |
title |
[ |
from |
[ |
to |
[ |
... |
Currently not used. |
No return value. Draws a plot to the current device.
This function simulates or reads financial data for the {fHMM} package.
prepare_data(controls, true_parameters = NULL, seed = NULL)prepare_data(controls, true_parameters = NULL, seed = NULL)
controls |
[ |
true_parameters |
[ |
seed |
[ |
An object of class fHMM_data.
controls <- set_controls() data <- prepare_data(controls) class(data) summary(data)controls <- set_controls() data <- prepare_data(controls) class(data) summary(data)
This function reorders the estimated states, which can be useful for a comparison to true parameters or the interpretation of states.
reorder_states(x, state_order = "mean")reorder_states(x, state_order = "mean")
x |
[ |
state_order |
[
|
An object of class fHMM_model, in which states are reordered.
dax_model_3t_reordered <- reorder_states(dax_model_3t, state_order = 3:1)dax_model_3t_reordered <- reorder_states(dax_model_3t, state_order = 3:1)
This function defines and validates specifications for model estimation.
set_controls( controls = list(), hierarchy = FALSE, states = if (!hierarchy) 2 else c(2, 2), sdds = if (!hierarchy) "normal" else c("normal", "normal"), horizon = if (!hierarchy) 100 else c(100, 30), period = NA, data = NA, file = NA, date_column = if (!hierarchy) "Date" else c("Date", "Date"), data_column = if (!hierarchy) "Close" else c("Close", "Close"), from = NA, to = NA, logreturns = if (!hierarchy) FALSE else c(FALSE, FALSE), merge = function(x) mean(x), fit = list(), runs = 10, origin = FALSE, accept = 1:3, gradtol = 0.01, iterlim = 100, print.level = 0, steptol = 0.01 ) validate_controls(controls) ## S3 method for class 'fHMM_controls' print(x, ...) ## S3 method for class 'fHMM_controls' summary(object, ...)set_controls( controls = list(), hierarchy = FALSE, states = if (!hierarchy) 2 else c(2, 2), sdds = if (!hierarchy) "normal" else c("normal", "normal"), horizon = if (!hierarchy) 100 else c(100, 30), period = NA, data = NA, file = NA, date_column = if (!hierarchy) "Date" else c("Date", "Date"), data_column = if (!hierarchy) "Close" else c("Close", "Close"), from = NA, to = NA, logreturns = if (!hierarchy) FALSE else c(FALSE, FALSE), merge = function(x) mean(x), fit = list(), runs = 10, origin = FALSE, accept = 1:3, gradtol = 0.01, iterlim = 100, print.level = 0, steptol = 0.01 ) validate_controls(controls) ## S3 method for class 'fHMM_controls' print(x, ...) ## S3 method for class 'fHMM_controls' summary(object, ...)
controls |
[ The
Either none, all, or selected elements can be specified. Unspecified parameters are set to their default values. Important: Specifications in |
hierarchy |
[ If By default, |
states |
[ If By default, |
sdds |
[
The distribution parameters, i.e. the
can be fixed via, e.g., If By default, |
horizon |
[ If By default, If |
period |
[ In this case, a
By default, |
data |
[ The
Either none, all, or selected elements can be specified. Unspecified parameters are set to their default values, see below. Specifications in |
file |
[ If Alternatively, it can be a |
date_column |
[ If By default, |
data_column |
[ If By default, |
from |
[ |
to |
[ |
logreturns |
[ If By default, |
merge |
[ In this case, a
|
fit |
[ The
Either none, all, or selected elements can be specified. Unspecified parameters are set to their default values, see below. Specifications in |
runs |
[ By default, |
origin |
[ In this case, a By default, |
accept |
[ By default, |
gradtol |
[ By default, |
iterlim |
[ By default, |
print.level |
[ By default, |
steptol |
[ By default, |
x, object
|
[ |
... |
Currently not used. |
See the vignette on controls for more details.
An object of class fHMM_controls, which is a list that contains
model and estimation specifications.
# 2-state HMM with t-distributions for simulated data set_controls( states = 2, # the number of states sdds = "t", # the state-dependent distribution runs = 50 # the number of optimization runs ) # 3-state HMM with normal distributions for the DAX closing prices set_controls( states = 3, sdds = "normal", file = download_data("^GDAXI"), # the data set date_column = "Date", # the column with the dates data_column = "Close" # the column with the data ) # hierarchical HMM with Gamma and Poisson state distributions set_controls( hierarchy = TRUE, # defines a hierarchy states = c(3, 2), # coarse scale and fine scale states sdds = c("gamma", "poisson"), # distributions for both layers horizon = c(100, NA), # 100 coarse-scale data points period = "m" # monthly simulated fine-scale data ) # hierarchical HMM with data from .csv-file set_controls( hierarchy = TRUE, states = c(3, 2), sdds = c("t", "t"), file = c( system.file("extdata", "dax.csv", package = "fHMM"), system.file("extdata", "dax.csv", package = "fHMM") ), date_column = c("Date", "Date"), data_column = c("Close", "Close"), logreturns = c(TRUE, TRUE) )# 2-state HMM with t-distributions for simulated data set_controls( states = 2, # the number of states sdds = "t", # the state-dependent distribution runs = 50 # the number of optimization runs ) # 3-state HMM with normal distributions for the DAX closing prices set_controls( states = 3, sdds = "normal", file = download_data("^GDAXI"), # the data set date_column = "Date", # the column with the dates data_column = "Close" # the column with the data ) # hierarchical HMM with Gamma and Poisson state distributions set_controls( hierarchy = TRUE, # defines a hierarchy states = c(3, 2), # coarse scale and fine scale states sdds = c("gamma", "poisson"), # distributions for both layers horizon = c(100, NA), # 100 coarse-scale data points period = "m" # monthly simulated fine-scale data ) # hierarchical HMM with data from .csv-file set_controls( hierarchy = TRUE, states = c(3, 2), sdds = c("t", "t"), file = c( system.file("extdata", "dax.csv", package = "fHMM"), system.file("extdata", "dax.csv", package = "fHMM") ), date_column = c("Date", "Date"), data_column = c("Close", "Close"), logreturns = c(TRUE, TRUE) )
A pre-computed 2-state HMM with state-dependent gamma distributions with
means fixed to 0.5 and 2 on 500 simulated observations.
data("sim_model_2gamma")data("sim_model_2gamma")
An object of class fHMM_model.
The model was estimated via:
controls <- set_controls( states = 2, sdds = "gamma(mu = 1|2)", horizon = 200, runs = 10 ) pars <- fHMM_parameters( controls = controls, Gamma = matrix(c(0.9, 0.2, 0.1, 0.8), nrow = 2), sigma = c(0.5, 1), seed = 1 ) data_sim <- prepare_data(controls, true_parameters = pars, seed = 1) sim_model_2gamma <- fit_model(data_sim, seed = 1) sim_model_2gamma <- decode_states(sim_model_2gamma) sim_model_2gamma <- compute_residuals(sim_model_2gamma) summary(sim_model_2gamma)
This helper function simulates HMM data.
simulate_hmm( controls = list(), hierarchy = FALSE, states = if (!hierarchy) 2 else c(2, 2), sdds = if (!hierarchy) "normal" else c("normal", "normal"), horizon = if (!hierarchy) 100 else c(100, 30), period = NA, true_parameters = fHMM_parameters(controls = controls, hierarchy = hierarchy, states = states, sdds = sdds), seed = NULL )simulate_hmm( controls = list(), hierarchy = FALSE, states = if (!hierarchy) 2 else c(2, 2), sdds = if (!hierarchy) "normal" else c("normal", "normal"), horizon = if (!hierarchy) 100 else c(100, 30), period = NA, true_parameters = fHMM_parameters(controls = controls, hierarchy = hierarchy, states = states, sdds = sdds), seed = NULL )
controls |
[ The
Either none, all, or selected elements can be specified. Unspecified parameters are set to their default values. Important: Specifications in |
hierarchy |
[ If By default, |
states |
[ If By default, |
sdds |
[
The distribution parameters, i.e. the
can be fixed via, e.g., If By default, |
horizon |
[ If By default, If |
period |
[ In this case, a
By default, |
true_parameters |
[ |
seed |
[ |
A list containing the following elements:
time_points, the vector (or matrix in the
hierarchical case) of time points,
markov_chain, the vector (or matrix in the
hierarchical case) of the simulated states,
data, the vector (or matrix in the hierarchical
case) of the simulated state-dependent observations,
T_star, the numeric vector of fine-scale chunk sizes
in the hierarchical case
simulate_hmm(states = 2, sdds = "normal", horizon = 10)simulate_hmm(states = 2, sdds = "normal", horizon = 10)
Standard & Poor’s 500 (S&P 500) index data from 1928 to 2022 from Yahoo Finance.
spxspx
A data.frame with 23864 rows and the following 7 columns:
Date: The date.
Open: Opening price.
High: Highest price.
Low: Lowest price.
Close: Close price adjusted for splits.
Adj.Close: Close price adjusted for dividends and splits.
Volume: Trade volume.
The data was obtained via:
spx <- download_data( symbol = "^GSPC", # S&P 500 identifier on Yahoo Finance from = "1928-01-01", # first observation to = "2022-12-31" # last observation )
The monthly unemployment rate in the USA from 1955 to 2022 on a daily observation basis.
unempunemp
A data.frame with 24806 rows and the following 3 columns:
date: The date.
rate: The unemployment rate.
rate_diff: The difference rate to previous month.
OECD (2023), Unemployment rate (indicator). doi: 10.1787/52570002-en (Accessed on 18 January 2023) https://www.oecd.org/en/data/indicators/unemployment-rate.html
A pre-computed HHMM with monthly unemployment rate in the US on the coarse scale using 3 states and S&P 500 index data on the fine scale using 2 states from 1970 to 2020 for demonstration purposes.
data("unemp_spx_model_3_2")data("unemp_spx_model_3_2")
An object of class fHMM_model.
The model was estimated via:
controls <- list(
hierarchy = TRUE, states = c(3, 2),
sdds = c("t", "t"), period = "m",
data = list(
file = list(unemp, spx),
data_column = c("rate_diff", "Close"),
date_column = c("date", "Date"),
from = "1970-01-01", to = "2020-01-01",
logreturns = c(FALSE, TRUE)
),
fit = list(runs = 50, iterlim = 1000, gradtol = 1e-6, steptol = 1e-6)
)
controls <- set_controls(controls)
unemp_spx_data <- prepare_data(controls)
unemp_spx_model_3_2 <- fit_model(unemp_spx_data, seed = 1, ncluster = 10)
unemp_spx_model_3_2 <- decode_states(unemp_spx_model_3_2)
unemp_spx_model_3_2 <- compute_residuals(unemp_spx_model_3_2)
summary(unemp_spx_model_3_2)
state_order <- matrix(c(3, 2, 1, 2, 2, 2, 1, 1, 1), 3, 3)
unemp_spx_model_3_2 <- reorder_states(unemp_spx_model_3_2, state_order)
Volkswagen AG (VW) stock data from 1998 to 2022 from Yahoo Finance.
vwvw
A data.frame with 6260 rows and the following 7 columns:
Date: The date.
Open: Opening price.
High: Highest price.
Low: Lowest price.
Close: Close price adjusted for splits.
Adj.Close: Close price adjusted for dividends and splits.
Volume: Trade volume.
The data was obtained via:
vw <- download_data( symbol = "VOW3.DE", # Volkswagen AG identifier on Yahoo Finance from = "1988-07-22", # first observation to = "2022-12-31" # last observation )