/
reconciliation.R
410 lines (363 loc) · 13.3 KB
/
reconciliation.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#' Forecast reconciliation
#'
#' This function allows you to specify the method used to reconcile forecasts
#' in accordance with its key structure.
#'
#' @param .data A mable.
#' @param ... Reconciliation methods applied to model columns within `.data`.
#'
#' @examples
#' if (requireNamespace("fable", quietly = TRUE)) {
#' library(fable)
#' lung_deaths_agg <- as_tsibble(cbind(mdeaths, fdeaths)) %>%
#' aggregate_key(key, value = sum(value))
#'
#' lung_deaths_agg %>%
#' model(lm = TSLM(value ~ trend() + season())) %>%
#' reconcile(lm = min_trace(lm)) %>%
#' forecast()
#' }
#'
#' @export
reconcile <- function(.data, ...){
UseMethod("reconcile")
}
#' @rdname reconcile
#' @export
reconcile.mdl_df <- function(.data, ...){
mutate(.data, ...)
}
#' Minimum trace forecast reconciliation
#'
#' Reconciles a hierarchy using the minimum trace combination method. The
#' response variable of the hierarchy must be aggregated using sums. The
#' forecasted time points must match for all series in the hierarchy (caution:
#' this is not yet tested for beyond the series length).
#'
#' @param models A column of models in a mable.
#' @param method The reconciliation method to use.
#' @param sparse If TRUE, the reconciliation will be computed using sparse
#' matrix algebra? By default, sparse matrices will be used if the MatrixM
#' package is installed.
#'
#' @seealso
#' [`reconcile()`], [`aggregate_key()`]
#'
#' @references
#' Wickramasuriya, S. L., Athanasopoulos, G., & Hyndman, R. J. (2019). Optimal forecast reconciliation for hierarchical and grouped time series through trace minimization. Journal of the American Statistical Association, 1-45. https://doi.org/10.1080/01621459.2018.1448825
#'
#' @export
min_trace <- function(models, method = c("wls_var", "ols", "wls_struct", "mint_cov", "mint_shrink"),
sparse = NULL){
if(is.null(sparse)){
sparse <- requireNamespace("Matrix", quietly = TRUE)
}
structure(models, class = c("lst_mint_mdl", "lst_mdl", "list"),
method = match.arg(method), sparse = sparse)
}
#' @export
forecast.lst_mint_mdl <- function(object, key_data,
new_data = NULL, h = NULL,
point_forecast = list(.mean = mean), ...){
method <- object%@%"method"
sparse <- object%@%"sparse"
if(sparse){
require_package("Matrix")
as.matrix <- Matrix::as.matrix
t <- Matrix::t
diag <- function(x) if(is.vector(x)) Matrix::Diagonal(x = x) else Matrix::diag(x)
solve <- Matrix::solve
cov2cor <- Matrix::cov2cor
} else {
cov2cor <- stats::cov2cor
}
point_method <- point_forecast
point_forecast <- list()
# Get forecasts
fc <- NextMethod()
if(length(unique(map(fc, interval))) > 1){
abort("Reconciliation of temporal hierarchies is not yet supported.")
}
# Compute weights (sample covariance)
res <- map(object, function(x, ...) residuals(x, ...), type = "response")
if(length(unique(map_dbl(res, nrow))) > 1){
# Join residuals by index #199
res <- unname(as.matrix(reduce(res, full_join, by = "date")[,-1]))
} else {
res <- matrix(invoke(c, map(res, `[[`, 2)), ncol = length(object))
}
# Construct S matrix - ??GA: have moved this here as I need it for Structural scaling
agg_data <- build_key_data_smat(key_data)
n <- nrow(res)
covm <- crossprod(stats::na.omit(res)) / n
if(method == "ols"){
# OLS
W <- diag(rep(1L, nrow(covm)))
} else if(method == "wls_var"){
# WLS variance scaling
W <- diag(diag(covm))
} else if (method == "wls_struct"){
# WLS structural scaling
W <- diag(vapply(agg_data$agg,length,integer(1L)))
} else if (method == "mint_cov"){
# min_trace covariance
W <- covm
} else if (method == "mint_shrink"){
# min_trace shrink
tar <- diag(apply(res, 2, compose(crossprod, stats::na.omit))/n)
corm <- cov2cor(covm)
xs <- scale(res, center = FALSE, scale = sqrt(diag(covm)))
xs <- xs[stats::complete.cases(xs),]
v <- (1/(n * (n - 1))) * (crossprod(xs^2) - 1/n * (crossprod(xs))^2)
diag(v) <- 0
corapn <- cov2cor(tar)
d <- (corm - corapn)^2
lambda <- sum(v)/sum(d)
lambda <- max(min(lambda, 1), 0)
W <- lambda * tar + (1 - lambda) * covm
} else {
abort("Unknown reconciliation method")
}
# Check positive definiteness of weights
eigenvalues <- eigen(W, only.values = TRUE)[["values"]]
if (any(eigenvalues < 1e-8)) {
abort("min_trace needs covariance matrix to be positive definite.", call. = FALSE)
}
# Reconciliation matrices
if(sparse){
row_btm <- agg_data$leaf
row_agg <- seq_len(nrow(key_data))[-row_btm]
S <- Matrix::sparseMatrix(
i = rep(seq_along(agg_data$agg), lengths(agg_data$agg)),
j = vec_c(!!!agg_data$agg),
x = rep(1, sum(lengths(agg_data$agg))))
J <- Matrix::sparseMatrix(i = S[row_btm,]@i+1, j = row_btm, x = 1L,
dims = rev(dim(S)))
U <- cbind(
Matrix::Diagonal(diff(dim(J))),
-S[row_agg,,drop = FALSE]
)
U <- U[, order(c(row_agg, row_btm)), drop = FALSE]
Ut <- t(U)
WUt <- W %*% Ut
P <- J - J %*% WUt %*% solve(U %*% WUt, U)
# P <- J - J%*%W%*%t(U)%*%solve(U%*%W%*%t(U))%*%U
}
else {
S <- matrix(0L, nrow = length(agg_data$agg), ncol = max(vec_c(!!!agg_data$agg)))
S[length(agg_data$agg)*(vec_c(!!!agg_data$agg)-1) + rep(seq_along(agg_data$agg), lengths(agg_data$agg))] <- 1L
R <- t(S)%*%solve(W)
P <- solve(R%*%S)%*%R
}
reconcile_fbl_list(fc, S, P, W, point_forecast = point_method)
}
#' Bottom up forecast reconciliation
#'
#' \lifecycle{experimental}
#'
#' Reconciles a hierarchy using the bottom up reconciliation method. The
#' response variable of the hierarchy must be aggregated using sums. The
#' forecasted time points must match for all series in the hierarchy.
#'
#' @param models A column of models in a mable.
#'
#' @seealso
#' [`reconcile()`], [`aggregate_key()`]
#' @export
bottom_up <- function(models){
structure(models, class = c("lst_btmup_mdl", "lst_mdl", "list"))
}
#' @export
forecast.lst_btmup_mdl <- function(object, key_data,
point_forecast = list(.mean = mean), ...){
# Keep only bottom layer
S <- build_smat_rows(key_data)
btm <- which(rowSums(S) == 1)
object <- object[btm]
point_method <- point_forecast
point_forecast <- list()
# Get base forecasts
fc <- vector("list", nrow(S))
fc[btm] <- NextMethod()
# Add dummy forecasts to unused levels
fc[seq_along(fc)[-btm]] <- fc[btm[1]]
P <- matrix(0L, nrow = ncol(S), ncol = nrow(S))
P[(btm-1L)*nrow(P) + seq_len(nrow(P))] <- 1L
reconcile_fbl_list(fc, S, P, W = diag(nrow(S)),
point_forecast = point_method)
}
#' Top down forecast reconciliation
#'
#' \lifecycle{experimental}
#'
#' Reconciles a hierarchy using the top down reconciliation method. The
#' response variable of the hierarchy must be aggregated using sums. The
#' forecasted time points must match for all series in the hierarchy.
#'
#' @param models A column of models in a mable.
#' @param method The reconciliation method to use.
#'
#' @seealso
#' [`reconcile()`], [`aggregate_key()`]
#'
#' @export
top_down <- function(models, method = c("forecast_proportions", "average_proportions", "proportion_averages")){
structure(models, class = c("lst_topdwn_mdl", "lst_mdl", "list"),
method = match.arg(method))
}
#' @export
forecast.lst_topdwn_mdl <- function(object, key_data,
point_forecast = list(.mean = mean), ...){
method <- object%@%"method"
point_method <- point_forecast
point_forecast <- list()
# TODO: Add check for grouped hierarchies
S <- build_smat_rows(key_data)
# Identify top and bottom level
top <- which.max(rowSums(S))
btm <- which(rowSums(S) == 1L)
if(method == "forecast_proportions") {
abort("`method = 'forecast_proportions'` is not yet supported")
fc <- NextMethod()
fc_mean <- lapply(fc, function(x) mean(x[[distribution_var(x)]]))
} else {
# Compute dis-aggregation matrix
history <- lapply(object, function(x) response(x)[[".response"]])
top_y <- history[[top]]
btm_y <- history[btm]
if (method == "average_proportions") {
prop <- map_dbl(btm_y, function(y) mean(y/top_y))
} else if (method == "proportion_averages") {
prop <- map_dbl(btm_y, mean) / mean(top_y)
} else {
abort("Unkown `top_down()` reconciliation `method`.")
}
# Keep only top layer
object <- object[top]
# Get base forecasts
fc <- vector("list", nrow(S))
fc[top] <- NextMethod()
# Add dummy forecasts to unused levels
fc[seq_along(fc)[-top]] <- fc[top]
}
P <- matrix(0L, nrow = ncol(S), ncol = nrow(S))
P[,top] <- prop
reconcile_fbl_list(fc, S, P, W = diag(nrow(S)),
point_forecast = point_method)
}
reconcile_fbl_list <- function(fc, S, P, W, point_forecast, SP = NULL) {
if(length(unique(map(fc, interval))) > 1){
abort("Reconciliation of temporal hierarchies is not yet supported.")
}
if(!inherits(S, "matrix")) {
# Use sparse functions
require_package("Matrix")
as.matrix <- Matrix::as.matrix
t <- Matrix::t
diag <- function(x) if(is.vector(x)) Matrix::Diagonal(x = x) else Matrix::diag(x)
cov2cor <- Matrix::cov2cor
} else {
cov2cor <- stats::cov2cor
}
if(is.null(SP)) {
SP <- S%*%P
}
fc_dist <- map(fc, function(x) x[[distribution_var(x)]])
is_normal <- all(map_lgl(fc_dist, function(x) inherits(x[[1]], "dist_normal")))
fc_mean <- as.matrix(invoke(cbind, map(fc_dist, mean)))
fc_var <- transpose_dbl(map(fc_dist, distributional::variance))
# Apply to forecasts
fc_mean <- as.matrix(SP%*%t(fc_mean))
fc_mean <- split(fc_mean, row(fc_mean))
if(is_normal){
R1 <- cov2cor(W)
W_h <- map(fc_var, function(var) diag(sqrt(var))%*%R1%*%t(diag(sqrt(var))))
fc_var <- map(W_h, function(W) diag(SP%*%W%*%t(SP)))
fc_dist <- map2(fc_mean, transpose_dbl(map(fc_var, sqrt)), distributional::dist_normal)
} else {
fc_dist <- map(fc_mean, distributional::dist_degenerate)
}
# Update fables
map2(fc, fc_dist, function(fc, dist){
dimnames(dist) <- dimnames(fc[[distribution_var(fc)]])
fc[[distribution_var(fc)]] <- dist
point_fc <- compute_point_forecasts(dist, point_forecast)
fc[names(point_fc)] <- point_fc
fc
})
}
build_smat_rows <- function(key_data){
row_col <- sym(colnames(key_data)[length(key_data)])
smat <- key_data %>%
unnest(!!row_col) %>%
dplyr::arrange(!!row_col) %>%
select(!!expr(-!!row_col))
agg_struc <- group_data(dplyr::group_by_all(as_tibble(map(smat, is_aggregated))))
# key_unique <- map(smat, function(x){
# x <- unique(x)
# x[!is_aggregated(x)]
# })
agg_struc$.smat <- map(agg_struc$.rows, function(n) diag(1, nrow = length(n), ncol = length(n)))
agg_struc <- map(seq_len(nrow(agg_struc)), function(i) agg_struc[i,])
out <- reduce(agg_struc, function(x, y){
# For now, assume x is aggregated into y somehow
n_key <- ncol(x)-2
nm_key <- names(x)[seq_len(n_key)]
agg_vars <- map2_lgl(x[seq_len(n_key)], y[seq_len(n_key)], `<`)
if(!any(agg_vars)) browser() # Something isn't right
# Match rows between summation matrices
not_agg <- names(Filter(`!`, y[seq_len(n_key)]))
cols <- group_data(group_by(smat[x$.rows[[1]][seq_len(ncol(x$.smat[[1]]))],], !!!syms(not_agg)))$.rows
cols_pos <- unlist(cols)
cols <- rep(seq_along(cols), map_dbl(cols, length))
cols[cols_pos] <- cols
x$.rows[[1]] <- c(x$.rows[[1]], y$.rows[[1]])
x$.smat <- list(rbind(
x$.smat[[1]],
y$.smat[[1]][, cols, drop = FALSE]
))
x
})
smat <- out$.smat[[1]]
smat[out$.rows[[1]],] <- smat
return(smat)
}
build_key_data_smat <- function(x){
kv <- names(x)[-ncol(x)]
agg_shadow <- as_tibble(map(x[kv], is_aggregated))
grp <- as_tibble(vctrs::vec_group_loc(agg_shadow))
num_agg <- rowSums(grp$key)
# Initialise comparison leafs with known/guaranteed leafs
x_leaf <- x[vec_c(!!!grp$loc[which(num_agg == min(num_agg))]),]
# Sort by disaggregation to identify aggregated leafs in order
grp <- grp[order(num_agg),]
grp$match <- lapply(unname(split(grp, seq_len(nrow(grp)))), function(level){
disagg_col <- which(!vec_c(!!!level$key))
agg_idx <- level[["loc"]][[1]]
pos <- vec_match(x_leaf[disagg_col], x[agg_idx, disagg_col])
pos <- vec_group_loc(pos)
# Add non-matches as leaf nodes
agg_leaf <- setdiff(seq_along(agg_idx), pos$key)
if(!is_empty(agg_leaf)){
pos <- vec_rbind(
pos,
structure(list(key = agg_leaf, loc = as.list(agg_idx[agg_leaf])),
class = "data.frame", row.names = agg_leaf)
)
x_leaf <<- vec_rbind(
x_leaf,
x[agg_idx[agg_leaf],]
)
}
pos$loc[order(pos$key)]
})
if(any(lengths(grp$loc) != lengths(grp$match))) {
abort("An error has occurred when constructing the summation matrix.\nPlease report this bug here: https://github.com/tidyverts/fabletools/issues")
}
idx_leaf <- vec_c(!!!x_leaf$.rows)
x$.rows[vec_c(!!!grp$loc)] <- vec_c(!!!grp$match)
return(list(agg = x$.rows, leaf = idx_leaf))
# out <- matrix(0L, nrow = nrow(x), ncol = length(idx_leaf))
# out[nrow(x)*(vec_c(!!!x$.rows)-1) + rep(seq_along(x$.rows), lengths(x$.rows))] <- 1L
# out
}