|
23 | 23 |
|
24 | 24 | @flax.struct.dataclass |
25 | 25 | class DCGAtK(base.Average): |
26 | | - r"""Computes DCG@k (Discounted Cumulative Gain at k) metrics. |
| 26 | + r"""Computes Discounted Cumulative Gain at k metric. |
27 | 27 |
|
28 | | - This implementation calculates DCG@k based on the principle: |
29 | | - $DCG@k(y, s) = \sum_{i | \text{rank}(s_i) \le k} \text{gain}(y_i) \times |
30 | | - \text{rank\_discount}(\text{rank}(s_i))$ |
31 | | - where $y_i$ is the label of item $i$, $s_i$ is its score, |
32 | | - and $\text{rank}(s_i)$ is the 1-based rank of item $i$ based on its score. |
| 28 | + DCG tells how good a list of search results or recommendations is, based on |
| 29 | + the relevance of the items and their positions in the list. |
33 | 30 |
|
34 | | - The gain is $gain(y_i) = 2^{y_i} - 1$. |
35 | | - The rank_discount is $1 / \log_2(\text{rank} + 1)$. |
| 31 | + This implementation calculates :math:`DCG@k` based on the following formula: |
| 32 | +
|
| 33 | + .. math:: |
| 34 | + DCG@K(y, s) = \sum_{i=1}^{K} \text{gain}(y_i) \times |
| 35 | + \text{rank_discount}(\text{rank}(s_i)) |
| 36 | +
|
| 37 | + where |
| 38 | +
|
| 39 | + - :math:`y_i` is the relevance label from the labels, |
| 40 | + - :math:`s_i` is its score from the prediction, |
| 41 | + - :math:`\text{rank}(s_i)` is the 1-based rank of item :math:`i`. |
| 42 | + - :math:`\text{gain}(y_i) = 2^{y_i} - 1`. |
| 43 | + - :math:`\text{rank_discount}(\text{rank}(s_i)) = \frac{1}{\log_2(\text{rank}(s_i) + 1)}`. |
| 44 | +
|
| 45 | + We get the final formula: |
| 46 | +
|
| 47 | + .. math:: |
| 48 | + DCG@K(y, s) = \sum_{i=1}^{K} \frac{2^{y_i} - 1}{\log_2(\text{rank}(s_i) + |
| 49 | + 1)} |
36 | 50 | """ |
37 | 51 |
|
38 | 52 | @classmethod |
|
0 commit comments