Mathematical Formulations for Learning-to-Rank (LTR) Loss Functions
This document summarizes the mathematical definitions and formulas for Pointwise, Pairwise, and Listwise ranking loss functions used across Information Retrieval and Recommender Systems.
Notation
Let:
- $x = {x_1, x_2, \dots, x_n}$ be a list of items/documents to rank for a given query.
- $y = {y_1, y_2, \dots, y_n}$ be the corresponding ground-truth relevance labels.
- $s = {s_1, s_2, \dots, s_n}$ be the predicted scores from the model ($s_i = f(x_i)$).
1. Pointwise Ranking Loss
Pointwise approaches predict a score or class for each item independently of all other items.
1.1 Mean Squared Error (MSE) / Regression Loss
Used when the relevance labels are continuous numerical variables (e.g., matching score or rating). \(L_{\text{MSE}}(y, s) = \frac{1}{n} \sum_{i=1}^{n} (s_i - y_i)^2\)
1.2 Binary Cross-Entropy (BCE) / Classification Loss
Used when relevance labels are binary ($y_i \in {0, 1}$). \(L_{\text{BCE}}(y, s) = -\frac{1}{n} \sum_{i=1}^{n} \left[ y_i \log \sigma(s_i) + (1 - y_i) \log(1 - \sigma(s_i)) \right]\) Where $\sigma(z) = \frac{1}{1 + \exp(-z)}$ is the sigmoid function.
2. Pairwise Ranking Loss
Pairwise approaches optimize the relative order between pairs of items, pushing the more relevant item to have a higher score than the less relevant item.
2.1 RankNet Loss (Binary Cross-Entropy over Pairs)
Calculates the probability that item $i$ should be ranked higher than item $j$, based on model scores, and minimizes the cross-entropy against the ground-truth relationship.
The modeled probability that item $i$ beats item $j$: \(P_{ij} = P(i \succ j \mid s) = \frac{1}{1 + \exp(-\sigma (s_i - s_j))}\)
The ground-truth probability based on labels ($y_i, y_j$): \(\bar{P}_{ij} = \begin{cases} 1 & \text{if } y_i > y_j \\ 0.5 & \text{if } y_i = y_j \\ 0 & \text{if } y_i < y_j \end{cases}\)
The loss over all pairs $(i, j)$ where $y_i > y_j$: \(L_{\text{RankNet}}(y, s) = -\sum_{i, j: y_i > y_j} \left[ \bar{P}_{ij} \log P_{ij} + (1 - \bar{P}_{ij}) \log(1 - P_{ij}) \right]\)
2.2 Hinge / Max-Margin Loss
Forces the score difference between a relevant item $i$ and an irrelevant item $j$ to be greater than a predefined margin $m$. \(L_{\text{Hinge}}(y, s) = \sum_{i, j: y_i > y_j} \max(0, m - (s_i - s_j))\)
2.3 Bayesian Personalized Ranking (BPR) Loss
Commonly used in recommendation scenarios leveraging implicit feedback ($y_i = 1$ for observed items, $y_j = 0$ for unobserved items). \(L_{\text{BPR}}(y, s) = -\sum_{i, j: y_i > y_j} \log \sigma(s_i - s_j)\)
3. Listwise Ranking Loss
Listwise approaches treat the full candidate permutation as a unified structural unit, evaluating multi-item interdependencies globally.
3.1 ListNet Loss
Translates predicted scores and true relevance values into independent Top-1 probability distributions, minimizing the Cross-Entropy between them.
Top-1 probability for predicted score $s_i$: \(P(s_i) = \frac{\exp(s_i)}{\sum_{k=1}^{n} \exp(s_k)}\)
Top-1 probability for true label $y_i$: \(P(y_i) = \frac{\exp(y_i)}{\sum_{k=1}^{n} \exp(y_k)}\)
Cross-Entropy Loss: \(L_{\text{ListNet}}(y, s) = -\sum_{i=1}^{n} P(y_i) \log P(s_i)\)
3.2 ListMLE Loss
Calculates the exact joint permutation probability of the ideal ground-truth ordering using the Plackett-Luce model and minimizes its negative log-likelihood.
Let $\pi$ be the optimal index permutation sorted by true relevance labels, where $\pi(i)$ is the item index at position $i$: \(L_{\text{ListMLE}}(y, s) = -\log P(\pi \mid s) = \sum_{i=1}^{n} \left[ -s_{\pi(i)} + \log \left( \sum_{j=i}^{n} \exp(s_{\pi(j)}) \right) \right]\)
3.3 LambdaMART / LambdaLoss Framework
Optimizes listwise non-differentiable information retrieval metrics (like NDCG) by constructing surrogate virtual gradients ($\lambda_i$) for individual items.
The cumulative gradient for item $i$: \(\lambda_i = \sum_{j=1}^{n} \lambda_{ij}\)
Where the pair-wise gradient component $\lambda_{ij}$ incorporates the absolute delta of the global metric (e.g., $\Delta \text{NDCG}$) caused by swapping items $i$ and $j$: \(\lambda_{ij} = \sigma \left( \frac{1}{1 + \exp(\sigma(s_i - s_j))} - I(y_i > y_j) \right) \left| \Delta \text{NDCG}_{ij} \right|\)