Let’s first load some libraries and define some helper functions (the helper functions were explainedin the previous blog posts):
1 |
|
Now, let’s load the data:
1 |
|
1 |
|
Let’s split the data into a train set, a validation set and a test set:
1 |
|
I will train the models on data from 2005 to 2012, look for the hyperparameters on data from 2013to 2016 and test the accuracy on data from 2016 to March 2018. For this kind of exercise, the idealsituation would be to perform cross-validation. Doing this with time-series data is not obviousbecause of the autocorrelation between observations, which would be broken by sampling independentlywhich is required by CV. Also, if for example you do leave-one-out CV,you would end up trying to predict a point in, say, 2017, with datafrom 2018, which does not make sense. So you should be careful about that. {forecast}
is ableto perform CV for time series and scikit-learn
, thePython package, is able to performcross-validation of time series datatoo. I will not do it in this blog post and simply focus on the genetic algorithm part.
Let’s start by defining the cost function to minimize. I’ll try several, in the first one I willminimize the RMSE:
1 |
|
If arima()
is not able to estimate a model for the given parameters, I force it to return NULL
,and in that case force the cost function to return a very high cost. If a model was successfully estimated,then I compute the RMSE.
Let’s also take a look at what auto.arima()
says:
1 |
|
1 |
|
Let’s compute the cost at this vector of parameters:
1 |
|
1 |
|
Ok, now let’s start with optimizing the hyperparameters. Let’s help the genetic algorithm a littlebit by defining where it should perform the search:
1 |
|
This matrix constraints the first parameter to lie between 0 and 3, the second one between 0 and 2,and so on.
Let’s call the genoud()
function from the {rgenoud}
package, and use 8 cores:
1 |
|
makePSOCKcluster()
is a function from the {parallel}
package. I must also export the globalvariables logged_train_data
or logged_validation_data
. If I don’t do that, the workers calledby genoud()
will not know about these variables and an error will be returned. The optiondata.type.int = TRUE
force the algorithm to look only for integers, and hard.generation.limit = TRUE
forces the algorithm to stop after 100 generations.
The process took 7 minutes, which is faster than doing the grid search.What was the solution found?
1 |
|
1 |
|
Let’s train the model using the arima()
function at these parameters:
1 |
|
1 |
|
Let’s extract the forecasts:
1 |
|
1 |
|
1 |
|
1 |
|
and plot the forecast to see how it looks:
1 |
|
The yellowish line and confidence intervals come from minimizing the genetic algorithm, and theredish from auto.arima()
. Interesting; the point estimate is very precise, but the confidenceintervals are very wide. Low bias, high variance.
Now, let’s try with another cost function, where I minimize the BIC, similar to the auto.arima()
function:
1 |
|
Let’s take a look at the cost at the parameter values returned by auto.arima()
:
1 |
|
1 |
|
Let the genetic algorithm run again:
1 |
|
This time, it took 6 minutes, a bit slower than before. Let’s take a look at the solution:
1 |
|
1 |
|
Let’s train the model at these parameters:
1 |
|
1 |
|
And let’s plot the results:
1 |
|
1 |
|
1 |
|
The solutions are very close, both in terms of point estimates and confidence intervals. Biasincreased, but variance lowered… This gives me an idea! What if I minimize the RMSE, whilekeeping the number of parameters low, as a kind of regularization? This is somewhat what minimisingBIC does, but let’s try to do it a more “naive” approach:
1 |
|
This is also similar to what auto.arima()
does; by default, the max.order
argument in auto.arima()
is set to 5, and is the sum of p + q + P + Q
. So I’ll try something similar.
Let’s take a look at the cost at the parameter values returned by auto.arima()
:
1 |
|
1 |
|
Let’s see what will happen:
1 |
|
It took 1 minute to train this one, quite fast! Let’s take a look:
1 |
|
1 |
|
And let’s plot it:
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
Looks like this was not the right strategy. There might be a better cost function than what I havetried, but looks like minimizing the BIC is the way to go.
Hope you enjoyed! If you found this blog post useful, you might want to followme on twitter for blog post updates orbuy me an espresso.