Lesson 4.2: Bagging (Bootstrap Aggregating)

In our last lesson, we saw the 'wisdom of the crowd' principle: averaging many models reduces variance. This lesson introduces the brilliant statistical trick that allows us to create our 'crowd' of models from a single dataset. We will master the concept of 'Bootstrap Aggregating'—or Bagging—the engine that drives the Random Forest algorithm.

Part 1: The Core Problem - How to Create 'Different' Training Sets?

The theory from the last lesson was that if we could train nn independent models, we could reduce our final variance by a factor of nn. The problem is, we only have **one training dataset**. How can we possibly create hundreds of different, independent training sets to build our crowd of models?

The answer is the **Bootstrap**, a powerful resampling technique we encountered in Module 6. It's the same idea, applied in a new way.

The Core Analogy: The 'Photocopy with a Twist' Method

Imagine your single training dataset is a 100-page book. You want to give slightly different versions of this book to 500 different student models to train on.

The bootstrap process is simple:

  1. Create Bootstrap Sample #1: To create the first student's book, you randomly pick a page from the original 100-page book, make a copy of it, and put the original page back. You repeat this process 100 times.
  2. The Result: Student #1's book is also 100 pages long. But because you sampled **with replacement**, it's a unique version. Some original pages might appear 2 or 3 times, while others (on average, about 37%) won't appear at all.
  3. Repeat: You repeat this process 500 times, creating 500 unique, same-sized training sets.

This "sampling with replacement" is the bootstrap. It's a computationally cheap and powerful way to simulate having multiple independent datasets, even when you only have one.

Part 2: The 'Bagging' Algorithm (Putting it all Together)

Bagging, short for **B**ootstrap **agg**regat**ing**, is the complete algorithm. It combines the bootstrap sampling method with model aggregation (voting).

The Bagging Algorithm

For b=1b=1 to BB (e.g., B=500) iterations:

  1. Step 1: Bootstrap.Create a bootstrap sample, DbD_b, by drawing nn observations from the original training set DD with replacement.
  2. Step 2: Train.Train a single, unconstrained (high-variance) decision tree, fbf_b, on this bootstrap sample DbD_b.

After the loop, you have a "forest" of BB different trees.


Step 3: Aggregate (Vote).

To make a prediction for a new, unseen data point, you let all BB trees "vote" on the outcome.

  • For Classification: The final prediction is the **majority vote**. If 300 trees vote "Buy" and 200 trees vote "Sell," the ensemble prediction is "Buy."
  • For Regression: The final prediction is the **average** of all the individual tree predictions.

Part 3: The 'Free' Validation Set: Out-of-Bag (OOB) Error

Bagging provides an elegant and "free" way to get an unbiased estimate of the model's test error without needing a separate validation set. This is the **Out-of-Bag (OOB) error**.

The Out-of-Bag (OOB) Principle

Remember that each bootstrap sample leaves out about 37% of the original data. This means that for any given observation xix_i in your training set, it was **not used** in the training of roughly 37% of your trees.

The OOB validation procedure is as follows:

  1. For each observation xix_i in the training set:
    • Find all the trees that were **not** trained on xix_i. This is its "Out-of-Bag" sub-forest.
    • Let this sub-forest of trees make a prediction for xix_i.
  2. Calculate the overall error (e.g., MSE or misclassification rate) across all these OOB predictions.

The resulting OOB error is a very good, unbiased estimate of the model's performance on unseen test data. This is a powerful feature, especially when you have a small dataset and can't afford to create a large validation set.

The Power and Limits of Bagging

    Bagging is an extremely powerful and general technique for reducing the variance of high-variance estimators (like deep decision trees).

    • The Good: It dramatically improves the stability and predictive accuracy of decision trees, turning a weak learner into a strong one.
    • The Bad: One major downside is that we lose interpretability. A single decision tree is a simple flowchart. A "forest" of 500 different trees is a black box. We can no longer easily see *why* the model made a certain prediction.
    • The Flaw: While bootstrap sampling creates different datasets, the trees in our forest are not fully independent. They are all trained on data from the same original source. If there is one very strong, dominant predictor in our dataset, **every single tree will probably pick that variable as its first split**. This makes all the trees look similar (they become correlated), which limits the variance-reduction benefits of averaging. We haven't achieved a truly "diverse" crowd.

What's Next? Decorrelating the Trees

We've almost built a Random Forest. We have the "Bootstrap Aggregating" part, but we are missing the "Random" part.

The final crucial innovation of Random Forest is a trick to solve the correlation problem. What if, at every single split, we didn't even allow the tree to *look at* all the features? What if we forced it to choose its best split from a small, random subset of predictors?

This is the idea of **feature randomness**. In our next lesson, we will add this final ingredient to complete our Random Forest model and see why this simple trick is so effective at creating a more diverse and powerful ensemble.