**One of the most popular regression methods an econometrician learns is the Ordinary Least Squares (OLS). It is a simple and elegant way of estimating parameters in linear regression. However, there is another technique to perform linear regression using concepts from machine learning. This concept is called gradient boosting and is also related to decision trees.**

**Decision trees**

A decision tree uses a tree-like model of decisions and possible consequences. It is a very common data mining algorithm used for operations research, specifically in decision analysis. The idea behind it can easily be understood with a small example. Suppose we want to decide whether to take the bus or walk to a certain destination depending on certain parameters. These parameters could include: what the weather is like, how much time it takes to get there or whether you are hungry. Then a simple decision tree can be made as is shown in the figure below.

Decision trees are also used for machine learning principles. This is called decision tree learning and it is a method commonly used in data mining. The goal is to create a model that predicts the value of a target variable based on several input variables. In our example, the target variable is whether to take the bus or walk and the input variables are the weather, the amount of time and whether we are hungry or not. The yellowish squares at the end of the decision tree are called leaves. These decision trees will be used when we will talk about gradient boosting.

Moreover, decision trees can already actually be of really good use. This is due to the fact that these trees are simple to construct and understand. However, there are some limitations to the usage of these trees. For example, decision trees can be non-robust, meaning that a small change in the training data set can result in a significant change in the final prediction or the tree itself. A solution to this is to use multiple decision trees, which is an ensemble method. If you want to learn more about ensemble methods I mention them in one of my previous articles about machine learning in financial markets. Moreover, the ensemble method of using multiple decision trees is used in many boosting methods such as gradient boosting which is explained in the following section.

**Gradient boosting**

Gradient boosting for regression was developed in 1999 by Jerome H. Friedman. Like other boosting methods, boosting can be interpreted as an optimization algorithm on a suitable cost or loss function. The word gradient is used here because we will use the derivative of the loss function for optimization. So you may wonder why I was talking about decision trees before. This is simply because it is very common that gradient boosting is used with decision trees. This will become more clear when explaining the algorithm.

It is easiest to explain this in the least-squares regression setting, where we have a model that we want to “teach” to predict the values by minimizing the mean squared error ([expand title= MSE] The general formula for this is: , where is the predicted value and is the observed value. [/expand]). Here is simply the explanatory variables in our model. Now we want to “boost” this procedure with an algorithm that includes stages. At each stage () suppose we have an imperfect model which we want to improve. This can be done by adding a new estimator to the model as such:

(1)

Or equivalently,

(2)

So gradient boosting will actually fit to the residual of the regression at each stage, where each attempts to correct the errors of its predecessor .

As mentioned earlier, the algorithm in least-squares setting tries to minimize the MSE at each stage of the learning process, which means that the loss function in our context is actually the MSE itself. There are other loss functions that could be used, but for the sake of simplicity, we will stick to this function. So we define our loss function to be:

(3)

The factor is used to make computations simpler. With this we have our input of the algorithm assuming we have a training data set .

Moving on to the first step of the algorithm, we must initialize a model with a constant value, which will look like the following:

(4)

where is simply the predicted value of our base model. The first step simply means that we need to find a that minimizes the loss function. So plugging in our loss function we would get:

(5)

That means that our initial predicted value is simply the sample average.

Now we will move on to step 2, where the decision trees will be introduced. We will first calculate the so-called pseudo residuals with the general formula:

(6)

This looks like a nasty formula, but it is actually something we already calculated. We take the derivative of the loss function with respect to the predicted value and fill in the predicted value of the previous iteration, which is . Note that this is only the case for a loss function that is the MSE. Furthermore, we have that for , which is the initial predicted value. Thus, is simply equal to , as we had a minus sign in front of the derivative in the equation for . Now we have found the residuals, we must fit a regression tree to the values and create terminal regions , for , where is the number of leaves the tree has. This is illustrated in the figure below. We see that for the small data set, the residuals for each observation have been calculated and that a regression tree is constructed based on these residuals. The terminal regions are simply labels that we put on the leaves to keep track of them during each stage of the algorithm.

Note that in real practice decision trees usually have more leaves and sometimes even multiple decision trees are used, but this is simply to illustrate how the process works. In the next part we calculate the output values for each leaf. This is done by doing something similar as in step 1:

(7)

