LambdaMART Lambda Gradients
Background
LambdaMART approximates gradients of the (non-differentiable) NDCG metric by attaching a per-pair "lambda" weighted by the absolute NDCG change from swapping ranks. The state-of-the-art listwise learning-to-rank algorithm for tabular features for over a decade.
Problem statement
Implement lambda_gradients(scores, labels) returning a length- array of gradients. For every pair with :
where gain , discount , and is the rank in the current scores. The per-item gradient (for maximising NDCG) is .
Input
scores— 1-D array of floats, model output scores.labels— 1-D array of integers, graded relevance.
Output
Returns np.ndarray of length , the per-item gradient.
Examples
Example 1 — all same label → zero gradients
scores=[1,2,3], labels=[1,1,1]
Output: all zeros
Example 2 — positive below negative gets pushed up
scores=[1.0, 2.0], labels=[1, 0]
Output: g[0] > 0 and g[1] < 0
Constraints
- Length mismatch is invalid.
- Walk all pairs where (skip ).
- Returns
np.ndarray.
Notes
- Why "lambda". Each pair contributes a single quantity (the lambda); summing them gives a gradient. There's no closed-form gradient of NDCG, so we approximate.
- MART = Multiple Additive Regression Trees. The lambdas become regression targets for gradient-boosted trees; that's the full LambdaMART recipe.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example - equal labels yield zero gradient
- •Sample - relevant item below irrelevant is pushed up
- •Example - matches independent gradient vector