Introduction to Time Series
Time series can be a bit difficult at first. There are new lingo that needs to be learned in addition to all the other statistical jargon. This blog explains the must-know words for time series so that we have a foundation to build on when diving deeper. Those words are stationarity, level, noise, trend, seasonality, and differencing.
When forecasting, the task is to predict the next value given some historical data. If we have historical data that looks like the graph below, what should be the next value?
Clearly the next value should be 100. Since the historical data is constant, it is really easy to predict the next value. We say this time series is stationary (we can think of it as the series is “stationed” at one value). Stationarity is used to describe a time series that doesn’t change over time. Mathematically speaking, if the mean and variance of the values of the time series are constant, then it is stationary. Level is the data representing the straight line.
Stationarity would be the ideal scenario but in the real world the data can be very hectic. There can be some jitters in the time series that hides the stationarity. These jitters are called noise. Noise is data that is random and cannot be predicted. Adding some noise to the graph above looks like this:
In this noisy example, the data is not stationary anymore. The value is not value is not exactly constant. Mathematically, the mean and variance is changing over time. However, visually we can estimate the next value should be around 100.
Trend is the overall direction that the time series is heading. An example would be “What is trending in Twitter”. What news seems to mention a lot over time? The graph below shows an upward trend.
As we can see, trends make sure that the data is NOT stationary. The values change over time.
Seasonality is a time series that repeats itself periodically. It makes predictable changes that occur regularly. The cosine graph (figure below) is a perfect example of seasonality. The red rectangle shows a season of the graph that keeps on repeating.
An example can be the sales of jackets in NYC. When winter hits, people are more likely to buy jackets. But as the weather transitions to spring and summer, sales of jackets will go down. And then go back up when winter gets closer.
Ideally we would like a stationary time series. However that is never the case. Time series have some trends, some seasonality, and some noise. What does that look like? Let us combine trend, seasonality and the noise.
The graph above now has the cosine seasonality with an upward trend and some noise. Now it almost looks like a time series that we are used to seeing. Having all three of them (noise, trend, and seasonality) will make it a bit harder for us to predict the next value. Something like just averaging will not be enough. However if we can remove the trend, seasonality and some noise, we might be left with a series that might be stationary or at least something easier to deal with.
One common technique used to do just that is differencing. Differencing is computing the differences between consecutive observations. In other words, just subtracting a value from the previous value. This will help remove seasonality and trend. If we difference our noisy, upward-trend cosine graph, we are left with this:
This series, after differencing, looks a lot more stationary (minus the noise). It will be more convenient to predict with it. If we were to predict using differencing, remember that we are predicting the change. To get the final prediction, we will have to add back the trend and seasonality.
This was spoof data created to explain the content. However in the real world, the data is not that simple, for example stock prices (figure below). Looking at the graph, it is not stationary and there is no clear seasonality or trends. We will have to bring out the big models, such as ARIMA, RNN, CNN, and others, to help us understand those series better.