Skip to content

Commit 0be45f9

Browse files
committed
Small fix to the structure of M5_CA1_basefc dataset.
1 parent aea8d46 commit 0be45f9

File tree

3 files changed

+103
-93
lines changed

3 files changed

+103
-93
lines changed

data-raw/CA_1_h_1_base_fc.rds

446 KB
Binary file not shown.

data-raw/M5data_forecasts.R

+103-93
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ n_samples_b = 2e4
1515
round_up = FALSE
1616

1717
path_m5_data <- "./data-raw/M5_data"
18-
m5_forecast_path <- "./data-raw/M5_for_data/"
18+
m5_forecast_path <- "./data-raw/"
1919

2020
m5::m5_download(path_m5_data)
2121

@@ -31,124 +31,134 @@ CAT = c("HOBBIES", "HOUSEHOLD", "FOODS")
3131
DEPT = c("HOBBIES_1", "HOBBIES_2", "HOUSEHOLD_1", "HOUSEHOLD_2",
3232
"FOODS_1", "FOODS_2", "FOODS_3")
3333

34-
store_path = paste0(m5_forecast_path, STORE)
34+
store_path = paste0(m5_forecast_path)
3535
len = 1941
36+
h=1
3637

3738
###############
3839
### Base forecasts upper time series
39-
print("Computing base forecasts of upper time series...")
40-
41-
h=1
4240

4341
if (!dir.exists(store_path)) dir.create(store_path,recursive = TRUE)
4442

4543
str_base_fc = paste0(store_path,"/CA_1_h_",h,"_base_fc.rds")
4644

