DEV Community

Rupali Gauravaram
Rupali Gauravaram

Posted on Originally published at chaiandcode.wordpress.com on

Two SARIMA models, one clear winner: picking with AIC value

This prompt seemed challenging from the start. Not only did it require me to write fewer than 40 lines of code, I also had to create my own time series dataset and build at least two SARIMA models to identify the best one using the Akaike Information Criterion (AIC).

Just as I was trying to process these constraints, I had an idea for the dataset. Where I live, we have an apartment grocery store where we buy dairy products like milk and curd almost daily. Since dairy is perishable, I decided to analyse 3 months of milk sales, which could help forecast the store’s stock requirement for the next month.

I created a dataset of daily dates from September to November 2026 (90 days), took a one-week sample of milk packets sold, repeated it across the 90 days and added random noise (normal distribution, standard deviation of 3). I rounded the values to integers so the packets stay whole numbers, and stored everything in a DataFrame called packets. I modelled packets rather than revenue because the shop restocks in packet numbers; revenue is just packets × ₹27, so the model choice would still be the same if I used revenue instead.

Next, I plotted packets_sold to see whether the series was additive or multiplicative. This matters for seasonal decomposition (splitting the series into trend, seasonality and noise): swings that grow over time are multiplicative, while swings that stay roughly constant are additive.


Plot showing packets_sold over 90 days

The swings have a roughly constant amplitude across all 90 days, so the series is additive. I then split the data into train and test sets in time order (80:20).


# train test splitX,y = packets[:-18], packets[-18:]
Enter fullscreen mode Exit fullscreen mode

Note: shuffling time series data lets future points into training, so the model effectively sees the future and the test scores look better than they really are.

After splitting the data, I ran seasonal_decompose on the training data (additive, period = 7 for the weekly cycle):


Plot showing seasonal decompose: Trend, Seasonality and Noise

The plot shows no real trend, so at first glance we won’t need non-seasonal differencing. I then checked whether the data is stationary with the Augmented Dickey-Fuller (ADF) test, choosing a significance level (alpha) of 0.05 before running it.

Note: the ADF test’s null hypothesis (H0) is that the data is non-stationary. If the p-value is below alpha, we reject H0 and treat the data as stationary; otherwise we fail to reject H0, and the data needs differencing to become stationary.

The ADF test showed that the training data is stationary, so the non-seasonal differencing order is d = 0.

Next, I applied first-order seasonal differencing (D = 1, seasonal period of 7): today’s sales minus sales from 7 days ago.

Then I plotted the ACF and PACF of the differenced series:

Neither plot shows significant lags at 1–4, so p = 0 and q = 0. A single negative spike at lag 7 that cuts off in the ACF points to a seasonal MA(1) term; a seasonal AR(1) term is the alternative.

So I built and fitted two models, model 1 as SARIMA(0,0,0)(0,1,1,7) and model 2 as SARIMA(0,0,0)(1,1,0,7), and compared their AIC values.

AIC balances how well a model fits the data against how complex it is, and lower is better. It can’t judge a single model on its own, it only compares models trained on exactly the same data.

As a rule of thumb:

  1. If the difference between AIC1 and AIC2 i.e. delta <= 2, the weaker model still has substantial support to be considered.
  2. If 4 <= delta <= 7, support is considerably less.
  3. If delta > 10, there is a significant difference between the model performances, support is low, choose the model with lower AIC value.

Model 1 scored 349.58 and model 2 scored 366.76 , a gap of -17.18 , so model 1, SARIMA(0,0,0)(0,1,1,7), is the clear winner.

Selecting a model via AIC is efficient, clean, and mathematically rigorous. It gives us an immediate compass for model selection when we’re deep in the tuning phase. Even so, a low AIC only tells us how well the model explains the past, not necessarily how robustly it will handle the unpredictable future.

Would you trust AIC alone, or always check the forecast error on held-out data too?


This was Day 6 of my 30 Days of Data Science writing challenge. Building this model under 40 lines with a small hand-built dataset was challenging but super fun. Through this I got a better understanding on how to pick the time series model components.

You can see the complete notebook on my GitHub: https://github.com/Rupali-Gauravaram/dev-prompt-generator/blob/main/posts/post_06.ipynb

Top comments (0)