Lecture 17 · room 204
HEMA · Università Cattolica del Sacro Cuore, Rome
Friday 27 November 2026
Lecture 16 · Decision trees · Mon 23 Nov
cp and cross-validation brings it back| Time | Min | |
|---|---|---|
| 10:00 | Last time · Set 14, the In class questions | 15’ |
| 10:15 | Many trees: bagging and random forests | 50’ |
| 11:05 | Boosting: learn from the mistakes, slowly | 25’ |
| 11:30 | Break | 15’ |
| 11:45 | Lab: churn, end to end | 65’ |
| 12:50 | Set 15 | 10’ |
On a table of customers, this is the model that wins
Today you learn to build the thing that wins. Week 7 is about living with what you cannot read.
A single tree is unstable: high variance. Statistics has one universal remedy for variance — average.
\operatorname{Var}\!\left(\tfrac{1}{B}\sum_{b=1}^{B} \hat f_b\right) \;=\; \frac{\sigma^2}{B} \qquad \text{if the } \hat f_b \text{ are independent}
Fit B trees, average their predictions (or vote), and the variance falls like 1/B.
Except you have one training set. Where do B independent trees come from?
Each tree sees about 63% of the rows; the other 37% are out of bag for that tree. Predict those with it, and you get a test error for free, without a test set: the OOB error.
Note
The bootstrap samples are not independent — they share most of their rows. So the trees are correlated, and the variance does not fall all the way to \sigma^2/B. That gap is what the next idea closes.
Bagging’s trees look alike because they all grab the same strong variable at the top. Correlated trees average badly.
At every split, consider only a random subset of m of the p predictors. Typically m \approx \sqrt{p}.
Attention
m = p is bagging. ranger and randomForest default to \sqrt{p}. It is the one setting worth touching; try p/3 and \sqrt p and keep the better OOB error.
Forests grow trees in parallel, each ignorant of the others. Boosting grows them in sequence, each one fitted to what the previous ones got wrong.
Three knobs: B trees, \lambda the learning rate (0.01–0.1: small steps), and d the depth of each tree (1–4: stumps and near-stumps).
Note
“Gradient” boosting: step 2 fits the tree to the gradient of the loss — for squared error, exactly the residual; for classification, the gradient of the log-loss. xgboost is this, with every engineering shortcut ever found.
| Random forest | Gradient boosting | |
|---|---|---|
| Trees | deep, independent | shallow, sequential |
| Learns by | averaging away variance | correcting bias, step by step |
| Tuning | almost none — m, done | B, \lambda, d, and they interact |
| Overfits? | hard to make it | easy: too many trees, too big a step |
| Accuracy | very good | usually best |
| First choice | when you have an afternoon | when you have a week |
library(tidyverse); library(ranger); library(ISLR2)
d <- Default |> mutate(default = factor(default))
set.seed(1); tr <- sample(nrow(d), 7000)
rf <- ranger(default ~ ., data = d[tr, ], num.trees = 500, importance = "impurity", probability = TRUE)
rf$prediction.error # OOB error, no test set needed
sort(rf$variable.importance, decreasing = TRUE) # which variables did the work
pred <- predict(rf, d[-tr, ])$predictions[, "Yes"] # probability of default, held-out rowslibrary(xgboost)
X <- model.matrix(default ~ . - 1, d) # numeric matrix, no intercept
y <- as.numeric(d$default == "Yes")
xgb <- xgboost(data = X[tr, ], label = y[tr],
nrounds = 300, eta = 0.05, max_depth = 3,
objective = "binary:logistic", verbose = 0)
p_xgb <- predict(xgb, X[-tr, ])Then the comparison that matters: forest vs boosting vs Monday’s pruned tree vs a plain logistic regression, on the same held-out rows, with the same metric.
Attention
nrounds = 300 at eta = 0.05 is a starting point, not an answer. Use xgb.cv() to pick nrounds by cross-validation; watch the test log-loss stop falling and start rising. That turning point is your model.
churn.csv on Blackboard: 7 000 telecom customers, 18 predictors, churn yes/no.
churn — it is 28.5% yes, and a random split can driftxgb.cv to choose nrounds; then xgboostvariable.importance from the forest vs the top splits of the tree: do they agree?Deliverable: the table in step 6, and one paragraph on which model you would deploy, and why the answer is not simply “the most accurate one”.
Random forests and gradient boosting · about 40 minutes · before Mon 30 Nov
Not graded. The test is a mirror, not an exam.
?ranger, ?xgboost, ?xgb.cvStatistics & Big Data · HEMA, Università Cattolica del Sacro Cuore