-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_estimates.R
191 lines (158 loc) · 6.68 KB
/
make_estimates.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
make.estimates <- function(data.series, model, distribution, k)
{
# k: the number of observations in the tail when estimating
# the POT model. The (k+1)th residual is used as a threshold
#######################################################################
### Check that model and distribution are allowed
valid.models <- c("garch", "gjr", "fam", "cs")
valid.distributions <- c("norm", "std")
if (!(model %in% valid.models))
{
stop("Invalid model chosen")
}
if (!(distribution %in% valid.distributions))
{
stop("Invalid conditional distribution chosen")
}
#######################################################################
#######################################################################
# Fit models and extract needed parameters (and residuals)
##########################################################
####### Use package fGarch for AR(1)-GARCH(1,1) model
# fGarch: AR(1)-GARCH(1,1)
if ((model == "garch"))
{
if (distribution == "norm") # Normal innovations
{
fitted.model <- garchFit(formula = ~ arma(1, 0) + garch(1, 1),
data = data.series,
cond.dist = "norm",
trace = FALSE)
}
if (distribution == "std") # Student t innovations
{
fitted.model <- garchFit(formula = ~ arma(1, 0) + garch(1, 1),
data = data.series,
cond.dist = "std",
shape = df,
include.shape = FALSE,
trace = FALSE)
}
# Produce forecasts of mean and standard deviation
model.forecast <- fGarch::predict(object = fitted.model, n.ahead = 1)
model.mean <- model.forecast$meanForecast
model.sd <- model.forecast$standardDeviation
# Get residuals (for EVT): standardize through (time dependent) fitted values
# and standard deviations
model.residuals <- fGarch::residuals(fitted.model, standardize=TRUE)
} else {
##########################################################
####### Use package rugarch for other models
# AR(1) - GJR-GARCH(1,1) model
if (model == "gjr")
{
if (distribution == "norm") # Normal innovations
{
fitted.model <- ugarchfit(spec=gjr.spec.norm,
data=data.series,
solver=slvr,
solver.control=slvr.ctrl)
}
if (distribution == "std") # Student t innovations
{
fitted.model <- ugarchfit(spec=gjr.spec.std,
data=data.series,
solver=slvr,
solver.control=slvr.ctrl)
}
}
# AR(1) - component-GARCH(1,1) model
if (model == "cs")
{
if (distribution == "norm") # Normal innovations
{
fitted.model <- ugarchfit(spec=cs.spec.norm,
data=data.series,
solver=slvr,
solver.control=slvr.ctrl)
}
if (distribution == "std") # Student t innovations
{
fitted.model <- ugarchfit(spec=cs.spec.std,
data=data.series,
solver=slvr,
solver.control=slvr.ctrl)
}
}
# Make forecasts of tomorrow's expected value and standard deviation
# for models from rugarch package
model.forecast <- ugarchforecast(fitted.model, n.ahead=1)
model.mean <- model.forecast@forecast$forecasts[[1]]$series
model.sd <- model.forecast@forecast$forecasts[[1]]$sigma
# Get residuals (for EVT): standardize through (time dependent) fitted values
# and standard deviations
model.residuals <- rugarch::residuals(fitted.model, standardize=TRUE)
}
#######################################################################
# Now calculate VaR, ES, VaR-break, ES difference, and excess residuals
############################################################
# Base model estimates
if (distribution == "norm")
{
model.var <- var.normal(mean=model.mean, sd=model.sd, probs=qs)
model.es <- es.normal(mean=model.mean, sd=model.sd, probs=qs)
}
if (distribution == "std")
{
model.var <- var.student(mean=model.mean, sd=model.sd, probs=qs, df=df)
model.es <- es.student(mean=model.mean, sd=model.sd, probs=qs, df=df)
}
# VaR-break
model.break <- (return.tomorrow > model.var)
# Difference between actual loss and ES estimate
model.diff <- (return.tomorrow - model.es)
# Excess residuals (page 294, McNeil-Frey)
model.exres <- model.diff / model.sd
############################################################
# Peak-Over-Threshold estimates
# Determine threshold
EVTmodel.threshold <- (sort(model.residuals, decreasing = TRUE))[(k+1)]
# Fit GPD to residuals
EVTmodel.fit <- gpd.fit(xdat = model.residuals,
threshold = EVTmodel.threshold,
npy = NULL,
show = FALSE)
# Extract scale and shape parameter estimates
EVTmodel.scale <- EVTmodel.fit$mle[1]
EVTmodel.shape <- EVTmodel.fit$mle[2]
# Estimate the innovation quantile (VaR for the residuals)
EVTmodel.zq <- var.gpd(threshold=EVTmodel.threshold,
scale=EVTmodel.scale,
shape=EVTmodel.shape,
probs=qs,
n=n,
k=k)
# Calculate VaR
EVTmodel.var <- model.mean + model.sd * EVTmodel.zq
# Calculate the Expected Shortfall
EVTmodel.es <- model.mean + model.sd * es.gpd(var=EVTmodel.zq,
threshold=EVTmodel.threshold,
scale=EVTmodel.scale,
shape=EVTmodel.shape)
# VaR-break
EVTmodel.break <- (return.tomorrow > EVTmodel.var)
# Difference between actual loss and ES estimate
EVTmodel.diff <- (return.tomorrow - EVTmodel.es)
# Exceedance residuals (page 294, McNeil-Frey)
# Note that these are created for all observations;
# only those on dates of VaR-breaks should be used
# in the bootstrap test
EVTmodel.exres <- EVTmodel.diff / model.sd
#######################################################################
# Return results
return(c(model.var, EVTmodel.var,
model.es, EVTmodel.es,
model.break, EVTmodel.break,
model.diff, EVTmodel.diff,
model.exres, EVTmodel.exres))
}