1200: Fix predicted_label handling and n > 1 column check in categorical metrics#1203
Open
nikosbosse wants to merge 1 commit into
Open
1200: Fix predicted_label handling and n > 1 column check in categorical metrics#1203nikosbosse wants to merge 1 commit into
nikosbosse wants to merge 1 commit into
Conversation
rps_ordinal() applied the forward instead of the inverse permutation when reordering predicted columns, and logs_categorical() ignored predicted_label entirely, so both returned wrong scores when predicted_label was not in factor level order. Both now reorder columns with order(as.numeric(predicted_label)) so scores are invariant to column labelling. assert_input_categorical() now also checks the number of columns of predicted for n > 1, matching the existing n == 1 check. Corrects an existing test expectation that encoded the wrong permutation and adds permutation-invariance tests. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1203 +/- ##
=======================================
Coverage 98.15% 98.15%
=======================================
Files 41 41
Lines 2225 2226 +1
=======================================
+ Hits 2184 2185 +1
Misses 41 41 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Collaborator
Author
|
Automated review (Claude Fable, directed by @nikosbosse): Verdict: approve. All checks passed; no must-fix findings.
|
nikosbosse
marked this pull request as draft
July 18, 2026 13:53
Collaborator
Author
|
lgtm |
nikosbosse
marked this pull request as ready for review
July 18, 2026 20:48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR closes #1200.
Three related bugs in the categorical (nominal/ordinal) metrics when called directly with a
predicted_labelthat is not in factor level order (forecasts scored viascore()were unaffected, as the pipeline sorts predictions into level order before calling the metrics):rps_ordinal()reordered the columns ofpredictedwith the forward permutationpredicted[, as.numeric(predicted_label)]instead of the inversepredicted[, order(as.numeric(predicted_label))], so scores were not invariant to how the columns were labelled.logs_categorical()ignoredpredicted_labelentirely and indexedpredicted[cbind(1:n, as.numeric(observed))], returning the negative log of the probability of the wrong outcome for permuted labels.assert_input_categorical()did not check the number of columns ofpredictedin the n > 1 branch (the n == 1 branch already did), so e.g. a 3x4 matrix with rows summing to 1 was silently accepted and scored meaninglessly.The fix reorders
predictedwithorder(as.numeric(predicted_label))in both metrics (preserving NA-observed and single-observation behaviour) and addsncols = Nto the n > 1assert_matrix()call. Note the latter is a new error on previously (silently, wrongly) accepted input. An existing test expectation intest-metrics-ordinal.Rencoded the wrong permutation and has been corrected; permutation-invariance tests and a wrong-width-matrix error test have been added.Tests were written first and failed against the unfixed code:
After the fix, the affected test files and the full test suite pass (0 failed, 910 passed).
Dev note: bug identified by an LLM audit (#1189); fix and tests implemented with LLM support, directed by @nikosbosse.
Checklist
lintr::lint_package()to check for style issues introduced by my changes.