Skip to content

Fix median aggregation returning arbitrary element instead of median#3696

Open
Chessing234 wants to merge 1 commit intoEleutherAI:mainfrom
Chessing234:fix/median-missing-sort
Open

Fix median aggregation returning arbitrary element instead of median#3696
Chessing234 wants to merge 1 commit intoEleutherAI:mainfrom
Chessing234:fix/median-missing-sort

Conversation

@Chessing234
Copy link
Copy Markdown

Summary

The median() aggregation function indexes into the unsorted input array:

def median(arr):
    return arr[len(arr) // 2]

This returns whichever element happened to land at the middle index by evaluation order, not the actual statistical median. For example, median([10, 1, 20, 5, 15]) returns 20 (element at index 2) instead of 10.

Any task using aggregation: median in its YAML config silently gets a wrong score.

Fix: Sort before indexing: sorted(arr)[len(arr) // 2].

Test plan

  • Verified [10, 1, 20, 5, 15][5 // 2] returns 20 (wrong), sorted(...)[5 // 2] returns 10 (correct)
  • Existing tests should continue to pass

🤖 Generated with Claude Code

median() indexes into the unsorted array, so it returns whichever
element happened to land at the middle position by evaluation order,
not the actual statistical median. Add sorted() before indexing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Chessing234 Chessing234 requested a review from 0xSMT as a code owner April 12, 2026 06:16
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.

1 participant