loelschlaeger / fHMM

Hidden Markov models for finance
https://loelschlaeger.de/fHMM/
GNU General Public License v3.0
16 stars 8 forks source link

period="m" not work properly when fitting HHMM model #93

Closed dongsen86 closed 3 months ago

dongsen86 commented 3 months ago

In your package example 3 (your code below), i.e. unemp_spx_model_3_2, period="m" is masked by default horizon value c(NA,30) ,so the fine scale dimension is 30 not "m", T-star is a list (30,30,30.........), not as expected like (19,20,22,23,......). If I set horizon=c(NA,NA), period="m"(or "w" "q" "y") works well, T-star is computed as expected, prepare_data() also works well, but error "Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)" is reported due to NA value existed in fhmm model data.

This issue also occurs when I try to replicate your paper " Detecting bearish and bullish markets in financial time series using hierarchical hidden Markov models"(Statistical Modelling 2023; 23(2): 107–126). In section "3.2 DeutscheBank", 5250 daily DAX data should be merged by month to 256 coarse scale observations (as presented in section "3.3 Simulation study"), but since period="m" not work, the coarse scale observation is reduce to 175(5250/30), or "Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)" is reported when period="m" works (horizon=c(NA,NA)), so the result can not be replicated (when the fine scale dimension is 30 (even 22) not "m", the result is significantly different from the result in the paper).

Thank you very much if you can solve this issue.

Example 3: Multiple data streams ----------------------------------------

define and set controls

contr_hhmm <- list( hierarchy = TRUE, states = c(3, 2), sdds = c("t", "t"), period = "m", #not work when horizon is default 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)) contr_hhmm <- set_controls(contr_hhmm)

prepare data

data_hhmm <- prepare_data(contr_hhmm)

fit the model

unemp_spx_model_3_2 <- fit_model(data_hhmm, seed = 1, ncluster = 25) ####report error "Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)" when horzion=(NA,NA) and period="m" works

loelschlaeger commented 3 months ago

Thanks for making us aware of this issue, I will check it ASAP and come back to you then.

loelschlaeger commented 3 months ago

Fixed the issue, available soon as version 1.4.0 on CRAN. Thanks again, @dongsen86 !