How DeepAR, N-BEATS and N-HiTS forecast
Today let's see what DeepAR, N-BEATS and N-HiTS each change in a neural network to forecast a time series, and how to check whether that change actually helps.
Suppose you plan stock for a takeaway food chain in Australia. You need to forecast the monthly takeaway food turnover, which is the total sales of takeaway food outlets, for each of 8 states and territories. The forecast has to reach 12 months ahead, and the unit is million Australian dollars.
The numbers come from the Australian Bureau of Statistics retail trade survey. There is one monthly series per state, from April 1982 to December 2018. We will train on the data up to December 2017 and keep the 12 months of 2018 aside. That way, every forecast can be checked against what really happened.
The chart below shows one of the eight series, New South Wales (NSW), for the three years 2015 to 2017. Turnover climbs from one year to the next, and inside each year the same shape repeats: February is the lowest month and December the highest.
All 8 series have this kind of shape, at very different levels. We will forecast them with one neural network, and each of the three models changes one part of that network:
- DeepAR forecasts a probability distribution for each month, instead of a single number.
- N-BEATS builds the forecast from a chain of blocks, where each block explains part of the series and passes the rest on.
- N-HiTS is N-BEATS with a smaller input and a smaller output in each block, so that it stays cheap on long horizons.
Switch between the line and the bars above and look at how the yearly pattern sits on top of the upward climb.
Turning eight series into training windows
A neural network learns from examples. Here, one example is a pair: 24 past months as the input, and the 12 months right after them as the target. We cut these pairs out of the series and call each one a training window. The 24 months the network looks back on are the lookback, and the 12 months it forecasts are the horizon.
The first code block loads the takeaway food series, splits off the 12 months of 2018, and prints each state's average monthly turnover in the training period.
NSW averages 239.2 million a month and the Northern Territory (NT) 10.6 million, so NSW is 22.6 times the size of NT. NT also has fewer months, because its series starts in April 1988.
One network has to learn from all eight series. But the squared error would be dominated by the big states, because a 5% miss on NSW is a much larger number than a 5% miss on NT. So we divide each window by the mean of its 24 input months, and subtract 1. A scaled value of 0 then means the average of that window, and -0.10 means 10% below it.
The network now sees only the shape of a window, whichever state it comes from. To get a forecast back in million dollars, we add 1 and multiply by the same window mean.
We slide the window one month at a time over the last 180 training months, January 2003 to December 2017. Every state has at least that much history, and 15 years keeps the training fast. Each state gives 145 windows: 180 months, minus the 24 of the input and the 12 of the target, plus 1.
The next block builds the 1,160 windows (8 states times 145) and prints the size of the input matrix X and the target matrix Y.
Each row of X is one window of 24 scaled months, and the same row of Y holds the 12 scaled months that followed it. The first NSW window starts in January 2003. That month is 8.0% below the average of its 24 months, and February 2003 is 26.0% below it.