Skip to content

Commit

Permalink
Fixed !93 (#94)
Browse files Browse the repository at this point in the history
* Fixed a bug around the `period` control.

* Fixed date conversion to `character()`.

* refitted models
  • Loading branch information
loelschlaeger authored Aug 5, 2024
1 parent d3e9e7e commit 25d3bf6
Show file tree
Hide file tree
Showing 26 changed files with 194 additions and 157 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: fHMM
Type: Package
Title: Fitting Hidden Markov Models to Financial Data
Version: 1.3.1
Version: 1.4.0
Authors@R: c(
person(given = "Lennart",
family = "Oelschl\U00E4ger",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# fHMM 1.4.0

* Fixed a bug around the `period` control (#93, thanks to @dongsen86).

* Fixed date conversion to `character()` (thanks to Hee-Young Kim).

# fHMM 1.3.1

* Added citation to JSS paper in DESCRIPTION.
Expand Down
70 changes: 25 additions & 45 deletions R/data_and_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
#' \item \code{Volume}: Trade volume.
#' }
#'
# #' @source
# #' <https://finance.yahoo.com/quote/%5EGSPC>
#'
#' @keywords data
"spx"

Expand All @@ -45,10 +42,6 @@
#' to = "2022-12-31" # last observation
#' )
#' }
#' The data is also available as \code{.csv} file via:
#' \preformatted{
#' system.file("extdata", "dax.csv", package = "fHMM")
#' }
#'
#' @format A \code{data.frame} with 9012 rows and the following 7 columns:
#' \itemize{
Expand All @@ -61,9 +54,6 @@
#' \item \code{Volume}: Trade volume.
#' }
#'
# #' @source
# #' <https://finance.yahoo.com/quote/%5EGDAXI>
#'
#' @keywords data
"dax"

Expand Down Expand Up @@ -93,9 +83,6 @@
#' \item \code{Volume}: Trade volume.
#' }
#'
# #' @source
# #' <https://finance.yahoo.com/quote/VOW3.DE>
#'
#' @keywords data
"vw"

Expand Down Expand Up @@ -134,17 +121,17 @@
#' The model was estimated via:
#' \preformatted{
#' 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)
#' 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)
Expand Down Expand Up @@ -255,28 +242,21 @@
#' @details
#' The model was estimated via:
#' \preformatted{
#' controls <- set_controls(
#' hierarchy = TRUE,
#' states = c(3, 2),
#' sdds = c("t", "t"),
#' period = "m",
#' data = list(
#' file = list(unemp, spx),
#' date_column = c("date", "Date"),
#' data_column = c("rate_diff", "Close"),
#' 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 <- 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 = 25)
#' 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)
Expand Down
14 changes: 6 additions & 8 deletions R/fHMM_controls.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
#' available data.
#'
#' @param period
#' Only relevant if \code{hierarchy = TRUE} and \code{horizon[2] = NA}.
#' Only relevant if \code{hierarchy = TRUE}.
#'
#' In this case, a \code{character} which specifies a flexible, periodic
#' fine-scale time horizon and can be one of
Expand All @@ -100,8 +100,8 @@
#' \item \code{"y"} for a year.
#' }
#'
#' By default, \code{period = "m"} if \code{hierarchy = TRUE} and
#' \code{horizon[2] = NA}, and \code{NA} else.
#' By default, \code{period = NA}. If \code{period} is not \code{NA}, it
#' overrules \code{horizon[2]}.
#'
#' @param data
#' Either \code{NA}, in which case data is simulated (the default), or a
Expand Down Expand Up @@ -316,7 +316,7 @@ set_controls <- function(
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 = if (hierarchy && is.na(horizon[2])) "m" else NA,
period = NA,
data = NA,
file = NA,
date_column = if (!hierarchy) "Date" else c("Date", "Date"),
Expand Down Expand Up @@ -459,7 +459,7 @@ set_controls <- function(
if (!"period" %in% names(controls)) {
controls[["period"]] <- period
}

### set missing 'data' controls
if (!"data" %in% names(controls)) {
controls[["data"]] <- data
Expand Down Expand Up @@ -644,16 +644,14 @@ validate_controls <- function(controls) {
)
}
}
if (!is.na(controls[["horizon"]][2])) {

This comment has been minimized.

Copy link
@loelschlaeger

loelschlaeger Aug 5, 2024

Author Owner

removed to avoid overwriting period control #93

controls[["period"]] <- NA_character_
}
if (!is.na(controls[["period"]])) {
if (!controls[["period"]] %in% c("w", "m", "q", "y")) {
stop(
"The control 'period' must be one of 'w', 'm', 'q', 'y'.",
call. = FALSE
)
}
controls[["horizon"]][2] <- NA_integer_
}
} else {
if (!simulated) {
Expand Down
2 changes: 1 addition & 1 deletion R/get_initial_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ get_initial_values <- function(

### cluster data
cluster <- stats::kmeans(
data, centers = states, iter.max = 100, nstart = 100
data[!is.na(data)], centers = states, iter.max = 100, nstart = 100

This comment has been minimized.

Copy link
@loelschlaeger

loelschlaeger Aug 5, 2024

Author Owner

remove NA when deriving initial values using clustering #93

)$cluster

### set tpm with state persistence
Expand Down
9 changes: 9 additions & 0 deletions R/read_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ read_data <- function(controls) {
data_raw[[i]] <- data_raw[[i]][!is.na(data_raw[[i]][[date_column[i]]]), ]
}
}

### transform dates to characters
for (i in 1:ifelse(controls[["hierarchy"]], 2, 1)) {
if (!is.na(date_column[i])) {
data_raw[[i]][[date_column[i]]] <- as.character(
oeli::check_date(data_raw[[i]][[date_column[i]]])
)
}
}

### replace NA values by neighbor means
for (i in 1:ifelse(controls[["hierarchy"]], 2, 1)) {
Expand Down
2 changes: 1 addition & 1 deletion R/simulate_hmm.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ simulate_hmm <- function(
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 = if (hierarchy && is.na(horizon[2])) "m" else NA,
period = NA,
true_parameters = fHMM_parameters(
controls = controls, hierarchy = hierarchy, states = states, sdds = sdds
),
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,20 @@ summary(model)
#>
#> Estimates:
#> lb estimate ub
#> Gamma_2.1 2.747e-03 5.024e-03 9.133e-03
#> Gamma_3.1 2.080e-13 2.060e-13 2.029e-13
#> Gamma_1.2 1.006e-02 1.839e-02 3.337e-02
#> Gamma_3.2 1.516e-02 2.446e-02 3.924e-02
#> Gamma_1.3 2.250e-11 2.232e-11 2.198e-11
#> Gamma_2.3 1.195e-02 1.898e-02 2.995e-02
#> Gamma_2.1 2.754e-03 5.024e-03 9.110e-03
#> Gamma_3.1 2.808e-16 2.781e-16 2.739e-16
#> Gamma_1.2 1.006e-02 1.839e-02 3.338e-02
#> Gamma_3.2 1.514e-02 2.446e-02 3.927e-02
#> Gamma_1.3 5.596e-17 5.549e-17 5.464e-17
#> Gamma_2.3 1.196e-02 1.898e-02 2.993e-02
#> mu_1 -3.862e-03 -1.793e-03 2.754e-04
#> mu_2 -7.994e-04 -2.649e-04 2.696e-04
#> mu_3 9.642e-04 1.272e-03 1.579e-03
#> sigma_1 2.354e-02 2.586e-02 2.840e-02
#> sigma_2 1.226e-02 1.300e-02 1.380e-02
#> sigma_2 1.225e-02 1.300e-02 1.380e-02
#> sigma_3 5.390e-03 5.833e-03 6.312e-03
#> df_1 5.551e+00 1.084e+01 2.115e+01
#> df_2 6.814e+00 4.866e+01 3.475e+02
#> df_1 5.550e+00 1.084e+01 2.116e+01
#> df_2 6.785e+00 4.866e+01 3.489e+02
#> df_3 3.973e+00 5.248e+00 6.934e+00
#>
#> States:
Expand All @@ -156,7 +156,7 @@ summary(model)
#>
#> Residuals:
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> -3.517897 -0.664017 0.012171 -0.003261 0.673180 3.693577
#> -3.517900 -0.664018 0.012170 -0.003262 0.673180 3.693568
```

Having estimated the model, we can visualize the state-dependent
Expand Down Expand Up @@ -262,7 +262,7 @@ optimization <- nlm(
)

(estimate <- optimization$estimate)
#> [1] -3.4633918 -3.4406564 0.0599985 1.0645290 0.1151781 1.0794625
#> [1] -3.46338992 -3.44065582 0.05999848 1.06452907 0.11517811 1.07946252
```

6. To interpret the estimate, it needs to be back transformed to the
Expand All @@ -282,8 +282,8 @@ par$Gamma
``` r
estimate$Gamma
#> state_1 state_2
#> state_1 0.96895127 0.03104873
#> state_2 0.03037199 0.96962801
#> state_1 0.96895125 0.03104875
#> state_2 0.03037204 0.96962796
```

``` r
Expand Down
Binary file modified data/dax_model_2n.rda
Binary file not shown.
Binary file modified data/dax_model_3t.rda
Binary file not shown.
Binary file modified data/dax_vw_model.rda
Binary file not shown.
Binary file modified data/sim_model_2gamma.rda
Binary file not shown.
Binary file modified data/unemp_spx_model_3_2.rda
Binary file not shown.
4 changes: 0 additions & 4 deletions man/dax.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions man/dax_model_2n.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified man/figures/README-plot_model_fit-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-plot_model_fit-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-plot_model_residuals-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-simulate-hmm-data-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions man/set_controls.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/simulate_hmm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 25d3bf6

Please sign in to comment.