This function defines and validates specifications for model estimation.
Usage
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, ...)Arguments
- controls
Either a
listor an object of classfHMM_controls.The
listcan contain the following elements, which are described in more detail below:hierarchy, defines an hierarchical HMM,states, defines the number of states,sdds, defines the state-dependent distributions,horizon, defines the time horizon,period, defines a flexible, periodic fine-scale time horizon,data, alistof controls that define the data,fit, alistof controls that define the model fitting
Either none, all, or selected elements can be specified.
Unspecified parameters are set to their default values.
Important: Specifications in
controlsalways override individual specifications.- hierarchy
A
logical, set toTRUEfor an hierarchical HMM.If
hierarchy = TRUE, some of the other controls must be specified for the coarse-scale and the fine-scale layer.By default,
hierarchy = FALSE.- states
An
integer, the number of states of the underlying Markov chain.If
hierarchy = TRUE,statesmust be avectorof length 2. The first entry corresponds to the coarse-scale layer, while the second entry corresponds to the fine-scale layer.By default,
states = 2ifhierarchy = FALSEandstates = c(2, 2)ifhierarchy = TRUE.- sdds
A
character, specifying the state-dependent distribution. One of"normal"(the normal distribution),"lognormal"(the log-normal distribution),"t"(the t-distribution),"gamma"(the gamma distribution),"poisson"(the Poisson distribution).
The distribution parameters, i.e. the
mean
mu,standard deviation
sigma(not for the Poisson distribution),degrees of freedom
df(only for the t-distribution),
can be fixed via, e.g.,
"t(df = 1)"or"gamma(mu = 0, sigma = 1)". To fix different values of a parameter for different states, separate by "|", e.g."poisson(mu = 1|2|3)".If
hierarchy = TRUE,sddsmust be avectorof length 2. The first entry corresponds to the coarse-scale layer, while the second entry corresponds to the fine-scale layer.By default,
sdds = "normal"ifhierarchy = FALSEandsdds = c("normal", "normal")ifhierarchy = TRUE.- horizon
A
numeric, specifying the length of the time horizon.If
hierarchy = TRUE,horizonmust be avectorof length 2. The first entry corresponds to the coarse-scale layer, while the second entry corresponds to the fine-scale layer.By default,
horizon = 100ifhierarchy = FALSEandhorizon = c(100, 30)ifhierarchy = TRUE.If
datais specified (i.e., notNA), the first entry ofhorizonis ignored and the (coarse-scale) time horizon is defined by available data.- period
Only relevant if
hierarchy = TRUE.In this case, a
characterwhich specifies a flexible, periodic fine-scale time horizon and can be one of"w"for a week,"m"for a month,"q"for a quarter,"y"for a year.
By default,
period = NA. Ifperiodis notNA, it overruleshorizon[2].- data
Either
NA, in which case data is simulated (the default), or alistof controls specifying the empirical data set.The
listcan contain the following elements, which are described in more detail below:file, defines the data set,date_column, defines the date column,data_column, defines the data column,from, defines a lower date limit,to, defines an upper date limit,logreturns, defines a data transformation to log-returns,merge, defines the merging for coarse-scale observations.
Either none, all, or selected elements can be specified.
Unspecified parameters are set to their default values, see below.
Specifications in
dataoverride individual specifications.- file
A
data.framewith data and dates for modeling.If
hierarchy = TRUE,filecan be alistof length 2. The first entry is adata.frameand provides the data for the coarse-scale layer, while the second entry corresponds to the fine-scale layer. Iffileis a singledata.frame, then the samedata.frameis used for both layers.Alternatively, it can be a
character(of length two), the path to a .csv-file with financial data.- date_column
A
character, the name of the column infilewith dates.If
hierarchy = TRUEandfileis alistof twodata.frames,data_columnmust be avectorof length 2. The first entry corresponds to the coarse-scale layer, while the second entry corresponds to the fine-scale layer.By default,
date_column = "Date".- data_column
A
character, the name of the column infilewith observations.If
hierarchy = TRUE,data_columnmust be avectorof length 2. The first entry corresponds to the coarse-scale layer, while the second entry corresponds to the fine-scale layer.By default,
data_column = "Close"ifhierarchy = FALSEanddata_column = c("Close", "Close")ifhierarchy = TRUE.- from
A
characterof the format"YYYY-MM-DD", setting a lower date limit. No lower limit iffrom = NA(default).- to
A
characterof the format"YYYY-MM-DD", setting an upper date limit. No lower limit ifto = NA(default).- logreturns
A
logical, ifTRUEthe data is transformed to log-returns.If
hierarchy = TRUE,logreturnsmust be avectorof length 2. The first entry corresponds to the coarse-scale layer, while the second entry corresponds to the fine-scale layer.By default,
logreturns = FALSEifhierarchy = FALSEandlogreturns = c(FALSE, FALSE)ifhierarchy = TRUE.- merge
Only relevant if
hierarchy = TRUE.In this case, a
functionwhich merges an input numeric vector of fine-scale dataxinto one coarse-scale observation. For example,merge = function(x) mean(x)(default) defines the mean of the fine-scale data as the coarse-scale observation,merge = function(x) mean(abs(x))for the mean of the absolute values,merge = function(x) sum(abs(x))for the sum of the absolute values,merge = function(x) (tail(x, 1) - head(x, 1)) / head(x, 1)for the relative change of the first to the last fine-scale observation.
- fit
A
listof controls specifying the model fitting.The
listcan contain the following elements, which are described in more detail below:runs, defines the number of numerical optimization runs,origin, defines initialization at the true parameters,accept, defines the set of accepted optimization runs,gradtol, defines the gradient tolerance,iterlim, defines the iteration limit,print.level, defines the level of printing,steptol, defines the minimum allowable relative step length.
Either none, all, or selected elements can be specified.
Unspecified parameters are set to their default values, see below.
Specifications in
fitoverride individual specifications.- runs
An
integer, setting the number of randomly initialized optimization runs of the model likelihood from which the best one is selected as the final model.By default,
runs = 10.- origin
Only relevant for simulated data, i.e., if the
datacontrol isNA.In this case, a
logical. Iforigin = TRUEthe optimization is initialized at the true parameter values. This setsrun = 1andaccept = 1:5.By default,
origin = FALSE.- accept
An
integer(vector), specifying which optimization runs are accepted based on the output code ofnlm.By default,
accept = 1:3.- gradtol
A positive
numericvalue, specifying the gradient tolerance, passed on tonlm.By default,
gradtol = 0.01.- iterlim
A positive
integervalue, specifying the iteration limit, passed on tonlm.By default,
iterlim = 100.- print.level
One of
0,1, and2to control the verbosity of the numerical likelihood optimization, passed on tonlm.By default,
print.level = 0.- steptol
A positive
numericvalue, specifying the step tolerance, passed on tonlm.By default,
gradtol = 0.01.- x, object
An object of class
fHMM_controls.- ...
Currently not used.
Value
An object of class fHMM_controls, which is a list that contains
model and estimation specifications.
Details
See the vignette on controls for more details.
Examples
# 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
)
#> fHMM controls:
#> * hierarchy: FALSE
#> * data type: simulated
#> * number of states: 2
#> * sdds: t()
#> * number of runs: 50
# 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
)
#> fHMM controls:
#> * hierarchy: FALSE
#> * data type: empirical
#> * number of states: 3
#> * sdds: normal()
#> * number of runs: 10
# 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 simulated coarse-scale data points
period = "m" # monthly simulated fine-scale data
)
#> fHMM controls:
#> * hierarchy: TRUE
#> * data type: simulated
#> * number of states: 3 2
#> * sdds: gamma() poisson()
#> * number of runs: 10
# 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)
)
#> fHMM controls:
#> * hierarchy: TRUE
#> * data type: empirical
#> * number of states: 3 2
#> * sdds: t() t()
#> * number of runs: 10
