Removing trend and seasonality
As mentioned earlier, a time series is stationary if, and only if, its mean does not depend on time, its variance is constant and also does not depend on time, and the autocorrelation does not vary either. This means that any time process with a trend and seasonality is not stationary.
The ARMA and ARIMA models that we will introduce in the next recipe require the data to be stationary (or close to). Thus, in this recipe, you will learn how to remove trend and seasonality from our river flow data.
Getting ready
To execute this recipe, you will need pandas
, NumPy
, Statsmodels
, and Matplotlib
. No other prerequisites are required.
How to do it…
Statsmodels
provides convenience methods that will help us detrend and remove the seasonality from our data (the ts_detrendAndRemoveSeasonality.py
file):
def period_mean(data, freq): ''' Method to calculate mean for each frequency ''' return np.array( [np.mean(data[i::freq]) for i in range(freq)]...