Skip to content
Tutorials Center
My learning

Topics

Lesson 56 of 76 ยท Modelling

Forecasting Basics

Forecast monthly data: naive and seasonal-naive baselines, moving averages, exponential smoothing, ARIMA with statsmodels, and honest rolling backtests.

A forecast answers "what will this number be next week, next month, next quarter?" Demand planning, staffing, budgets and capacity all depend on it. Forecasting looks like ordinary prediction, but time changes the rules. Rows are not independent, the future is not available when you train, and the data you are asked to predict lies after all the data you have. Models that ignore this produce impressive results in the notebook and disappointing ones in production.

This lesson builds forecasts from the simplest rules upward: baselines first, then exponential smoothing and ARIMA from statsmodels, and finally a backtest that compares them fairly. The theme is that a complicated model has to earn its place by beating a simple one. For exploring trend and seasonality before you forecast, see Trends, Seasonality and Time Patterns; for the pandas tools behind the examples, see the pandas time series lessons and resampling.

Three rules for time-ordered data

  1. Split by time, never at random. Train on the past, test on the future. A random split lets the model peek at the months on both sides of each test month.
  2. Forecast a horizon you will really need. A model judged on next month may fail at six months ahead. Choose the horizon from the decision (an order placed three months in advance needs a three-month forecast).
  3. Beat a baseline first. If a one-line rule is as accurate as your model, ship the rule.
flowchart LR
    A["Past data"] --> B["Fit the model"]
    B --> C["Forecast the next<br/>h periods"]
    C --> D{"Compare with<br/>what happened"}
    E["Actual future"] --> D
    D --> F["Error per horizon"]

A forecast is judged only against data the model has not seen, and that comes later in time.

The data

The examples use six years of synthetic monthly sales with an upward trend, a yearly seasonal wave and some random noise. We hold back the last 12 months as the test period. Because we generated the series, we know it has a steady trend plus a seasonal pattern, so a good model should find both.

Baselines

Four rules cost almost nothing:

  • Naive: the forecast for every future month is the last observed value.
  • Seasonal naive: each future month repeats the same month one year earlier (period 12 for monthly data).
  • Drift: extend the straight line between the first and last observation.
  • Moving average: the average of the last few observations, flat into the future.

We score each with MAE (mean absolute error), the average size of the miss in the original units. See Evaluating Models with the Right Metrics for the other options.

Python Four baseline forecasts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The seasonal naive rule beats the plain naive rule because it respects the yearly wave, but it still misses by about 18 units: it copies last year's levels and ignores that the whole series has since moved up with the trend. The drift line follows the trend and misses the seasonal wave, yet at 11.3 it beats both of them, which shows how much of the error here comes from the trend. A better model must combine both ideas, and that is what exponential smoothing and ARIMA do.

Exponential smoothing

A moving average gives the last few points equal weight and the older ones none. Exponential smoothing weights every past point, with weights that decay geometrically: recent observations count most. A single parameter, alpha between 0 and 1, controls the speed. A high alpha reacts quickly to change; a low alpha gives a smoother, steadier forecast.

The family grows by adding components:

  • Simple exponential smoothing tracks only a level, so its forecast is flat.
  • Holt's method adds a trend component.
  • Holt-Winters adds a seasonal component as well (additive when the seasonal swing is constant, multiplicative when it grows with the level).

statsmodels fits the parameters by minimizing the in-sample error, so you choose the structure and the library finds the numbers.

Python Simple versus Holt-Winters smoothing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The simple version is exactly as bad as the naive rule (20.9), because its alpha is pushed to about 1 and it just repeats the last value. Adding a trend brings it to 11.3, the same as the drift baseline, since the seasonal wave is still unexplained. Holt-Winters brings the error down to about 2 units, close to the noise we put into the data (a standard deviation of 4 means that some error is unavoidable). The fitted Holt-Winters parameters are all close to 0, which says the level, trend and seasonal pattern are stable and hardly need updating; on a series whose pattern drifts they would be larger. Notice the match between the model's components and the structure of the data: that is the whole job of choosing among smoothing models.

ARIMA in plain words

ARIMA models describe a series using its own past. The name lists three parts, written as (p, d, q):

  • AR(p), autoregression: today's value depends on the previous p values.
  • I(d), integration: difference the series d times (replace each value by its change from the previous one) to remove a trend and make the series steady.
  • MA(q), moving average of errors: today's value depends on the previous q forecast errors.

A seasonal ARIMA adds the same three parts at the seasonal lag, written (P, D, Q, s). For monthly data with yearly seasonality, s = 12; seasonal differencing (D = 1) subtracts the value from a year ago. A model such as order=(1, 1, 1), seasonal_order=(0, 1, 1, 12) is a sturdy general-purpose choice for series with a trend and a yearly pattern.

Choosing the orders by hand is part art. Plots of the autocorrelation function help, and information criteria such as AIC compare candidates, but in a business setting it is common to fix a small set of reasonable orders and decide by backtest. ARIMA also gives something smoothing does not do as naturally: prediction intervals, a range around each forecast. Planning with a range ("between 276 and 288 units") is almost always better than planning with a single number.

Python Seasonal ARIMA with a prediction interval

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The plain ARIMA ignores the seasonal wave, so it is no better than the baselines. Adding the seasonal part gives an error of about 3 units. The interval is also a check: an 80% interval should contain roughly eight of every ten true values over many forecasts. With only 12 points, you cannot expect the share to be exactly 0.8.

A model with a small in-sample error is not necessarily a good forecaster. A very flexible model can fit the past perfectly and still fail on the future. Always judge on data from after the training period.

Backtesting: testing over time

