Random forest algorithm
WHAT is a Random Forest?
The two sources of randomness (rows + features) are the whole trick. They make the trees disagree in their errors so that averaging helps.
WHY does averaging help? (Derive from first principles)
Suppose we have trees, each producing a prediction that is a random variable with variance . Let be the average pairwise correlation between two trees' predictions. What is the variance of the average ?
Step 1 — variance of a sum. For any random variables, Why this step? Variance of a sum splits into individual variances plus every covariance pair — this is the definition of variance applied to a sum.
Step 2 — plug in identical variances and correlations. There are diagonal terms each , and off-diagonal terms each :
Step 3 — divide by (because , and ):
HOW to build one (the algorithm)

Free lunch: Out-of-Bag (OOB) error
Probability a given row is NOT in a bootstrap sample. Each of the draws misses that row with probability . Independently over draws: Why this step? is the classic limit definition of .
Feature importance (bonus interpretability)
Two common measures:
- Mean Decrease in Impurity (MDI): sum the impurity reduction (Gini/entropy) each feature causes, averaged over all trees.
- Permutation importance: shuffle one feature's values and measure how much OOB accuracy drops. Big drop ⇒ important feature.
Common Mistakes (Steel-man + fix)
Active Recall
Recall What are the TWO randomness sources and what does each achieve?
(1) Bootstrap rows → bagging, reduces variance via averaging. (2) Random feature subset per split → de-correlates trees, lowers , shrinks the variance floor .
Recall Why grow trees deep and unpruned?
We want low-bias, high-variance learners; averaging over many trees kills the variance, so bias is what we minimize per tree.
Recall Derive the variance of the averaged ensemble.
; second term → 0 as , leaving floor .
Recall What fraction of data is OOB per tree and why?
, because .
Recall Feynman: explain a random forest to a 12-year-old.
Imagine one really clever friend who sometimes gets carried away and gives weird answers. Instead of trusting just her, you ask 100 friends, but you only let each friend peek at some of the clues and some of the past examples. Then you go with whatever most of them say. Because they each looked at different things, their silly mistakes don't line up — so the crowd's answer is steadier and usually right. That crowd of decision-tree "friends" is a random forest.
80/20 — the vital few
- Two randomnesses: rows (bagging) + features per split (de-correlation).
- Variance law: → lowering matters most.
- Deep unpruned trees; average/vote; ~37% OOB for free validation.
Connections
- Decision Trees — the base learner inside every forest.
- Bagging (Bootstrap Aggregating) — the row-sampling half of RF.
- Bias-Variance Tradeoff — why averaging low-bias/high-variance trees works.
- Gradient Boosting — contrasting ensemble: sequential, bias-reducing, not variance-reducing.
- Cross-Validation — replaced for free by OOB error.
- Feature Importance — MDI & permutation importance.
- Gini Impurity & Entropy — split criteria used inside each tree.
What defines a Random Forest beyond plain bagging?
Variance of an averaged ensemble of B trees with variance σ² and pairwise correlation ρ?
As B→∞, what does ensemble variance approach?
Why do we grow forest trees deep and unpruned?
What fraction of rows is out-of-bag for each tree and why?
Typical m (features per split) for classification vs regression?
Two ways to measure feature importance in RF?
How does a RF predict for classification vs regression?
Why can't adding more trees drive error to zero?
Purpose of OOB error?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, ek single decision tree bahut smart hota hai par thoda "moody" — wo training data ke chote chote quirks bhi yaad kar leta hai, isliye uski predictions me variance zyada hota hai. Random forest ka funda simple hai: ek tree pe bharosa mat karo, bahut saare trees banao aur unka vote (classification) ya average (regression) le lo. Jab bahut saare thodi-thodi galti karne wale experts ki galtiyan alag-alag directions me hoti hain, to average lene pe wo galtiyan cancel ho jaati hain aur asli signal bach jaata hai.
Ab do randomness ka role samjho. Pehla: har tree ko bootstrap sample milta hai — rows ko with replacement uthate hain (isko bagging bolte hain). Dusra, aur ye asli magic hai: har split pe tree ko sirf kuch random features (m out of p, usually √p) dekhne dete hain. Kyu? Kyunki agar sab trees ko saare features de do, to sab ke sab wahi ek strong feature top split pe use karenge aur sab trees ek jaise (correlated) ban jaayenge — averaging ka fayda kam. Random features force karte hain ki trees alag-alag socho, yani correlation ρ kam.
Variance ka formula yaad rakho: . Jaise-jaise trees B badhao, dusra term gayab ho jaata hai, par pehla term ek floor hai jise sirf trees badha ke tod nahi sakte. Isliye "aur trees dalo" se accuracy infinite tak nahi badhti — ρ kam karna hi real game hai. Ye ek badi galti hai jo students karte hain.
Ek aur cool cheez — OOB error. Bootstrap me har tree ke liye lagbhag rows chhoot jaati hain (). Wo left-out rows free ki validation set ban jaati hain, to alag se cross-validation ki zarurat hi nahi. Aur haan — forest me trees ko deep aur unpruned rakho, kyunki hume low-bias individuals chahiye; variance to averaging khud sambhal legi.