CART (Classification and Regression Trees) › Formula - Regression
Machine Learning from ScratchAll chapters 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:
where:
- is the mean squared error for node (the average squared deviation of target values from their mean)
- is the number of samples in node (total count of data points in this node)
- is the target value for sample (the actual observed response value for the -th sample in node )
- is the mean target value in the node (the average of all target values in node )
- indicates summation over all samples in node
The mean target value is calculated as:
where:
- is the mean target value (the arithmetic average of all target values in node , which becomes the prediction for this node)
- is the normalization factor (ensures we compute the average rather than the sum)
- is the sum of all target values in node
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:
where:
- is the weighted mean squared error after splitting node (the combined MSE of both child nodes, weighted by their sizes)
- is the left child node after the split (the subset of samples that satisfy the split condition)
- is the right child node after the split (the subset of samples that do not satisfy the split condition)
- is the number of samples in the parent node (total samples before splitting)
- is the number of samples in the left child node (samples satisfying the split condition)
- is the number of samples in the right child node (samples not satisfying the split condition, where )
- and 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:
where:
- is the variance of the target variable in node (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:
where:
- is the weighted variance after splitting node (the combined variance of both child nodes, weighted by their sizes)
- is the variance of the left child node (calculated as )
- is the variance of the right child node (calculated as )
- All other symbols have the same meaning as defined previously
The variance reduction for a split is:
where:
- 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)
- is the variance of the parent node before splitting
- 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 , we consider splits of the form for all possible thresholds . For categorical features, we consider all possible subsets of categories.
The information gain (or variance reduction) for a split is:
where:
- is the variance reduction (the decrease in variance achieved by the split, with higher values indicating better splits)
- is the variance of the parent node before splitting
- is the weighted variance contribution of the left child node
- is the weighted variance contribution of the right child node
- The right-hand side can be rewritten as , 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 unique values, this requires evaluations. For categorical features with categories, it requires evaluating possible splits, making it computationally expensive for high-cardinality categorical features.
where:
- is the number of unique values for a continuous feature (the distinct values that could serve as split thresholds)
- represents linear computational complexity (evaluating each unique value as a potential split point)
- is the number of categories for a categorical feature (the distinct category labels)
- is the number of possible binary partitions of categories (exponential growth with the number of categories)