Compute the XGBoost Optimal Leaf Weight and Split Gain from Gradients and Hessians

Quick Overview

Implement the XGBoost optimal leaf weight and split gain from per-sample gradients and hessians, including the lambda and gamma regularization terms and the empty-child and min_child_weight checks. It tests the second-order objective behind gradient-boosted trees.

Compute the XGBoost Optimal Leaf Weight and Split Gain from Gradients and Hessians

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Given per-sample gradients `grad` and hessians `hess`, implement `leaf_weight`, which returns the optimal leaf weight $$ w^* = -\frac{G}{H + \lambda} $$ where $G$ and $H$ are the sums of `grad` and `hess` and $\lambda$ is `reg_lambda`. Also implement `split_gain`, which returns the XGBoost gain of splitting the node into the samples where the boolean `left_mask` is true (left child) and false (right child): $$ \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 $$ Here $G_L, H_L$ and $G_R, H_R$ are the gradient and hessian sums of each child, $G$ and $H$ are the sums over the whole node, and $\gamma$ is `gamma`. ```python def leaf_weight(grad, hess, reg_lambda=1.0): ... def split_gain(grad, hess, left_mask, reg_lambda=1.0, gamma=0.0, min_child_weight=1.0): ... ``` Example: ```python g = [-2, -1, 1, 3] h = [1, 1, 1, 1] mask = [True, True, False, False] split_gain(g, h, mask, reg_lambda=1.0, gamma=0.5, min_child_weight=1.0) # 0.5 * (9/3 + 16/3 - 1/5) - 0.5 = 3.5667 (rounded) leaf_weight([1, -2, 3], [1, 1, 2], reg_lambda=1.0) # -0.4 ``` The left child holds the first two samples ($G_L = -3$, $H_L = 2$), the right child holds the last two ($G_R = 4$, $H_R = 2$), and the node has $G = 1$, $H = 4$. For the leaf weight, $G = 2$ and $H = 4$, so $w^* = -2/5$. ```hint Check before you divide Both functions need only sums of gradients and hessians. Decide which sums each child needs, and apply the empty-child and min_child_weight rules before computing any score. ``` ### Constraints & Assumptions - `grad`, `hess` and `left_mask` are array-likes of equal length. Hessians are non-negative. - If either child is empty, `split_gain` returns `float("-inf")`. - If `HL < min_child_weight` or `HR < min_child_weight`, `split_gain` returns `float("-inf")`. Equality is allowed. - Both functions return a Python `float`. ### What a Strong Answer Covers - Where the leaf weight formula comes from in the second-order approximation of the loss. - How the gain relates to the objective of the parent and the two children, and why `gamma` appears once. - The child and parent sums, and the order of the empty-child and `min_child_weight` checks. - The return types and the time and space cost of the computation. ### Follow-up Questions - How would you find the best split threshold over a sorted feature efficiently? - How does XGBoost handle missing values when scoring a split?

Overview: Implement the XGBoost optimal leaf weight and split gain from per-sample gradients and hessians, including the lambda and gamma regularization terms and the empty-child and min_child_weight checks. It tests the second-order objective behind gradient-boosted trees.

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

Given per-sample gradients grad and hessians hess, implement leaf_weight, which returns the optimal leaf weight

w∗=−GH+λw^* = -\frac{G}{H + \lambda}

where GG and HH are the sums of grad and hess and λ\lambda is reg_lambda. Also implement split_gain, which returns the XGBoost gain of splitting the node into the samples where the boolean left_mask is true (left child) and false (right child):

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

Here GL,HLG_L, H_L and GR,HRG_R, H_R are the gradient and hessian sums of each child, GG and HH are the sums over the whole node, and γ\gamma is gamma.

def leaf_weight(grad, hess, reg_lambda=1.0): ...

def split_gain(grad, hess, left_mask, reg_lambda=1.0, gamma=0.0, min_child_weight=1.0): ...

Example:

g = [-2, -1, 1, 3]
h = [1, 1, 1, 1]
mask = [True, True, False, False]
split_gain(g, h, mask, reg_lambda=1.0, gamma=0.5, min_child_weight=1.0)
# 0.5 * (9/3 + 16/3 - 1/5) - 0.5 = 3.5667 (rounded)

leaf_weight([1, -2, 3], [1, 1, 2], reg_lambda=1.0)
# -0.4

The left child holds the first two samples (GL=−3G_L = -3, HL=2H_L = 2), the right child holds the last two (GR=4G_R = 4, HR=2H_R = 2), and the node has G=1G = 1, H=4H = 4. For the leaf weight, G=2G = 2 and H=4H = 4, so w∗=−2/5w^* = -2/5.

Constraints & Assumptions

  • grad , hess and left_mask are array-likes of equal length. Hessians are non-negative.
  • If either child is empty, split_gain returns float("-inf") .
  • If HL < min_child_weight or HR < min_child_weight , split_gain returns float("-inf") . Equality is allowed.
  • Both functions return a Python float .

What a Strong Answer Covers Guidance

  • Where the leaf weight formula comes from in the second-order approximation of the loss.
  • How the gain relates to the objective of the parent and the two children, and why gamma appears once.
  • The child and parent sums, and the order of the empty-child and min_child_weight checks.
  • The return types and the time and space cost of the computation.

Follow-up Questions Guidance

  • How would you find the best split threshold over a sorted feature efficiently?
  • How does XGBoost handle missing values when scoring a split?