The Math Behind ML Regressors

Six regression models — all pointing at the same goal of predicting a continuous output — yet each one reaches that goal through a completely different mathematical lens. In this post I walk through the core terminology, the governing equations, and — most importantly — why those equations make a model strong or weak for particular kinds of data, all without touching a single dataset or line of code. The math speaks for itself.


01Decision Tree Regressor

Decision Tree Regressor diagram Image generated using DALL·E 3

Key Terms

TermWhat it means
NodeA decision point that tests one feature against a threshold
SplitThe act of dividing a node's data into two child groups based on a threshold
LeafA terminal node that holds the predicted value — no further splitting
Impurity / VarianceHow spread-out the target values are inside a node
Information GainHow much a split reduces variance — the criterion the tree maximizes
max_depthHard limit on how many levels the tree may grow
min_samples_splitMinimum samples a node must contain before it is allowed to split

The Mathematics

A decision tree recursively partitions the feature space $\mathcal{X} \subseteq \mathbb{R}^p$ into axis-aligned rectangular regions $R_1, R_2, \dots, R_M$ and assigns each region a constant prediction. For regression the splitting criterion is variance reduction.

At any node containing sample indices $\mathcal{S}$, the variance of the targets is:

$$ \text{Var}(\mathcal{S}) = \frac{1}{|\mathcal{S}|} \sum_{i \in \mathcal{S}} \left(y_i - \bar{y}_\mathcal{S}\right)^2 $$

where $\bar{y}_\mathcal{S}$ is the mean target in that node. A candidate split on feature $j$ at threshold $t$ divides $\mathcal{S}$ into:

$$ \mathcal{S}_L = \{i \in \mathcal{S} : x_{ij} \le t\}, \qquad \mathcal{S}_R = \{i \in \mathcal{S} : x_{ij} > t\} $$

The variance reduction (information gain for regression) achieved by this split is:

$$ \Delta\text{Var} = \text{Var}(\mathcal{S}) - \frac{|\mathcal{S}_L|}{|\mathcal{S}|}\,\text{Var}(\mathcal{S}_L) - \frac{|\mathcal{S}_R|}{|\mathcal{S}|}\,\text{Var}(\mathcal{S}_R) $$

The tree greedily picks the $(j^*, t^*)$ pair that maximises $\Delta\text{Var}$. Once a leaf $R_m$ is formed, its prediction is simply the mean of all training targets that fall into it:

$$ \hat{y}(\mathbf{x}) = \frac{1}{|R_m|} \sum_{i \in R_m} y_i \quad \text{for all } \mathbf{x} \in R_m $$

Why This Formula Makes the Model Behave the Way It Does

Where it thrives: The greedy variance-reduction split can carve out any shape of decision boundary without assuming linearity, so it naturally handles complex, non-linear relationships and feature interactions. It also doesn't care about feature scale — no normalisation needed — and handles mixed feature types gracefully.
Where it struggles: The greedy, locally-optimal splits offer no global guarantee. Without constraints on max_depth, the tree memorises the training data perfectly (each leaf holds exactly one sample, $\text{Var} = 0$) — a textbook case of overfitting. The piecewise-constant prediction also means the model cannot extrapolate smoothly beyond the range it was trained on: it just returns the nearest leaf mean, which is often a poor prediction far from the training distribution.

02k-Nearest Neighbors (KNN)

K-Nearest Neighbors regression diagram Image generated using DALL·E 3

Key Terms

TermWhat it means
$k$The number of neighbors to consider when making a prediction
Distance metricFunction that quantifies how "close" two data points are in feature space
Euclidean distanceStraight-line distance in $\mathbb{R}^p$ — the default metric
Manhattan distanceSum of absolute differences along each dimension
Minkowski distanceGeneral family that subsumes Euclidean ($p=2$) and Manhattan ($p=1$)
Lazy learnerKNN stores the entire training set and defers all computation to prediction time — there is no training phase
Curse of dimensionalityIn high-dimensional spaces, distances between all points converge, making "nearest" meaningless

The Mathematics

KNN is a non-parametric, instance-based method. It stores the entire training set $\mathcal{D} = \{(\mathbf{x}_i, y_i)\}_{i=1}^{N}$ and, at prediction time, finds the $k$ training points closest to a query $\mathbf{x}^*$.

