ARIMA and SARIMA
- Lesson 12
- 40 min
- Module 4: Statistical forecasters
Where we are
Exponential smoothing gave us a strong statistical forecast. It describes a series by its level, trend and season. In this lesson, we meet ARIMA, which forecasts from past values and past errors, and we see when it adds value over ETS.
Lesson 12 of 27 · Forecasting and Time Series: Predict What Comes NextARIMA and SARIMA
1. Forecasts serve decisions
2. Read a time series
3. Baselines and honest testing
4. Statistical forecasters
5. Machine learning forecasters
6. Hard series and many series
7. Uncertainty and the decision
8. Run a forecast in production
What you will be able to do
Forecast from a series’ past values and past errors with ARIMA, and let AutoARIMA choose the model. Read its forecast range, which widens with the horizon. Then decide with the backtest table when ARIMA is worth its cost over ETS.
NotebookDo this lesson in a notebook
The notebook has every code cell, task and check of this lesson. It computes differences by hand, and fits AutoARIMA to the parasol with its range. It shows what goes wrong without a season. Then it compares AutoARIMA with ETS in a small backtest. Its setup cells install the packages and download the Larkfield data, so it needs no local setup. About 40 minutes, with about 100 seconds of waiting.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- ETS, AutoETS and the backtest table, from Exponential smoothing (ETS, Holt-Winters).
- Yesterday’s sales predict today’s sales (autocorrelation), from Yesterday predicts today.
- Spread and percentiles, from Describe a dataset in the mathematics course.
What you need on your computer
Nothing: the notebook runs in Colab or Kaggle. To run it on your own computer, use Python 3.12 with Jupyter or VS Code. The notebook’s first cell installs the packages.
Tested with Python 3.12, pandas 2.3.3 and statsforecast 2.1.1, in Jupyter on macOS.
How a bus delay travels
Section titled “How a bus delay travels”Think of a bus line with ten stops. At stop 3, the bus is 6 minutes late. How late will it be at stop 4?
- Probably late too, but a little less. The driver catches up a part of the delay at each stop. So the last delay predicts the next one, in part.
- At stop 5, a road work costs 4 extra minutes. That surprise is still felt at stop 6 and stop 7. Then it is gone. So a recent surprise also predicts the next delays, for a short time.
ARIMA forecasts in the same way. It uses a part of the last values, and a part of the last surprises.
The problem: the baselines use the past all or nothing
Section titled “The problem: the baselines use the past all or nothing”In Yesterday predicts today, the parasol’s sales on one day were strongly related to the day before. The autocorrelation at lag 1 was 0.827. Yesterday tells us much about today, but not everything.
The naive forecast uses this all or nothing. It copies the last value fully, as if the relation were 1. The moving average treats yesterday like any of 28 days. Neither one asks the data how much yesterday matters, or how long a surprise lasts.
Kenji also has a second question: “Your forecast says 12 parasols on day 14. How sure are you? Could it be 25?” A method that gives only one number cannot answer.
The idea: past values, differences and past errors
Section titled “The idea: past values, differences and past errors”ARIMA is a family of models with three parts. Its name has one letter for each:
- AR, autoregressive (p): the next value is a weighted sum of the last p values. “Auto” means that the series predicts itself.
- I, integrated (d): the model works on the differences (today minus yesterday), taken d times, and adds them back up at the end.
- MA, moving average of errors (q): the next value also uses the last q surprises, the errors of the model’s own one-step forecasts. This “moving average” is not the moving average of the last module: it averages errors, not sales.
A model is named ARIMA(p, d, q). SARIMA, seasonal ARIMA, adds the same three parts one season back: (P, D, Q) with the season length s. For daily sales, s = 7, so P = 1 means “the value one week before”.
A tiny example: differences
Section titled “A tiny example: differences”AR and MA parts need a series that stays around one level. A series that climbs does not. Its differences often do:
| Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Value | 20 | 22 | 25 | 24 | 28 | 31 | 30 |
| Difference | +2 | +3 | −1 | +4 | +3 | −1 |
The values rise from 20 to 30. The differences go up and down around their mean, 1.67. This is d = 1: one round of differences.
A tiny example: one AR part on the differences
Section titled “A tiny example: one AR part on the differences”Suppose the model learned “the next change is half of the last change”: an AR part with weight 0.5 on the differences. The last value is 30 and the last change was −1:
| Step | Change = 0.5 × last change | Value = last value + change |
|---|---|---|
| 1 | 0.5 × (−1) = −0.5 | 30 − 0.5 = 29.5 |
| 2 | 0.5 × (−0.5) = −0.25 | 29.5 − 0.25 = 29.25 |
| 3 | 0.5 × (−0.25) = −0.125 | 29.125 |
| 4 | −0.0625 | 29.0625 |
The change shrinks at every step, like the bus delay. The forecast settles near 29. Far ahead, an ARIMA forecast without a season settles on a flat line or a straight trend: the memory of the last values fades.
Why the range widens
Section titled “Why the range widens”Each step of the forecast is built on the forecast of the step before, not on a real value. So the errors add up. Take the simplest case, a random walk: each day is the last day plus a random step. A daily error of 3 units then grows like this:
| Days ahead | 1 | 4 | 9 | 14 |
|---|---|---|---|---|
| Typical error (3 × √days) | 3.0 | 6.0 | 9.0 | 11.2 |
The error grows with the square root of the horizon: four times the days, twice the error. Real ARIMA models grow more slowly or more quickly than this, but they grow. This is why a forecast for day 14 must be less sure than a forecast for tomorrow.
AutoARIMA chooses the model
Section titled “AutoARIMA chooses the model”There are many possible values for p, d, q, P, D and Q. AutoARIMA from statsforecast first tests whether differences are needed. Then it tries many combinations and keeps the one with the best AICc, the fit score of the last lesson.
In the notebook, Priya fits it to the parasol up to Sunday 22 June 2025, with an 80% and a 90% range:
from statsforecast import StatsForecastfrom statsforecast.arima import arima_stringfrom statsforecast.models import AutoARIMA
parasol = load_sales(["P025"])origin = pd.Timestamp("2025-06-22")train = parasol[parasol["ds"] <= origin][["unique_id", "ds", "y"]]real = parasol[(parasol["ds"] > origin) & (parasol["ds"] <= origin + pd.Timedelta(days=14))]sf = StatsForecast(models=[AutoARIMA(season_length=7)], freq="D")fc = sf.forecast(df=train, h=14, level=[80, 90])sf.fit(train)print("model chosen:", arima_string(sf.fitted_[0, 0].model_).strip())fc["width 80%"] = fc["AutoARIMA-hi-80"] - fc["AutoARIMA-lo-80"]print(fc[["ds", "AutoARIMA", "AutoARIMA-lo-80", "AutoARIMA-hi-80", "width 80%"]].round(1).iloc[[0, 6, 13]].to_string(index=False))print("14-day total: AutoARIMA", round(fc["AutoARIMA"].sum(), 1), "| real", int(real["y"].sum()))level=[80, 90] asks for two ranges. An 80% range should hold the real value on about 8 days out of 10. The output:
model chosen: ARIMA(0,1,2)(1,0,1)[7] ds AutoARIMA AutoARIMA-lo-80 AutoARIMA-hi-80 width 80%2025-06-23 10.5 5.4 15.7 10.22025-06-29 11.7 4.7 18.8 14.12025-07-06 11.9 3.1 20.8 17.714-day total: AutoARIMA 160.1 | real 197Text description of the diagram
The diagram has two rows of three boxes. The first row is blue, the day-to-day part. p = 0: no past values. d = 1: one round of differences, the change from day to day. q = 2: the last 2 errors. The second row is orange, the same parts one week back, with a season of 7 days. P = 1: the value one week before. D = 0: no weekly differences. Q = 1: the error one week before. A note says that (p, d, q) is the day-to-day part, and (P, D, Q)[7] the same parts one season back. AutoARIMA keeps the combination with the best fit score.
So for the parasol, AutoARIMA models the daily changes with the last two surprises, plus the same weekday one week before. It describes the series differently from ETS(A,N,A). Its 14-day total is different too: 160.1 against 176.3 for ETS.
The range widens with the horizon
Section titled “The range widens with the horizon”- Sold
- AutoARIMA, 80% range
- 90% range
Text description of the chart
The chart shows four weeks of parasol sales before 22 June 2025 as a grey line, mostly between 1 and 25 a day. After the origin, the AutoARIMA forecast is a blue line between 9.8 and 13.7 units a day. Two dashed blue lines show the 80% range: from 5.4 to 15.7 on day 1, and from 3.1 to 20.8 on day 14. Two dashed green lines show the 90% range, which is wider. The real sales are grey dots. In the first week, they are between 4 and 13. Two quiet days, with 4 and 6, are just below the 80% range. In the heat-wave week they are 13, 11, 18, 16, 17, 24 and 48. Only the last two, 24 and 48, are above the 80% range.
The width of the 80% range, day by day:
Text description of the chart
A column chart with five columns, one per day. The width of the 80% range is 10.2 units on day 1 and 12.6 on day 4. It is 14.1 on day 7, 15.8 on day 10 and 17.7 on day 14.
Kenji can now answer his own question. On day 14, the forecast is 11.9 parasols, and 8 days out of 10 should fall between 3.1 and 20.8. Here, 10 of the 14 real days were inside the 80% range. Two quiet days were below it, and the last two days of the heat wave were above it. In the heat-wave week, every day was near the top of the range or above it. The 14-day total (197) was far above the forecast (160.1). A range shows what the model does not know. It cannot show a cause that is not in the parasol’s past, such as a heat wave. Module 7 checks the ranges of every method, and uses them for the order.
The parasol at every origin
Section titled “The parasol at every origin”Explore
AutoARIMA and ETS on the parasol
Predict first: On the parasol, which wins over all 13 origins: AutoARIMA or ETS?
- Sold in the 14 days
- 101
- Forecast for the 14 days
- 106
- WAPE at this origin
- 51.7%
- WAPE, all 13 origins
- 62.6%
At the origin 8 Jun 2025, AutoARIMA forecasts 106 units for the next 14 days. 101 were sold. Its WAPE here is 51.7%: it is too high. Over all 13 origins, AutoARIMA has a WAPE of 62.6%: number 1 of 4 methods. It beats the baseline, seasonal naive (66.1%).
- Units sold
- Forecast of the chosen method
- Baseline: seasonal naive
Show every method, ranked
| Rank | Method | Family | WAPE at this origin | WAPE, all 13 origins |
|---|---|---|---|---|
| 1 | AutoARIMA | statistical | 51.7% | 62.6% |
| 2 | moving average (28 days) | baseline | 101.3% | 64.9% |
| 3 | ETS | statistical | 45.5% | 65.1% |
| 4 | seasonal naive | baseline | 74.3% | 66.1% |
Text description of the explorer
The explorer shows a chart of the parasol’s daily sales. The four weeks before the chosen origin are a grey line. The 14 days after it are grey dots on a shaded test window. The chosen method’s forecast is a blue line, and the seasonal naive forecast is a dashed orange line. A strip above the chart shows the 13 test windows on the year. The controls choose the origin, from 13 April 2025 to 15 March 2026. They also choose one of four methods: seasonal naive, the 28-day moving average, ETS and AutoARIMA. The readout shows the units sold in the 14 days, the forecast, the WAPE at this origin and the WAPE over all 13 origins. The WAPE over all 13 origins is 62.6% for AutoARIMA and 64.9% for the moving average (28 days). It is 65.1% for ETS and 66.1% for seasonal naive.
On the parasol, AutoARIMA is the best of the four: 62.6% against 65.1% for ETS. The parasol has runs of busy and quiet days that follow the weather, and the error parts of ARIMA follow such runs a little better.
Larkfield: AutoARIMA in the course backtest
Section titled “Larkfield: AutoARIMA in the course backtest”The course ran AutoARIMA in the full backtest, as ETS: 50 products, 13 origins, 650 fits. The table now has these statistical rows:
| Method | MASE | WAPE % | bias % | fit s (13 origins) | cost $ / order |
|---|---|---|---|---|---|
| seasonal naive | 1.034 | 51.3 | −0.5 | 0.05 | 16,739 |
| moving average (28 days) | 0.875 | 43.5 | −0.1 | 0.1 | 19,444 |
| ETS (AutoETS) | 0.832 | 41.3 | +0.1 | 46 | 16,578 |
| AutoARIMA | 0.834 | 41.4 | +1.4 | 996 | 16,257 |
On error, ETS and AutoARIMA are the same: 41.3% and 41.4%. On cost, AutoARIMA is about 320 dollars cheaper per order. Per product, AutoARIMA has the lower WAPE on 19 of the 50: fewer than half.
The time is the big difference:
Text description of the chart
A column chart with three columns on an axis from 0 to 1,000 seconds. The seasonal naive forecast takes 0.05 seconds, ETS 46 seconds and AutoARIMA 996 seconds.
AutoARIMA searches through many models for every series at every origin, and each ARIMA fit is slow. The fit times change by about 30% from run to run, but the ratio stays large.
When ARIMA adds value over ETS, and when it does not
Section titled “When ARIMA adds value over ETS, and when it does not”ARIMA adds value when the errors have a memory. Sometimes a busy day is followed by more busy days, for reasons that are not in the level or the season. The error parts of ARIMA follow such runs. The parasol is such a series: AutoARIMA beats ETS on it (62.6% against 65.1%). ARIMA can also take extra inputs, such as a promotion flag. ETS cannot. The next lesson uses this.
ARIMA does not add value when level, trend and season describe the series well. That is most Larkfield products: ETS is better or equal on the other 31 of 50. Then ARIMA costs more than 21 times the time for no gain in error.
What it costs:
- Time: about 1.5 seconds a fit here, against 0.07 for ETS. For 600 series every week, about 15 minutes instead of under a minute. With long seasons, much more: the next lesson shows it.
- Explanation: “the level, plus a Saturday effect” is easy to tell Kenji. “Two moving-average terms on the differences and a seasonal AR term” is not.
- Stability: AutoARIMA can choose a different model at the next origin. For the parasol, it chose ARIMA(1,1,1)(1,0,1)[7] at the first two origins. At the other 11, it chose ARIMA(0,1,2)(1,0,1)[7]. The forecast can jump when the model changes, even if the data hardly did.
A good rule: start with ETS. Try AutoARIMA on the series where ETS’s errors still show runs, or when you need an extra input. Keep it only if the backtest says so, for the decision cost.
Common mistake
AutoARIMA without a season
Symptom: Tomás runs AutoARIMA() on the parasol, with no arguments, for the same 14 days after 22 June 2025:
sf1 = StatsForecast(models=[AutoARIMA()], freq="D")fc1 = sf1.forecast(df=train, h=14)sf1.fit(train)print("model chosen:", arima_string(sf1.fitted_[0, 0].model_).strip())print("forecast:", fc1["AutoARIMA"].round(1).tolist())model chosen: ARIMA(0,1,2)forecast: [11.0, 11.1, 11.1, 11.1, 11.1, 11.1, 11.1, 11.1, 11.1, 11.1, 11.1, 11.1, 11.1, 11.1]The forecast is a flat line at about 11.1 parasols a day. The Saturdays and the quiet Wednesdays are gone.
Cause: the default season_length is 1: no season. AutoARIMA did not look for a weekly part at all. It found a good day-to-day model, ARIMA(0,1,2), and that model settles on a flat line after two days.
Fix: always give the season length: AutoARIMA(season_length=7) for daily data with a weekly season. Then AutoARIMA chose ARIMA(0,1,2)(1,0,1)[7], with a weekly pattern.
Consequence in AI work: an “automatic” model searches only where its settings let it. A default can silently remove what you know about the data. Write the settings down, and check that the chosen model has the parts that you expect.
Guided practice: AutoARIMA in the notebook
Section titled “Guided practice: AutoARIMA in the notebook”- Open the notebook from the box Do this lesson in a notebook at the top of this page.
- Run the setup cells and the helper cell. The data cell ends with the
Ready:line. - In section 1, predict the six differences. Then run the cell. You see
[2, 3, -1, 4, 3, -1]. - In section 2, predict if the range is as wide on day 14 as on day 1. Then run the two cells. You see the model and the fan chart.
- In section 3, predict the forecast without a season. Then run the cell. You see a flat line.
- In section 4, run the small backtest: 10 products, 4 origins. You see ETS at
35.3and AutoARIMA at36.5. It takes about 100 seconds. - In section 5, write the product-by-product count in the Your turn cell, and run it.
- Run the check cell. It prints
Check passed.
Your turn: is the advanced model better on most products?
Section titled “Your turn: is the advanced model better on most products?”Tomás says: “AutoARIMA is the more advanced model, so it wins on most products.” In the notebook, section 4 backtests ETS and AutoARIMA on 10 of the 50 products (every fifth one). It uses 4 of the origins (every fourth one, spread over the year). Before you compute anything, predict: on how many of the 10 products does AutoARIMA have the smaller error? For each product, add up the absolute errors of each method, and count the products where AutoARIMA’s sum is smaller. Put the count in arima_wins.
Show the answer
e_arima = (fc_arima["y"] - fc_arima["yhat"]).abs().groupby(fc_arima["unique_id"]).sum()e_ets = (fc_ets["y"] - fc_ets["yhat"]).abs().groupby(fc_ets["unique_id"]).sum()arima_wins = int((e_arima < e_ets).sum())print(arima_wins, "of", len(e_arima))2 of 10On these 10 products and 4 origins, AutoARIMA wins on 2, and ETS on 8. In the full course backtest, AutoARIMA wins on 19 of the 50 products. So Tomás is wrong: the more complex model is not better on most products. It is better on some, and it costs more than 21 times the fit time on all of them.
Knowledge check
Section titled “Knowledge check”Check your understanding
A series has the values 50, 53, 57, 56 and 60. Compute its differences. An AR part on the differences says 'the next change is 0.4 times the last change'. What are the forecasts for the next two days?
Show the answer
The differences are +3, +4, −1 and +4. The last change is +4, so the next change is 0.4 × 4 = 1.6, and the forecast is 60 + 1.6 = 61.6. The change after that is 0.4 × 1.6 = 0.64, so the forecast is 61.6 + 0.64 = 62.24. The changes shrink, so the forecast settles.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Kenji sees the AutoARIMA range for the parasol: 10.2 units wide on day 1 and 17.7 units wide on day 14. He asks: 'Is the model broken on day 14?' What do you answer?
Show the answer
No. The range should widen. Each day’s forecast is built on the forecasts of the days before, not on real values, so the errors add up. In a random walk, the typical error grows with the square root of the days: four times the days, twice the error. A forecast for two weeks ahead must be less sure than a forecast for tomorrow. A model whose range does not widen claims to know more than it can.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Tomás wants to replace ETS with AutoARIMA for all 600 product series. Use the backtest table to say what he gains and what it costs.
Show the answer
- Error: no gain. MASE 0.834 against 0.832, WAPE 41.4% against 41.3%. AutoARIMA wins on 19 of 50 products, ETS on the other 31.
- Cost: a small gain, about 320 dollars per order (16,257 against 16,578).
- Time: more than 21 times as long: 996 seconds against 46 for 650 fits. For 600 series, that is about 15 minutes a week instead of under a minute.
- Explanation and stability: harder to explain to Kenji, and the chosen model can change from week to week.
Keep ETS as the default. Use AutoARIMA where the backtest shows a gain for the decision, for example on series like the parasol, or when an extra input is needed.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- ARIMA(p, d, q) forecasts from past values (AR, p), on the differences (I, d), with past errors (MA, q). SARIMA adds the same parts one season back: (P, D, Q)[s].
- Differences turn a climbing series into changes around one level: 20, 22, 25 becomes +2, +3.
- AutoARIMA chose ARIMA(0,1,2)(1,0,1)[7] for the parasol. Without
season_length, it chose ARIMA(0,1,2) and a flat forecast. - The range widens with the horizon, because the errors of the steps add up. For the parasol, it grows from 10.2 units on day 1 to 17.7 on day 14.
- In the backtest, AutoARIMA equals ETS on error (41.4% against 41.3%) and is a little cheaper (16,257 dollars). It takes more than 21 times as long (996 seconds), and wins on 19 of 50 products.
You can now fit ARIMA, read its six numbers and its widening range, and say when it is worth its time. So far, every series had one season, the week. Next, Grace’s hourly tickets have two seasons, the day and the week, and a 168-hour season makes ARIMA very slow.