Asymmetric Pseudo-Huber Boosting Objective with Gradient and Hessian

Quick Overview

Implement an asymmetric pseudo-Huber loss for gradient boosting, with its weighted mean and per-sample gradient and Hessian. It tests deriving second-order derivatives, applying sample weights the way boosters expect, and keeping the math stable for extremely large and tiny residuals.

Asymmetric Pseudo-Huber Boosting Objective with Gradient and Hessian

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

With residual `r = y_pred - y_true`, per-sample loss is `c·δ²·(sqrt(1 + (r/δ)²) - 1)`, where `c = over_weight` if `r > 0`, else `under_weight` (including `r = 0`). Implement `asymmetric_pseudo_huber_loss` returning the `sample_weight`-weighted mean as a `float`, and `asymmetric_pseudo_huber_objective` returning per-sample `(grad, hess)` arrays with respect to `y_pred`, each multiplied by its sample weight (not divided by the weight sum). Both functions take `(y_true, y_pred, delta=1.0, under_weight=1.0, over_weight=1.0, sample_weight=None)`. Example: ```text y_true = [0, 0, 0], y_pred = [1, -2, 0] delta=1.0, under_weight=2.0, over_weight=1.0 loss -> ((sqrt(2)-1) + 2*(sqrt(5)-1)) / 3 ≈ 0.96212 grad -> [0.70711, -1.78885, 0.0] hess -> [0.35355, 0.17889, 2.0] ``` ```hint Derivatives Name the square root `h = sqrt(1 + (r/δ)²)` and differentiate with respect to `r`; since `y_true` is fixed, that is the same as differentiating with respect to `y_pred`. The identity `h² = 1 + (r/δ)²` simplifies the second derivative a lot. ``` ```hint Numerics at both ends For a tiny residual, `h - 1` subtracts two nearly equal numbers. For a huge one, squaring `r/δ` overflows before the square root can bring it back. Each end needs a different way of writing the same quantity. ``` ### Constraints & Assumptions - Inputs are array-likes, flattened to 1-D. `sample_weight=None` means all ones. - Raise `ValueError` on: length mismatch, `delta <= 0`, negative asymmetry or sample weights, and (loss only) total weight `<= 0`. - Must stay finite and accurate for `|r|` up to `1e200` (gradient saturates to `±c·δ`) and for `|r|` down to `1e-9` (no cancellation in the loss). ### What a Strong Answer Covers - Derives the gradient and Hessian with respect to `y_pred` and checks them against the example. - Applies the asymmetry factor per sample with the stated rule at `r = 0`, and applies sample weights as a weighted mean in the loss and as plain per-sample factors in the objective. - Keeps the loss accurate for tiny residuals and every output finite for huge ones, naming the floating-point operation that fails at each end. - Implements the input validation from the constraints. - Describes the shape of the loss near zero and far from it, including where the derivatives are continuous. ### Follow-up Questions - Why can the Hessian collapse toward zero for large residuals, and how would you handle it? - How do `under_weight` and `over_weight` relate to quantile regression? - How would you choose `delta`, and what happens to the objective as it becomes very large or very small?

Overview: Implement an asymmetric pseudo-Huber loss for gradient boosting, with its weighted mean and per-sample gradient and Hessian. It tests deriving second-order derivatives, applying sample weights the way boosters expect, and keeping the math stable for extremely large and tiny residuals.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

With residual r = y_pred - y_true, per-sample loss is c·δ²·(sqrt(1 + (r/δ)²) - 1), where c = over_weight if r > 0, else under_weight (including r = 0). Implement asymmetric_pseudo_huber_loss returning the sample_weight-weighted mean as a float, and asymmetric_pseudo_huber_objective returning per-sample (grad, hess) arrays with respect to y_pred, each multiplied by its sample weight (not divided by the weight sum).

Both functions take (y_true, y_pred, delta=1.0, under_weight=1.0, over_weight=1.0, sample_weight=None).

Example:

y_true = [0, 0, 0], y_pred = [1, -2, 0]
delta=1.0, under_weight=2.0, over_weight=1.0
loss -> ((sqrt(2)-1) + 2*(sqrt(5)-1)) / 3  ≈ 0.96212
grad -> [0.70711, -1.78885, 0.0]
hess -> [0.35355,  0.17889, 2.0]

Constraints & Assumptions

  • Inputs are array-likes, flattened to 1-D. sample_weight=None means all ones.
  • Raise ValueError on: length mismatch, delta <= 0 , negative asymmetry or sample weights, and (loss only) total weight <= 0 .
  • Must stay finite and accurate for |r| up to 1e200 (gradient saturates to ±c·δ ) and for |r| down to 1e-9 (no cancellation in the loss).

What a Strong Answer Covers Guidance

  • Derives the gradient and Hessian with respect to y_pred and checks them against the example.
  • Applies the asymmetry factor per sample with the stated rule at r = 0 , and applies sample weights as a weighted mean in the loss and as plain per-sample factors in the objective.
  • Keeps the loss accurate for tiny residuals and every output finite for huge ones, naming the floating-point operation that fails at each end.
  • Implements the input validation from the constraints.
  • Describes the shape of the loss near zero and far from it, including where the derivatives are continuous.

Follow-up Questions Guidance

  • Why can the Hessian collapse toward zero for large residuals, and how would you handle it?
  • How do under_weight and over_weight relate to quantile regression?
  • How would you choose delta , and what happens to the objective as it becomes very large or very small?