-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfforma.Rmd
453 lines (368 loc) · 13.1 KB
/
fforma.Rmd
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
---
title: "Feature-based forecasting algorithms for large collections of time series"
author: "Rob J Hyndman"
date: "25 January 2019"
abstract: "I will discuss two algorithms used in forecasting large collections of diverse time series. Each of these algorithms uses a meta-learning approach with vectors of features computed from time series to guide the way the forecasts are computed. In FFORMS (Feature-based FORecast Model Selection), we use a random forest classifier to identify the best forecasting method using only time series features. A key advantage of our proposed framework is that the time-consuming process of building a classifier is handled in advance of the forecasting task at hand, and only the selected forecasting model needs to be computed in real time. In FFORMA (Feature-based FORecast Model Averaging), we use gradient boosting to obtain the weights for forecast combinations using as inputs only a vector of time series features. This is slower than FFORMS (because forecasts from all candidate models must be computed), but it provides substantially more accurate forecasts. Both approaches perform very well compared to competitive methods in large forecasting competitions, with FFORMA achieving 2nd place in the recent M4 forecasting competition."
fontsize: 14pt
titlefontsize: 22pt
output:
binb::monash:
fig_height: 5
fig_width: 8
highlight: tango
incremental: no
keep_tex: no
includes:
in_header: preamble.tex
colortheme: monashblue
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = FALSE, message = FALSE, warning = FALSE, cache = TRUE,
dev.args = list(bg = grey(0.9), pointsize = 11)
)
library(tidyverse)
library(Mcomp)
library(fpp2)
library(GGally)
library(tsfeatures)
source("functions.R")
set.seed(20180605)
options(digits = 3, width = 63)
```
# Makridakis forecasting competitions
## M competition: 1982
\placefig{0.1}{1.4}{height=8.2cm,width=10cm}{M1}
\only<2->{\placefig{1}{4.3}{height=4cm,width=10cm,keepaspectratio}{SMakridakis}}
\only<3>{\begin{textblock}{5.5}(6.5,2)
\begin{block}{M-competition}
\begin{itemize}
\item 1001 series from demography, industry, economics.
\item Annual, quarterly, monthly data.
\item Anyone could submit forecasts.
\item Multiple forecast measures used.
\end{itemize}
\end{block}\end{textblock}}
}
## M3 competition: 2000
\full{M3paper}
## M3 competition: 2000
\fontsize{13}{14}\sf
\begin{block}{}
``The M3-Competition is a final attempt by the authors to settle the accuracy issue of
various time series methods\dots\ The extension involves the inclusion of more methods/ researchers (in particular in the areas of neural networks and expert systems) and more series.''
\end{block}
* 3003 series
* All data from business, demography, finance and economics.
* Series length between 14 and 126.
* Either non-seasonal, monthly or quarterly.
* All time series positive.
## M4 competition: 2018
\full{m4}
## M4 competition: 2018
* January -- May 2018
* 100,000 time series: yearly, quarterly, monthly, weekly, daily, hourly.
* Point forecast and prediction intervals assessed.
* Code must be public
* 248 registrations, 50 submissions.
\pause
### Winning methods
1. Hybrid of Recurrent Neural Network and Exponential Smoothing models
2. FFORMA: Feature-based forecast combinations using xgboost to find weights
# Time series features
```{r scalem3}
scalem3 <- list()
for (i in 1:3003)
{
scalem3[[i]] <- M3[[i]]$x - min(M3[[i]]$x)
scalem3[[i]] <- as.numeric(scalem3[[i]] / max(scalem3[[i]]))
}
```
## Key idea
\placefig{9.1}{.5}{width=3.6cm}{tukey}
\begin{textblock}{3}(9.7,5.4)\small\textit{John W Tukey}\end{textblock}
\begin{textblock}{8}(0.7,1.2)
\begin{alertblock}{Cognostics}
Computer-produced diagnostics\\ (Tukey and Tukey, 1985).
\end{alertblock}
\end{textblock}\pause
\vspace*{2.5cm}
\alert{Examples for time series}
* lag correlation
* size and direction of trend
* strength of seasonality
* timing of peak seasonality
* spectral entropy
\vspace*{0.3cm}
\begin{block}{}
Called ``features'' in the machine learning literature.
\end{block}
```{r M3data, include=FALSE}
M3data <- purrr::map(
Mcomp::M3,
function(x) {
tspx <- tsp(x$x)
ts(c(x$x, x$xx), start = tspx[1], frequency = tspx[3])
}
)
```
\fontsize{9}{10}\sf
```{r M3Features, include=FALSE, dependson="M3data"}
lambda_stl <- function(x, ...) {
lambda <- forecast::BoxCox.lambda(x, lower = 0, upper = 1, method = "loglik")
y <- forecast::BoxCox(x, lambda)
c(stl_features(y, s.window = "periodic", robust = TRUE, ...),
lambda = lambda
)
}
M3Features <- bind_cols(
tsfeatures(M3data, c("frequency", "entropy")),
tsfeatures(M3data, "lambda_stl", scale = FALSE)
) %>%
select(frequency, entropy, trend, seasonal_strength, e_acf1, lambda) %>%
replace_na(list(seasonal_strength = 0)) %>%
dplyr::rename(
Frequency = frequency,
Entropy = entropy,
Trend = trend,
Season = seasonal_strength,
ACF1 = e_acf1,
Lambda = lambda
) %>%
mutate(Period = as.factor(Frequency))
```
```{r M3examples, include=FALSE, dependson="M3Features"}
# Consider only long series
n <- unlist(lapply(M3, function(x) {
x$n
}))
M3Featureslong <- M3Features[n > 50, ]
M3long <- M3[names(M3)[n > 50]]
fnames <- c("M3Freq", "M3spec", "M3trend", "M3season", "M3acf", "M3lambda")
k <- NROW(M3Featureslong)
for (i in 1:6)
{
j <- order(M3Featureslong[[i]])
savepdf(paste(fnames[i], "Lo", sep = ""), width = 20, height = 7)
print(autoplot(M3long[[j[1]]]$x) +
ylab(M3long[[j[1]]]$sn) + xlab(""))
endpdf()
savepdf(paste(fnames[i], "Hi", sep = ""), width = 20, height = 7)
print(autoplot(M3long[[j[k]]]$x) +
ylab(M3long[[j[k]]]$sn) + xlab(""))
endpdf()
}
```
## Distribution of Period for M3
```{r M3period, dependson="M3Features"}
ggally_barDiag(M3Features,
mapping = aes(Period), width = 0.2,
colour = "#cc5900", fill = "#cc5900"
)
```
## Distribution of Seasonality for M3
```{r M3season, dependson="M3Features"}
gghist(M3Features, aes_string("Season"))
```
\only<2->{
\begin{textblock}{6}(0.2,3)
\begin{alertblock}{Low Seasonality}
\includegraphics[width=6cm]{M3seasonLo.pdf}
\end{alertblock}
\end{textblock}
}
\only<3>{
\begin{textblock}{6}(6.6,3)
\begin{alertblock}{High Seasonality}
\includegraphics[width=6cm]{M3seasonHi.pdf}
\end{alertblock}
\end{textblock}
}
## Distribution of Trend for M3
```{r M3trend, dependson="M3Features"}
gghist(M3Features, aes_string("Trend"))
```
\only<2->{
\begin{textblock}{6}(0.2,3)
\begin{alertblock}{Low Trend}
\includegraphics[width=6cm]{M3trendLo.pdf}
\end{alertblock}
\end{textblock}
}
\only<3>{
\begin{textblock}{6}(6.6,3)
\begin{alertblock}{High Trend}
\includegraphics[width=6cm]{M3trendHi.pdf}
\end{alertblock}
\end{textblock}
}
## Distribution of Residual ACF1 for M3
```{r M3ACF1, dependson="M3Features"}
gghist(M3Features, aes_string("ACF1"))
```
\only<2->{
\begin{textblock}{6}(0.2,3)
\begin{alertblock}{Low ACF1}
\includegraphics[width=6cm]{M3acfLo.pdf}
\end{alertblock}
\end{textblock}
}
\only<3>{
\begin{textblock}{6}(6.6,3)
\begin{alertblock}{High ACF1}
\includegraphics[width=6cm]{M3acfHi.pdf}
\end{alertblock}
\end{textblock}
}
## Distribution of Spectral Entropy for M3
```{r M3entropy, dependson="M3Features"}
gghist(M3Features, aes_string("Entropy"))
```
\only<2->{
\begin{textblock}{6}(0.2,3)
\begin{alertblock}{Low Entropy}
\includegraphics[width=6cm]{M3specLo.pdf}
\end{alertblock}
\end{textblock}
}
\only<3>{
\begin{textblock}{6}(6.6,3)
\begin{alertblock}{High Entropy}
\includegraphics[width=6cm]{M3specHi.pdf}
\end{alertblock}
\end{textblock}
}
## Feature distributions
```{r ACF1SE, dependson="M3Features"}
ggplot(M3Features, aes(x = Entropy, y = ACF1)) + geom_point()
```
## Feature distributions
```{r TrendSE, dependson="M3Features"}
ggplot(M3Features, aes(x = Entropy, y = Trend)) + geom_point()
```
## Feature distributions
```{r M3pairs, dependson="M3Features"}
# Fig 1 of paper
yk_ggally_densityDiag <- wrap(gghist, adjust = 0.5)
yk_ggally_barDiag <- wrap(ggally_barDiag,
colour = "#cc5900",
fill = "#cc5900", width = 0.2
)
M3Features %>%
select(Period, Entropy, Trend, Season, ACF1, Lambda) %>%
ggpairs(
diag = list(
continuous = yk_ggally_densityDiag,
discrete = yk_ggally_barDiag
),
axisLabels = "none",
lower = list(continuous = wrap("points", alpha = 0.5, size = 0.2))
) -> p
print(p)
savepdf("PairwisePlot")
print(p)
endpdf()
```
# Feature-based forecasting algorithms
## Features used to select a forecasting model
\begin{textblock}{12}(0.1,1.3)\small
\begin{multicols}{2}
\begin{itemize}\tightlist
\item length
\item strength of seasonality
\item strength of trend
\item linearity
\item curvature
\item spikiness
\item stability
\item lumpiness
\item parameter estimates of Holt's linear trend method
\item spectral entropy
\item Hurst exponent
\item nonlinearity
\item parameter estimates of Holt-Winters' additive method
\item unit root test statistics
\item crossing points, flat spots
\item peaks, troughs
\item ACF and PACF based features - calculated on raw, differenced, and remainder series.
\item ARCH/GARCH statistics and ACF of squared series and residuals.
\end{itemize}
\end{multicols}
\end{textblock}
## Features used to select a forecasting model
\alert{Why these features?}
* Hyndman, Wang and Laptev. “Large scale unusual time series detection” (ICDM 2015).
* Kang, Hyndman & Smith-Miles. “Visualising forecasting algorithm performance using time series instance spaces” (IJF 2017).
* Talagala, Hyndman and Athanasopoulos. “Meta-learning how to forecast time series” (2018).
* Implemented in the tsfeatures R package
## \fontsize{15}{15}\bf\sffamily FFORMS: Feature-based FORecast Model Selection
* Using large collection of time series, each series split into training and test sets.
* Features computed on training data.
* All forecasting methods fitted to training data, and forecasts obtained for test data period.
* Forecast accuracy for each method/series computed from test data.
* Train a random forest to identify the most accurate forecasting method for a given time series using only a vector of features.
## \fontsize{15}{15}\bf\sffamily FFORMA: Feature-based FORecast Model Averaging
* Like FFORMS but using gradient boosted trees (xgboost) rather than random forest.
* Trained on temporal holdout version of M4 dataset, where size of test sets equal to required forecast horizons
* Optimization criterion: forecast accuracy not classification accuracy.
* Probability of each model being best is used to construct model weights for combination forecast.
* 5 days computing time.
## \fontsize{15}{15}\bf\sffamily FFORMA: Feature-based FORecast Model Averaging
\placefig{0.2}{1.4}{width=12.4cm, trim=0 0 0 70, clip=true}{fforma_graphic}
\vspace*{5.2cm}
### M4 competition results (based on average OWA)
```{r, results='asis'}
tribble(
~Place, ~OWA, ~Method,
"1st", 0.821, NA,
"2nd", 0.838, "(FFORMA)",
"3rd", 0.841, NA
) %>%
baretable(digits=3)
```
## \fontsize{15}{15}\bf\sffamily FFORMA: Feature-based FORecast Model Averaging
### Models included
1. Naive
1. Seasonal naive
1. Random walk with drift
1. Theta method
1. ARIMA
1. ETS
1. TBATS
1. STL decomposition with AR for seasonally adjusted series
1. Neural network autoregression
## \fontsize{15}{15}\bf\sffamily FFORMA: Feature-based FORecast Model Averaging
\only<1>{\placefig{0.2}{1.4}{width=12.4cm, trim=0 0 0 0, clip=true}{prototypes}}
\only<2>{\placefig{0.2}{1.4}{width=12.4cm, trim=0 0 0 0, clip=true}{prototype1}}
\only<3>{\placefig{0.2}{1.4}{width=12.4cm, trim=0 0 0 0, clip=true}{prototype2}}
\only<4>{\placefig{0.2}{1.4}{width=12.4cm, trim=0 0 0 0, clip=true}{prototype3}}
\only<5>{\placefig{0.2}{1.4}{width=12.4cm, trim=0 0 0 0, clip=true}{prototype4}}
\only<6>{\placefig{0.2}{1.4}{width=12.4cm, trim=0 0 0 0, clip=true}{prototype5}}
## Papers and packages
\fontsize{14}{17}\sf
\begin{block}{R packages}
\begin{itemize}\tightlist
\item \alert{tsfeatures}: Calculating time series features. \newline\url{github.com/robjhyndman/tsfeatures}
\item \alert{seer}: FFORMS --- selecting forecasting model using features. \newline\url{github.com/thiyangt/seer}
\item \alert{M4metalearning}: FFORMA -- forecast combinations using features to choose weights. \newline\url{github.com/robjhyndman/M4metalearning}
\end{itemize}
\end{block}
\begin{alertblock}{Papers}
Available from \url{robjhyndman.com}
\end{alertblock}
## Acknowledgements
\begin{textblock}{12.5}(0.2,1.2)
\begin{block}{}\fontsize{9}{10}\sf
\centering\begin{tabular}{p{3.4cm}p{3.4cm}p{3.4cm}}
\includegraphics[height=3.4cm, width=10cm, keepaspectratio]{kate} &
\includegraphics[height=3.4cm, width=10cm, keepaspectratio]{yanfei}&
\includegraphics[height=3.4cm, width=10cm, keepaspectratio]{earowang} \\
Kate Smith-Miles & Yanfei Kang & Earo Wang \\
\includegraphics[height=3.4cm, width=10cm, keepaspectratio]{thiyanga} &
\includegraphics[height=3.4cm, width=10cm, keepaspectratio]{george} &
\includegraphics[height=3.4cm, width=10cm, keepaspectratio]{pablo} \\
Thiyanga Talagala & George Athanasopoulos & Pablo \rlap{Montero-Manso}
\end{tabular}
\end{block}
\end{textblock}