keblu / msgarch Goto Github PK
View Code? Open in Web Editor NEWMSGARCH R Package
Home Page: https://cran.r-project.org/web/packages/MSGARCH/index.html
MSGARCH R Package
Home Page: https://cran.r-project.org/web/packages/MSGARCH/index.html
For instance, sp500 is a matrix, SMI not, it is an object y.
So we need to have SP500, SMI, etc. all upper case letters, and directly these as objects, no y hidden
We need to improve the speed; slow especially when MCMC are used
I found no convergence when i run this program. just by replacing "mle" with "bayes" i found no results, even i have tried it on fast computer.
ctr.bay=list(N.burn=500, N.mcmc=10000, N.thin=10, do.enhance.theta0 = TRUE)
fit = MSGARCH::fit.bayes(spec = spec, y = sp500, ctr = ctr.bay)
risk.bay = MSGARCH::risk(object = fit, level = c(0.95), ES = FALSE, do.its = TRUE)
kindly guide me.
I think ht should return the conditional variance, not vol
Sorry if this is not the place for asking questions. I'd like to know if it is possible to indicate the lags, for example, ARCH(1) or ARCH(2), GARCH(1,1) or GARCH(1,2) in the model selections? It seems that the arguments, 'sARCH', 'sGARCH' etc. fix the parameters to be ARCH(1) and GARCH(1,1), but there are no other arguments controlling the lags.
I just realized that the check function gives us warnings when S3 method are not consistent in parameters:
cdf(spec, x, theta, y, log, is.its) : call cdf_MSGARCH_SPEC
cdf(fit, x, log, is.its) : call cdf.MSGARCH_MLE_FIT or cdf.MSGARCH_BAY_FIT
will give a warning. I don't know if there is a way to suppress the warning without splitting the function in 2 (e.g., cdf_fit, cdf_spec)
We should use parameter labels.
which(grepl("nu", spec$label))
Fix multiple outputs
I found an error when using MCMC as the estimation method.
Error in { : task 1 failed - "chol(): decomposition failed"
f_RCPP_adaptMCMC(theta0 = par0
#version infomation
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936
[2] LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] parallel stats graphics grDevices utils datasets
[7] methods base
other attached packages:
[1] PerformanceAnalytics_1.4.3541 xts_0.9-7
[3] zoo_1.8-0 doRNG_1.6.6
[5] rngtools_1.2.4 pkgmaker_0.22
[7] registry_0.3 doParallel_1.0.11
[9] doSNOW_1.0.15 snow_0.4-2
[11] iterators_1.0.8 foreach_1.4.3
[13] MSGARCH_1.2
loaded via a namespace (and not attached):
[1] Rcpp_0.12.13 magrittr_1.5 fanplot_3.4.1
[4] xtable_1.8-2 lattice_0.20-34 stringr_1.2.0
[7] tools_3.3.3 grid_3.3.3 digest_0.6.12
[10] Matrix_1.2-8 codetools_0.2-15 stringi_1.1.5
[13] compiler_3.3.3 expm_0.999-2
multi_day_VaR <- function(dur) {
library(MSGARCH)
data_est = All_dataset[c(dur),2]
data_est = as.numeric(as.character(data_est))
spec <- CreateSpec(variance.spec = list(model = c("gjrGARCH")),
distribution.spec = list(distribution = c("sstd")),
#distribution.spec = list(distribution = c("snorm")),
switch.spec = list(do.mix = FALSE, K = 2))
ctr.bay = list(n.burn = 5000L, n.mcmc = 10000L, n.thin = 10L)
out.bay = MSGARCH::FitMCMC(spec = spec, data = data_est, ctr = ctr.bay)
sim = Sim(object = out.bay, n.ahead = 250, n.sim = 1)
ret = t(apply(sim$draw, MARGIN = 2,FUN = cumsum))
VaR = matrix(0,20,1)#1,5,10,20 days
VaR[1,1] = quantile(ret[,1],probs = c(0.01)) # daily
VaR[2,1] = quantile(ret[,5],probs = c(0.01)) # weekly
VaR[3,1] = quantile(ret[,20],probs = c(0.01)) # monthly
VaR[4,1] = quantile(ret[,60],probs = c(0.01)) # quarterly
VaR[5,1] = quantile(ret[,250],probs = c(0.01)) # yearly
VaR[6,1] = quantile(ret[,1],probs = c(0.05)) # daily
VaR[7,1] = quantile(ret[,5],probs = c(0.05)) # weekly
VaR[8,1] = quantile(ret[,20],probs = c(0.05)) # monthly
VaR[9,1] = quantile(ret[,60],probs = c(0.05)) # quarterly
VaR[10,1] = quantile(ret[,250],probs = c(0.05)) # yearly
#for n day ahead return
VaR[11,1] = quantile(sim$draw[1,],probs = c(0.01))
VaR[12,1] = quantile(sim$draw[5,],probs = c(0.01))
VaR[13,1] = quantile(sim$draw[20,],probs = c(0.01))
VaR[14,1] = quantile(sim$draw[60,],probs = c(0.01))
VaR[15,1] = quantile(sim$draw[250,],probs = c(0.01))
VaR[16,1] = quantile(sim$draw[1,],probs = c(0.05))
VaR[17,1] = quantile(sim$draw[5,],probs = c(0.05))
VaR[18,1] = quantile(sim$draw[20,],probs = c(0.05))
VaR[19,1] = quantile(sim$draw[60,],probs = c(0.05))
VaR[20,1] = quantile(sim$draw[250,],probs = c(0.05))
VaR
}
#-----------------------------------------------------
#install.packages("doRNG")
#install.packages("doSNOW")
library(foreach)
library(doSNOW)
library(doParallel)
library(doRNG)
start.time <- Sys.time()
#setup parallel backend to use many processors
cl <- makeCluster(5, outfile="") #not to overload your computer
registerDoSNOW(cl)
#parallel foreach loop
N=1
set.seed(123)
data_startpoint = 82
VaR_matrix = matrix(0,20,max(N))
VaR_matrix_MS = matrix(0,20,max(N))
tempMatrix = matrix(0,20,max(N))
############################################################
VaR_matrix_MS <- foreach(i = 1:N, .combine = 'cbind', .verbose=T ,.multicombine=T ) %dorng% {
set.seed(123)
dur = (data_startpoint+i-1):(data_startpoint+1500+i-2)
multi_day_VaR(dur)
}
#stop cluster
stopCluster(cl)
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
I am trying to fit a 3 regime MS GARCH model using the following specification:
spec <- CreateSpec(variance.spec = list(model = c("sGARCH")), distribution.spec = list(distribution = c("std")), switch.spec = list( K = 3),constraint.spec = list(regime.const = c("nu")))
The MLE estimation works find but when I am using MCMC the following error is printed:
Error in f_RCPP_adaptMCMC(theta0 = par0, acc_rate = 0.25, sigma = diag(length(par0)), :
Evaluation error: 'names' attribute [17] must be the same length as the vector [16].
This error has something to do with the constraint specification because when I remove it, it works fine.
Can you kindly help.
@keblu
I am trying to fit an MS GARCH model on a vector y, however when the loop below reaches i=3 I get this R Error: $ operator is invalid for atomic vectors. The code I am using is the following:
library(MSGARCH)
fitmlnorm <- list()
for (i in 1:3)
{
spec <- CreateSpec(variance.spec = list(model = c("sGARCH")), distribution.spec = list(distribution = c("norm")), switch.spec = list(K = i))
fitmlnorm[[i]]<-FitML(spec, y)
}
the numeric data of y can be found here:
link
From testing it out, the problem seems to be with the data y as with other vectors y the loop works without incident e.g. for the following vector:
link
Suppose we have already fitted a model using the fit.bayes function. How can we carry out out-sample forecasting of the volatility using the model fitted, i.e the value of the conditional variance at n step ahead based on the model estimated?
We should have the ... arguments available in all plotting functions. This way, we can change the axes, the labels, etc.
Probably in the mle or find.theta0 function; should fix this
Is it possible that you give a clear distinction between the function predict() and PredPdf().
What are each function predicting exactly in mathematical terms and maybe you can provide some example. I had a look at some presentations you gave regarding the package and also at the paper you publish but still it is all very confusing.
Thank you in advance
This should display the help of MSGARCH
How do you carry out out-sample forecasting using a fit.bayes object? I presume it is using the simahead function and if so how can the output be interpreted?
This should work for vector as inputs
Hi keblu!I am using your package fitting a markov switching Garch model and I want to compare this model with a Garch(1,1) model. So I want to know how to plot the msGARCH model on sample ponit? I'm looking forward to your reply,thank you!
I had this question when I tried to import the code:
spec<- CreateSpec(variance.spec = list(model = c("sGARCH")),distribution.spec = list(distribution = c("std")),switch.spec = list(do.mix = FALSE, K = 3))
data("SMI", package = "MSGARCH")
fit <- FitML(spec = spec, data = SMI)
state <- State(object = fit)
plot(state, type.prob = "smoothed")
but I only get "the smoothed probability for state3",I also want to get the plot for state2 and state1.How could I get them?
Please help me!Thank you~
For weird reasons that I still don't know, at random time, the C++ object functionalities (tested on unc.vol) would only output NA or a random very low number such as 1e-254, but when the C++ object was recreated (via create.spec) the object would behave normally. This was happening randomly so I have implemented a work-around where the object would be recreated until the funcitonallities would behave normally (when this happen, we only need to recreate the object once). This was also the reason why f.enhance.theta0 would sometime output an error with uniroot.
pow and log function does not accept an int as first argument in Solaris. Need to cast int to long or double.
In earlier version of MSGARCH, when we estimate through ML approach, Differential Evolution algorithm was available, but now there is BFGS and others.
I have had use MSGARCH package for the estimation in my thesis, with Differential Evolution. On the bases of this package, I set my objective and significance of thesis.
We should have a unified framework for inputs/output in Rcpp, while keeping a unified framework in R (e.g., matrix inputs for Rcpp but vectors in R). We keep this of GSOC 2017
Dear Prof. Keven,
My name is André. I am professor of Economics at Federal University of Paraíba, Brazil.
I am trying to estimate a MSGARCH model by using weekly inflation rate from Brazilian economy during the last 13 yeas and compare it with the traditional GARCH(p,q) specification to find the best fit and a well defined measure of volatility persistence.
I am using your package 'MSGARCH' in R to this end and make some simulations.
The problem I am facing is that I cannot find the standard errors of the coefficients generated my 'fit.mle' function of the package.
I am doing the following:
library(fracdiff)
z <- diff(ipcs)
spec = MSGARCH::create.spec()
set.seed(123)
fit = MSGARCH::fit.mle(spec = spec, y = z)
names(fit) # where are the standard errors?
print(fit) # I cannot find the stardard errors for:
# alpha0_1 alpha1_1
# beta_1 alpha0_2 alpha1_2 beta_2
Please, see the results presented at p. 509 (Table 2) by Hass et al (2004) 'A New Approach to Markov-Switching GARCH models', Journal of Financial Econometrics, 2004, vol. 2(4): 493-530.
Can you help me in find out that standard errors?
Best regards,
André.
Given the fit,bayes option, how can I use the output to calculate Bayes Factors (rather than using the approximation BF = log(BIC)) for model comparisons?
Thank you for building and maintaining this excellent package. I'm using it for my PhD, and I noticed a possible bug. A summary of a fitted model (with fit.mle) shows AIC < -2LL for single regime GARCH estimates, e.g. LL = -235.4 and AIC = 467. I tried to debug the AIC calculation, and it calls a function f.AIC, which calculates the traditional AIC = 2k - 2LL. But since the summary shows AIC < -2LL, the likelihoods must be from different sources. Indeed, the f.AIC function calculates the LL from the pdf of the fitted model, while, for example, the kernel function simply extracts the LL from the fitted model, which could explain the difference. It is my understanding that the kernel function is used to maximize the LL, which would imply the AIC calculation is off. If so, this bug perhaps also extends to other information criteria.
Finally, I'm using R 3.3.2 and the latest version of MSGARCH on CRAN 0.17.7. Please let me know if you need any other information.
I have to plots the MCMC of bayesian, along with it i need these values as well for further analysis. In old version of MSGARCH I was able to do these steps but now it is hard for me.
We need to get rid of the looping for the spec. Still, we need to see why sometimes we get wired instanciations of the spec
When I use MLE method as estimation method(), I suffered an error. When I run all process without function, I can get a numerical result. But when I run it as a function, the output is an error. Error in eigen(mNegHessian) : infinite or missing values in 'x'
Could you please tell me how to solve it?
// package info
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936
[2] LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] parallel stats graphics grDevices utils datasets
[7] methods base
other attached packages:
[1] GAS_0.2.4 DEoptim_2.2-4
[3] doParallel_1.0.11 doSNOW_1.0.15
[5] snow_0.4-2 iterators_1.0.8
[7] foreach_1.4.3 PerformanceAnalytics_1.4.3541
[9] xts_0.9-7 zoo_1.8-0
[11] MSGARCH_1.1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.13 lattice_0.20-34 codetools_0.2-15
[4] truncnorm_1.0-7 MASS_7.3-45 grid_3.3.3
[7] coda_0.19-1 adaptMCMC_1.2 Matrix_1.2-8
[10] tools_3.3.3 numDeriv_2016.8-1 compiler_3.3.3
[13] fanplot_3.4.1 Rsolnp_1.16 expm_0.999-2
//Code
`multi_day_VaR <- function(est_begin, est_end) {
library(MSGARCH)
data_est = All_dataset[c(est_begin:est_end),2]#6years 1500, AIC: 3728.115
#data_est = as.numeric(as.character(data_est))
spec <- CreateSpec(variance.spec = list(model = c("gjrGARCH")),
distribution.spec = list(distribution = c("sstd")),
switch.spec = list(do.mix = FALSE, K = 2))
set.seed(123)
out.mle = MSGARCH:: FitML(spec = spec, data = data_est)
sim = Sim(object = out.mle, n.ahead = 250L, n.sim = 1500L, par = out.mle$par, n.burnin = 500L)
ret = t(apply(sim$draw, MARGIN = 2,FUN = cumsum))
VaR = matrix(0,20,1)#1,5,10,20 days
VaR[1,1] = quantile(ret[,1],probs = c(0.01)) # daily
VaR[2,1] = quantile(ret[,5],probs = c(0.01)) # weekly
VaR[3,1] = quantile(ret[,20],probs = c(0.01)) # monthly
VaR[4,1] = quantile(ret[,60],probs = c(0.01)) # quarterly
VaR[5,1] = quantile(ret[,250],probs = c(0.01)) # yearly
VaR[6,1] = quantile(ret[,1],probs = c(0.05)) # daily
VaR[7,1] = quantile(ret[,5],probs = c(0.05)) # weekly
VaR[8,1] = quantile(ret[,20],probs = c(0.05)) # monthly
VaR[9,1] = quantile(ret[,60],probs = c(0.05)) # quarterly
VaR[10,1] = quantile(ret[,250],probs = c(0.05)) # yearly
#for n day ahead return
VaR[11,1] = quantile(sim$draw[1,],probs = c(0.01))
VaR[12,1] = quantile(sim$draw[5,],probs = c(0.01))
VaR[13,1] = quantile(sim$draw[20,],probs = c(0.01))
VaR[14,1] = quantile(sim$draw[60,],probs = c(0.01))
VaR[15,1] = quantile(sim$draw[250,],probs = c(0.01))
VaR[16,1] = quantile(sim$draw[1,],probs = c(0.05))
VaR[17,1] = quantile(sim$draw[5,],probs = c(0.05))
VaR[18,1] = quantile(sim$draw[20,],probs = c(0.05))
VaR[19,1] = quantile(sim$draw[60,],probs = c(0.05))
VaR[20,1] = quantile(sim$draw[250,],probs = c(0.05))
VaR
}
`
Fix object of spec in input
New warning appear in 3.3.0 : implicit list embedding of S4 objects is deprecated.
you already guide me in generation of simulated series. Now, would it possible to repeat the process of simulation of GARCH(1,1) for lets suppose 1000 times, and then i want to estimate each series, and save the value of parameters. Would it possible?
We should allow the user to tune the MCMC scheme with the acceptance rate; i.e. pass the acceptance rate target of adaptMCMC in the controls
We should avoid student and std, but have only std naming for instance
Currently suboptimal with risk and crps functions => use f.process.ctr
Hello,
after we talked about your package at Brussel SoFiE summer school, I wanted to try out your package for an empirical application in a paper with my supervisor. Thanks a lot for me not having to implement all that stuff on my own :) 👍
However, there is a weird error when I try to use your generic functions on MSGARCH-objects after reloading them from disk:
library(MSGARCH)
# create model specification
spec <- CreateSpec()
# load data
data("SMI", package = "MSGARCH")
# fit the model on the data by ML
fit <- FitML(spec = spec, data = SMI)
summary(fit)
# predict works
predict(fit)
save(fit, file = "fit.RData")
rm(fit)
# reload from disc
load("fit.RData")
# test whether generic functions work
fit
# however, error message says
# Error in .External(list(name = "CppMethod__invoke_notvoid", address = <pointer: 0x0>, :
# NULL value passed as symbol address
# or
predict(fit)
# generates the same error
Do you have an idea what may be the cause? I am happy to help if I can!
Best,
Onno
Check with Brian best way to prceed
Summary method for specification, MLE, etc. should take into account the options() parameters. For instance, with 4 digits, should output 4 digits parameters
I am try to generate GARCH (1,1) series with simulation, but with different parameter from default parameters. For example; alpha0 = 0.01 , alpha1 = 0.33, beta1 = 0.66, nu = 2.5.
There are no standard errors for the parameter estimates. How to evaluate the significance of the parameters?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.