The difference from step 1 is that we now include the output of the previous step whereas in step 1 there was no previous step. Furthermore, we do not take the summation over all samples, only the summation over all samples that belong to a specific leaf in the regression tree. Note, given our choice of loss function, the output values will always be the average of the residuals that end up in the same leaf. So taking the two leaves in the figure above we have and .

At last, we simply make a new prediction for each sample using:

(8)

where is the so-called learning rate and ranges from 0 to 1. Usually, this learning rate is set to a low number to increase the accuracy of the model in the long run. According to Leslie N. Smith from the US Naval Research Laboratory, you could estimate a good learning rate by training the model initially with a very low learning rate and increasing it either linearly or exponentially at each iteration, but this is beyond the scope of this article.

All of this is iterated until stage of the algorithm. At the last stage, we should get an output value of that has a very low mean squared error and a very accurate prediction for each observation. This is a very detailed description of how gradient boosting works.

**XGBoost**

eXtreme Gradient Boosting takes gradient boosting even further. It became well known in the machine learning competition circles after its usage in the winning solution of the Higgs Machine Learning Challenge. What makes XGBoost different is that it takes into account a regularization term to avoid overfitting of the model. On the top right of the figure below we see an example of overfitting a model. The best fit is shown in the lower right corner.

So in principle, XGBoost is simply gradient boosting but the loss function looks a bit different as it takes into account the possible overfitting unextreme gradient boosting is known to be prone to. It consists of a regularization term that takes into account the output value of the decision trees. This may look like something as follows:

(9)

where is the prediction of observation and can be seen as the score on the -th leaf. Furthermore, is the number of leaves in the decision tree, which the user can set. The last term is the regularization part of our new equation. Now we want to minimize this objective function with respect to the scores of each leaf. I will not show how this is calculated as it is a complicated proof, but for the interested reader you can find it here. From this proof we actually get that the optimal value is:

(10)

where is the first derivative of the loss function with respect to and is the second derivative of the loss function with respect to . Note that is a scaling factor that can be set by the user who wants to run the algorithm. The larger the value of , the more emphasis we give on the regularization penalty, which will lead the optimal output value to get closer to 0. This is exactly what regularization is supposed to do.

Noticing that in the formula for , is simply the negative residual for observation as we have that our loss function is the MSE. One can show this by simply taking the derivative of with respect to , which is equal to . This expression is simply the negative residual for the -th observation. Therefore, for we take the derivative of with respect to , which equals to . Hence, we can rewrite as follows:

(11)

This is often called the similarity score and XGBoost uses it to calculate the optimal output value for each leaf in the decision trees. Notice that this is almost exactly the same formula for the output value, , used in unexetreme gradient boosting. The only difference here is that we have a regularization paramater in our denominator.

**OLS vs XGBoost**

OLS has proved itself to be very useful in finding linear connections in your data. However, with XGBoost this can be done with much more precision, especially when your data set is complex. Nonetheless, the real downside of XGBoost is that it is very prone to overfitting the data. We saw that we can usually solve this by penalizing the regularization term more, but it can still have this issue. In addition, these kinds of boosting models suffer from high estimation variance compared to the linear regression with OLS. This could be due to the fact that we have a rather complex parameter space, whereas OLS has a very simple parameter space.

In addition, OLS is usually more intuitive which makes it a lot more user friendly. Furthemore, OLS gives us an idea of how we should model our data and is very interpretable. So, XGBoost will generally fit training data much better than linear regression, but it is also prone to overfitting and is less easily interpreted. However, boosting algorithms are far more superior to OLS when it comes to high dimensional data, as these algorithms always pick the most relevant variables. Thus, either one may end up being better, depending on your data and your needs. Therefore, you should always check whether the complicated model is actually needed in your case.

**References**

Chen, T., & Guestrin, C. (2016). XGBoost: A Scalable Tree Boosting System. *Proceedings of the 22nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining*. PDF

Friedman, J. H. (2002). Stochastic gradient boosting. *Computational Statistics & Data Analysis*, *38*(4), 367–378. https://doi.org/10.1016/s0167-9473(01)00065-2

L. N. Smith, “Cyclical Learning Rates for Training Neural Networks,” *2017 IEEE Winter Conference on Applications of Computer Vision (WACV)*, Santa Rosa, CA, USA, 2017, pp. 464-472, doi: 10.1109/WACV.2017.58. Link

*This article is written b**y Sam Ansari*