One test year is one roll of the dice: the winner might just have been lucky in that year. A backtest repeats the exercise at several forecast origins. At each origin you train on everything up to that point, forecast the next h periods, record the errors, then move the origin forward. With an expanding training window this is also called rolling-origin or walk-forward evaluation, the time-series version of cross-validation.

flowchart TD
    A["Origin 1: train months 1-36<br/>forecast months 37-42"] --> B["Origin 2: train 1-42<br/>forecast 43-48"]
    B --> C["Origin 3: train 1-48<br/>forecast 49-54"]
    C --> D["Origin 4: train 1-54<br/>forecast 55-60"]
    D --> E["Average the errors<br/>per method"]

Each fold trains only on the past of its own origin and tests on the following months.

The next example runs four methods through five origins (a 6-month horizon) and reports the average MAE per method, plus the error relative to the seasonal-naive baseline. A ratio below 1 means the method adds value over the baseline.

Python A rolling-origin backtest of four methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Averaged over the origins, the picture matches the single split: both Holt-Winters (3.8) and the seasonal ARIMA (3.7) are about five times more accurate than the baselines (17 to 19), and neither is clearly better than the other: each wins in some origins and loses in others. When two methods are this close, prefer the one that is easier to explain, cheaper to run and simpler to monitor. Look at the per-origin table as well as the average: a method that is great in four folds and terrible in one is risky.

Prophet: trend, seasonality and holidays

Prophet (the prophet package, first released by Facebook, now Meta) is a forecasting library built for business series with strong seasonality, holidays and a few missing days. It fits an additive model: y(t) = trend(t) + seasonality(t) + holidays(t) + noise.

  • Trend: a piecewise-linear (or saturating logistic) curve. The slope is allowed to change at automatically placed changepoints; a prior shrinks most of those changes towards zero, so the curve bends only where the data insists. changepoint_prior_scale sets how flexible it is.
  • Seasonality: smooth yearly, weekly (and optionally daily) cycles, written as sums of sines and cosines (Fourier terms). You can add your own, such as monthly.
  • Holidays: a table of dates and names; each gets its own effect, and you can give it a window around the date.
  • Why people use it: it tolerates missing values and outliers, needs very little tuning, and its parts can be plotted separately, which helps explain a forecast.

Usage is short. The data frame must have a date column named ds and a value column named y:

Python
from prophet import Prophet            # pip install prophet (not available in the in-browser runner)

model = Prophet(yearly_seasonality=True, weekly_seasonality=True,
                changepoint_prior_scale=0.05)
model.add_country_holidays(country_name="US")
model.fit(df)                                          # df has columns ds and y

future = model.make_future_dataframe(periods=90)       # 90 more days
forecast = model.predict(future)                       # yhat, yhat_lower, yhat_upper, trend, ...
model.plot(forecast)
model.plot_components(forecast)                        # trend, weekly and yearly pictures

The package needs a compiled backend, so we cannot run it in the browser. The next example rebuilds the core idea with numpy: a piecewise-linear trend with hinge terms at candidate changepoints, yearly Fourier terms and weekday dummies, fitted by regularised least squares. It is a teaching stand-in, not Prophet itself (Prophet uses Bayesian fitting and uncertainty intervals), but the structure is the same.

Python The Prophet idea in numpy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fitted slope rises from about 19 to about 50 per year, close to the true values (18 and 48) used to generate the data, because the penalised hinge terms picked up the changepoint. Evaluate any such model the same way as the others in this lesson: on the held-out last 90 days, against the seasonal-naive baseline, and with the backtesting loop from the previous section.

Practical advice

  • Aggregate to the level you need. Daily data is noisy; weekly or monthly totals are easier to forecast, and decisions are often made at that level.
  • Use known future information. Holidays, promotions and prices planned ahead can be added as extra inputs (called regressors or exogenous variables), but only if you will know them at forecast time.
  • Refit regularly and watch the error over time; patterns change.
  • Report a range, state the horizon, and say what the model assumes (the future behaves like the past).
  • Do not forecast what you cannot. One-off shocks, new products without history and structural breaks need judgment, not just a model.

Machine learning models (gradient boosting on lag features, for example) are also used for forecasting and can use many extra inputs, but they must be backtested in the same time-aware way, and the lags must be built so that nothing from the future leaks in.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is a random train/test split a bad idea for forecasting?

    A random split mixes past and future. The model can interpolate between neighbours, which hides how badly it extrapolates into a genuinely unseen future.
  2. 2. For monthly data with a yearly pattern, what does the seasonal naive forecast for next March use?

    Seasonal naive repeats the value from one season ago, which for monthly data with yearly seasonality is the same month last year.
  3. 3. What does a high smoothing parameter alpha mean in exponential smoothing?

    A high alpha puts most of the weight on the newest observations, so the level moves fast. A low alpha averages over a long history.
  4. 4. What is the main advantage of a rolling-origin backtest over a single holdout year?

    Repeating the train-then-forecast step at several origins gives a distribution of errors rather than one number.
  5. 5. In the model ARIMA(1,1,1), what does the middle 1 do?

    The middle number d is the number of times the series is differenced, replacing values by their change from the previous value.

Summary

  • Split time series by time, choose a horizon that matches the decision, and always compare with baselines.
  • Naive, seasonal naive, drift and moving-average rules are quick benchmarks that a model must beat.
  • Exponential smoothing weights recent data most; Holt-Winters adds trend and seasonality.
  • ARIMA models use past values and past errors, with differencing for trend; the seasonal version handles yearly patterns and gives prediction intervals.
  • Backtest with rolling origins and report the average and the spread of errors, not a single split.
  • When methods perform alike, choose the simpler and more explainable one, and report forecasts as ranges.

Next: Working with Text Data, where the rows are sentences and the features have to be built from words.