ML Study Hub
← Back to Study Hub
Module 04 · Trees & Ensembles
← Hub
Intuition ID3 vs CART RF Proof AdaBoost Traps Past Papers Entropy Tool Quiz Checklist
Module 04 · Non-Parametric Partitions & Ensembles

Decision Trees & Ensemble Methods

The single most heavily tested subject area. Quinlan's ID3 Entropy and Information Gain on the 14-day Weather dataset, Breiman's CART Gini Index construction, the mathematical proof of Random Forest variance reduction, and AdaBoost reweighting.

Jan 2025 (Q6 20M) Nov 2025 (Q4 20M, Q7 20M) CT-2 (Q3 20M, Q4 20M) Weather 14-Day ID3 Random Forest Proof

01. Intuition & Concept Contrast

Decision trees perform recursive greedy axis-aligned orthogonal partitions of feature space. A single tree suffers from high variance (overfitting); ensemble methods combine multiple trees to eliminate variance or bias.

Splitting Metric

Information Gain (ID3) vs Gini Index (CART)

Shannon Entropy (ID3): Measures thermodynamic disorder in bits: $H(S) = -\sum p_i \log_2(p_i)$. Computationally heavier due to $\log_2$. Supports multi-way splits.

Gini Impurity (CART): Probability of incorrect classification under random labeling: $\text{Gini} = 1 - \sum p_i^2$. Computationally faster (pure squaring). Strictly binary splits.

Ensemble Architecture

Bagging (Random Forest) vs Boosting (AdaBoost)

Bagging (Parallel Variance Reduction): Trains unpruned deep trees independently on bootstrap samples. Reduces variance without changing bias.

Boosting (Sequential Bias Reduction): Trains shallow trees (stumps) sequentially; each tree focuses on errors made by previous trees. Reduces bias and variance.

02. Splitting Criteria Formulations (ID3 vs CART)

