CART (Classification and Regression Trees) › Formula - Regression

Michael BrenndoerferJune 27, 20256 min read
Machine Learning from ScratchAll chapters
Part 5 of 9Tree-Based Models
Chapter 41CART (Classification and Regression Trees) › Formula - Regression
Reading section 3 of 9 · Formula - Regression · 6 minSection 3 of 9 · 6 min

Book contents

Section 3 of 9 · 6 min read

CART (Classification and Regression Trees) › Formula - RegressionOne section · Sections in this chapter3 / 9 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The mathematical foundation of CART for regression problems focuses on minimizing variance (or equivalently, mean squared error) at each split. Unlike classification, where we measure impurity, regression trees aim to create leaf nodes with the lowest possible variance in the target variable.

Mean Squared Error

For regression problems, CART uses the mean squared error (MSE) to evaluate the quality of a split. The MSE measures the average squared difference between the predicted values and the actual target values in a node.

We start with the basic MSE formula for a node:

MSE(S)=1∣S∣∑i∈S(yi−yˉ)2MSE(S) = \frac{1}{|S|} \sum_{i \in S} (y_i - \bar{y})^2

where:

  • MSE(S)MSE(S) is the mean squared error for node SS (the average squared deviation of target values from their mean)
  • ∣S∣|S| is the number of samples in node SS (total count of data points in this node)
  • yiy_i is the target value for sample ii (the actual observed response value for the ii-th sample in node SS)
  • yˉ\bar{y} is the mean target value in the node (the average of all target values in node SS)
  • i∈Si \in S indicates summation over all samples in node SS

The mean target value yˉ\bar{y} is calculated as:

yˉ=1∣S∣∑i∈Syi\bar{y} = \frac{1}{|S|} \sum_{i \in S} y_i

where:

  • yˉ\bar{y} is the mean target value (the arithmetic average of all target values in node SS, which becomes the prediction for this node)
  • 1∣S∣\frac{1}{|S|} is the normalization factor (ensures we compute the average rather than the sum)
  • ∑i∈Syi\sum_{i \in S} y_i is the sum of all target values in node SS

The reason we use squared differences is to penalize larger errors more heavily than smaller ones. This ensures that the algorithm prioritizes splits that create more homogeneous groups in terms of their target values. When all samples in a node have the same target value (complete homogeneity), the MSE equals zero.

To find the best split, we calculate the weighted sum of the MSEs of the resulting child nodes:

MSEsplit(S)=∣SL∣∣S∣⋅MSE(SL)+∣SR∣∣S∣⋅MSE(SR)MSE_{split}(S) = \frac{|S_L|}{|S|} \cdot MSE(S_L) + \frac{|S_R|}{|S|} \cdot MSE(S_R)

where:

  • MSEsplit(S)MSE_{split}(S) is the weighted mean squared error after splitting node SS (the combined MSE of both child nodes, weighted by their sizes)
  • SLS_L is the left child node after the split (the subset of samples that satisfy the split condition)
  • SRS_R is the right child node after the split (the subset of samples that do not satisfy the split condition)
  • ∣S∣|S| is the number of samples in the parent node SS (total samples before splitting)
  • ∣SL∣|S_L| is the number of samples in the left child node (samples satisfying the split condition)
  • ∣SR∣|S_R| is the number of samples in the right child node (samples not satisfying the split condition, where ∣SL∣+∣SR∣=∣S∣|S_L| + |S_R| = |S|)
  • ∣SL∣∣S∣\frac{|S_L|}{|S|} and ∣SR∣∣S∣\frac{|S_R|}{|S|} are the weights (proportions of samples going to each child node, so larger child nodes have more influence on the overall MSE measure)

The algorithm selects the split that minimizes this weighted MSE. This makes intuitive sense: we want splits that create child nodes with as low variance as possible in their target values.

Variance Reduction

An alternative and equivalent approach is to use variance instead of MSE. The variance for a node is calculated as:

Var(S)=1∣S∣∑i∈S(yi−yˉ)2Var(S) = \frac{1}{|S|} \sum_{i \in S} (y_i - \bar{y})^2

