### Question:
ExpectedReciprocalRank extends AbstractJudgmentScorer { @Override public double score(Judgments judgments, SearchResultSet searchResultSet) { double maxInTheseJudgments = getMax(judgments); if (maxScore != null && maxInTheseJudgments > maxScore) { throw new IllegalArgumentException( String.format(Locale.US, "Maximum in this jugment set (%s) is > than the max (%s)", maxInTheseJudgments, maxScore)); } double max = maxScore != null ? maxScore : maxInTheseJudgments; if (max < 0) { throw new IllegalArgumentException("maximum relevance grade must be > 0"); } double twoToTheMax = FastMath.pow(2, max); double p = 1.0; double err = 0.0; for (int i = 0; i < getAtN() && i < searchResultSet.size(); i++) { String id = searchResultSet.get(i); double grade = getGrade(judgments, id, max); if (grade <= 0.0) { continue; } int rank = i + 1; double mappedRelevance = mapRelevanceScore( grade, twoToTheMax); err = err + (p * mappedRelevance / rank); p = p * (1.0 - mappedRelevance); } addScore(judgments.getQueryInfo(), err); return err; } ExpectedReciprocalRank(int atN, Map<String, String> params); @Override double score(Judgments judgments, SearchResultSet searchResultSet); Double getMaxScore(); double getNoJudgment(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); static final String MAX_SCORE; static final String NO_JUDGMENT; static final double DEFAULT_NO_JUDGMENT; }### Answer:
@Test public void testTop10() { ExpectedReciprocalRank err = new ExpectedReciprocalRank(10, DEFAULT_PARAMS); Judgments judgments = newJudgments(); for (int i = 0; i < 10; i++) { judgments.addJudgment(RESULTS.get(i), 3); } assertEquals(0.935, err.score(judgments, RESULTS), 0.01); }
@Test public void testBottom5In10() { ExpectedReciprocalRank err = new ExpectedReciprocalRank(10, DEFAULT_PARAMS); Judgments judgments = newJudgments(); for (int i = 5; i < 15; i++) { judgments.addJudgment(RESULTS.get(i), 3); } assertEquals(0.591, err.score(judgments, RESULTS), 0.01); }
@Test public void test9thIsRelevant() { ExpectedReciprocalRank err = new ExpectedReciprocalRank(10, DEFAULT_PARAMS); Judgments judgments = newJudgments(); for (int i = 9; i < 15; i++) { judgments.addJudgment(RESULTS.get(i), 3); } assertEquals(0.589, err.score(judgments, RESULTS), 0.01); }
@Test public void testJudgmentBeyondMax() { Map<String, String> tmpParams = new HashMap<>(); tmpParams.put(ExpectedReciprocalRank.MAX_SCORE, "2"); ExpectedReciprocalRank err = new ExpectedReciprocalRank(10, tmpParams); Judgments judgments = newJudgments(); for (int i = 0; i < 10; i++) { judgments.addJudgment(RESULTS.get(i), 3); } assertThrows(IllegalArgumentException.class, () -> err.score(judgments, RESULTS)); }