Skip to content

[Feature]: Add NDCG (Normalized Discounted Cumulative Gain) metric to rec_sys#3670

Closed
steaphenai wants to merge 17 commits intopytorch:masterfrom
steaphenai:feat/NDCG-metric-steaphen
Closed

[Feature]: Add NDCG (Normalized Discounted Cumulative Gain) metric to rec_sys#3670
steaphenai wants to merge 17 commits intopytorch:masterfrom
steaphenai:feat/NDCG-metric-steaphen

Conversation

@steaphenai
Copy link
Copy Markdown
Contributor

@steaphenai steaphenai commented Mar 13, 2026

Fixes #3581
Refs #3568 #3569 #3610

Description: Implements NDCG (Normalized Discounted Cumulative Gain) metric for recommendation systems and ranking evaluation.

Implementation Details

Mathematical Background

  • DCG: Discounted Cumulative Gain - rewards relevant items, penalizes low positions
  • IDCG: Ideal DCG - perfect ranking baseline
  • NDCG: DCG / IDCG - normalized score from 0.0 to 1.0

Formula: NDCG@K = Σ(2^rel_i - 1) / log2(i + 1)

Testing

  • 26 tests passing locally (verified against ranx)
  • Parametrized tests across multiple configurations
  • Edge cases covered (perfect prediction, zero relevance, graded labels)
  • Distributed training testedcs

Check list:

  • New tests are added (if a new feature is added)
  • New doc strings: description and/or example code are in RST format
  • Documentation is updated (if required)

steaphenai and others added 12 commits March 3, 2026 17:53
Added ranx = pytest.importorskip(...) to make ranx optional
Wrapped ranx validation checks with if ranx is not None
Added pytest.skip() for tests that specifically require ranx

Tests now pass without ranx installed while still validating against it when available. The NDCG metric implementation itself has no ranx dependency.
Use try/except to handle missing ranx dependency instead of pytest.importorskip
Removed try/except import and conditional checks for ranx.
ranx is now a required dev dependency in requirements-dev.txt, so tests can use it directly without fallback logic.
Co-authored-by: vfdev <vfdev.5@gmail.com>
Add detailed explanation of relevance types for NDCG.
@github-actions github-actions Bot added docs module: metrics Metrics module labels Mar 13, 2026
@steaphenai steaphenai changed the title Feat/ndcg metric steaphen [Feature]: Add NDCG (Normalized Discounted Cumulative Gain) metric to rec_sys Mar 13, 2026
@steaphenai steaphenai closed this Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Add NDCG (Normalized Discounted Cumulative Gain) metric to rec_sys

1 participant