Skip to content

Commit

Permalink
+ new backtest
Browse files Browse the repository at this point in the history
  • Loading branch information
ArdiaD committed Feb 17, 2018
1 parent adf7b90 commit e89207c
Showing 1 changed file with 51 additions and 30 deletions.
81 changes: 51 additions & 30 deletions Examples/run_backtesting.R
Expand Up @@ -8,50 +8,71 @@ library("MSGARCH")
data("SMI", package = "MSGARCH")

## Model specification
models <- list(GARCH = CreateSpec(variance.spec = list(model = c("gjrGARCH")),
distribution.spec = list(c("norm")),
switch.spec = list(K = 1)),
MSGARCH = CreateSpec(variance.spec = list(model = c("gjrGARCH")),
distribution.spec = list(c("norm")),
switch.spec = list(K = 2)))

N_out_of_sample <- 10 # number of out-of-sample evaluation
N_sample <- 2000 # fit sample size
alpha <- 0.05 # risk Level
k_update <- 1 # frequence at which we update the model (reestimation)
models <- list(GARCH = MSGARCH::CreateSpec(variance.spec = list(model = c("gjrGARCH")),
distribution.spec = list(c("sstd")),
switch.spec = list(K = 1)),
MSGARCH = MSGARCH::CreateSpec(variance.spec = list(model = c("gjrGARCH")),
distribution.spec = list(c("sstd")),
switch.spec = list(K = 2)))

n_backtest <- 1000 # number of out-of-sample evaluation
n_its <- 1500 # fit sample size
alpha <- 0.05 # risk Level
k_update <- 100 # frequence at which we update the model (reestimation)

## Initialization
VaR_OOS <- matrix(NA, nrow = N_out_of_sample, ncol = length(models))
y_OOS <- matrix(NA, nrow = N_out_of_sample, ncol = 1)
VaR <- matrix(NA, nrow = n_backtest, ncol = length(models))
y_ots <- matrix(NA, nrow = n_backtest, ncol = 1)
model_fit <- vector(mode = "list", length = length(models))

## Backtest
for (i in 1:N_out_of_sample) { # iterate over out-of-sample time
cat("Backtest. Iteration: ", i, "\n")
y_fit <- SMI[i:(N_sample + i - 1)] # training sample
y_OOS[i] <- SMI[N_sample + i] # store out-of-sample data
for (j in 1:length(models)) { # iterate over model
# do we update the model ?

# iterate over out-of-sample time
for (i in 1:n_backtest) {
cat("Backtest - Iteration: ", i, "\n")
y_its <- SMI[i:(n_its + i - 1)] # in-sample data
y_ots[i] <- SMI[n_its + i] # out-of-sample data

# iterate over models
for (j in 1:length(models)) {

# do we update the model estimation
if (k_update == 1 || i %% k_update == 1) {
model_fit[[j]] <- FitML(spec = models[[j]], data = y_fit) # fit the model
cat("Model", j, "is reestimated\n")
model_fit[[j]] <- MSGARCH::FitML(spec = models[[j]], data = y_its)
}

# calculate VaR 1-step ahead
VaR_OOS[i,j] <- Risk(model_fit[[j]],
n.ahead = 1,
alpha = alpha,
do.es = FALSE,
do.its = FALSE)$VaR
}
VaR[i,j] <- MSGARCH::Risk(model_fit[[j]]$spec, par = model_fit[[j]]$par,
data = y_its,
n.ahead = 1,
alpha = alpha,
do.es = FALSE,
do.its = FALSE)$VaR
}
}

## Test the VaR
library("GAS")
CCtest_pvalue <- NULL
CC_pval <- DQ_pval <- NULL
for (j in 1:length(models)) { #iterate over model
test <- GAS::BacktestVaR(data = y_OOS,
VaR = VaR_OOS[,j],
test <- GAS::BacktestVaR(data = y_ots,
VaR = VaR[,j],
alpha = alpha)

CCtest_pvalue[j] <- test$LRcc[j] #P-Value
CC_pval[j] <- test$LRcc[2]
DQ_pval[j] <- test$DQ$pvalue
}
names(CC_pval) <- names(DQ_pval) <- c("GARCH", "MSGARCH")

print(CC_pval)
print(DQ_pval)

par(mfrow = c(1,1))
plot(y_ots, pch = 20, las = 1, xlab = "time index", ylab = "return [%]")
lines(VaR[,1], type = 'l', col = "red", lwd = 2)
lines(VaR[,2], type = 'l', col = "blue", lwd = 2)
legend("topleft", legend = c("GARCH", "MSGARCH"), col = c("red", "blue"), lwd = 2, cex = 2)
grid()


0 comments on commit e89207c

Please sign in to comment.