where:

  • Var(S)Var(S) is the variance of the target variable in node SS (identical to MSE, measuring spread of target values)
  • All other symbols have the same meaning as in the MSE formula above

For a split, we calculate the weighted sum of the variances of the child nodes:

Varsplit(S)=∣SL∣∣S∣⋅Var(SL)+∣SR∣∣S∣⋅Var(SR)Var_{split}(S) = \frac{|S_L|}{|S|} \cdot Var(S_L) + \frac{|S_R|}{|S|} \cdot Var(S_R)

where:

  • Varsplit(S)Var_{split}(S) is the weighted variance after splitting node SS (the combined variance of both child nodes, weighted by their sizes)
  • Var(SL)Var(S_L) is the variance of the left child node (calculated as 1∣SL∣∑i∈SL(yi−yˉL)2\frac{1}{|S_L|} \sum_{i \in S_L} (y_i - \bar{y}_L)^2)
  • Var(SR)Var(S_R) is the variance of the right child node (calculated as 1∣SR∣∑i∈SR(yi−yˉR)2\frac{1}{|S_R|} \sum_{i \in S_R} (y_i - \bar{y}_R)^2)
  • All other symbols have the same meaning as defined previously

The variance reduction for a split is:

Variance_Reduction=Var(S)−Varsplit(S)Variance\_Reduction = Var(S) - Var_{split}(S)

where:

  • Variance_ReductionVariance\_Reduction is the reduction in variance achieved by the split (the decrease in variance from parent to weighted child nodes, with higher values indicating better splits)
  • Var(S)Var(S) is the variance of the parent node before splitting
  • Varsplit(S)Var_{split}(S) is the weighted variance after splitting

The algorithm selects the split that provides the maximum variance reduction, which is equivalent to minimizing the weighted MSE.

Split Selection Process

The algorithm evaluates all possible splits for each feature and selects the one that provides the maximum variance reduction. For a continuous feature with values x1,x2,...,xnx_1, x_2, ..., x_n, we consider splits of the form x≤tx \leq t for all possible thresholds tt. For categorical features, we consider all possible subsets of categories.

The information gain (or variance reduction) for a split is:

Gain=Var(S)−∣SL∣∣S∣⋅Var(SL)−∣SR∣∣S∣⋅Var(SR)Gain = Var(S) - \frac{|S_L|}{|S|} \cdot Var(S_L) - \frac{|S_R|}{|S|} \cdot Var(S_R)

where:

  • GainGain is the variance reduction (the decrease in variance achieved by the split, with higher values indicating better splits)
  • Var(S)Var(S) is the variance of the parent node SS before splitting
  • ∣SL∣∣S∣⋅Var(SL)\frac{|S_L|}{|S|} \cdot Var(S_L) is the weighted variance contribution of the left child node
  • ∣SR∣∣S∣⋅Var(SR)\frac{|S_R|}{|S|} \cdot Var(S_R) is the weighted variance contribution of the right child node
  • The right-hand side can be rewritten as Var(S)−Varsplit(S)Var(S) - Var_{split}(S), showing this is equivalent to the variance reduction formula above

The algorithm selects the split with the maximum gain.

Mathematical Properties

The MSE and variance measures are monotonically decreasing as we make better splits. This property ensures that the algorithm chooses splits that improve the overall model quality.

The variance functions are convex, meaning that the weighted average of variances from child nodes is less than or equal to the parent node's variance. This ensures that splitting does not increase the overall variance.

For each feature, the algorithm must evaluate all possible split points. For continuous features with nn unique values, this requires O(n)O(n) evaluations. For categorical features with kk categories, it requires evaluating 2k−1−12^{k-1} - 1 possible splits, making it computationally expensive for high-cardinality categorical features.

where:

  • nn is the number of unique values for a continuous feature (the distinct values that could serve as split thresholds)
  • O(n)O(n) represents linear computational complexity (evaluating each unique value as a potential split point)
  • kk is the number of categories for a categorical feature (the distinct category labels)
  • 2k−1−12^{k-1} - 1 is the number of possible binary partitions of kk categories (exponential growth with the number of categories)