Skip to main content
NDCG (Normalized Discounted Cumulative Gain) is a ranking quality metric that handles graded relevance — items can be highly relevant, somewhat relevant, or not relevant at all, rather than just relevant or not. It also applies a logarithmic discount so that items ranked higher contribute more to the score than items ranked lower.

Constants

These tolerances are used by IsClose and IsZero for floating-point comparisons throughout the library.

NDCG

Returns the Normalized Discounted Cumulative Gain at cutoff K. NDCG compares the DCG of the actual ranked list against the ideal DCG (the best possible ordering of the same items). A score of 1.0 indicates a perfect ranking; 0.0 means no relevant items were retrieved. Returns 0.0 when the ideal DCG is zero.
[]string
required
The ranked list of item identifiers returned by the system, ordered from most to least relevant.
map[string]int
required
A map of item identifier to integer relevance grade. Higher grades indicate greater relevance. Items not present in the map are treated as grade 0.
int
required
The cutoff depth. Only the first k positions are evaluated.
Returns float64 — DCG of the predicted ranking divided by the ideal DCG, in the range [0, 1].

Example

The ideal top-3 ranking would be “A” (grade 3), “E” (grade 3), “B” (grade 2). The actual top-3 is “A” (3), “B” (2), “C” (1). The actual DCG is lower because “E” was not retrieved at rank 2, giving NDCG ≈ 0.727.

DCG

Computes the Discounted Cumulative Gain for a sequence of relevance grades. Grades are discounted by the logarithm of their rank position so that higher-ranked items contribute more to the total. The formula applied at each position i (0-indexed) is:
[]int
required
A slice of integer relevance grades in rank order. The first element is rank 1, the second is rank 2, and so on.
Returns float64 — the summed discounted gain across all positions.

IsClose

Reports whether two float64 values are approximately equal, using both absolute and relative tolerances defined by AbsTol and RelTol. The comparison uses the formula:
float64
required
The first value to compare.
float64
required
The second value to compare.
Returns bool — true if a and b are within tolerance of each other.

IsZero

Reports whether a float64 value is approximately zero. Equivalent to calling IsClose(a, 0.0).
float64
required
The value to test.
Returns bool — true if a is within tolerance of 0.0.