Skip to content

Commit 3020e3f

Browse files
committed
minor changes pre submission
1 parent 680d26f commit 3020e3f

13 files changed

+72
-60
lines changed

R/bayesRecon-package.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
#'
33
#' The package implements reconciliation via conditioning for probabilistic forecasts of hierarchical time series. The main functions are
44
#'
5-
#' * [reconc_gaussian]: implements analytic formulae for the reconciliation of Gaussian base forecasts.
5+
#' * [reconc_gaussian]: implements analytic formulae for the reconciliation of Gaussian base forecasts;
66
#' * [reconc_BUIS]: a generic tool for the reconciliation of any probabilistic time series forecast via importance sampling;
7-
#' this is the recommended option for non-Gaussian base forecasts.
7+
#' this is the recommended option for non-Gaussian base forecasts;
88
#' * [reconc_MCMC]: a generic tool for the reconciliation of probabilistic count time series forecasts via Markov Chain Monte Carlo.
99
#'
10+
#' @section Utility functions:
11+
#'
12+
#' * [temporal_aggregation]: creates a list of aggregated time series from a time series of class \link[stats]{ts};
13+
#' * [get_reconc_matrices]: creates the aggregation and summing matrices for a temporal hierarchy of time series from user-selected list of aggregation levels.
1014
#'
1115
#' @references
1216
#' Corani, G., Azzimonti, D., Augusto, J.P.S.C., Zaffalon, M. (2021). *Probabilistic Reconciliation of Hierarchical Forecast via Bayes’ Rule*. In: Hutter, F., Kersting, K., Lijffijt, J., Valera, I. (eds) Machine Learning and Knowledge Discovery in Databases. ECML PKDD 2020. Lecture Notes in Computer Science(), vol 12459. Springer, Cham. \doi{10.1007/978-3-030-67664-3_13}.

R/hierarchy.R

