@@ -15,7 +15,7 @@ n_samples_b = 2e4
15
15
round_up = FALSE
16
16
17
17
path_m5_data <- " ./data-raw/M5_data"
18
- m5_forecast_path <- " ./data-raw/M5_for_data/ "
18
+ m5_forecast_path <- " ./data-raw/"
19
19
20
20
m5 :: m5_download(path_m5_data )
21
21
@@ -31,124 +31,134 @@ CAT = c("HOBBIES", "HOUSEHOLD", "FOODS")
31
31
DEPT = c(" HOBBIES_1" , " HOBBIES_2" , " HOUSEHOLD_1" , " HOUSEHOLD_2" ,
32
32
" FOODS_1" , " FOODS_2" , " FOODS_3" )
33
33
34
- store_path = paste0(m5_forecast_path , STORE )
34
+ store_path = paste0(m5_forecast_path )
35
35
len = 1941
36
+ h = 1
36
37
37
38
# ##############
38
39
# ## Base forecasts upper time series
39
- print(" Computing base forecasts of upper time series..." )
40
-
41
- h = 1
42
40
43
41
if (! dir.exists(store_path )) dir.create(store_path ,recursive = TRUE )
44
42
45
43
str_base_fc = paste0(store_path ," /CA_1_h_" ,h ," _base_fc.rds" )
46
44
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 {
74
49
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 )
90
61
train.agg = ts.agg $ train
91
62
test.agg = ts.agg $ test
92
63
93
64
model = model_upper(train.agg )
94
65
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 ) ,
97
68
sigma = model $ scale ,
98
69
actual = test.agg ,
99
70
residuals = model $ residuals
100
71
)
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
111
73
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
+ }
116
91
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
+ }
122
109
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
128
110
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 )
135
159
}
136
160
137
161
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
-
151
162
usethis :: use_data(M5_CA1_basefc , overwrite = TRUE )
152
163
153
- unlink(" ./data-raw/M5_for_data/" ,recursive = TRUE )
154
164
unlink(" ./data-raw/M5_data/" ,recursive = TRUE )
0 commit comments