-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path05-Find_Best_Garch_Model.R
107 lines (84 loc) · 3.85 KB
/
05-Find_Best_Garch_Model.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# A Garch Tutorial with R - Finding the best model
# Paper at <https://rac.anpad.org.br/index.php/rac/article/view/1420>
#
# This script will estimate several garch models and find the best using the BIC
# criteria. A plot with the results, Figure 02 in the paper, is saved in a .png file
# at folder /figs.
## MAIN OPTIONS
max_lag_AR <- 1 # used 1 in paper
max_lag_MA <- 1 # used 1 in paper
max_lag_ARCH <- 2 # used 2 in paper
max_lag_GARCH <- 1 # used 1 in paper
dist_to_use <- c('norm', 'std') # see rugarch::ugarchspec help for more
models_to_estimate <- c('sGARCH', 'eGARCH', 'gjrGARCH') # see rugarch::rugarchspec help for more
## END OPTIONS
library(tidyverse)
library(purrr)
graphics.off()
my_d <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(my_d)
source('fcts/garch_fcts.R')
# get price data
df_prices <- read_rds('data/RAC-GARCH-Data.rds')
out <- find_best_arch_model(x = df_prices$log_ret,
type_models = models_to_estimate,
dist_to_use = dist_to_use,
max_lag_AR = max_lag_AR,
max_lag_MA = max_lag_MA,
max_lag_ARCH = max_lag_ARCH,
max_lag_GARCH = max_lag_GARCH)
# get table with estimation results
tab_out <- out$tab_out
# pivot table to long format (better for plotting)
df_long <- tidyr::pivot_longer(data = tab_out %>%
select(model_name,
type_model,
type_dist,
AIC, BIC), cols = c('AIC', 'BIC'))
models_names <- unique(df_long$model_name)
best_models <- c(tab_out$model_name[which.min(tab_out$AIC)],
tab_out$model_name[which.min(tab_out$BIC)])
# figure out where is the best model
df_long <- df_long %>%
mutate(order_model = if_else(model_name %in% best_models, 'Best Model', 'Not Best Model') ) %>%
na.omit()
# make table with best models
df_best_models <- df_long %>%
group_by(name) %>%
summarise(model_name = model_name[which.min(value)],
value = value[which.min(value)],
type_model = type_model[which.min(value)])
# plot results
p1 <- ggplot(df_long %>%
arrange(type_model),
aes(x = reorder(model_name,
order(type_model)),
y = value,
shape = type_dist,
color = type_model)) +
geom_point(size = 3.5, alpha = 0.65) +
coord_flip() +
theme_bw(base_family = "TT Times New Roman") +
facet_wrap(~name, scales = 'free_x') +
geom_point(data = df_best_models, mapping = aes(x = reorder(model_name,
order(type_model)),
y = value),
color = 'blue', size = 5, shape = 8) +
labs(title = 'Selecting Garch Models by Fitness Criteria',
subtitle = 'The best model is the one with lowest AIC or BIC (with star)',
x = '',
y = 'Value of Fitness Criteria',
shape = 'Type of Dist.',
color = 'Type of Model') +
theme(legend.position = "right")
x11() ; p1 ; ggsave('figs/fig04_best_garch.png')
# estimate best garch model by BIC (used in next section)
best_spec = ugarchspec(variance.model = list(model = out$best_bic$type_model,
garchOrder = c(out$best_bic$lag_arch,
out$best_bic$lag_garch)),
mean.model = list(armaOrder = c(out$best_bic$lag_ar,
out$best_bic$lag_ma)),
distribution = 'std')
my_best_garch <- ugarchfit(spec = best_spec,
data = df_prices$log_ret)
write_rds(my_best_garch, 'data/garch_model.rds')