Entropy & Information Gain (Quinlan's ID3)
$$H(S) = -\sum_{c=1}^C p_c \log_2(p_c), \qquad \operatorname{Gain}(S, A) = H(S) - \sum_{v \in \text{Values}(A)} \frac{|S_v|}{|S|} H(S_v)$$ Select attribute $A$ that maximizes $\operatorname{Gain}(S, A)$.
Gini Impurity & Gini Split (Breiman's CART)
$$\operatorname{Gini}(S) = 1 - \sum_{c=1}^C p_c^2, \qquad \operatorname{Gini}_{\text{split}}(S, A) = \sum_{v \in \text{Values}(A)} \frac{|S_v|}{|S|} \operatorname{Gini}(S_v)$$ Select attribute $A$ that minimizes $\operatorname{Gini}_{\text{split}}(S, A)$ (or maximizes Gini Gain).
Feature Quinlan's ID3 Breiman's CART
Splitting Metric Information Gain (Shannon Entropy) Gini Impurity / Index
Split Type Multi-way (branch per categorical level) Strictly Binary ($A \le \theta$ or $A \in \{v\}$)
Continuous Features Requires manual discretization Native sorting and binary thresholds
Pruning Strategy Reduced error pruning after growth Cost-Complexity Pruning ($R_\alpha(T)$)

03. Random Forest Mathematical Variance Reduction Proof

Nov 2025 (Q4) Guaranteed Exam Derivation (8 Marks). Mathematical derivation proving how Random Forests eliminate tree variance.

The Governing Ensemble Variance Theorem
$$\operatorname{Var}\left(\hat{f}_{\text{RF}}(\mathbf{x})\right) = \rho \sigma^2 + \frac{1 - \rho}{B}\sigma^2$$ Where $B$ is number of trees, $\sigma^2$ is individual tree variance, and $\rho$ is pairwise tree correlation.

Step-by-Step Derivation:

1. Let an ensemble of $B$ bootstrap regression trees be denoted $T_1(\mathbf{x}), \dots, T_B(\mathbf{x})$.

2. Assume each individual tree has identical variance: $\operatorname{Var}(T_b(\mathbf{x})) = \sigma^2$.

3. Assume pairwise correlation between any two distinct trees is $\operatorname{Corr}(T_j, T_k) = \rho$, so that covariance is $\operatorname{Cov}(T_j, T_k) = \rho \sigma^2$ for $j \neq k$.

4. The ensemble prediction is the arithmetic mean: $\bar{T} = \frac{1}{B}\sum_{b=1}^B T_b(\mathbf{x})$.

5. Expanding the variance of the sum:

$$\begin{aligned} \operatorname{Var}(\bar{T}) &= \operatorname{Var}\left(\frac{1}{B}\sum_{b=1}^B T_b\right) \\[6pt] &= \frac{1}{B^2} \left[ \sum_{b=1}^B \operatorname{Var}(T_b) + \sum_{j \neq k} \operatorname{Cov}(T_j, T_k) \right] \\[6pt] &= \frac{1}{B^2} \left[ B\sigma^2 + B(B-1)\rho \sigma^2 \right] \\[6pt] &= \frac{\sigma^2}{B} + \frac{B - 1}{B}\rho \sigma^2 \\[6pt] &= \mathbf{\rho \sigma^2 + \frac{1 - \rho}{B}\sigma^2} \quad \text{Q.E.D.} \end{aligned}$$
The Two Essential Engineering Mechanisms:
1. As $B \to \infty$: The term $\frac{1-\rho}{B}\sigma^2 \to 0$, leaving residual variance $\rho \sigma^2$.
2. Feature Subspace Randomization ($m \approx \sqrt{p}$ or $p/3$): If we only use Bagging, all trees split on the single strongest feature (e.g. house size), making trees highly correlated ($\rho \to 1$). By forcing trees to choose splits only from a randomly selected subset of $m$ features at each node, trees become de-correlated: $\boldsymbol{\rho \to 0}$.
As $\rho \to 0$ and $B \to \infty$, ensemble variance approaches 0, producing a model with low bias and low variance.

04. AdaBoost Adaptive Boosting Mechanics

Nov 2025 (Q7) & CT-2 (Q4) Core Theory (8-10 Marks).

Step 1: Uniform Weight Initialization $$w_1(i) = \frac{1}{N}, \qquad \forall i \in \{1, \dots, N\}$$
Step 2: Weighted Classification Error ($\epsilon_t$) $$\epsilon_t = \sum_{i: h_t(\mathbf{x}_i) \neq y_i} w_t(i)$$
Step 3: Learner Voting Weight ($\alpha_t$) $$\alpha_t = \frac{1}{2}\ln\left(\frac{1 - \epsilon_t}{\epsilon_t}\right)$$

If $\epsilon_t < 0.5 \implies \alpha_t > 0$. Accurate learners receive large positive voting power.

Step 4: Sample Weight Update $$w_{t+1}(i) = \frac{w_t(i) \exp(-\alpha_t y_i h_t(\mathbf{x}_i))}{Z_t}$$

Misclassified instances ($y_i h_t < 0$) have weights multiplied by $e^{+\alpha_t}$ (scaled up); correctly classified instances are multiplied by $e^{-\alpha_t}$ (scaled down).

Step 5: Final Strong Classifier $$H(\mathbf{x}) = \operatorname{sign}\left(\sum_{t=1}^T \alpha_t h_t(\mathbf{x})\right)$$

05. Notation Decoding Matrix

Symbol Formal Name Plain-English Meaning Exam Benchmark Value
$H(S)$ Shannon Entropy Impurity / disorder of set $S$ in bits $H(9\text{Yes}, 5\text{No}) = \mathbf{0.9403}$ bits
$\operatorname{Gain}(S, A)$ Information Gain Reduction in entropy achieved by splitting on $A$ $\text{Gain}(\text{Outlook}) = \mathbf{0.2467}$ (Highest)
$\operatorname{Gini}(S)$ Gini Impurity Probability of misclassifying randomly picked item $\text{Gini}(4\text{Yes}, 3\text{No}) = 1 - (16/49) - (9/49) = \mathbf{0.4898}$
$\rho$ Ensemble Tree Correlation Degree to which individual trees make identical errors Lowered toward $0$ via random feature sampling
$\alpha_t$ Learner Importance Voting weight assigned to base stump $t$ $\alpha_t = \frac{1}{2}\ln(\frac{1-\epsilon_t}{\epsilon_t})$

06. Repeatable Step-by-Step Solving Protocol

Step 1: Compute Overall Dataset Impurity $$H(S) = -p_+ \log_2(p_+) - p_- \log_2(p_-), \qquad \text{or} \quad \operatorname{Gini}(S) = 1 - p_+^2 - p_-^2$$
Step 2: Partition Dataset for Every Candidate Attribute

Count $(+, -)$ outcomes for each distinct category $v$ of attribute $A$.

Step 3: Calculate Sub-Node Impurities & Weighted Average $$\text{Weighted Impurity} = \sum_v \frac{|S_v|}{|S|} \cdot \text{Impurity}(S_v)$$
Step 4: Determine Information Gain (or Gini Reduction) $$\operatorname{Gain} = \text{Parent Impurity} - \text{Weighted Impurity}$$
Step 5: Select Best Attribute for Root Node

Choose attribute with Highest Information Gain (ID3) or Lowest Weighted Gini (CART).

Step 6: Expand Pure and Impure Branches

If a branch contains only one class ($H = 0$), terminate as leaf. If impure, recurse on remaining attributes.

07. 6 Deadly Exam Traps & Common Student Mistakes

Trap 01: Natural Logarithm Confusion

Computing $H(S)$ with $\ln$ Instead of $\log_2$

Entropy in ID3 must be in base 2 (bits). To compute with basic calculators: $\log_2(p) = \frac{\ln(p)}{\ln(2)} \approx \frac{\ln(p)}{0.69315}$. Using natural logs gives values in "nats" and loses marks.

Trap 02: Missing Negative Sign in Entropy

Forgetting $- \sum p \log_2(p)$

Since $p_i \le 1$, $\log_2(p_i)$ is negative. Omitting the leading minus sign produces negative entropy values. Entropy is strictly non-negative ($H \ge 0$).

Trap 03: Inverting CART Criterion

Picking the Highest Weighted Gini

Higher Information Gain is better, but Lower Weighted Gini Impurity is better. If using Gini directly, pick the attribute that minimizes $\text{Gini}_{\text{split}}$.

Trap 04: Random Forest Misconception

Assuming RF is Just Bagging

Bagging alone does not decorrelate trees ($\rho \approx 1$). Feature Subspace Sampling ($m \approx \sqrt{p}$) is the critical invention that decorrelates trees ($\rho \to 0$).

Trap 05: Unnormalized AdaBoost Weights

Forgetting Normalization Constant $Z_t$

After multiplying weights by $e^{\pm \alpha_t}$, you must divide each weight by $Z_t = \sum w_{t+1}(i)$ so that updated weights sum to $1.0$.

Trap 06: Zero Entropy Leaves

Splitting Pure Nodes

When a branch has all Yes or all No ($H = 0$), do not attempt further splits! It is a terminal leaf.

08. Complete University Question Bank (All Past PYQs Solved)

Jan 2025 · Question 6 The 14-Day Weather Dataset Full ID3 Construction (20 Marks)
Question Statement:
A sports club has historical data on 14 days with attributes Outlook, Temperature, Humidity, Wind, and Decision (Play: 9 Yes, 5 No).
Construct a complete decision tree model using Quinlan's ID3 algorithm. Describe attribute selection, splitting criteria, and build the tree. (20 Marks)
DayOutlookTemperatureHumidityWindDecision
1sunnyhothighweakNo
2sunnyhothighstrongNo
3overcasthothighweakYes
4rainfallmildhighweakYes
5rainfallcoolnormalweakYes
6rainfallcoolnormalstrongNo
7overcastcoolnormalstrongYes
8sunnymildhighweakNo
9sunnycoolnormalweakYes
10rainfallmildnormalweakYes
11sunnymildnormalstrongYes
12overcastmildhighstrongYes
13overcasthotnormalweakYes
14rainfallmildhighstrongNo

Solution:

1. Root Node Entropy of Entire Dataset ($S$):

Total: $N = 14$, Positive ($p_+ = 9/14$), Negative ($p_- = 5/14$):

$$H(S) = -\left(\frac{9}{14}\log_2\frac{9}{14} + \frac{5}{14}\log_2\frac{5}{14}\right) \approx -(-0.4087 - 0.5316) = \mathbf{0.9403 \text{ bits}}$$
2. Information Gain for Candidate Attributes:

A. Outlook:

  • Sunny ($5$ days: $2$ Yes, $3$ No): $H(S_{\text{sunny}}) = -\frac{2}{5}\log_2\frac{2}{5} - \frac{3}{5}\log_2\frac{3}{5} = \mathbf{0.9710}$
  • Overcast ($4$ days: $4$ Yes, $0$ No): $H(S_{\text{overcast}}) = 0.0000$ (Pure Node!)
  • Rainfall ($5$ days: $3$ Yes, $2$ No): $H(S_{\text{rainfall}}) = -\frac{3}{5}\log_2\frac{3}{5} - \frac{2}{5}\log_2\frac{2}{5} = \mathbf{0.9710}$
$$\operatorname{Gain}(S, \text{Outlook}) = 0.9403 - \left[\frac{5}{14}(0.9710) + \frac{4}{14}(0) + \frac{5}{14}(0.9710)\right] = 0.9403 - 0.6936 = \mathbf{0.2467}$$

B. Humidity: High ($3$ Yes, $4$ No; $H=0.9852$), Normal ($6$ Yes, $1$ No; $H=0.5917$):

$$\operatorname{Gain}(S, \text{Humidity}) = 0.9403 - \left[\frac{7}{14}(0.9852) + \frac{7}{14}(0.5917)\right] = 0.9403 - 0.7885 = \mathbf{0.1518}$$

C. Wind: Weak ($6$ Yes, $2$ No; $H=0.8113$), Strong ($3$ Yes, $3$ No; $H=1.0000$):

$$\operatorname{Gain}(S, \text{Wind}) = 0.9403 - \left[\frac{8}{14}(0.8113) + \frac{6}{14}(1.0000)\right] = 0.9403 - 0.8922 = \mathbf{0.0481}$$

D. Temperature: Hot ($2$ Yes, $2$ No), Mild ($4$ Yes, $2$ No), Cool ($3$ Yes, $1$ No):

$$\operatorname{Gain}(S, \text{Temperature}) = 0.9403 - 0.9111 = \mathbf{0.0292}$$
3. Root Node Selection:

$\operatorname{Gain}(\text{Outlook}) = \mathbf{0.2467}$ is the highest. Therefore, Outlook is selected as the Root Node.

4. Expanding Branches:
  • Branch 1 (Outlook = Overcast): All $4$ examples (Days 3, 7, 12, 13) are "Yes". $\implies$ Leaf: Play = Yes.
  • Branch 2 (Outlook = Sunny): Contains Days 1, 2, 8, 9, 11 (2 Yes, 3 No). Evaluating Humidity:
    • Humidity = High (Days 1, 2, 8): All 3 are "No" $\implies$ Leaf: Play = No.
    • Humidity = Normal (Days 9, 11): Both are "Yes" $\implies$ Leaf: Play = Yes.
  • Branch 3 (Outlook = Rainfall): Contains Days 4, 5, 6, 10, 14 (3 Yes, 2 No). Evaluating Wind:
    • Wind = Weak (Days 4, 5, 10): All 3 are "Yes" $\implies$ Leaf: Play = Yes.
    • Wind = Strong (Days 6, 14): Both are "No" $\implies$ Leaf: Play = No.
[Outlook] ├── (Overcast) ─────────────> [YES] ├── (Sunny) ──────> [Humidity] │ ├── (High) ───> [NO] │ └── (Normal) ─> [YES] └── (Rainfall) ───> [Wind] ├── (Weak) ───> [YES] └── (Strong) ─> [NO]
CT-2 · Question 3 CART Customer Purchase Decision Tree Construction (20 Marks)
Question Statement:
7 customers with Age Group (Young, Middle, Senior), Income Level (Low, Med, High), and Buy (4 Yes, 3 No).
C1: Young, Low, No | C2: Young, Med, No | C3: Middle, Low, Yes | C4: Middle, High, Yes
C5: Senior, Med, Yes | C6: Senior, High, Yes | C7: Senior, Low, No
(a) Gini Index at root (4M)
(b) Gini Index for Age Group and Income Level splits (8M)
(c) Best attribute to split (4M)
(d) What happens if Gini values tie (2M)
(e) Advantages of CART over ID3 (2M)

Solution:

(a) Root Gini Index (4 Marks)
$$\operatorname{Gini}(S) = 1 - \left(\frac{4}{7}\right)^2 - \left(\frac{3}{7}\right)^2 = 1 - \frac{16}{49} - \frac{9}{49} = 1 - \frac{25}{49} = \frac{24}{49} \approx \mathbf{0.4898}$$
(b) Split Gini Computations (8 Marks)

1. Attribute: Age Group

  • Young ($2$ customers: $0$ Yes, $2$ No): $\operatorname{Gini} = 1 - 0^2 - 1^2 = \mathbf{0.0}$ (Pure)
  • Middle ($2$ customers: $2$ Yes, $0$ No): $\operatorname{Gini} = 1 - 1^2 - 0^2 = \mathbf{0.0}$ (Pure)
  • Senior ($3$ customers: $2$ Yes, $1$ No): $\operatorname{Gini} = 1 - (2/3)^2 - (1/3)^2 = 1 - 5/9 = 4/9 \approx \mathbf{0.4444}$
$$\operatorname{Gini}_{\text{split}}(\text{Age}) = \frac{2}{7}(0) + \frac{2}{7}(0) + \frac{3}{7}\left(\frac{4}{9}\right) = \frac{12}{63} = \frac{4}{21} \approx \mathbf{0.1905}$$

2. Attribute: Income Level

  • Low ($3$ customers: $1$ Yes, $2$ No): $\operatorname{Gini} = 1 - (1/3)^2 - (2/3)^2 = 4/9 \approx \mathbf{0.4444}$
  • Medium ($2$ customers: $1$ Yes, $1$ No): $\operatorname{Gini} = 1 - (1/2)^2 - (1/2)^2 = 1/2 = \mathbf{0.5000}$
  • High ($2$ customers: $2$ Yes, $0$ No): $\operatorname{Gini} = 1 - 1^2 = \mathbf{0.0}$
$$\operatorname{Gini}_{\text{split}}(\text{Income}) = \frac{3}{7}\left(\frac{4}{9}\right) + \frac{2}{7}\left(\frac{1}{2}\right) + \frac{2}{7}(0) = \frac{4}{21} + \frac{1}{7} = \frac{4+3}{21} = \frac{7}{21} \approx \mathbf{0.3333}$$
(c) Best Attribute to Split (4 Marks)

Since $\operatorname{Gini}_{\text{split}}(\text{Age Group}) = 0.1905 < \operatorname{Gini}_{\text{split}}(\text{Income}) = 0.3333$, Age Group produces much purer sub-nodes and is chosen as the Best Split.

(d) Tie-Breaking Protocol (2 Marks)

If two attributes have identical Gini values, the algorithm breaks the tie by: (1) selecting the split that produces more balanced child node sample counts, (2) selecting the attribute with lower computation cost, or (3) using arbitrary programmatic indexing order.

(e) Advantages of CART over ID3 (2 Marks)
  1. Binary Splits: Avoids fragmented over-partitioning caused by multi-way splits.
  2. No Logarithms: Computationally faster since $1 - \sum p_i^2$ avoids expensive transcendental $\log_2$ evaluations.
Nov 2025 · Question 4 Housing Price Decision Tree Overfitting & Random Forest Variance Proof (20 Marks)
Question Statement:
A single Decision Tree model to predict housing prices has zero training error but poor test performance.
i) Diagnose the problem with the model. (6 Marks)
ii) Propose a solution using Ensembling. (6 Marks)
iii) Explain in detail how Random Forest addresses this through mathematical proof. (8 Marks)

