-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodify.R
74 lines (63 loc) · 2.42 KB
/
modify.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
# Compute returns, fit EGARCH(1, 1) models and compute innovations
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(rugarch))
#' Compute innovations corresponding to the Garch model
#'
#' @param fit Fitted GARCH model of class uGARCHfit.
#' @param x Double vector of raw returns.
#'
#' @return Double vector of innovations.
get_innovations <- function(fit, x) {
as.double((x - coef(fit)["mu"]) / sigma(fit))
}
#' Compute predicted returns
#'
#' @param fit Fitted GARCH model of class uGARCHfit.
#' @param x Double vector of innovations.
#'
#' @return Double vector of predicted returns.
get_predictions <- function(fit, x) {
sigma_pred <- as.double(sigma(ugarchforecast(fit, n.ahead = 1)))
as.double(x * sigma_pred + coef(fit)["mu"])
}
# Calculate daily returns
stock <- readr::read_csv("data/stock-price-raw.csv", col_names = TRUE,
col_types = readr::cols()) %>%
rename(date = ...1,
us = `US-SP500`,
uk = `UK-FTSE100`,
jpn = `JPN-Nikke225`) %>%
mutate(across(c(us, uk, jpn),
~ log(. / lag(.)),
.names = "{col}_return")) %>%
slice(-1)
# Specification for the EGARCH(1, 1) model
garch_spec <- ugarchspec(variance.model = list(model = "eGARCH",
garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(0, 0)),
distribution.model = "std")
# Fit EGARCH for each country
fits <- stock %>%
select(ends_with("return")) %>%
purrr::map(~ ugarchfit(garch_spec, .x))
# Compute innovations and predictions
for (country in c("us", "uk", "jpn")) {
cols <- paste0(country, c("_return", "_innovation", "_prediction"))
stock <- stock %>%
mutate("{cols[2]}" := get_innovations(fits[[cols[1]]], get(cols[1])),
"{cols[3]}" := get_predictions(fits[[cols[1]]], get(cols[2])))
}
# Write data
stock %>%
readr::write_csv("data/stock-price-mod.csv")
# Write EGARCH model parameters
offset <- purrr::map_dbl(fits, ~ coef(.)["mu"])
sigma_pred <- purrr::map_dbl(fits,
~ as.double(
sigma(ugarchforecast(., n.ahead = 1))
)
)
country <- purrr::map_chr(names(fits),
~ stringr::str_split(., "_", simplify = TRUE)[1])
tibble(offset, sigma_pred, country) %>%
readr::write_csv("data/model-parameters.csv")