Quant Memo
Core

Cost-Complexity Pruning

A grown-out decision tree almost always overfits. Cost-complexity pruning trims it back systematically by penalizing tree size directly, producing a single tuning parameter that trades training fit against tree complexity.

Prerequisites: Decision Trees and CART

Grow a decision tree without stopping and it keeps splitting until every leaf holds one or two observations — a perfect training fit and a badly overfit model. You could stop early with a fixed rule, but that requires guessing a threshold in advance and applies it uniformly, even to branches that would have benefited from growing further. Cost-complexity pruning grows the tree all the way out, then trims it back afterward, guided by an explicit penalty on size.

The analogy: growing a hedge, then trimming it back

Rather than guessing in advance how tall each part of a hedge should be, let it grow out fully and unevenly. Then trim it back using one rule: cut any branch whose extra height isn't earning its keep relative to how much taller it makes the hedge overall. Different parts get cut back by different amounts, proportional to what they actually contribute.

The mechanics: penalizing tree size directly

Define a cost over tree TT with T|T| leaves:

Cα(T)=leaves mim(yiy^m)2  +  αT.C_\alpha(T) = \sum_{\text{leaves } m} \sum_{i \in m} (y_i - \hat y_m)^2 \;+\; \alpha |T| .

In plain English: the first term is training error; the second charges α\alpha per leaf, so a bigger tree pays a bigger tax regardless of fit. At α=0\alpha=0 the fully-grown tree wins outright; as α\alpha rises, small fit gains from extra leaves stop being worth their cost, and the optimal subtree shrinks. Pruning computes, as α\alpha increases from 0, the nested sequence of optimal subtrees, then picks the α\alpha (and subtree) that does best on held-out data.

Worked example: pruning a small tree

A fully-grown tree for predicting default has 6 leaves, training error 4. Collapsing two sibling leaves into one gives 5 leaves, error 6. At α=0\alpha=0: 6-leaf tree wins (leaves free). At α=1\alpha=1: 6-leaf costs 4+6=104+6=10, pruned costs 6+5=116+5=11 — full tree still wins. At α=3\alpha=3: 6-leaf costs 4+18=224+18=22, pruned costs 6+15=216+15=21 — now pruning wins, since the extra leaf's marginal gain no longer justifies its cost. Sweeping α\alpha this way and cross-validating picks the value that generalizes best.

Bias–variance explorer
model complexity →sweet spot
test error 1.54train error 0.92underfitting

Drag model complexity above and watch training error keep falling while test error eventually turns back up — exactly the trade-off α\alpha controls: a large tree (low α\alpha) chases training error down but overfits, while enough pruning (higher α\alpha) finds the complexity that minimizes error on new data.

What this means in practice

Cost-complexity pruning turns "grow a tree" into a properly regularized procedure: grow fully (cheap), then use held-out data to pick how far to trim, rather than guessing stopping rules while growing. It's why "depth" and "leaf count" are treated as tunable regularization knobs in every tree-based method, not arbitrary settings.

Cost-complexity pruning grows a tree fully, then trims using Cα(T)=error(T)+αTC_\alpha(T) = \text{error}(T) + \alpha|T| — a single penalty on size, with α\alpha chosen by cross-validation rather than fixed in advance.

Don't confuse pruning with early stopping (capping depth while growing). Early stopping applies uniformly and can block a genuinely useful split simply because it comes late; pruning after full growth trims different branches by different amounts based on what they actually contribute.

Related concepts

Practice in interviews

Further reading

  • Breiman, Friedman, Olshen & Stone, Classification and Regression Trees (1984), ch. 3
  • Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, ch. 9.2
ShareTwitterLinkedIn