Skip to content

Fix bugs in Scholar.Metrics.Ranking - #375

Merged
josevalim merged 2 commits into
elixir-nx:mainfrom
RicardoSantos-99:fix-ranking-ties
Nov 18, 2026
Merged

josevalim merged 2 commits into
elixir-nx:mainfrom
RicardoSantos-99:fix-ranking-ties

Conversation

@RicardoSantos-99

Copy link
Copy Markdown
Contributor

Closes #360.

  • Tied scores are resolved in the model's favour. dcg sorted by y_score and then sorted the result by y_true, so each group of tied scores came out in its best order, and a constant score got NDCG 1.0. Each group now gets the mean gain of its items, which is the expected DCG when ties are broken at random. scikit-learn does the same, following McSherry and Najork (ECIR 2008).
  • k equal to the number of elements raises. Nx.split needs a split smaller than the axis. The discounts past k are now zeroed instead, which also averages a tied group cut by k, as scikit-learn does.
  • :k accepts any positive number. Nx.split reads a float as a fraction, so k: 0.5 kept half of the list and k: 2.0 raised. It now takes a positive integer or nil, like scikit-learn's k.
  • dcg_n and ndcg_n don't validate their options. Only dcg/3 did, so ndcg_n(y_true, y_score, k: -1) returned the same as k: 4 and unknown options were ignored.
  • f64 inputs are computed in f32. dcg_value cast to :f32, and Nx.log2 in Nx 1.0 computes log(2) in f32 (fixed on Nx main in Keep the input type in Nx.log2, Nx.log10 and Nx.log with a number base nx#1871, not released yet). The result type now follows the inputs, and the discount uses ln 2 directly.
  • NDCG is NaN when all relevances are zero. It is now 0, as in scikit-learn.
  • The doctests never ran. ranking_test.exs had no doctest, and the expected values were not what inspect prints. dcg/3 had no documentation either, since the @doc sat on dcg_n/3. It now holds the docs, with a tie example.

Checked against scikit-learn 1.6.1 on 600 random lists of 1 to 39 items, 440 of them with ties and 359 with k, in f32 and f64, on the default backend and under EXLA. DCG and NDCG agree to about 1e-7 in f32 and below 1e-15 in f64. Before, 411 of them disagreed and 25 raised.

The tests cover the examples from the issue, a tied group cut by k, k equal to the number of elements, f64 precision, more than 255 groups of tied scores, zero relevances, invalid options and jit_apply.

dcg sorted by y_score and then sorted the result by y_true, so each group
of tied scores came out in the order most favourable to the model, and a
constant score got NDCG 1.0. Each group now gets the mean gain of its
items, as in scikit-learn.

The discounts past :k are now zeroed instead of splitting the tensor,
which also fixes k equal to the number of elements and a tied group cut
by k. :k must be a positive integer or nil, dcg_n validates its options,
and f64 inputs keep f64 precision.
The ideal DCG is now computed from y_true sorted, without averaging ties,
and an ideal DCG of zero gives 0 instead of NaN, as in scikit-learn.
@josevalim
josevalim merged commit 53a29a6 into elixir-nx:main Nov 18, 2026
2 checks passed
@josevalim

Copy link
Copy Markdown
Contributor

Thank you!!!

@RicardoSantos-99
RicardoSantos-99 deleted the fix-ranking-ties branch November 18, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ranking.dcg resolves tied scores in the model's favour, so a constant model scores NDCG 1.0

2 participants