### Example 1: N(0,1) innovations ##############################################
## Generate data from a GARCH(1,1) with N(0,1) innovations
library(rugarch)
uspec <- ugarchspec(variance.model = list(model = "sGARCH",
garchOrder = c(1, 1)),
distribution.model = "norm",
mean.model = list(armaOrder = c(0, 0)),
fixed.pars = list(mu = 0,
omega = 0.1, # alpha_0
alpha1 = 0.2, # alpha_1
beta1 = 0.3)) # beta_1
X <- ugarchpath(uspec, n.sim = 1e4, rseed = 271) # sample (set.seed() fails!)
X.t <- as.numeric(X@path$seriesSim) # actual path (X_t)
## Fitting via ugarchfit()
uspec. <- ugarchspec(variance.model = list(model = "sGARCH",
garchOrder = c(1, 1)),
distribution.model = "norm",
mean.model = list(armaOrder = c(0, 0)))
fit <- ugarchfit(uspec., data = X.t)
coef(fit) # fitted mu, alpha_0, alpha_1, beta_1
Z <- fit@fit$z # standardized residuals
stopifnot(all.equal(mean(Z), 0, tol = 1e-2),
all.equal(var(Z), 1, tol = 1e-3))
## Fitting via fit_GARCH_11()
fit. <- fit_GARCH_11(X.t)
fit.$coef # fitted alpha_0, alpha_1, beta_1
Z. <- fit.$Z.t # standardized residuals
stopifnot(all.equal(mean(Z.), 0, tol = 5e-3),
all.equal(var(Z.), 1, tol = 1e-3))
## Compare
stopifnot(all.equal(fit.$coef, coef(fit)[c("omega", "alpha1", "beta1")],
tol = 5e-3, check.attributes = FALSE)) # fitted coefficients
summary(Z. - Z) # standardized residuals
### Example 2: t_nu(0, (nu-2)/nu) innovations ##################################
## Generate data from a GARCH(1,1) with t_nu(0, (nu-2)/nu) innovations
uspec <- ugarchspec(variance.model = list(model = "sGARCH",
garchOrder = c(1, 1)),
distribution.model = "std",
mean.model = list(armaOrder = c(0, 0)),
fixed.pars = list(mu = 0,
omega = 0.1, # alpha_0
alpha1 = 0.2, # alpha_1
beta1 = 0.3, # beta_1
shape = 4)) # nu
X <- ugarchpath(uspec, n.sim = 1e4, rseed = 271) # sample (set.seed() fails!)
X.t <- as.numeric(X@path$seriesSim) # actual path (X_t)
## Fitting via ugarchfit()
uspec. <- ugarchspec(variance.model = list(model = "sGARCH",
garchOrder = c(1, 1)),
distribution.model = "std",
mean.model = list(armaOrder = c(0, 0)))
fit <- ugarchfit(uspec., data = X.t)
coef(fit) # fitted mu, alpha_0, alpha_1, beta_1, nu
Z <- fit@fit$z # standardized residuals
stopifnot(all.equal(mean(Z), 0, tol = 1e-2),
all.equal(var(Z), 1, tol = 5e-2))
## Fitting via fit_GARCH_11()
fit. <- fit_GARCH_11(X.t, distr = "st")
c(fit.$coef, fit.$df) # fitted alpha_0, alpha_1, beta_1, nu
Z. <- fit.$Z.t # standardized residuals
stopifnot(all.equal(mean(Z.), 0, tol = 2e-2),
all.equal(var(Z.), 1, tol = 2e-2))
## Compare
fit.coef <- coef(fit)[c("omega", "alpha1", "beta1", "shape")]
fit..coef <- c(fit.$coef, fit.$df)
stopifnot(all.equal(fit.coef, fit..coef, tol = 7e-2, check.attributes = FALSE))
summary(Z. - Z) # standardized residuals
Run the code above in your browser using DataLab