5. Moving averages and the parts of a time series
Level 8: Time Series - Stationarity
Moving average smoothing is a naive and effective technique in time series forecasting. It can be used to prepare data, for feature engineering and even directly to make forecasts. Smoothing means applying a technique that removes the fine-grained variation between consecutive steps, in the hope of stripping out noise and better exposing the signal of the underlying causal processes.
# trailing moving average (uses only the past)
rolling = df.rolling(window=3)
rolling_mean = rolling.mean()
rolling_mean.head(10)
# compare original and transformed
df[-100:].plot()
rolling_mean[-100:].plot(color='red')This is a trailing moving average: the mean in row t is computed from t-2, t-1 and t. That detail matters because there is also the centered moving average, which uses future values and therefore is only good for descriptive analysis, never as a feature in a predictive model. The first two rows come out null: not until the third observation are there three values to average.
The moving average as a forecast. The moving average value can be used directly to make forecasts. It is a naive model and it assumes that the trend and seasonality components have already been removed or adjusted for. It lends itself very well to a walk-forward scheme: as new observations arrive (for instance, each day), the model is updated and a forecast is made for the following day.
X = df.values
window = 3
history = [X[i] for i in range(window)]
test = [X[i] for i in range(window, len(X))]
predictions = []
# walk forward over the time steps of the test set
for t in range(len(test)):
length = len(history)
yhat = np.mean([history[i] for i in range(length - window, length)])
obs = test[t]
predictions.append(yhat)
history.append(obs)
rmse = np.sqrt(mean_squared_error(test, predictions))
print('RMSE: %.3f' % rmse)
plt.plot(test)
plt.plot(predictions, color='red')
plt.show()
plt.plot(test[:100]) # zoom
plt.plot(predictions[:100], color='red')
plt.show()The loop is the essence of walk-forward: you forecast with what you know, you observe the actual value, you append it to the history and only then do you move to the next step. At no point is future information used. When you zoom in you will see that the red line runs systematically behind the blue one: any smoothing introduces lag, and that lag is exactly the price you pay for reducing noise.
The components of a time series. A useful abstraction for choosing forecasting methods is to decompose the series into systematic and non-systematic components.
- Systematic: components with consistency or recurrence, which can be described and modeled.
- Non-systematic: components that cannot be modeled directly.
A series is taken to consist of three systematic components and one non-systematic one:
- Level: the average value of the series.
- Trend: the increase or decrease in value over time.
- Seasonality: the repeating short-term cycle.
- Noise: the random variation in the series (this is the non-systematic one).
Our series may contain a trend, that is, a sustained increase or decrease over time, and there may be an advantage in identifying it, modeling it and even removing it. It may also contain a seasonal component, a cycle that repeats monthly or annually. That repeating cycle can obscure the signal we want to model or, on the contrary, supply a strong signal to our predictive models. There are many kinds of seasonality: time of day, daily, weekly, monthly, annual. Determining whether seasonality exists is partly subjective, and the simplest approach is to plot the data and review them at different scales, perhaps adding trend lines.
A series from which the seasonal component has been removed is called seasonally stationary. If the dataset has no trend, or we have managed to remove it, it is said to be trend stationary.