The Minkowski distance of order $p$ between $\mathbf{x}^*$ and training point $\mathbf{x}_i$ is:

$$ d_p(\mathbf{x}^*, \mathbf{x}_i) = \left(\sum_{j=1}^{n} \left|x^*_j - x_{ij}\right|^p\right)^{1/p} $$

The Euclidean case ($p=2$) is the most common:

$$ d(\mathbf{x}^*, \mathbf{x}_i) = \sqrt{\sum_{j=1}^{n} \left(x^*_j - x_{ij}\right)^2} $$

Let $\mathcal{N}_k(\mathbf{x}^*)$ be the index set of the $k$ training points with smallest distance to $\mathbf{x}^*$. The prediction is the (optionally distance-weighted) mean of their targets:

$$ \hat{y}(\mathbf{x}^*) = \frac{1}{k} \sum_{i \in \mathcal{N}_k(\mathbf{x}^*)} y_i \quad \text{(uniform weights)} $$ $$ \hat{y}(\mathbf{x}^*) = \frac{\displaystyle\sum_{i \in \mathcal{N}_k(\mathbf{x}^*)} \frac{y_i}{d(\mathbf{x}^*, \mathbf{x}_i)}} {\displaystyle\sum_{i \in \mathcal{N}_k(\mathbf{x}^*)} \frac{1}{d(\mathbf{x}^*, \mathbf{x}_i)}} \quad \text{(distance weights)} $$

The hyperparameter $k$ controls the bias-variance tradeoff:

$k = 1$: pure memorisation, zero training error, high variance (overfitting).
$k = N$: constant prediction equal to the global mean, maximum bias (underfitting).
Optimal $k$ lies somewhere between — typically found by cross-validation.

Why This Formula Makes the Model Behave the Way It Does

Where it thrives: There is absolutely no assumption about the functional form of the relationship — KNN's prediction boundary automatically adapts to whatever local structure the data has. With a well-chosen $k$ and a meaningful distance metric, it can capture highly non-linear patterns effortlessly. It also has a near-zero training cost (just store the data), and adding new training points does not require retraining.
Where it struggles: Because the distance formula treats all features symmetrically, features with large numeric ranges will dominate the distance computation — mandatory feature normalisation is not optional, it is required. More fundamentally, the curse of dimensionality makes the Euclidean distance increasingly uninformative as the number of features grows: every point becomes roughly equidistant from every other point. Prediction time scales as $O(N \cdot p)$ per query (naive search), which is prohibitively slow for large datasets.

03Extra Trees Regressor

Extra Trees Regressor ensemble diagram Image generated using DALL·E 3

Key Terms

TermWhat it means
Extremely Randomised TreesThe full name — the algorithm deliberately injects extra randomness beyond Random Forests at both the split-feature and split-threshold levels
Random thresholdInstead of searching for the optimal threshold for each candidate feature, ET draws the threshold uniformly at random from the feature's range
Feature subsampling ($m_{try}$)At each node only a random subset of $m_{try}$ features is considered, identical to Random Forest
No bootstrapBy default, ET trains every tree on the full training set — only the split randomness decorrelates the trees
n_estimatorsNumber of trees in the ensemble; more trees reduce variance at the cost of compute
AggregationFinal prediction = average of all individual tree predictions (regression)

The Mathematics

Extra Trees (ET) is a fully randomised ensemble of $B$ decision trees. It shares the bagging-ensemble prediction formula with Random Forest but replaces the optimal-split search with a random threshold — the defining mathematical difference.

Step 1 — (No bootstrap): All $B$ trees see the complete training set $\mathcal{D} = \{(\mathbf{x}_i, y_i)\}_{i=1}^{N}$. This is the default; bootstrapping can be enabled but is off by default.

Step 2 — Random feature and threshold selection: At every split node, $m_{try}$ features are drawn at random. For each candidate feature $j$, a threshold $t_j$ is drawn uniformly at random from the observed range of that feature in the node:

$$ t_j \sim \text{Uniform}\!\left(\min_{i \in \mathcal{S}} x_{ij},\; \max_{i \in \mathcal{S}} x_{ij}\right) $$

