Implement Tweedie Deviance Loss and Its Log-Link Gradient in NumPy

Quick Overview

Implement the weighted Tweedie deviance for a power between 1 and 2 with log-scale predictions, plus its gradient with respect to the raw prediction. Tests numerical stability with exact zero targets, sample weight normalization, and deriving a loss gradient through a log link.

Implement Tweedie Deviance Loss and Its Log-Link Gradient in NumPy

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

You are given non-negative targets `y_true`, log-scale predictions `raw_pred` (so $\mu = e^{\text{raw\_pred}}$), a `power` $p$ and an optional `sample_weight`. Implement two NumPy functions: - `tweedie_deviance(y_true, raw_pred, power, sample_weight=None)` returns the weighted mean deviance as a Python `float`. - `tweedie_deviance_gradient(y_true, raw_pred, power, sample_weight=None)` returns its gradient with respect to `raw_pred`, an array of shape `(n,)`. Weights are normalized to sum to 1, and the default is uniform. The unit deviance and the loss are $$ d_i = 2\left(\frac{y_i^{2-p}}{(1-p)(2-p)} - \frac{y_i\,\mu_i^{1-p}}{1-p} + \frac{\mu_i^{2-p}}{2-p}\right), \qquad \text{loss} = \sum_i w_i\, d_i . $$ Example: ```python y_true = [0.0, 1.0] raw_pred = [0.0, 0.0] power = 1.5 tweedie_deviance(y_true, raw_pred, power) # 2.0 tweedie_deviance_gradient(y_true, raw_pred, power) # array([1.0, 0.0]) ``` ```hint Work in log space Rewrite every power of $\mu$ as an exponential of a multiple of `raw_pred` before writing code, then check what each term becomes when $y = 0$. ``` ```hint Gradient Differentiate one sample's deviance with respect to `raw_pred` and factor the result. It should vanish at a point you would expect for a well-fitted prediction. ``` ### Constraints & Assumptions - Raise `ValueError` if `power` is not strictly inside $(1, 2)$, if any target is negative, or if the lengths of `y_true`, `raw_pred` and `sample_weight` differ. - Targets may contain many exact zeros. - Results must stay finite for `|raw_pred|` up to 40. - Weights are positive. Integer weights must give the same result as repeating rows. ### What a Strong Answer Covers - Validation of the power range, negative targets and length mismatches, plus weight normalization with a uniform default. - A deviance written in terms of the log prediction, with an argument for why it stays finite over the stated range and handles zero targets. - A correct derivation of the gradient with respect to `raw_pred`, including the chain rule through the log link and the normalized weight on each sample. - A hand check against the example and the cost of the computation. ### Follow-up Questions - What is the Hessian with respect to `raw_pred`, and why does it matter for gradient boosting? - At what constant prediction is the summed gradient zero, and how would you use that to initialize a boosting model? - Why does a power between 1 and 2 suit targets with many exact zeros, such as insurance claim amounts?

Overview: Implement the weighted Tweedie deviance for a power between 1 and 2 with log-scale predictions, plus its gradient with respect to the raw prediction. Tests numerical stability with exact zero targets, sample weight normalization, and deriving a loss gradient through a log link.

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

You are given non-negative targets y_true, log-scale predictions raw_pred (so μ=eraw_pred\mu = e^{\text{raw\_pred}}), a power pp and an optional sample_weight. Implement two NumPy functions:

  • tweedie_deviance(y_true, raw_pred, power, sample_weight=None) returns the weighted mean deviance as a Python float .
  • tweedie_deviance_gradient(y_true, raw_pred, power, sample_weight=None) returns its gradient with respect to raw_pred , an array of shape (n,) .

Weights are normalized to sum to 1, and the default is uniform. The unit deviance and the loss are

di=2(yi2−p(1−p)(2−p)−yi μi1−p1−p+μi2−p2−p),loss=∑iwi di.d_i = 2\left(\frac{y_i^{2-p}}{(1-p)(2-p)} - \frac{y_i\,\mu_i^{1-p}}{1-p} + \frac{\mu_i^{2-p}}{2-p}\right), \qquad \text{loss} = \sum_i w_i\, d_i .

Example:

y_true = [0.0, 1.0]
raw_pred = [0.0, 0.0]
power = 1.5

tweedie_deviance(y_true, raw_pred, power)           # 2.0
tweedie_deviance_gradient(y_true, raw_pred, power)  # array([1.0, 0.0])

Constraints & Assumptions

  • Raise ValueError if power is not strictly inside (1,2)(1, 2) , if any target is negative, or if the lengths of y_true , raw_pred and sample_weight differ.
  • Targets may contain many exact zeros.
  • Results must stay finite for |raw_pred| up to 40.
  • Weights are positive. Integer weights must give the same result as repeating rows.

What a Strong Answer Covers Guidance

  • Validation of the power range, negative targets and length mismatches, plus weight normalization with a uniform default.
  • A deviance written in terms of the log prediction, with an argument for why it stays finite over the stated range and handles zero targets.
  • A correct derivation of the gradient with respect to raw_pred , including the chain rule through the log link and the normalized weight on each sample.
  • A hand check against the example and the cost of the computation.

Follow-up Questions Guidance

  • What is the Hessian with respect to raw_pred , and why does it matter for gradient boosting?
  • At what constant prediction is the summed gradient zero, and how would you use that to initialize a boosting model?
  • Why does a power between 1 and 2 suit targets with many exact zeros, such as insurance claim amounts?