Fit GJR-GARCH(1,1) model using rstan
library(rstan)
## Loading required package: StanHeaders
##
## rstan version 2.32.7 (Stan version 2.32.2)
## For execution on a local, multicore CPU with excess RAM we recommend calling
## options(mc.cores = parallel::detectCores()).
## To avoid recompilation of unchanged Stan programs, we recommend calling
## rstan_options(auto_write = TRUE)
## For within-chain threading using `reduce_sum()` or `map_rect()` Stan functions,
## change `threads_per_chain` option:
## rstan_options(threads_per_chain = 1)
## Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file
library(rstudioapi)
mydata <- list( n = n, y = return);
myinit <- function() {
list( omega = 0.1, alpha = 0.1, beta = 0.1, mu = 0.0, gamma = 0.1)
}
fit <- stan(file = 'gjr-garch.stan', data = mydata, init = myinit,
chains = 4, warmup = 1000, iter = 11000, thin=10, cores = parallel::detectCores())
## WARNING: Rtools is required to build R packages, but is not currently installed.
##
## Please download and install the appropriate version of Rtools for 4.6.0 from
## https://cran.r-project.org/bin/windows/Rtools/.
## Trying to compile a simple C file
## Running "C:/PROGRA~1/R/R-46~1.0/bin/x64/Rcmd.exe" SHLIB foo.c
## using C compiler: 'gcc.exe (GCC) 14.2.0'
## gcc -I"C:/PROGRA~1/R/R-46~1.0/include" -DNDEBUG -I"C:/Users/omori/AppData/Local/R/win-library/4.6/Rcpp/include/" -I"C:/Users/omori/AppData/Local/R/win-library/4.6/RcppEigen/include/" -I"C:/Users/omori/AppData/Local/R/win-library/4.6/RcppEigen/include/unsupported" -I"C:/Users/omori/AppData/Local/R/win-library/4.6/BH/include" -I"C:/Users/omori/AppData/Local/R/win-library/4.6/StanHeaders/include/src/" -I"C:/Users/omori/AppData/Local/R/win-library/4.6/StanHeaders/include/" -I"C:/Users/omori/AppData/Local/R/win-library/4.6/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/omori/AppData/Local/R/win-library/4.6/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include "C:/Users/omori/AppData/Local/R/win-library/4.6/StanHeaders/include/stan/math/prim/fun/Eigen.hpp" -std=c++1y -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -std=gnu2x -mfpmath=sse -msse2 -mstackrealign -c foo.c -o foo.o
## cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
## In file included from C:/Users/omori/AppData/Local/R/win-library/4.6/RcppEigen/include/Eigen/Core:19,
## from C:/Users/omori/AppData/Local/R/win-library/4.6/RcppEigen/include/Eigen/Dense:1,
## from C:/Users/omori/AppData/Local/R/win-library/4.6/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
## from <command-line>:
## C:/Users/omori/AppData/Local/R/win-library/4.6/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: cmath: No such file or directory
## 679 | #include <cmath>
## | ^~~~~~~
## compilation terminated.
## make: *** [C:/PROGRA~1/R/R-46~1.0/etc/x64/Makeconf:297: foo.o] Error 1
## WARNING: Rtools is required to build R packages, but is not currently installed.
##
## Please download and install the appropriate version of Rtools for 4.6.0 from
## https://cran.r-project.org/bin/windows/Rtools/.
param <-c("omega","alpha","beta", "mu", "gamma")
print(fit, pars = param, digits=3)
## Inference for Stan model: anon_model.
## 4 chains, each with iter=11000; warmup=1000; thin=10;
## post-warmup draws per chain=1000, total post-warmup draws=4000.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
## omega 0.215 0.001 0.050 0.125 0.180 0.214 0.247 0.315 4066 1.000
## alpha 0.031 0.000 0.022 0.001 0.014 0.027 0.043 0.082 3894 0.999
## beta 0.670 0.001 0.057 0.553 0.631 0.669 0.710 0.777 3951 1.000
## mu 0.045 0.000 0.027 -0.008 0.027 0.044 0.064 0.100 4019 0.999
## gamma 0.244 0.001 0.045 0.162 0.213 0.241 0.273 0.342 3739 1.000
##
## Samples were drawn using NUTS(diag_e) at Sat May 30 16:08:29 2026.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at
## convergence, Rhat=1).
stan_dens(fit, pars = param, nrow = 3, ncol = 2, separate_chains = TRUE)

stan_trace(fit, pars = param, nrow = 3, ncol = 2)

stan_ac(fit, pars = param, nrow = 3, ncol = 2, separate_chains = TRUE)
## Warning in (function (mapping = NULL, data = NULL, stat = "count", position =
## "stack", : Ignoring unknown parameters: `size`

mcmcout <- rstan::extract(fit)
nparam <- length(param)
cat("AIC=",-2*max(mcmcout$loglik)+2*nparam,"BIC=",-2*max(mcmcout$loglik)+nparam*log(n))
## AIC= 3870.351 BIC= 3896.274