47-
48-
M5_CA1_basefc = list()
49-
50-
train_u <- list()
51-
52-
# Store
53-
ts.agg = get_upp_ts(dset.store.train, dset.store.test, "store_id",
54-
STORE, h = h, len = len)
55-
train.agg = ts.agg$train
56-
test.agg = ts.agg$test
57-
58-
model = model_upper(train.agg)
59-
fc.model = forecast(model, h = 1)
60-
M5_CA1_basefc$upper[[STORE]] = list(
61-
mu = fc.model$mean,
62-
sigma = model$scale,
63-
actual = test.agg,
64-
residuals = model$residuals
65-
)
66-
train_u[[STORE]] <- train.agg
67-
68-
# Category
69-
for (cat.id in CAT) {
70-
ts.agg = get_upp_ts(dset.store.train, dset.store.test, "cat_id",
71-
cat.id, h = h, len = len)
72-
train.agg = ts.agg$train
73-
test.agg = ts.agg$test
45+
if(file.exists(str_base_fc)){
46+
print("Loading base forecasts of upper time series...")
47+
M5_CA1_basefc <- readRDS(str_base_fc)
48+
}else{
7449

75-
model = model_upper(train.agg)
76-
fc.model = forecast(model, h = 1)
77-
M5_CA1_basefc$upper[[cat.id]] = list(
78-
mu = fc.model$mean,
79-
sigma = model$scale,
80-
actual = test.agg,
81-
residuals = model$residuals
82-
)
83-
train_u[[cat.id]] <- train.agg
84-
}
85-
86-
# Department
87-
for (dept.id in DEPT) {
88-
ts.agg = get_upp_ts(dset.store.train, dset.store.test, "dept_id",
89-
dept.id, h = h, len = len)
50+
print("Computing base forecasts of upper time series...")
51+
52+
53+
54+
M5_CA1_basefc = list()
55+
56+
train_u <- list()
57+
58+
# Store
59+
ts.agg = get_upp_ts(dset.store.train, dset.store.test, "store_id",
60+
STORE, h = h, len = len)
9061
train.agg = ts.agg$train
9162
test.agg = ts.agg$test
9263

9364
model = model_upper(train.agg)
9465
fc.model = forecast(model, h = 1)
95-
M5_CA1_basefc$upper[[dept.id]] = list(
96-
mu = fc.model$mean,
66+
M5_CA1_basefc$upper[[STORE]] = list(
67+
mu = as.numeric(fc.model$mean),
9768
sigma = model$scale,
9869
actual = test.agg,
9970
residuals = model$residuals
10071
)
101-
train_u[[dept.id]] <- train.agg
102-
}
103-
104-
105-
saveRDS(M5_CA1_basefc, str_base_fc)
106-
107-
108-
train_b <- list()
109-
110-
for (item.id in unique(dset.store.train$item_id)) {
72+
train_u[[STORE]] <- train.agg
11173

112-
bts = get_bott_ts(dset.store.train, dset.store.test,
113-
item_id, h, len = 1941)
114-
train = bts$train
115-
test = bts$test
74+
# Category
75+
for (cat.id in CAT) {
76+
ts.agg = get_upp_ts(dset.store.train, dset.store.test, "cat_id",
77+
cat.id, h = h, len = len)
78+
train.agg = ts.agg$train
79+
test.agg = ts.agg$test
80+
81+
model = model_upper(train.agg)
82+
fc.model = forecast(model, h = 1)
83+
M5_CA1_basefc$upper[[cat.id]] = list(
84+
mu = as.numeric(fc.model$mean),
85+
sigma = model$scale,
86+
actual = test.agg,
87+
residuals = model$residuals
88+
)
89+
train_u[[cat.id]] <- train.agg
90+
}
11691

117-
model = model_bottom(train, model_str = "MNN",
118-
occ_str = "auto", #"odds-ratio",
119-
distr = "dgamma")
120-
fc.model = forecast(model, h = 1, interval="simulated",
121-
scenarios=TRUE, nsim = n_samples_b)
92+
# Department
93+
for (dept.id in DEPT) {
94+
ts.agg = get_upp_ts(dset.store.train, dset.store.test, "dept_id",
95+
dept.id, h = h, len = len)
96+
train.agg = ts.agg$train
97+
test.agg = ts.agg$test
98+
99+
model = model_upper(train.agg)
100+
fc.model = forecast(model, h = 1)
101+
M5_CA1_basefc$upper[[dept.id]] = list(
102+
mu = as.numeric(fc.model$mean),
103+
sigma = model$scale,
104+
actual = test.agg,
105+
residuals = model$residuals
106+
)
107+
train_u[[dept.id]] <- train.agg
108+
}
122109

123-
# round to integer (up or to the closest integer, depending on round_up)
124-
samples = if (round_up) ceiling(fc.model$scenarios[1,]) else round(fc.model$scenarios[1,])
125-
samples[samples<0] = 0 # set negative to zero
126-
samples <- as.integer(samples)
127-
pmf = PMF.from_samples(samples) # empirical pmf
128110

129-
M5_CA1_basefc$bottom[[item.id]] = list(
130-
pmf = pmf,
131-
actual = test
132-
#residuals = model$residuals
133-
)
134-
train_b[[item.id]] <- train
111+
saveRDS(M5_CA1_basefc, str_base_fc)
112+
113+
114+
train_b <- list()
115+
116+
for (item.id in unique(dset.store.train$item_id)) {
117+
118+
print(paste0("Doing ",item.id,"..."))
119+
bts = get_bott_ts(dset.store.train, dset.store.test,
120+
item_id, h, len = 1941)
121+
train = bts$train
122+
test = bts$test
123+
124+
model = model_bottom(train, model_str = "MNN",
125+
occ_str = "auto", #"odds-ratio",
126+
distr = "dgamma")
127+
fc.model = forecast(model, h = 1, interval="simulated",
128+
scenarios=TRUE, nsim = n_samples_b)
129+
130+
# round to integer (up or to the closest integer, depending on round_up)
131+
samples = if (round_up) ceiling(fc.model$scenarios[1,]) else round(fc.model$scenarios[1,])
132+
samples[samples<0] = 0 # set negative to zero
133+
samples <- as.integer(samples)
134+
pmf = PMF.from_samples(samples) # empirical pmf
135+
136+
M5_CA1_basefc$bottom[[item.id]] = list(
137+
pmf = pmf,
138+
actual = test
139+
#residuals = model$residuals
140+
)
141+
train_b[[item.id]] <- train
142+
print("Done.\n")
143+
}
144+
145+
146+
hier = get_hier_M5(save_ = FALSE)
147+
148+
M5_CA1_basefc$A <- hier$A
149+
M5_CA1_basefc$S <- hier$S
150+
151+
152+
Q_u = unlist(lapply(train_u, function(x) mean(abs(x[-1] - x[-length(x)]))))
153+
Q_b = unlist(lapply(train_b, function(x) mean(abs(x[-1] - x[-length(x)]))))
154+
155+
M5_CA1_basefc$Q_u <- Q_u
156+
M5_CA1_basefc$Q_b <- Q_b
157+
158+
saveRDS(M5_CA1_basefc, str_base_fc)
135159
}
136160

137161

138-
hier = get_hier_M5(save_ = FALSE)
139-
140-
M5_CA1_basefc$A <- hier$A
141-
M5_CA1_basefc$S <- hier$S
142-
143-
144-
Q_u = unlist(lapply(train_u, function(x) mean(abs(x[-1] - x[-length(x)]))))
145-
Q_b = unlist(lapply(train_b, function(x) mean(abs(x[-1] - x[-length(x)]))))
146-
147-
M5_CA1_basefc$Q_u <- Q_u
148-
M5_CA1_basefc$Q_b <- Q_b
149-
150-
151162
usethis::use_data(M5_CA1_basefc, overwrite = TRUE)
152163

153-
unlink("./data-raw/M5_for_data/",recursive=TRUE)
154164
unlink("./data-raw/M5_data/",recursive=TRUE)

data/M5_CA1_basefc.rda

240 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)