Solution:

(i) Diagnosis (6 Marks)

The model suffers from **Overfitting** driven by **High Variance and Low Bias** in the Bias-Variance decomposition: $\mathbb{E}[(y - \hat{f})^2] = \text{Bias}^2 + \text{Var} + \sigma_\epsilon^2$. Greedy unpruned recursive splitting memorizes training noise.

(ii) Proposed Solution: Random Forest Regression (6 Marks)

Deploy a Random Forest ensemble combining Bootstrap Aggregation (Bagging) with Feature Subspace Randomization.

(iii) Mathematical Variance Reduction Proof (8 Marks)
$$\operatorname{Var}\left(\frac{1}{B}\sum_{b=1}^B T_b\right) = \mathbf{\rho \sigma^2 + \frac{1 - \rho}{B}\sigma^2}$$

As $B \to \infty$, the term $\frac{1-\rho}{B}\sigma^2 \to 0$, leaving residual variance $\rho \sigma^2$.

Standard Bagging trees are highly correlated ($\rho \approx 1$). Random Forests restrict feature splits to a random subset of $m \approx p/3$ features, de-correlating the trees ($\rho \to 0$) and driving total ensemble variance to near zero.

09. Interactive Offline Entropy & Information Gain Calculator

Enter positive and negative sample counts to calculate Shannon Entropy in bits ($H$) and verify your homework or exam numericals.

