-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcode.R
151 lines (119 loc) · 5.25 KB
/
code.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
# This script defines some functions to generate the base forecasts.
makebf <- function(obj_bights, list_subsets, H, config_forecast_agg, config_forecast_bot, refit_step, mc.cores = 1){
n <- obj_bights$nts
m <- obj_bights$nbts
results <- vector("list", n)
results <- mclapply(seq(n), function(j){
if(j <= n-m){
config_forecast <- config_forecast_agg
}else{
config_forecast <- config_forecast_bot
}
rolling.forecast(obj_bights$yts[, j], list_subsets, H, config_forecast, refit_step = refit_step, j)
}, mc.cores = mc.cores)
allmf <- simplify2array(lapply(results, "[[", "mf"))
allfuture <- simplify2array(lapply(results, "[[", "future"))
allqf <- lapply(results, "[[", "list_qf")
allresiduals <- simplify2array(lapply(results, "[[", "e_residuals"))
allmf <- aperm(allmf, c(3, 1, 2))
allfuture <- aperm(allfuture, c(3, 1, 2))
allresiduals <- t(allresiduals)
list(allmf = allmf, allfuture = allfuture, allqf = allqf, allresiduals = allresiduals)
}
rolling.forecast <- function(series, list_subsets, H, config_forecast, refit_step, idseries){
n_subsets <- length(list_subsets)
mf <- future <- matrix(NA, nrow = n_subsets, ncol = H)
list_qf <- vector("list", n_subsets)
fit_fct <- config_forecast$fit_fct
refit_fct <- config_forecast$refit_fct
param_fit_fct <- config_forecast$param_fit_fct
param_refit_fct <- config_forecast$param_refit_fct
param_forecast <- config_forecast$param_forecast
for(i in seq(n_subsets)){
ts_split <- list_subsets[[i]]
if(is.ts(series)){
learn_series <- subset(series, start = ts_split[1], end = ts_split[2])
}else{
learn_series <- series[seq(ts_split[1], ts_split[2])]
}
future[i, ] <- series[ts_split[2] + seq(1, H)]
if( (i-1) %% refit_step == 0){
if(use.trueparam && idseries > my_bights$naggts){
ar_param <- obj_simul$param$ar_param[[idseries - my_bights$naggts]]
ma_param <- obj_simul$param$ma_param[[idseries - my_bights$naggts]]
stopifnot(!is.null(ar_param) && !is.null(ma_param))
model <- Arima(y = learn_series, order = c(length(ar_param), 0, length(ma_param)),
fixed = c(ar_param, ma_param, 0))
}else{
model <- do.call(fit_fct, c(list(y = learn_series), param_fit_fct))
}
}else{
model <- do.call(refit_fct, c(list(y = learn_series, model = model), param_refit_fct))
}
if(i == 1){
e_residuals <- as.numeric(resid(model))
}
f <- do.call(forecast, c(list(object = model, h = H), param_forecast))
mf[i, ] <- f$mean
quantf <- sapply(seq(H), function(h){
c(rev(f$lower[h, ]), f$upper[h, ])
})
list_qf[[i]] <- quantf
#nsamples <- param_forecast$npaths
#f <- t(replicate(nsamples, simulate(model, bootstrap = do.bootstrap.residuals, nsim = H, future = T)))
#mf[i, ] <- apply(f, 2, mean)
#list_qf[[i]] <- f
}
output <- list(future = future, mf = mf, list_qf = list_qf, e_residuals = e_residuals)
}
makeINFO <- function(tags){
myedges <- data.frame(rbind(cbind(tags[, 1], tags[, 2]),
cbind(tags[, 2], tags[, 3])))
itree <- graph.data.frame(myedges)
itree <- simplify(itree, remove.loops = F)
# Compute A - for each agg. node, compute the associated leafs
all.nodes.names <- V(itree)$name
agg.nodes.names <- aggSeries <- all.nodes.names[which(degree(itree, V(itree), "out")!=0)]
n_agg <- length(agg.nodes.names)
bottomSeries <- tags[, ncol(tags)]
n_bottom <- ncol(bts)
A <- matrix(0, nrow = n_agg, ncol = n_bottom)
for(i in seq_along(agg.nodes.names)){
agg.node.name <- agg.nodes.names[i]
reachable <- which(shortest.paths(itree, agg.node.name, mode="out") != Inf)
terminal.nodes <- reachable[which(degree(itree, reachable, mode="out") == 0)]
terminal.nodes.names <- all.nodes.names[terminal.nodes]
ids <- match(terminal.nodes.names, bottomSeries)
stopifnot(all(!is.na(ids)))
A[i, ids] <- 1
}
output <- list(bottomSeries = bottomSeries, aggSeries = aggSeries, itree = itree,
A = A, n_agg = n_agg, n_bottom = n_bottom)
return(output)
}
makeINFO2 <- function(tags){
myedges <- data.frame(do.call(rbind, lapply(seq(ncol(tags) - 1), function(j){
cbind(tags[, j], tags[, j+1])
})))
itree <- graph.data.frame(myedges)
itree <- simplify(itree, remove.loops = F)
# Compute A - for each agg. node, compute the associated leafs
all.nodes.names <- V(itree)$name
agg.nodes.names <- aggSeries <- all.nodes.names[which(degree(itree, V(itree), "out")!=0)]
n_agg <- length(agg.nodes.names)
bottomSeries <- tags[, ncol(tags)]
n_bottom <- length(bottomSeries)
A <- matrix(0, nrow = n_agg, ncol = n_bottom)
for(i in seq_along(agg.nodes.names)){
agg.node.name <- agg.nodes.names[i]
reachable <- which(shortest.paths(itree, agg.node.name, mode="out") != Inf)
terminal.nodes <- reachable[which(degree(itree, reachable, mode="out") == 0)]
terminal.nodes.names <- all.nodes.names[terminal.nodes]
ids <- match(terminal.nodes.names, bottomSeries)
stopifnot(all(!is.na(ids)))
A[i, ids] <- 1
}
output <- list(bottomSeries = bottomSeries, aggSeries = aggSeries, itree = itree,
A = A, n_agg = n_agg, n_bottom = n_bottom)
return(output)
}