The split $(j^*, t_{j^*})$ that maximises variance reduction among all $m_{try}$ randomly generated candidates is chosen:

$$ (j^*, t_{j^*}) = \underset{j \in \mathcal{F},\; t_j}{\arg\max}\; \Delta\text{Var}(j, t_j) $$ $$ \Delta\text{Var}(j, t) = \text{Var}(\mathcal{S}) - \frac{|\mathcal{S}_L|}{|\mathcal{S}|}\,\text{Var}(\mathcal{S}_L) - \frac{|\mathcal{S}_R|}{|\mathcal{S}|}\,\text{Var}(\mathcal{S}_R) $$

where $\mathcal{F}$ is the randomly drawn set of $m_{try}$ feature indices, and $\mathcal{S}_L$, $\mathcal{S}_R$ are the resulting child nodes.

Step 3 — Aggregation: The ensemble prediction is the simple average:

$$ \hat{y}_{\text{ET}}(\mathbf{x}^*) = \frac{1}{B} \sum_{b=1}^{B} f_b(\mathbf{x}^*) $$

The same bias-variance decomposition that governs Random Forest applies here. The extra randomness in the threshold further reduces inter-tree correlation $\rho$, pushing the irreducible ensemble variance $\rho\sigma^2$ even lower at the cost of a slight increase in individual-tree bias:

$$ \text{Var}\!\left[\hat{y}_{\text{ET}}\right] = \rho_{\text{ET}} \sigma^2 + \frac{1-\rho_{\text{ET}}}{B}\, \sigma^2, \quad \rho_{\text{ET}} \le \rho_{\text{RF}} $$

Why This Formula Makes the Model Behave the Way It Does

Where it thrives: By randomising the threshold, ET eliminates the most expensive inner loop of tree building — scanning every possible split point — making it significantly faster to train than Random Forest on the same data. The lower inter-tree correlation $\rho_{\text{ET}}$ means the ensemble variance can be smaller than Random Forest's, often yielding competitive or superior generalisation. Like all tree ensembles, it needs no feature scaling and handles mixed feature types and non-linear relationships out of the box.
Where it struggles: The random threshold introduces additional bias into each individual tree — if the optimal split threshold is near the boundary of the feature range, a uniformly random draw is unlikely to find it. On datasets where the signal is clean and splits are highly informative, this extra bias can outweigh the variance gain, causing ET to underperform Random Forest. Like all tree ensembles, it cannot extrapolate beyond the range of the training targets, and memory footprint grows linearly with the number of trees.

04Random Forest Regressor

Random Forest Regressor ensemble diagram Image generated using DALL·E 3

Key Terms

TermWhat it means
Bagging (Bootstrap Aggregating)Each tree is trained on a random bootstrap sample (drawn with replacement) of the training data
Bootstrap sampleA sample of size $N$ drawn with replacement from the original $N$ training points — ~63% unique points per sample
Out-of-Bag (OOB) sampleThe ~37% of training points not drawn for a given tree — used as a free validation set
Feature subsampling ($m_{try}$)At each split only a random subset of $m_{try}$ features are considered — the key decorrelation step
n_estimatorsNumber of trees in the forest; more trees = lower variance, diminishing returns after a point
AggregationFinal prediction = average of all individual tree predictions (regression)
Feature importanceMean decrease in impurity (or permutation importance) averaged across all trees and all splits on that feature

The Mathematics

A Random Forest is a bagged ensemble of $B$ decorrelated decision trees. It introduces two sources of randomness — bootstrap sampling and feature subsampling — to reduce the variance of the ensemble without inflating bias.

Step 1 — Bootstrap sampling: For tree $b = 1, \dots, B$, draw a bootstrap dataset:

$$ \mathcal{D}^{(b)} = \{(\mathbf{x}_{i_1}, y_{i_1}),\dots, (\mathbf{x}_{i_N}, y_{i_N})\} \quad \text{where } i_1, \dots, i_N \overset{\text{i.i.d.}}{\sim} \text{Uniform}\{1,\dots,N\} $$

Step 2 — Feature subsampling: At every split node within tree $b$, only a random subset of $m_{try}$ features is considered:

$$ m_{try} \approx \frac{p}{3} \quad \text{(regression default)} $$

where $p$ is the total number of features. The split is then chosen to maximise variance reduction over only those $m_{try}$ candidate features.

Step 3 — Aggregation (averaging): The forest prediction for a query $\mathbf{x}^*$ is the simple average over all $B$ trees:

$$ \hat{y}_{\text{RF}}(\mathbf{x}^*) = \frac{1}{B} \sum_{b=1}^{B} f_b(\mathbf{x}^*) $$

The variance of this average over an ensemble of correlated trees with individual variance $\sigma^2$ and pairwise correlation $\rho$ is:

$$ \text{Var}\!\left[\hat{y}_{\text{RF}}\right] = \rho \sigma^2 + \frac{1-\rho}{B}\, \sigma^2 $$

As $B \to \infty$ the second term vanishes, leaving $\rho \sigma^2$ as the irreducible ensemble variance. Reducing $\rho$ between trees is exactly the job of feature subsampling — it is the mathematical engine behind the forest's power.

The OOB error is computed without a separate validation set: for each training point $i$, only the trees for which $i$ was not in the bootstrap sample make a prediction:

$$ \hat{y}_i^{\text{OOB}} = \frac{1}{|\mathcal{B}^{-i}|} \sum_{b \in \mathcal{B}^{-i}} f_b(\mathbf{x}_i) $$ $$ \text{OOB MSE} = \frac{1}{N} \sum_{i=1}^{N} \left(y_i - \hat{y}_i^{\text{OOB}}\right)^2 $$

where $\mathcal{B}^{-i}$ is the set of trees whose bootstrap sample did not include point $i$.

Why This Formula Makes the Model Behave the Way It Does

Where it thrives: The variance formula $\rho\sigma^2 + \frac{1-\rho}{B}\sigma^2$ reveals everything: by keeping the bias of each tree roughly the same as a single deep tree (low bias) but dramatically reducing variance through averaging and decorrelation, the forest hits an excellent bias-variance tradeoff out of the box. There is very little tuning required — the default $m_{try} = p/3$ and a large $B$ already give strong performance across a wide variety of datasets. The OOB error is a nearly free internal validation score, and the feature-importance scores give genuine insight into which variables drive predictions.
Where it struggles: Like all tree ensembles, it cannot extrapolate beyond the range of the training targets. When all features are strongly relevant, the forced feature subsampling may actually slow convergence compared to a single deep tree. The model is also less interpretable than a single tree — you trade the white-box transparency of one tree for the black-box predictive power of hundreds. Memory footprint grows linearly with $B$, and prediction requires evaluating all $B$ trees, which can be slow at inference time for very large forests.

05Support Vector Regression (SVR)

Support Vector Regression epsilon-tube diagram Image generated using DALL·E 3

Key Terms

TermWhat it means
HyperplaneThe regression surface $f(\mathbf{x}) = \mathbf{w}^\top \mathbf{x} + b$ — a flat affine subspace in feature space
$\varepsilon$-tube (insensitive zone)A margin of width $2\varepsilon$ around the regression line — errors inside are ignored entirely
Support vectorsOnly the training points that lie on or outside the tube; all others have zero influence
Slack variables $\xi_i, \xi_i^*$The extra error absorbed by points that fall outside the $\varepsilon$-tube
Regularisation $C$Penalty for violations of the tube — large $C$ tries harder to fit all points, small $C$ allows more violation
Kernel $K(\mathbf{x}_i, \mathbf{x}_j)$Implicit feature map to a higher-dimensional space — enables non-linear regression without explicit expansion
Dual variables $\alpha_i, \alpha_i^*$Lagrange multipliers from the dual problem; non-zero only for support vectors

The Mathematics

SVR seeks a function $f(\mathbf{x}) = \mathbf{w}^\top \mathbf{x} + b$ that fits the training data within a tube of half-width $\varepsilon$, while being as flat as possible (small $\|\mathbf{w}\|$). Flatness acts as regularisation, preventing overfitting. The primal optimisation problem is:

$$ \min_{\mathbf{w},\, b,\, \xi,\, \xi^*} \quad \frac{1}{2}\|\mathbf{w}\|^2 + C\sum_{i=1}^{n}\left(\xi_i + \xi_i^*\right) $$