Click button to compute node impurities.

10. Active Recall Self-Test Quiz

Answer the questions below to evaluate progress (0/4 answered).
1. What is the Shannon Entropy of a node containing 4 'Yes' and 0 'No' instances?
Correct: (c) $H(S) = -1\log_2(1) - 0 = 0$. Pure leaves have zero impurity.
2. In the 14-day Weather dataset, which attribute has the highest Information Gain and is chosen as the root node?
Correct: (a) Outlook reduces entropy most ($0.2467$ bits) and forms the root split.
3. As the number of trees $B \to \infty$ in a Random Forest, what is the asymptotic ensemble variance?
Correct: (b) $\operatorname{Var}(\bar{T}) = \rho \sigma^2 + \frac{1-\rho}{B}\sigma^2 \to \mathbf{\rho \sigma^2}$.
4. How does AdaBoost update sample weights after training a weak classifier?
Correct: (c) Multiplies weights by $e^{-\alpha_t y_i h_t}$, focusing subsequent stumps on hard mistakes.

11. Exam Hall Answer Sheet Presentation Checklist

1. State Both Impurity Formulas with Log Bases

Write $H(S) = -\sum p_i \log_2(p_i)$ and $\operatorname{Gini}(S) = 1 - \sum p_i^2$ with base 2 highlighted.

2. Compute Root Node Impurity First

Write $H(9, 5) = \mathbf{0.9403 \text{ bits}}$. Draw a box around this benchmark.

3. Construct a Clean Information Gain Table

List Outlook ($0.2467$), Humidity ($0.1518$), Wind ($0.0481$), Temperature ($0.0292$) side-by-side.

4. Draw the Final Tree Graphically

Draw boxed nodes for attributes, labeled arrows for categories, and double-lined boxes for terminal leaf predictions.

5. For Random Forest, Derive the Full Variance Equation

Show $\frac{1}{B^2}[B\sigma^2 + B(B-1)\rho\sigma^2] = \rho\sigma^2 + \frac{1-\rho}{B}\sigma^2$. Mention feature subsampling ($m \approx \sqrt{p}$).

6. State CART Advantages

Always mention: (1) Binary splits prevent over-fragmentation, (2) Avoids transcendental $\log_2$ evaluations.