Random Forest › Formula
Machine Learning from ScratchAll chapters Random Forest › FormulaOne section · Sections in this chapter2 / 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 Random Forest involves understanding how individual decision trees are combined to form the ensemble. Let's break this down step by step, starting with the basic structure and progressing to the complete ensemble formulation.
Individual Decision Tree
Each tree in the Random Forest is a decision tree that makes predictions by recursively partitioning the feature space. We denote the -th tree in the ensemble as , where the capital represents the tree as a function or mapping from input features to a prediction. For a given input , this tree produces a prediction . Here, means we apply the -th decision tree to the input to obtain its predicted value.
The prediction from a single tree can be written as:
where:
- : Prediction from the -th tree in the ensemble
- : Total number of training observations in the dataset
- : Weight assigned to training observation when making a prediction for input
- : Target value (actual outcome) of training observation
- : Input feature vector for which we are making a prediction
The weights are determined by which leaf node the input falls into. If falls into the same leaf as training observation , then where is the number of training observations in that leaf. Otherwise, . The weights always sum to 1: .
Bootstrap Sampling
Each tree is trained on a bootstrap sample of the original training data. A bootstrap sample is created by randomly sampling observations with replacement from the original observations. This means some observations may appear multiple times in a bootstrap sample, while others may not appear at all.
Mathematically, for tree , we create a bootstrap sample by sampling with replacement from the original dataset .
The probability that a specific observation is included in a bootstrap sample is:
where:
- : Total number of observations in the original dataset
- : Probability that observation appears at least once in a bootstrap sample of size
As becomes large, this probability approaches , meaning approximately 63.2% of the original observations appear in each bootstrap sample.

Random Feature Selection
At each split in each tree, only a random subset of features is considered for the split. If the original dataset has features, then at each split, we randomly select features where typically for classification or for regression.
This random feature selection introduces additional diversity among the trees and helps prevent overfitting to specific features.

Ensemble Prediction
The final Random Forest prediction combines the predictions from all trees in the ensemble. For regression problems, the prediction is the average of all tree predictions:
where:
- : Final ensemble prediction for input
- : Total number of trees in the Random Forest
- : Prediction from the -th tree
- : Function representing the -th tree applied to input
For classification problems, the prediction is made by taking a majority vote among all the trees in the forest. This means that each tree "votes" for a class label, and the class that receives the most votes is chosen as the final prediction.
In mathematical terms, this is called taking the "mode" of the predicted class labels, where the mode is simply the value that appears most frequently in a set. If there is a tie, some implementations may break the tie randomly or by another rule.
Or, if we consider the probability of each class:
where:
- : Estimated probability that input belongs to class
- : Probability that tree assigns to class for input
Complete Random Forest Algorithm
Putting it all together, the Random Forest algorithm can be written as:
Input: Training data , number of trees , number of features to consider at each split
For to :
- Create bootstrap sample by sampling observations with replacement from
- Train decision tree on using the following splitting rule:
- At each split, randomly select features from the available features
- Choose the best split among the selected features
- Continue until stopping criteria are met (e.g., minimum samples per leaf, maximum depth)
Output: Ensemble of trees
Prediction: For new input :
- Regression:
- Classification:
Mathematical Properties
Random Forests possess several key mathematical properties that help explain their effectiveness. One of the most important is the bias-variance decomposition. When we use a single decision tree, the model tends to have low bias but high variance, meaning it can fit the training data closely but may not generalize well to new data. By averaging the predictions of many trees, Random Forests reduce the variance component of the error while keeping the bias relatively low. The overall error of the ensemble can be expressed as:
This formula shows that the total prediction error consists of three parts: the squared bias (how far the average model prediction is from the true values), the variance (how much the predictions fluctuate for different training sets), and the irreducible noise inherent in the data. By averaging over multiple trees, Random Forests specifically target and reduce the variance term, leading to more stable and reliable predictions.


Another important property is the concept of out-of-bag (OOB) error. Because each tree in the forest is trained on a bootstrap sample, about 37% of the original data points are left out of the training set for any given tree. These unused data points, known as "out-of-bag" observations, can be used to estimate the model's generalization error without the need for a separate validation set. This built-in validation approach provides an efficient way to assess model performance.
Finally, Random Forests offer a natural way to measure feature importance. By tracking how much each feature contributes to reducing impurity.such as Gini impurity for classification tasks or mean squared error for regression.across all trees in the forest, we can identify which features are most influential in making predictions. This helps us interpret the model and understand which variables are driving its decisions.