Gradient Boosting: Sparsity-Aware Split Finding with a Default Direction for Missing Values

Quick Overview

Implement sparsity-aware split finding for a gradient-boosted tree: a regularized gain function, a search over features and midpoint thresholds that tries missing values on each side, and a mask that routes rows. It tests prefix sums, tie-breaking rules and min_child_weight handling.

Gradient Boosting: Sparsity-Aware Split Finding with a Default Direction for Missing Values

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement three functions for split finding in a gradient-boosted tree whose features can be missing (`NaN`). `split_gain` returns the regularized second-order gain of one candidate split. $G_L, H_L$ and $G_R, H_R$ are the gradient and hessian sums of the left and right children, $G = G_L + G_R$ and $H = H_L + H_R$: $$ \text{gain} = \frac{1}{2}\left(\frac{G_L^2}{H_L+\lambda} + \frac{G_R^2}{H_R+\lambda} - \frac{G^2}{H+\lambda}\right) - \gamma $$ `find_best_split` searches every feature and threshold, routing the `NaN` rows left and then right, and returns `(feature, threshold, default_left, gain)` as `(int, float, bool, float)`, or `None`. `apply_split` returns the boolean mask of rows that go to the left child. ```python def split_gain(G_left, H_left, G_right, H_right, reg_lambda=1.0, gamma=0.0): ... def find_best_split(X, grad, hess, reg_lambda=1.0, gamma=0.0, min_child_weight=0.0): ... def apply_split(X, feature, threshold, default_left): ... ``` The search follows these rules: - Candidate thresholds are midpoints between consecutive distinct non-missing sorted values. A row goes left if and only if `x < threshold`. - Skip any candidate where either child's hessian sum is below `min_child_weight`. - Only a strictly positive gain is accepted. Return `None` if no candidate qualifies. - Ties keep the first candidate found: lowest feature, then lowest threshold, then `default_left=True`. - `apply_split` sends `NaN` rows to the side given by `default_left`. Example, with `reg_lambda=1.0` and `gamma=0.0`: ```text X = [[1.0], [2.0], [3.0], [nan]] grad = [-2.0, -1.0, 3.0, -4.0] hess = [1.0, 1.0, 1.0, 1.0] find_best_split(X, grad, hess) Output: (0, 2.5, True, 6.775) ``` The winning split sends 1.0, 2.0 and the missing row left ($G_L = -7$, $H_L = 3$) and sends 3.0 right ($G_R = 3$, $H_R = 1$), for a gain of $\tfrac{1}{2}\left(\tfrac{49}{4} + \tfrac{9}{2} - \tfrac{16}{5}\right) = 6.775$. ```hint Missing rows Missing rows never take part in a threshold comparison. Check how their gradient and hessian sums change as the threshold moves along one feature. ``` ```hint Scan order After sorting a feature's observed values, the left child's sums are running totals. Think about which sorted positions give a valid threshold when values repeat, and how your comparison operator decides which of two equal gains survives. ``` ### Constraints & Assumptions - `X` has shape `(n, d)`. Missing values are `np.nan`. - `len(grad) == len(hess) == n` and every `hess` value is positive. - Features with fewer than 2 distinct observed values produce no candidates. - Defaults are `reg_lambda=1.0`, `gamma=0.0` and `min_child_weight=0.0`. ### What a Strong Answer Covers - The gain formula with both regularization terms, and which rows the parent sums $G$ and $H$ include. - How sorting and prefix sums give the child sums at each threshold, and how repeated values are handled. - How the missing rows are scored on each side, and what that costs per threshold. - Where `min_child_weight`, the strictly positive gain rule and the tie order are enforced in the scan. - Time and space complexity, plus edge cases such as an all-missing feature, a constant feature and a feature with no missing values. ### Follow-up Questions - How does this scale to millions of rows, and what does XGBoost's `hist` method change? - Why is it enough to compute `G_miss` and `H_miss` once per feature instead of once per threshold? - At prediction time, what happens to a missing value in a feature that had no missing values during training?

Overview: Implement sparsity-aware split finding for a gradient-boosted tree: a regularized gain function, a search over features and midpoint thresholds that tries missing values on each side, and a mask that routes rows. It tests prefix sums, tie-breaking rules and min_child_weight handling.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement three functions for split finding in a gradient-boosted tree whose features can be missing (NaN).

split_gain returns the regularized second-order gain of one candidate split. GL,HLG_L, H_L and GR,HRG_R, H_R are the gradient and hessian sums of the left and right children, G=GL+GRG = G_L + G_R and H=HL+HRH = H_L + H_R:

gain=12(GL2HL+λ+GR2HR+λ−G2H+λ)−γ\text{gain} = \frac{1}{2}\left(\frac{G_L^2}{H_L+\lambda} + \frac{G_R^2}{H_R+\lambda} - \frac{G^2}{H+\lambda}\right) - \gamma

find_best_split searches every feature and threshold, routing the NaN rows left and then right, and returns (feature, threshold, default_left, gain) as (int, float, bool, float), or None. apply_split returns the boolean mask of rows that go to the left child.

def split_gain(G_left, H_left, G_right, H_right, reg_lambda=1.0, gamma=0.0): ...

def find_best_split(X, grad, hess, reg_lambda=1.0, gamma=0.0, min_child_weight=0.0): ...

def apply_split(X, feature, threshold, default_left): ...

The search follows these rules:

  • Candidate thresholds are midpoints between consecutive distinct non-missing sorted values. A row goes left if and only if x < threshold .
  • Skip any candidate where either child's hessian sum is below min_child_weight .
  • Only a strictly positive gain is accepted. Return None if no candidate qualifies.
  • Ties keep the first candidate found: lowest feature, then lowest threshold, then default_left=True .
  • apply_split sends NaN rows to the side given by default_left .

Example, with reg_lambda=1.0 and gamma=0.0:

X    = [[1.0], [2.0], [3.0], [nan]]
grad = [-2.0, -1.0, 3.0, -4.0]
hess = [1.0, 1.0, 1.0, 1.0]
find_best_split(X, grad, hess)
Output: (0, 2.5, True, 6.775)

The winning split sends 1.0, 2.0 and the missing row left (GL=−7G_L = -7, HL=3H_L = 3) and sends 3.0 right (GR=3G_R = 3, HR=1H_R = 1), for a gain of 12(494+92−165)=6.775\tfrac{1}{2}\left(\tfrac{49}{4} + \tfrac{9}{2} - \tfrac{16}{5}\right) = 6.775.

Constraints & Assumptions

  • X has shape (n, d) . Missing values are np.nan .
  • len(grad) == len(hess) == n and every hess value is positive.
  • Features with fewer than 2 distinct observed values produce no candidates.
  • Defaults are reg_lambda=1.0 , gamma=0.0 and min_child_weight=0.0 .

What a Strong Answer Covers Guidance

  • The gain formula with both regularization terms, and which rows the parent sums GG and HH include.
  • How sorting and prefix sums give the child sums at each threshold, and how repeated values are handled.
  • How the missing rows are scored on each side, and what that costs per threshold.
  • Where min_child_weight , the strictly positive gain rule and the tie order are enforced in the scan.
  • Time and space complexity, plus edge cases such as an all-missing feature, a constant feature and a feature with no missing values.

Follow-up Questions Guidance

  • How does this scale to millions of rows, and what does XGBoost's hist method change?
  • Why is it enough to compute G_miss and H_miss once per feature instead of once per threshold?
  • At prediction time, what happens to a missing value in a feature that had no missing values during training?