+24-24
Original file line numberDiff line numberDiff line change
@@ -182,36 +182,36 @@
182182
#' @description
183183
#' Creates a list of aggregated time series from a time series of class \link[stats]{ts}.
184184
#'
185-
#' @param y Univariate time series of class \link[stats]{ts}.
186-
#' @param aggf User-selected list of aggregation levels.
185+
#' @param y univariate time series of class \link[stats]{ts}.
186+
#' @param agg_levels user-selected list of aggregation levels.
187187
#'
188188
#' @details
189-
#' If `aggf=NULL` then `aggf` is automatically generated by taking all the factors of the time series frequency.
189+
#' If `agg_levels=NULL` then `agg_levels` is automatically generated by taking all the factors of the time series frequency.
190190
#'
191-
#' @return A list of \link[stats]{ts} objects each containing the aggregates time series in the order defined by `aggf`.
191+
#' @return A list of \link[stats]{ts} objects each containing the aggregates time series in the order defined by `agg_levels`.
192192
#'
193-
#' @seealso [get_reconc_matrices()]
193+
#' @seealso [get_reconc_matrices]
194194
#' @export
195-
temporal_aggregation <- function(y, aggf=NULL) {
195+
temporal_aggregation <- function(y, agg_levels=NULL) {
196196
f = stats::frequency(y)
197197
L = length(y)
198198
s = stats::time(y)[1]
199-
if (is.null(aggf)) {
200-
aggf = c()
199+
if (is.null(agg_levels)) {
200+
agg_levels = c()
201201
for (i in 1:f) {
202202
if (f %% i == 0 && L >= i) {
203-
aggf = c(aggf, i)
203+
agg_levels = c(agg_levels, i)
204204
}
205205
}
206206
} else {
207-
aggf = sort(aggf[aggf <= L])
208-
if (!(1 %in% aggf)) {
209-
aggf = c(1, aggf)
207+
agg_levels = sort(agg_levels[agg_levels <= L])
208+
if (!(1 %in% agg_levels)) {
209+
agg_levels = c(1, agg_levels)
210210
}
211211
}
212212
out = list()
213-
for (i in 1:length(aggf)) {
214-
k = aggf[i]
213+
for (i in 1:length(agg_levels)) {
214+
k = agg_levels[i]
215215
num_aggs = floor(L / k)
216216
y_trunc = y[(L - num_aggs*k + 1):L]
217217
y_matrix = matrix(y_trunc, nrow = k, ncol = num_aggs)
@@ -220,7 +220,7 @@ temporal_aggregation <- function(y, aggf=NULL) {
220220
y_agg = stats::ts(data = apply(y_matrix, 2, sum), frequency = y_f, start = y_start)
221221
out[[i]] = y_agg
222222
}
223-
names(out) <- paste0("f=", f / aggf)
223+
names(out) <- paste0("f=", f / agg_levels)
224224
out = rev(out)
225225
return(out)
226226
}
@@ -231,10 +231,10 @@ temporal_aggregation <- function(y, aggf=NULL) {
231231
#' Creates the aggregation and summing matrices for a temporal hierarchy of time series
232232
#' from user-selected list of aggregation levels.
233233
#'
234-
#' @param aggf User-selected list of aggregation levels.
235-
#' @param h Number of steps ahead for the bottom level forecasts.
234+
#' @param agg_levels user-selected list of aggregation levels.
235+
#' @param h number of steps ahead for the bottom level forecasts.
236236
#'
237-
#' @return A list containing the named elements
237+
#' @return A list containing the named elements:
238238
#'
239239
#' * `A` the aggregation matrix;
240240
#' * `S` the summing matrix.
@@ -244,19 +244,19 @@ temporal_aggregation <- function(y, aggf=NULL) {
244244
#'library(bayesRecon)
245245
#'
246246
#'#Create monthly hierarchy
247-
#'aggf <- c(1,2,3,4,6,12)
247+
#'agg_levels <- c(1,2,3,4,6,12)
248248
#'h <- 12
249-
#'rec_mat <- get_reconc_matrices(aggf, h)
249+
#'rec_mat <- get_reconc_matrices(agg_levels, h)
250250
#'S <- rec_mat$S
251251
#'A <- rec_mat$A
252252
#'
253-
#' @seealso [temporal_aggregation()]
253+
#' @seealso [temporal_aggregation]
254254
#'
255255
#' @export
256-
get_reconc_matrices <- function(aggf, h) {
256+
get_reconc_matrices <- function(agg_levels, h) {
257257
A = list()
258-
for (i in 1:length(aggf)) {
259-
k = aggf[i]
258+
for (i in 1:length(agg_levels)) {
259+
k = agg_levels[i]
260260
if (k==1) {
261261
next
262262
}

R/reconc.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
#'library(bayesRecon)
113113
#'
114114
#'# Create a minimal hierarchy with 2 bottom and 1 upper variable
115-
#'rec_mat <- get_reconc_matrices(aggf=c(1,2), h=2)
115+
#'rec_mat <- get_reconc_matrices(agg_levels=c(1,2), h=2)
116116
#'S <- rec_mat$S
117117
#'
118118
#'
@@ -178,7 +178,7 @@
178178
#'
179179
#'
180180
#' @seealso
181-
#' [reconc_gaussian()]
181+
#' [reconc_gaussian]
182182
#'
183183
#' @export
184184
reconc_BUIS <- function(S,
@@ -313,7 +313,7 @@ reconc_BUIS <- function(S,
313313
#'library(bayesRecon)
314314
#'
315315
#'# Create a minimal hierarchy with 2 bottom and 1 upper variable
316-
#'rec_mat <- get_reconc_matrices(aggf=c(1,2), h=2)
316+
#'rec_mat <- get_reconc_matrices(agg_levels=c(1,2), h=2)
317317
#'S <- rec_mat$S
318318
#'
319319
#'#Set the parameters of the Gaussian base forecast distributions
@@ -342,7 +342,7 @@ reconc_BUIS <- function(S,
342342
#' Zambon, L., Agosto, A., Giudici, P., Corani, G. (2023). *Properties of the reconciled distributions for Gaussian and count forecasts*. \doi{10.48550/arXiv.2303.15135}.
343343
#'
344344
#'
345-
#' @seealso [reconc_BUIS()]
345+
#' @seealso [reconc_BUIS]
346346
#'
347347
#' @export
348348
reconc_gaussian <- function(S, base_forecasts.mu,

R/reconc_MH.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#' forecast distribution, which is obtained via conditioning.
88
#' It only works with Poisson or Negative Binomial base forecasts.
99
#'
10-
#' We strongly recommend to use the function [reconc_BUIS()], which implements
10+
#' We strongly recommend to use the function [reconc_BUIS], which implements
1111
#' the Bottom-Up Importance Sampling algorithm instead of MCMC.
1212
#' If you use this function, we suggest the usage of tools to check the convergence.
1313
#'
@@ -43,7 +43,7 @@
4343
#'library(bayesRecon)
4444
#'
4545
#'# Create a minimal hierarchy with 2 bottom and 1 upper variable
46-
#'rec_mat <- get_reconc_matrices(aggf=c(1,2), h=2)
46+
#'rec_mat <- get_reconc_matrices(agg_levels=c(1,2), h=2)
4747
#'S <- rec_mat$S
4848
#'
4949
#'#Set the parameters of the Poisson base forecast distributions
@@ -75,7 +75,7 @@
7575
#'
7676
#'
7777
#' @seealso
78-
#' [reconc_BUIS()]
78+
#' [reconc_BUIS]
7979
#'
8080
#' @export
8181
reconc_MCMC <- function(S,

README.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ which can be obtained using the function `get_reconc_matrices`.
4444
```{r}
4545
library(bayesRecon)
4646
47-
rec_mat <- get_reconc_matrices(aggf=c(1,2), h=2)
47+
rec_mat <- get_reconc_matrices(agg_levels=c(1,2), h=2)
4848
S <- rec_mat$S
4949
print(S)
5050
```

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ obtained using the function `get_reconc_matrices`.
3434
``` r
3535
library(bayesRecon)
3636

37-
rec_mat <- get_reconc_matrices(aggf=c(1,2), h=2)
37+
rec_mat <- get_reconc_matrices(agg_levels=c(1,2), h=2)
3838
S <- rec_mat$S
3939
print(S)
4040
#> [,1] [,2]

examples/hydepark.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ train = window(HydePark.ts, end = c(1973, 12))
3131
test = window(HydePark.ts, start = c(1974))
3232

3333
### Build hierarchy
34-
aggf = c(1,2,3,4,6,12)
35-
train.aggregate = bayesRecon::temporal_aggregation(train, aggf)
34+
agg_levels = c(1,2,3,4,6,12)
35+
train.aggregate = bayesRecon::temporal_aggregation(train, agg_levels)
3636
levels <- attributes(train.aggregate)$names
3737

3838
### Hierarchical forecasting
@@ -58,7 +58,7 @@ for (l in seq_along(train.aggregate)) {
5858
}
5959

6060
### Reconciliation
61-
tmp = bayesRecon::get_reconc_matrices(aggf, bottom.f = frequency(train), bottom.H = 12)
61+
tmp = bayesRecon::get_reconc_matrices(agg_levels, bottom.f = frequency(train), bottom.H = 12)
6262
S = tmp$S
6363
A = tmp$A
6464
reconc.res = bayesRecon::reconc_IS(S, base_forecasts = fc.samples, in_type = "samples", distr = "discrete")

man/bayesRecon-package.Rd

+10-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_reconc_matrices.Rd

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reconc_BUIS.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reconc_MCMC.Rd

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reconc_gaussian.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/temporal_aggregation.Rd

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)