# Ensembling Methods

# Introduction

In Decision Tree section, we talked about how decision tree is applied in regression and classification task and how we can grow a decision tree. However, as also mentioned in the section, decision tree has limited power to generalize well. Thus, it has been proposed to use ensembling methods. In a word, multiple trained models perform better than the single model. But why?

Let’s have n i.i.d. random variables $X_i$ for $0 \leq i \leq n$ and assume $Var(X_i) = \sigma^2$ for all $X_i$. Then, we have the variance of the mean:

If we remove the independent assumption, then each random variable is correlated.

where p is pearson correlation coefficient $p_{X,Y} = \frac{Cov(X,Y)}{\sigma_x\sigma_y}$. We know that Cov(X,X) = Var(X).

**Math**: The following proof might be helpful for understanding the steps above.

**Back to topic**: Now, if we treat each random variable is the error of a trained model, we can see that the variance can be reduced by:

**1,** increase the number of random variable (i.e. number of models) n to make the second term smaller

**2,** reduce the correlation between each random variable to make the first term smaller, and it become more i.i.d.

How do we achieve those? Here, we will introduce **Bagging** and **Boosting**.

# Bagging

## Bootstrap

Bootstrap is basically a re-sampling technique for improving estimators on the data. In this algorithm, we keep sampling from the empirical distribution of the data to estimate statistics of the data.

Let’s say we have an trained estimator E for the statistic such as median of the data. The question is how confident our estimator is and how much variance it is. We can use bootstrap to find out. In bootstrap algorithm, we do:

**1,** Generate bootstrap samples $\mathbb{B}_1,\dots,\mathbb{B}_B$ where $\mathbb{B}_b$ is created by picking n samples from the dataset of size n **with** replacement

**2,** Evaluate the estimator on each $\mathbb{B}_b$ as:

**3,** Estimate the mean and variance of E:

This can tell us how our estimator performs on estimating the median of the data.

## Bagging

Bagging basically uses the idea of bootstrap for regression or Classification. It represents **Bootstrap aggregation**.

The algorithm is as the following:

For $b=1,\dots,B$,

**1,** Draw a bootstrap $\mathbb{B}_b$ of size n from training dataset

**2,** Train a tree classifier or tree regression model $f_b$ on $\mathbb{B}_b$.

To predict, for a new point $x_0$, we compute:

For regression problem, we can see this is just the average of of prediction of each trained classifier. For classification task, we can use a voting mechanism for the final result.

For example, let’s say we have an input feature $x\in \mathbb{R}^5$ for a binary classification. We can use bootstrap strategy to train multiple classifier as:

Let’s back to the equation:

As we talked about, one way to reduce the variance is that we have less correlation on each trained model. Bagging achieves this by training on different datasets. One might concert about the fact that this will increase the bias since each bootstrap does not take the full training samples from the original dataset. However, it turns out that the decrease in variance is more than the increase in bias. Also, we can keep reducing variance by introducing more models (i.e. increase M or n in the equation). This will not lead to overfitting because $p$ is insensitive to M. Thus, overall variance can only decrease.

However, there are two key points that should be emphasized:

**1,** With bagging,each tree does not need to be perfect. “Ok” is fine.

**2,** Bagging often improves when the function is non-linear.

### Out-of-bag estimation

In each bootstrap, we can only contain a portion of original dataset. Let’s assume that we sample it from uniform distribution with replacement. As dataset size $n\rightarrow \infty$, we have the probability of a sample not being selected as:

This is roughly one third. That means one third of data not being selected in a single bootstrap. To test our bagging-trained models, for i-th sample, we can ask those models (approxmiately M/3 models) that are not trained on this sample to make prediction. By doing this over the entire dataset, we can obtain the out-of-bag error estimation. In the extreme case where $M\rightarrow\infty$, those models that are not trained on i-th sample are trained on all other samples. This gives us the same results as leave-one-out corss-validation does.

## Random Forests

There are drawbacks of Bagging. The bagged tress trained from bootstrap is related because bootstraps are correlated. This is unwanted because we want to have less correlation. The bagging will not be able to have the best performance. Thus, random forest is proposed. The modification is small but works. Instead of growing a tree on all dimensions, random forests propose to grow a tree on randomly selected subset of dimensions. In details,

For $b=1,\dots,B$,

**1,** Draw a bootstrap $\mathbb{B}_b$ of size n from training dataset

**2,** Train a tree classifier on $\mathbb{B}_b$. For each training, we randomly select a predefined m dimensions of d ($m \approx \sqrt(d)$). For each bootstrap, we have different m dimensions.

# Boosting

We know that bagging is to reduce the variance from a single tree. Boosting is, on the other hand, to reduce bias. In bagging, we generate bootstrap sample to train each individual model. In boosting, we re-weighted each sample in the bootstrap after every training iteration. Graphically, we have:

Formally, we have the AdaBoost Algorithm as:

**1,** Initialize $w_i \leftarrow \frac{1}{N}$ for $i=1,2,\dots,n$ and it is binary classification.

**2,** For m=0 to M:

Sample a bootstrap dataset $B_m$ of size n according to distribution $w_t(i)$

