Forecasting Product Demand with SARIMA

Monthly furniture sales from the Superstore dataset, forecast with a seasonal ARIMA model, plus what to watch out for when validating time series.
Machine Learning
Data Analysis
Author
Published

April 6, 2022

Demand forecasting means predicting how much of a product people will buy. Manufacturers use it to plan production, and retailers use it to decide what to stock. Get it wrong one way and you’re left with unsold stock, the other way and you run out.

I used it as a hands-on way to learn classical time series forecasting. The notebook started from Susan Li’s well-known walkthrough, An End-to-End Project on Time Series Analysis and Forecasting with Python, which uses the same Superstore dataset, and I extended it to compare the three product categories.

Code: Zilch123/DemandForecast

The data

The Superstore dataset has about 10,000 order lines from 2014 to 2017, across three categories: furniture, office supplies and technology. Each order has a date and a sales value.

I started with furniture. The steps to turn raw orders into a time series:

  1. Keep only the order date and sales.
  2. Add up sales per day.
  3. Resample to monthly, taking the average daily sales for each month.
furniture = furniture.groupby('Order Date')['Sales'].sum().reset_index()
furniture = furniture.set_index('Order Date')
y = furniture['Sales'].resample('MS').mean()

Monthly data smooths out the noise of individual days, but it leaves only 48 data points for 4 years. That’s not much, and it limits how complex the model can be.

Trend and seasonality

Splitting the series into trend, seasonality and residual shows what we’re dealing with:

decomposition = sm.tsa.seasonal_decompose(y, model='additive')
decomposition.plot()

Furniture sales split into trend, seasonal and residual components

There’s a strong yearly pattern: sales drop at the start of every year and peak towards the end. The trend moves around but doesn’t grow much over the 4 years.

Why SARIMA

ARIMA models a series from its own past values (AR), differencing to remove the trend (I) and past forecast errors (MA). SARIMA adds the same three parts again at a seasonal lag, here 12 months. That means it can model the yearly pattern directly, without removing it first.

A SARIMA model is written as (p, d, q) × (P, D, Q, s):

  • p, d, q: autoregressive order, differencing and moving average order for the month-to-month part
  • P, D, Q: the same for the seasonal part
  • s: the season length, 12 for monthly data

Picking the parameters

I tried every combination of 0 and 1 for each parameter and kept the one with the lowest AIC. AIC rewards a good fit but penalises extra parameters, which matters with only 48 points.

p = d = q = range(0, 2)
pdq = list(itertools.product(p, d, q))
seasonal_pdq = [(x[0], x[1], x[2], 12) for x in pdq]

for param in pdq:
    for param_seasonal in seasonal_pdq:
        try:
            model = sm.tsa.statespace.SARIMAX(y, order=param, seasonal_order=param_seasonal,
                                              enforce_stationarity=False, enforce_invertibility=False)
            results = model.fit()
            print('ARIMA{}x{} - AIC:{}'.format(param, param_seasonal, results.aic))
        except:
            continue

The best was (1, 1, 1) × (1, 1, 0, 12), with an AIC of 297.8. Several combinations failed to converge, which is another sign the dataset is small.

Checking the forecasts

I compared the model’s one-step-ahead predictions for 2017 against the actual sales:

One-step-ahead predictions for 2017 against actual sales

The predictions follow the seasonal pattern well. The RMSE was about 200, against monthly averages of roughly 400 to 1,300.

One caveat I’d point out now: the model was fitted on all the data, including 2017, so this isn’t a true out-of-sample test. A one-step-ahead prediction also gets to see the actual value from the month before. A fairer check is to fit on 2014 to 2016 only, then forecast all of 2017 in one go and compare.

Forecasting ahead

pred_uc = results.get_forecast(steps=100)
pred_ci = pred_uc.conf_int()

100-month forecast with its confidence interval

The forecast keeps the seasonal shape, but the grey confidence interval widens quickly the further out it goes. The model is honest about getting less sure over time.

Two things stand out in this plot:

  • 100 months is too far. Forecasting 8 years ahead from 4 years of history isn’t meaningful. For planning stock, a few months ahead is what matters, and that’s where the interval is narrowest.
  • The lower bound goes below zero. Sales can’t be negative, but SARIMA doesn’t know that. Modelling the log of sales would keep forecasts positive.

Comparing the categories

Running the same steps for office supplies and technology and plotting the three together:

Monthly sales for furniture, office supplies and technology

All three share the same yearly pattern, low in January and high towards the end of the year. Furniture usually sells more than office supplies per month, probably because each item costs more, although office supplies overtake furniture in a few months. Technology is the most volatile, with sharp spikes.

Because the seasonality is shared, a model per category would work, but so would a single model that uses one category’s sales to help forecast the others.

What I’d do next

  • Validate properly on a held-out year, as described above.
  • Model log sales so forecasts stay positive.
  • Forecast at a finer level, by product or region, which is where stock decisions are actually made.
  • Compare against simpler baselines (same month last year) and other methods like Prophet or gradient boosting with lag features.
  • Wrap it in a small Streamlit app, so someone can pick a category and a horizon and see the forecast.