Vectorized Quantile Pinball Loss

Quick Overview

Implement the quantile (pinball) loss in NumPy with sample weights and three reduction modes, its gradient with respect to the predictions, and a loop-free version that scores several quantile levels at once. It tests asymmetric losses, subgradients at ties, broadcasting and input validation.

Vectorized Quantile Pinball Loss

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement three NumPy functions for the quantile (pinball) loss: ```python pinball_loss(y_true, y_pred, tau, sample_weight=None, reduction="mean") pinball_loss_gradient(y_true, y_pred, tau, sample_weight=None) multi_quantile_pinball_loss(y_true, y_pred, taus, sample_weight=None) ``` With `r = y_true - y_pred`, the per-element loss is `max(tau * r, (tau - 1) * r)`. At `tau = 0.5` the mean loss equals half the MAE. The functions must behave as follows, with weights `w` defaulting to 1. Below, `y` is `y_true` and `q` is `y_pred`. - `reduction="mean"` returns `sum(w * l) / sum(w)`, `reduction="sum"` returns `sum(w * l)`, and `reduction="none"` returns the unweighted per-element array. - `pinball_loss_gradient` returns the gradient of the weighted mean loss with respect to `y_pred`. The per-element slope is `-tau` if `y > q`, `1 - tau` if `y < q` and `0.5 - tau` on ties, scaled by `w / sum(w)`. - `multi_quantile_pinball_loss` takes `y_pred` of shape `(n, k)` and returns a `(k,)` array of weighted mean losses, where column `j` is scored at `taus[j]`. It must not use Python loops. - All functions raise `ValueError` for any tau outside the open interval `(0, 1)`, a shape or weight-length mismatch, or an unknown `reduction`. For example: ```python y = [1, 2, 3] q = [2, 2, 1] pinball_loss(y, q, 0.3, reduction="none") # [0.7, 0.0, 0.6] pinball_loss(y, q, 0.3) # 1.3 / 3, about 0.4333 pinball_loss_gradient(y, q, 0.3) # [0.7, 0.2, -0.3] / 3 multi_quantile_pinball_loss(y, [[2, 1], [2, 1], [1, 1]], [0.3, 0.7]) # [0.4333, 0.7] ``` ```hint Shapes for many quantiles Decide which axis `y_true`, `y_pred` and `taus` each live on, so that one broadcast expression produces the whole `(n, k)` matrix of losses. ``` ```hint Validation order Look at where `sample_weight` gets validated. A weight array of the wrong length should fail for every `reduction`, including `"none"`. ``` ### Constraints & Assumptions - Inputs are array-likes of finite floats. `pinball_loss` and `pinball_loss_gradient` flatten `y_true` and `y_pred` to 1-D. - Weights are non-negative with a positive sum. - Results must match `sklearn.metrics.mean_pinball_loss` (with `alpha=tau`) to `rtol=1e-7`. ### What a Strong Answer Covers - A branch-free per-element loss and the three reductions, with weights applied the way `mean_pinball_loss` applies them. - The gradient slope on each side of the kink and at ties, its sign with respect to `y_pred`, and the `w / sum(w)` scaling. - A broadcast multi-quantile computation that returns a `(k,)` vector without a Python loop. - Validation of tau, shapes, weight length and `reduction` on every code path. - Time and space complexity in terms of `n` and `k`. ### Follow-up Questions - Independent per-quantile models can produce crossing quantiles. How would you prevent that? - Why is the pinball loss hard for second-order gradient boosting, and how is that handled? - Which constant minimizes the pinball loss, and how does that relate to evaluating quantile forecasts?

Overview: Implement the quantile (pinball) loss in NumPy with sample weights and three reduction modes, its gradient with respect to the predictions, and a loop-free version that scores several quantile levels at once. It tests asymmetric losses, subgradients at ties, broadcasting and input validation.

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

Implement three NumPy functions for the quantile (pinball) loss:

pinball_loss(y_true, y_pred, tau, sample_weight=None, reduction="mean")
pinball_loss_gradient(y_true, y_pred, tau, sample_weight=None)
multi_quantile_pinball_loss(y_true, y_pred, taus, sample_weight=None)

With r = y_true - y_pred, the per-element loss is max(tau * r, (tau - 1) * r). At tau = 0.5 the mean loss equals half the MAE.

The functions must behave as follows, with weights w defaulting to 1. Below, y is y_true and q is y_pred.

  • reduction="mean" returns sum(w * l) / sum(w) , reduction="sum" returns sum(w * l) , and reduction="none" returns the unweighted per-element array.
  • pinball_loss_gradient returns the gradient of the weighted mean loss with respect to y_pred . The per-element slope is -tau if y > q , 1 - tau if y < q and 0.5 - tau on ties, scaled by w / sum(w) .
  • multi_quantile_pinball_loss takes y_pred of shape (n, k) and returns a (k,) array of weighted mean losses, where column j is scored at taus[j] . It must not use Python loops.
  • All functions raise ValueError for any tau outside the open interval (0, 1) , a shape or weight-length mismatch, or an unknown reduction .

For example:

y = [1, 2, 3]
q = [2, 2, 1]

pinball_loss(y, q, 0.3, reduction="none")  # [0.7, 0.0, 0.6]
pinball_loss(y, q, 0.3)                    # 1.3 / 3, about 0.4333
pinball_loss_gradient(y, q, 0.3)           # [0.7, 0.2, -0.3] / 3

multi_quantile_pinball_loss(y, [[2, 1], [2, 1], [1, 1]], [0.3, 0.7])  # [0.4333, 0.7]

Constraints & Assumptions

  • Inputs are array-likes of finite floats. pinball_loss and pinball_loss_gradient flatten y_true and y_pred to 1-D.
  • Weights are non-negative with a positive sum.
  • Results must match sklearn.metrics.mean_pinball_loss (with alpha=tau ) to rtol=1e-7 .

What a Strong Answer Covers Guidance

  • A branch-free per-element loss and the three reductions, with weights applied the way mean_pinball_loss applies them.
  • The gradient slope on each side of the kink and at ties, its sign with respect to y_pred , and the w / sum(w) scaling.
  • A broadcast multi-quantile computation that returns a (k,) vector without a Python loop.
  • Validation of tau, shapes, weight length and reduction on every code path.
  • Time and space complexity in terms of n and k .

Follow-up Questions Guidance

  • Independent per-quantile models can produce crossing quantiles. How would you prevent that?
  • Why is the pinball loss hard for second-order gradient boosting, and how is that handled?
  • Which constant minimizes the pinball loss, and how does that relate to evaluating quantile forecasts?