Random Forests and Boosting for ARCH-like volatility forecasts
Tree models are not just useful for point and mean forecasts.
In the last article, we discussed how Decision Trees and Random Forests can be used for forecasting. While mean and point forecasts are the most obvious applications, they might not always be the most useful ones.
Consider the classic example of financial returns, where the conditional mean is hard, if not impossible, to predict. Conditional variance on the other hand has been shown to exert some auto-regressive properties that can be modelled. In fact there exist countless models from the (G)ARCH-family that enjoy widespread popularity.
Most GARCH models make primarily linear assumptions about the auto-regressive patterns. This begs the question if we can introduce non-linear relationships for more flexibility. Given the promising performance of tree models for mean forecasts, the roadmap is clear. Can we build respective models for variance forecasts?
Conditional variance and modelling options
Although there exists a lot of information on conditional variance models on the internet, let us quickly walk through the basics. For more insights, feel free to study the references in the GARCH wikipedia article. Alternatively, you might also get some additional insights from these two articles here and here.
To get started, we state our forecast target:
Pretty simple - we want to predict variance of a time-series based on the series' past realizations. There is only one problem: We do not observe the time-series variance.
Of course, this is also true when for the time-series' mean. However, by minimizing the mean-squared-error, our model will generate predictions for the mean (a.k.a. expected value). In a time-series modelling problem, this looks as follows:
In words: A model that minimizes the MSE of the conditionals can be used as an estimator for the conditional mean. Of course, this requires the set of admissible models to be large enough. A linear model, for example, will fail as an approximation for a highly non-linear conditional mean.
Luckily, Decision Tree ensembles are very flexible and thus should make a decent set of candidate models.
Now hopes are up that there exists a suitable loss function that will find a similar estimator for conditional variances. Indeed there is a very convenient way:
Estimating Variance directly
This approach requires only a tiny bit of probability theory and a loose assumption on our data. Recall that the variance of any random variable can be decomposed as follows:
Notice that the expectation on the right can be estimated via the MSE minimization from before. Thus, with a respective estimator (e.g. Decision Tree ensemble), we can make use of the next formula:
Obviously, we don't observe the actual conditional mean but only have our tree-based estimator at hand. Therefore:
Plugging this back into the variance formula, we get:
This implies that the variance of our target variable equals the expectation of the squared mean-transformed variable.
Notice that we conditioned the transformed variable on the lagged original time-series. We can equally condition the transformed variable on the lagged transformed time-series. The latter is obviously much more convenient and popular. Thus, we'll use this approach from now on.
Going back to our first equation, we can now easily build an estimator for this mean:
We can now conclude the following:
If we train another model to minimize MSE on the squared transformed variable, that model becomes an estimator for the conditional variance.
Obviously, this method has one caveat: The model must only produce non-negative predictions. Otherwise, there is no guarantee that we won't receive negative variance predictions on a test set.
This is where Decision Trees and Random Forests come in handy. The squared data for training is always non-negative. Therefore, a trained Decision Tree will only produce non-negative predictions, too.
A raw linear regression model on the other hand would not have such guarantees. Unfortunately, this also excludes Gradient Boosted Trees from the range of possible models. Standard Boosting produces a weighted sum of Decision Trees. Consequently, a single negative weight could result in negative predictions.
There is, nevertheless, a way to make Gradient Boosting work as well:
Using an adjusted loss function
Since Gradient Boosting is so powerful, it would be a bummer to not be able to use it for our problem. Luckily, most popular Boosting libraries allow to define custom loss functions. This is exactly what we will do here.
Consider again the mean-subtracted variable:
As a result we can impose the following distributional assumption:
The output from a Gradient Boosting algorithm itself can be negative. Adding exponentiation ensures non-negative variance. Now we simply need to translate the above into a log-likelihood loss function and calculate gradient and hessian. Then we plug everything into a Boosting algorithm:
Keep in mind that we need to apply the chain-rule to our additional exponential transformation.For Microsoft's LightGBM package, our custom loss would now look as follows:
Now, let us implement both approaches and compare them against benchmark models:
Tree ensembles for volatility forecasts in Python
In general, the implementation should be straightforward. Apart from avoiding careless mistakes, there is nothing too special that be need to be aware of.
As a dataset, we'll use five years of standardized Dow Jones log-returns:
Notice that we are performing a z-Normalization. This will make the subsequent GARCH model more robust to degenerate scaling of the time-series. Also, we reserve the last 30 days of data for a test set. Be careful not to introduce lookahead bias here.
Finally, it is reasonable to presume that conditional expected returns are always zero, i.e.
Otherwise, somebody else would have likely discovered this anomaly before us and arbitraged it away. Therefore, we don't need to apply the mean-subtraction in the first place. Rather, we'll directly use raw log-returns for our variance estimation.
Random Forest volatility forecasts
For the Random Forest model, we use
RandomForestRegressor. To limit the risk of overfitting, we run the algorithm with a
max_depth=3. Given the typically noisy behaviour of financial time-series, such regularization seems reasonable.
Finally, we set
k=5 which lets our algorithm consider the past five observations for forecasting. While we could set this lower or higher, there is a tradeoff between omitting information and overfitting. In production, you would obviously do more backtesting to find the optimal number of lags.
In code, we now have
To generate forecasts, we apply Monte-Carlo sampling. Then, we use those samples to estimate the 90% forecast interval. This is necessary because our model can only predict conditional variance one step ahead. Presuming Gaussian noise, this looks as follows:
50000 samples per timestamp should suffice for now. If you need more accuracy, feel free to increase this amount by a lot.
Gradient Boosting volatility forecasts
The Gradient Boosting variant is quite similar to the Random Forest approach. We only need to take care of the LightGBM-specifics for data preprocessing. Also keep in mind that our target now needs to be the raw, z-normalized time-series, not the squared one.
To check if our models are actually any good, let us compare them against two benchmarks:
GARCH and kernel density benchmarks and evaluation
Since we want to improve conditional volatility forecasts, GARCH seems to be the most obvious comparison. To match the number of lags in our tree ensembles, we'll use a GARCH(5,5) model.
As a second benchmark, let us use a simple i.i.d. kernel density fit. I.e. we presume that each future observation is drawn independently from the same density as the training data:
At last, the actual evaluation. As a performance measure, we want to use the out-of-sample log-likelihood - the higher the better. That way we will see which forecasted probability density performs best on our test set.
Since we could only sample from the forecast distributions, we'll fit a kernel density to each of the Monte-Carlo samples. For our benchmark, we obviously have the kernel density fit from the training data already.
Finally, we calculate the log-likelihood for the kernel densities as a proxy:
In fact, our tree models perform best for our 30 day test set. Surprisingly, the GARCH model performed worse than our kernel density benchmark. Keep in mind though that a single evaluation doesn't allow any generalizing conclusions.
Nevertheless, our Random Forest and Gradient Boosting ARCH models appear to work reasonably well.
This article gave a quick demonstration of how tree ensembles can be used for volatility forecasts. Although the example models could possibly be enhanced a lot, the initial ideas already seem to work quite well.
Possible enhancement could be a better choice of conditional distributions. I.e. all our models, except the kernel density benchmark, worked with Gaussianity assumptions. In for financial time-series, this might obviously be a sub-optimal choice. If, for example, we wanted to account for heavy conditional tails, a Student's T-distribution would be better suited.
PS: Notebook for this article is available on GitHub.
 Breiman, Leo. Random forests. Machine learning, 2001, 45.1, p. 5-32.
 Bollerslev, Tim. Modelling the coherence in short-run nominal exchange rates: a multivariate generalized ARCH model. The review of economics and statistics, 1990, p. 498-505.
 Ke, Guolin, et al. Lightgbm: A highly efficient gradient boosting decision tree. Advances in neural information processing systems, 2017, 30