Time series are stationary if they do not have trend or seasonal effects. Summary statistics calculated on the time series are consistent over time, like the mean or the variance of the observations. It can be observed easily through plots or summary statistics.
So this recipe is a short example on how to determine if a time series is stationary. Let's get started.
import numpy as np import pandas as pd import matplotlib.pyplot as plt
Let's pause and look at these imports. Numpy and pandas are general ones. Here matplotlib.pyplot will help us in plotting
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', parse_dates=['date']).set_index('date')
Here, we have used one time series data from github. Also, we have set our index to date.
Now our dataset is ready.
We have simply plotted dataset, taking time on x axis and values on y axis.
X = df.value split = round(len(X) / 2) X1, X2 = X[0:split], X[split:] mean1, mean2 = X1.mean(), X2.mean() var1, var2 = X1.var(), X2.var()
We have split our dataset in two set. Next, we are trying to caluclate mean and variance of both split dataset.
print('mean1=%f, mean2=%f' % (mean1, mean2)) print('variance1=%f, variance2=%f' % (var1, var2))
Simply print the mean and variance.
Once we run the above code snippet, we will see:
Scroll down the ipython file to visualize the output.
A disparity in values can be seen indicating presence of non-stationary points.