Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 28 ยท Time Series

Time Series Basics

Learn the building blocks of time series in Statsmodels: trend and seasonality decomposition, autocorrelation (ACF, PACF), stationarity and the ADF test, and differencing.

A time series is a sequence of values recorded in time order: daily sales, monthly rainfall, hourly electricity demand. Order matters, so the usual assumption of independent observations fails. Yesterday's value helps predict today's, and that dependence is exactly what time-series models use.

Statsmodels' statsmodels.tsa package contains the classics. Before you fit any of them, you need four concepts: components, autocorrelation, stationarity and differencing.

Trend, seasonality and noise

A series is often described as a sum (or product) of parts:

  • Trend: the slow long-run direction.
  • Seasonality: a pattern that repeats at a fixed period (12 for monthly data with a yearly cycle).
  • Residual: what is left over.

The index of your data should be a pandas DatetimeIndex with a frequency, so Statsmodels knows the spacing and period.

Python Decompose a seasonal series

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 recovered trend rises about 0.8 per month, the number we simulated. Use model="multiplicative" when the seasonal swings grow with the level. For a more flexible method, STL (statsmodels.tsa.seasonal.STL) handles changing seasonality and outliers.

Autocorrelation: ACF and PACF

The autocorrelation function (ACF) at lag k is the correlation between the series and itself shifted k steps. The partial autocorrelation function (PACF) removes the influence of the shorter lags, isolating the direct link at lag k. Together they are the fingerprints used to choose model orders in ARIMA Models.

Python ACF and PACF of an AR(1) series

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.

For an AR(1) series the ACF fades gradually while the PACF cuts off after lag 1. A moving-average (MA) process shows the opposite pattern. The shaded bands mark the range of pure noise; bars outside it are statistically meaningful.

Pattern ACF PACF
AR(p) decays cuts off after lag p
MA(q) cuts off after lag q decays
Random noise all inside the band all inside the band

Stationarity

Most classical models need a stationary series: constant mean and variance, and autocorrelation that depends only on the lag, not on the date. A series with a trend, or one that wanders like a random walk, is not stationary. Regressing one such series on another can even produce convincing but meaningless relationships (spurious regression).

The Augmented Dickey-Fuller (ADF) test has the null hypothesis "the series has a unit root (is non-stationary)". A small p-value rejects it, which is evidence of stationarity. The KPSS test flips the null: its null is stationarity.

Python ADF test and differencing

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 random walk has a p-value above 0.05 (cannot reject non-stationarity); its first difference (y[t] - y[t-1]) is stationary. Differencing is the "I" (integrated) in ARIMA. For seasonal patterns, take a seasonal difference, y[t] - y[t-12].

Splitting and the time index

Never shuffle a time series for validation. Train on the past, test on the future: hold out the last block of observations. Keep the index as a DatetimeIndex with an explicit frequency (series.asfreq("MS")) and check for gaps.

Python A clean time index and a time-ordered split

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.

Random train/test splits leak future information into the training set and give flattering, false results. Always split by time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What do the ACF and PACF of an AR(1) series look like?

    An autoregressive process leaves a decaying ACF and a PACF with a spike only at its order.
  2. 2. The ADF test returns p = 0.62 for a series. What do you conclude?

    The ADF null hypothesis is a unit root; a large p-value fails to reject it.
  3. 3. How do you remove a yearly seasonal pattern in monthly data by differencing?

    A seasonal difference subtracts the value one full period earlier.
  4. 4. Why not shuffle time series data into random train and test sets?

    Forecasting must predict the future from the past, so validation should respect time order.

Summary

  • Time series have trend, seasonality and noise; seasonal_decompose and STL separate them.
  • ACF and PACF reveal the dependence structure and guide model orders.
  • Stationarity matters: test with ADF (and KPSS), fix by differencing.
  • Use a DatetimeIndex with a frequency and split by time, never at random.

Next: ARIMA Models