Fit the model $F_m$ on bootstrap $B_t$

Set $\epsilon_m = \sum_{i=1}^n w_m(i) \mathbb{1}[y_i\neq F_m(x_i)] $ and $\alpha_m = \frac{1}{2}\ln\frac{1-\epsilon_m}{\epsilon_m}$

Scale $\bar{w}_{m+1}(i) = w_m(i)\exp(-\alpha_m y_i F_m(x_i))$ and normalize

$w_{m+1}(i) = \frac{\bar{w}_{m+1}(i)}{\sum_j \bar{w}_{m+1}(j)}$

**3,** The classification rule is $f_{boost}(x_0) = sign(\sum_{m=1}^M \alpha_m)$

In each iteration, the misclassified samples are up-weighted cumulatively. The final prediction is weighted by weighted error. The summation allows additive terms for adding more modeling capability but will result in a high variance since each trained model is dependent. Thus, increasing M will increase variance as well.

## Analysis of boosting

it is worth talking about the accuracy of boosting on training data. This is purely theoretic, and you can skip it if you want.

**Theorem**: With AdaBoost algorithm, if $\epsilon_m$ is the weighted error of classifier $f_m$, then the final classification $f_{noost}(x_0)=sign(\sum_{m=1}^M \alpha_mf_m(x_0))$. The training error can be bounded as:

What it means that even though each $\epsilon_m$ is just a little better than random guessing, the sum over M models can produce a large negative value in the exponent when M is larger. Thus, we have a small upper bound.

**Proof**:

To prove this, we want to find an intermediate value as the stepping stone. That is, we find a < b and b < c, then a < c.

Recall that:

Let’s define:

Now, we can re-write:

We can use this to re-write:

We know that $w_1(i) = \frac{1}{n}$ since I initialized this way. So we have:

where we define $h_M(x) = \sum_{m=1}^M \alpha_m F_m(x)$. And $\prod_{m=1}^M Z_m$ is our “b” above. Next, we can re-write the weights as:

Then, we can plug our training error back. Note that $0 < \exp(z_1), 1<\exp(z_2)$ for any $z_1 <0< z_2$. We have:

We have shown that the training error is less or equal to an intermediate value “b”. Then, we work on a single $Z_m$:

where $\epsilon_m = \sum_{i:y_i\neq F_m(x_i)} w_m(i)$. If we minimize $Z_m$ with respect to $\alpha_m$, we can get:

This is exactly what we have set up at the beginning.

We can plug this back to find out:

We know that $1 - x \leq \exp(-x)$. Then, we can say:

$$Z_m = (1 - 4(\frac{1}{2} - \epsilon_m)^2)^{\frac{1}{2}} \leq (\exp(-4(\frac{1}{2} - \epsilon_m)^2))^{\frac{1}{2}} = \exp(-2(\frac{1}{2} - \epsilon_m)^2)

For all $Z_m$, we can have:

## Forward Stagewise Additive Modeling

Before talking about a new boosting algorithm, it is worth talking about the general framework of ensembling. It is called **Forward Stagewise Additive Modeling**. In details, we have

**Input**: Labeled training data $(x_1,y_1),\dot,(x_N,y_N)$

**Output**: Ensemble classifier f(x)

1, Initialize $f_0(x) = 0$

2, for m=1 to M do:

Compute $(\beta_m,\gamma_m) = \arg\min_{\beta,\gamma}\sum_{i=1}^N L(y_i,f_{m-1}(x_i) + \beta G(x_i;\gamma))$

Set $f_m(x) = f_{m-1}(x) + \beta_m G(x;\gamma_m)$

3, Output $f(x) = f_m(x)$

In each iteration, we fix the weights and parameters of all the trained models from previous steps. We have a weak learner G(x) parameterized by $\gamma$. We can show that Adaboost is a special case of this formulation of binary setting and exponential loss:

Also, we can also show that if we plug in a squared loss, then:

It means squared loss in this formulation is equally saying that we are fitting the individual classifier to the residual $(y_i-f_{m-1}(x_i))$. This just opens a short introduction to stagewise additive learning, if you want to see more, you should check more on textbooks.

## Gradient boosting

Boosting is used in many areas. It is also one of examples in stagewise additive modeling. The core idea is that every iteration we learn a weak learner. That is, we just need each one to perform a little better than random guess. At the end, we can aggregate all weak learns together to form a strong one. In Adaboost, for every iteration, we want the new model to focus on the re-weighted data samples. For gradient boosting, the core idea is that we want the new model to focus on gradients from biased predictions.

There are several steps to follow:

1, initialize $f_0(x) = c$

2, At i-th iteration, for sample $j=1,\dots,N$, we compute:

At this point, we have new pairs $(x_1,g_{1i}),\dots,(x_N,g_{Ni})$ for i-th iteration.

3, Fit a new decision or regression tree on the new pairs $(x_1,g_{1i}),\dots,(x_N,g_{Ni})$ for i-th iteration. That is,

4, We set

We can do this M iterations to get $f_M(X)$, which is out final output model.

Again, this is just a short introduction to **Gradient Boosting**. More can be found on textbooks. There are two links that I found very helpful: