Making time series stationary

ARIMA Models in Python

James Fulton

Climate informatics researcher

Overview

  • Statistical tests for stationarity
  • Making a dataset stationary
ARIMA Models in Python

The augmented Dicky-Fuller test

  • Tests for trend non-stationarity
  • Null hypothesis is time series is non-stationary
ARIMA Models in Python

Applying the adfuller test

from statsmodels.tsa.stattools import adfuller

results = adfuller(df['close'])
ARIMA Models in Python

Interpreting the test result

print(results)
(-1.34, 0.60, 23, 1235, {'1%': -3.435, '5%': -2.913, '10%': -2.568}, 10782.87)
  • 0th element is test statistic (-1.34)
    • More negative means more likely to be stationary
  • 1st element is p-value: (0.60)
    • If p-value is small $\rightarrow$ reject null hypothesis. Reject non-stationary.
  • 4th element is the critical test statistics
ARIMA Models in Python

Interpreting the test result

print(results)
(-1.34, 0.60, 23, 1235, {'1%': -3.435, '5%': -2.863, '10%': -2.568}, 10782.87)
  • 0th element is test statistic (-1.34)
    • More negative means more likely to be stationary
  • 1st element is p-value: (0.60)
    • If p-value is small $\rightarrow$ reject null hypothesis. Reject non-stationary.
  • 4th element is the critical test statistics
1 https://www.statsmodels.org/dev/generated/statsmodels.tsa.stattools.adfuller.html
ARIMA Models in Python

The value of plotting

  • Plotting time series can stop you making wrong assumptions
ARIMA Models in Python

The value of plotting

ARIMA Models in Python

Making a time series stationary

ARIMA Models in Python

Taking the difference

Difference: $\Delta y_t = y_t - y_{t-1}$

ARIMA Models in Python

Taking the difference

df_stationary = df.diff()
            city_population
date                       
1969-09-30              NaN
1970-03-31        -0.116156
1970-09-30         0.050850
1971-03-31        -0.153261
1971-09-30         0.108389
ARIMA Models in Python

Taking the difference

df_stationary = df.diff().dropna()
            city_population
date                       
1970-03-31        -0.116156
1970-09-30         0.050850
1971-03-31        -0.153261
1971-09-30         0.108389
1972-03-31        -0.029569
ARIMA Models in Python

Taking the difference

ARIMA Models in Python

Other transforms

Examples of other transforms

  • Take the log
    • np.log(df)
  • Take the square root
    • np.sqrt(df)
  • Take the proportional change
    • df.shift(1)/df
ARIMA Models in Python

Let's practice!

ARIMA Models in Python

Preparing Video For Download...