Time Series Model: A Guide
Whether we wish to predict financial market trends or electricity consumption, time is an important factor that must be considered in our models. For example, it would be interesting to forecast at what hour is peak consumption in electricity. This could be useful for adjusting the price or the production of electricity.
Enter time series. A time series is a series of data points ordered in time. In a time series, time is often the independent variable, and the goal is usually to make a forecast for the future.
However, there are other aspects that come into play when dealing with time series.
In this post, I’ll introduce different characteristics of time series and how we can model them to obtain as accurate as possible forecasts.
Informally, autocorrelation is the similarity between observations as a function of the time lag between them.
Above is an example of an autocorrelation plot. If you look closely, you’ll see that the first value and the 24th value have a high autocorrelation. Similarly, the 12th and 36th observations are highly correlated. This means that we will find a very similar value every 24th unit of time.
Notice how the plot looks like a sinusoidal function. This is a hint for seasonality, and you can find its value by finding the period in the plot above, which would give 24 hours.
More on Data ScienceA Guide to Recurrent Neural Networks: Understanding RNN and LSTM Networks
Seasonality refers to periodic fluctuations. For example, electricity consumption is high during the day and low during night, or online sales increase during Christmas before slowing down again.
As you can see above, there is a daily seasonality. Every day, you see a peak towards the evening, and the lowest points are the beginning and the end of each day.
Remember that seasonality can also be derived from an autocorrelation plot if it has a sinusoidal shape. Simply look at the period, and it gives the length of the season.
Stationarity is an important characteristic of time series. A time series is said to be stationary if its statistical properties don't change over time. In other words, it has a constant mean and variance, and its covariance is independent of time.
Looking at the same plot, we see that the process above is stationary. The mean and variance don't vary over time.
Often, stock prices are not a stationary process. We might see a growing trend, or its volatility might increase over time (meaning that variance is changing.)
Ideally, we’d want to have a stationary time series for modeling. Of course, not all of them are stationary, but we can make different transformations to make them stationary.
You may have noticed that the title of the plot above is "Dickey-Fuller." This is the statistical test that we run to determine if a time series is stationary or not.
Without getting into any technical details, the Dickey-Fuller test tests the null hypothesis to determine if a unit root is present.
If it is, then p > 0, and the process is not stationary.
Otherwise, p = 0, the null hypothesis is rejected, and the process is considered to be stationary.
For example, the process below isn't stationary. Notice how the mean isn't constant through time.
There are many ways to model a time series in order to make predictions. The most popular ways include:
The moving average model is probably the most naive approach to time series modeling. This model simply states that the next observation is the mean of all past observations.
While simple, this model can be surprisingly effective, and it represents a good starting point.
Otherwise, the moving average can be used to identify interesting trends in the data. We can define a window to apply the moving average model to smooth the time series and highlight different trends.
In the plot above, we applied the moving average model to a 24-hour window. The green line smoothed the time series, and we can see that there are two peaks in a 24-hour period.
Of course, the longer the window, the smoother the trend will be. Below is an example of a moving average in a smaller window.
Exponential smoothing uses similar logic to moving average, but this time, a different decreasing weight is assigned to each observation. In other words, less importance is given to observations as we move further from the present.
Mathematically, exponential smoothing is expressed as:
Here, alpha is a smoothing factor that takes values between zero and one. It determines how fast the weight decreases for previous observations.
From the plot above, the dark blue line represents the exponential smoothing of the time series using a smoothing factor of 0.3, while the orange line uses a smoothing factor of 0.05.
As you can see, the smaller the smoothing factor, the smoother the time series will be. This makes sense, because as the smoothing factor approaches zero, we approach the moving average model.
Double exponential smoothing is used when there is a trend in the time series. In that case, we use this technique, which is simply a recursive use of exponential smoothing twice.
Mathematically:
Here, beta is the trend smoothing factor, and it takes values between zero and one.
Below, you can see how different values of alpha and beta affect the shape of the time series.
This method extends double exponential smoothing by adding a seasonal smoothing factor. Of course, this is useful if you notice seasonality in your time series.
Mathematically, triple exponential smoothing is expressed as:
Where gamma is the seasonal smoothing factor and L is the length of the season.
More on Data ScienceUnderstanding the K-Nearest Neighbor (KNN) Algorithm
SARIMA is actually the combination of simpler models that create a complex model that can present a time series exhibiting non-stationary properties and seasonality.
First, we have the autoregression model, AR(p). This is basically a regression of the time series onto itself. Here, we assume that the current value depends on its previous values with some lag. It takes a parameter p, which represents the maximum lag. To find it, we look at the partial autocorrelation plot and identify the lag after which most lags are not significant.
In the example below, p would be four.
Next, we’ll add the moving average model MA(q). This takes a parameter q which represents the biggest lag after which other lags are not significant on the autocorrelation plot.
Below, q would be four.
After that, we’ll add the order of integration I(d). The parameter d represents the number of differences required to make the series stationary.
Finally, we’ll add the final component: seasonality S(P, D, Q, s), where s is simply the season's length. This component requires the parameters P and Q which are the same as p and q, but for the seasonal component. Finally, D is the order of seasonal integration representing the number of differences required to remove seasonality from the series.
Combining all, we get the SARIMA (p, d, q)(P, D, Q, s) model.
The main takeaway is this: Before modeling with SARIMA, we must apply transformations to our time series to remove seasonality and any non-stationary behaviors.
That was a lot of theory to wrap our head around. Let's apply the techniques discussed above in our first project.
We will try to predict the stock price of a specific company. Now, predicting stock prices is virtually impossible. However, it remains a fun exercise, and it will be a good way to practice what we have learned.
We will use the historical stock price of the New Germany Fund (GF) to try to predict the closing price in the next five trading days. (You can code along with the dataset and notebook.)
First, we’ll import some libraries that will be helpful throughout our analysis. Also, we need to define the mean average percentage error (MAPE), as this will be our error metric.
Then, we’ll import our data set and the first ten entries.You should get:
As you can see, we have a few entries concerning a different stock than the New Germany Fund (GF). Also, we have an entry concerning intraday information, but we only want end of day (EOD) information.
First, we’ll remove unwanted entries.
Then, we’ll remove unwanted columns, as we solely want to focus on the stock's closing price.
If you preview the dataset, you should see:
Now, we’re ready for exploratory data analysis.
We’ll plot the closing price over the entire time period of our data set.
You should get:
Clearly, this is not a stationary process, and it's hard to tell if there is some kind of seasonality.
Let's use the moving average model to smooth our time series. For that, we’ll rely on a helper function that will run the moving average model over a specified time window, and it will plot the result smoothed curve:
Using a time window of five days, we get:
We can hardly see a trend because it's too close to the actual curve. Let's smooth by the previous month, and previous quarter to compare the results.
Trends are easier to spot now. Notice how the 30-day and 90-day trends show a downward curve at the end. This might mean that the stock is likely to go down in the following days.
Now, let's use exponential smoothing to see if it can pick up a better trend.
Here, we use 0.05 and 0.3 as values for the smoothing factor. Feel free to try other values and see what the results are.
As you can see, an alpha value of 0.05 smoothed the curve while picking up most of the upward and downward trends.
Now, let's use double exponential smoothing.
And you get:
Again, experiment with different alpha and beta combinations to get better looking curves.
As outlined previously, we must turn our series into a stationary process in order to model it. Therefore, let's apply the Dickey-Fuller test to see if it is a stationary process:
You should see:
By the Dickey-Fuller test, the time series is unsurprisingly non-stationary. Also, looking at the autocorrelation plot, we see that it'svery high, and it seems that there's no clear seasonality.
To get rid of the high autocorrelation and make the process stationary, let's take the first difference (line 23 in the code block.) We simply subtract the time series from itself with a lag of one day, and we get:
Our series is now stationary, and we can start modeling.
Now, for SARIMA, we first need to define a few parameters and a range of values for other parameters to generate a list of all possible combinations of p, q, d, P, Q, D, s.
Now, in the code cell above, we have 625 different combinations. We will try each combination and train SARIMA with each to find the best performing model. This might take while depending on your computer's processing power.
Once this is done, we’ll print out a summary of the best model, and you should see:
We can finally predict the closing price of the next five trading days and evaluate the mean absolute percentage error (MAPE) of the model.
In this case, we have a MAPE of 0.79 percent, which is very good.
More on Data ScienceC-Means Clustering Explained
Now, to compare our prediction with actual data, we can take financial data from Yahoo Finance and create a DataFrame.
Then, we make a plot to see how far we were from the actual closing prices:
It seems that we are a bit off in our predictions. In fact, the predicted price is essentially flat, meaning that our model is probably not performing well.
Again, this is not due to our procedure, but to the fact that predicting stock prices is essentially impossible.
From the first project, we learned the entire procedure of making a time series stationary before using SARIMA to model. It's a long and tedious process with a lot of manual tweaking.
Now, let's introduce Facebook's Prophet. It's a forecasting tool available in both Python and R. This tool allows both experts and non-experts to produce high quality forecasts with minimal efforts.
The title says it all: We will use Prophet to help us predict air quality.You can code along with the full notebook and data set.
As always, we start by importing some useful libraries. We will then print out the first five rows:
As you can see, the data set contains information about the concentrations of different gasses. They were recorded at every hour for each day.
If you explore the data set a bit more, you’ll notice that there are several instances of the value -200. Of course, it does not make sense to have a negative concentration, so we will need to clean the data before modeling.
Therefore, we need to clean the data.
Here, we start off by parsing our date column to turn into "dates."
Then, we’ll turn all the measurements into floats.
After, we’ll take the average of each measurement to aggregate the data by day.
At this point, we still have some NaN that we need to get rid of. Therefore, we remove the columns that have more than eight NaN. That way, we can then remove rows containing NaN values without losing too much data.
Finally, we aggregate the data by week, because it will give a smoother trend to analyze.
We can plot the trends of each chemical. Here, we show that of NOx.
Oxides of nitrogen are very harmful, as they react to form smog and acid rain, as well as causing the formation of fine particles and ground level ozone. These have adverse health effects, so the concentration of NOx is a key feature of air quality.
More on Data ScienceTop 10 Predictive Analytics Tools to Know
We will solely focus on modeling the NOx concentration. Therefore, we’ll remove all other irrelevant columns.
Then, we’ll import Prophet.
Prophet requires the date column to be named ds and the feature column to be named y, so we make the appropriate changes.
At this point, our data looks like this:
Then, we define a training set. For that we will hold out the last 30 entries for prediction and validation. Afterwards, we simply initialize Prophet, fit the model to the data, and make predictions.
You should see the following:
Here, yhat represents the prediction, while yhat_lower and yhat_upper represent the lower and upper bound of the prediction respectively. Prophet allows you to easily plot the forecast, and we get:
As you can see, Prophet simply used a straight downward line to predict the concentration of NOx in the future.
Then, we can check if the time series has any interesting features, such as seasonality:
Here, Prophet only identified a downward trend with no seasonality.
Evaluating the model's performance by calculating its mean absolute percentage error (MAPE) and mean absolute error (MAE), we see that the MAPE is 13.86 percent and the MAE is 109.32, which is not that bad. Remember that we did not fine tune the model.
Finally, we just plot the forecast with its upper and lower bounds:
You’ve now learned how to robustly analyze and model a time series and applied your knowledge in two different projects.
, .