subject to the constraints:

$$ y_i - \mathbf{w}^\top \mathbf{x}_i - b \;\le\; \varepsilon + \xi_i, \qquad \xi_i \ge 0 $$ $$ \mathbf{w}^\top \mathbf{x}_i + b - y_i \;\le\; \varepsilon + \xi_i^*, \qquad \xi_i^* \ge 0 $$

The $\varepsilon$-insensitive loss function used implicitly is:

$$ L_\varepsilon(y, f) = \begin{cases} 0 & \text{if } |y - f| \le \varepsilon \\ |y - f| - \varepsilon & \text{otherwise} \end{cases} $$

Introducing Lagrange multipliers $\alpha_i, \alpha_i^* \ge 0$, the dual problem yields the solution:

$$ \mathbf{w} = \sum_{i=1}^{n} (\alpha_i - \alpha_i^*)\, \mathbf{x}_i $$ $$ f(\mathbf{x}) = \sum_{i=1}^{n} (\alpha_i - \alpha_i^*)\, K(\mathbf{x}_i, \mathbf{x}) + b $$

where $K(\mathbf{x}_i, \mathbf{x}) = \phi(\mathbf{x}_i)^\top \phi(\mathbf{x})$ is the kernel function. Common kernels:

KernelFormulaUse case
Linear$K(\mathbf{x}_i, \mathbf{x}_j) = \mathbf{x}_i^\top \mathbf{x}_j$Linearly separable data
RBF / Gaussian$K(\mathbf{x}_i, \mathbf{x}_j) = \exp\!\left(-\gamma\|\mathbf{x}_i - \mathbf{x}_j\|^2\right)$Non-linear, smooth patterns
Polynomial$K(\mathbf{x}_i, \mathbf{x}_j) = (\mathbf{x}_i^\top \mathbf{x}_j + c)^d$Polynomial relationships

Crucially, only the support vectors (points where $\alpha_i - \alpha_i^* \ne 0$) enter the prediction sum. The $\varepsilon$-insensitive loss ensures that the majority of training points contribute zero to this sum.

Why This Formula Makes the Model Behave the Way It Does

Where it thrives: The $\varepsilon$-insensitive loss makes SVR remarkably robust to small perturbations and noise — data points within the tube have zero influence on the boundary, so minor measurement errors are simply ignored. The kernel trick allows fitting highly non-linear surfaces without explicitly computing the (potentially infinite-dimensional) feature expansion. Minimising $\|\mathbf{w}\|^2$ provides strong regularisation, so SVR generalises well even in relatively high-dimensional spaces with limited data.
Where it struggles: SVR requires feature scaling — the RBF kernel is purely distance-based and features with large magnitudes will dominate. The three hyperparameters $C$, $\varepsilon$, and $\gamma$ (for RBF) interact in subtle ways and demand thorough cross-validation to tune correctly. Solving the quadratic programming dual problem scales roughly as $O(N^2)$ to $O(N^3)$ in the number of training samples, making SVR impractical on large datasets without approximations like Nyström kernel methods.

06Gradient Boosting (XGBoost)

XGBoost ensemble of trees diagram Image generated using DALL·E 3

Key Terms

TermWhat it means
BoostingSequential ensemble technique where each new model corrects the residual errors of all previous models
Residual / Pseudo-residualThe negative gradient of the loss — what the next tree is trained on
Learning rate $\eta$Shrinkage applied to each tree's contribution to prevent overcorrecting
Regularisation $\lambda, \gamma$Penalties on leaf weights and tree complexity baked into XGBoost's objective
GainXGBoost's split score — information gain adjusted by regularisation terms
n_estimatorsNumber of trees (boosting rounds)
max_depthMaximum depth of each individual tree (kept shallow, typically 3–6)

The Mathematics

XGBoost builds an additive model. After $K$ boosting rounds, the prediction is:

$$ \hat{y}_i^{(K)} = \sum_{k=1}^{K} f_k(\mathbf{x}_i) $$

where each $f_k$ is a regression tree. At round $t$, the new tree $f_t$ is found by minimising a regularised second-order Taylor expansion of the loss:

$$ \mathcal{L}^{(t)} \approx \sum_{i=1}^{n} \left[ g_i\, f_t(\mathbf{x}_i) + \tfrac{1}{2}\, h_i\, f_t(\mathbf{x}_i)^2 \right] + \Omega(f_t) $$

where $g_i$ and $h_i$ are the first and second derivatives (gradient and Hessian) of the loss with respect to the current prediction $\hat{y}_i^{(t-1)}$:

$$ g_i = \frac{\partial \ell(y_i,\, \hat{y}_i^{(t-1)})}{\partial \hat{y}_i^{(t-1)}}, \qquad h_i = \frac{\partial^2 \ell(y_i,\, \hat{y}_i^{(t-1)})}{\partial (\hat{y}_i^{(t-1)})^2} $$

The regularisation term penalises tree complexity:

$$ \Omega(f_t) = \gamma T + \frac{\lambda}{2}\sum_{j=1}^{T} w_j^2 $$

where $T$ is the number of leaves and $w_j$ is the weight (output value) of leaf $j$. The optimal weight for leaf $j$ and the corresponding split gain are:

$$ w_j^* = -\frac{\sum_{i \in R_j} g_i}{\sum_{i \in R_j} h_i + \lambda} $$ $$ \text{Gain} = \frac{1}{2} \left[ \frac{\left(\sum_{i \in \mathcal{S}_L} g_i\right)^2}{\sum_{i \in \mathcal{S}_L} h_i + \lambda} + \frac{\left(\sum_{i \in \mathcal{S}_R} g_i\right)^2}{\sum_{i \in \mathcal{S}_R} h_i + \lambda} - \frac{\left(\sum_{i \in \mathcal{S}} g_i\right)^2}{\sum_{i \in \mathcal{S}} h_i + \lambda} \right] - \gamma $$

Each tree's contribution is then shrunk by the learning rate before being added:

$$ \hat{y}_i^{(t)} = \hat{y}_i^{(t-1)} + \eta \cdot f_t(\mathbf{x}_i) $$

Why This Formula Makes the Model Behave the Way It Does

Where it thrives: The second-order Taylor expansion gives XGBoost a much more accurate curvature estimate of the loss landscape than ordinary gradient boosting — it "sees" not just the slope but also how fast the slope changes, leading to better-calibrated leaf weights and faster convergence. The built-in $\lambda$ and $\gamma$ regularisation directly suppress overfitting at the objective level, not just as a post-hoc depth cap. This combination makes XGBoost exceptionally powerful on structured tabular data with complex, noisy, non-linear feature interactions. The learning rate $\eta$ slows down correction deliberately, allowing many shallow trees to combine without overfitting.
Where it struggles: Because it is still a tree ensemble, it shares the decision tree's fundamental inability to extrapolate: predictions outside the training range are capped at the extreme leaf values. On very small datasets, even with regularisation, the many boosting rounds can overfit. It is also noticeably slower to train than a random forest because trees must be built sequentially — parallelism exists only within each tree, not across trees.

Side-by-Side Summary

Model Core idea Training cost Extrapolation? Needs scaling?
Decision Tree Greedy variance-reduction splits → piecewise constant $O(N p \log N)$ No No
KNN Average of $k$ nearest neighbors — zero training $O(1)$ train, $O(Np)$ predict Partially Yes
Extra Trees Fully randomised trees (random thresholds) → averaged prediction $O(B \cdot N \sqrt{p} \log N)$ No No
Random Forest Bagged + decorrelated deep trees → averaged prediction $O(B \cdot N \sqrt{p} \log N)$ No No
SVR Flat hyperplane inside $\varepsilon$-tube, kernel trick for non-linearity $O(N^2)$–$O(N^3)$ Yes (with RBF) Yes
Gradient Boosting Sequential trees on 2nd-order loss residuals + regularisation $O(K \cdot N p \log N)$ No No
The math doesn't just describe what these models do — it explains the choices you make when deploying them. Feature scale matters because distance formulas are in the exponent. Overfitting happens because the optimisation objective has no regularisation term. Extrapolation fails because tree-leaf predictions are constants, not functions. Reading the formulas is reading the model's personality.