1197: Fix sample metrics for vector input with a single observation#1206
Open
nikosbosse wants to merge 2 commits into
Open
1197: Fix sample metrics for vector input with a single observation#1206nikosbosse wants to merge 2 commits into
nikosbosse wants to merge 2 commits into
Conversation
bias_sample(), ae_median_sample(), se_mean_sample() and mad_sample() either returned silently wrong results or errored when `observed` was a scalar and `predicted` a vector of samples, although this input shape is documented and accepted by assert_input_sample(). All sample metrics now convert a vector of samples to a 1xN matrix via a new internal helper ensure_sample_matrix(), which also deduplicates the inline conversions in crps_sample() and pit_histogram_sample(). Also corrects the integer bias formula in the bias_sample() docs, which stated P_t(x_t + 1) instead of P_t(x_t - 1) (the code was correct). 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 #1206 +/- ##
=======================================
Coverage 98.15% 98.16%
=======================================
Files 41 41
Lines 2225 2231 +6
=======================================
+ Hits 2184 2190 +6
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.
|
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
nikosbosse
marked this pull request as draft
July 18, 2026 13:53
nikosbosse
marked this pull request as ready for review
July 18, 2026 20:58
Collaborator
Author
|
lgtm |
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 #1197.
The sample-based metrics document that when
observedis a single number,predictedcan be given as a vector of samples, andassert_input_sample()accepts this shape. However, four metrics mishandled it:ae_median_sample(1, c(0, 2, 4))returned1 1 3instead of1andse_mean_sample(1, c(0, 2, 4))returned1 1 9instead of1:as.matrix()turned the length-3 vector into a 3x1 matrix (three one-sample forecasts) and the scalarobservedwas recycled.bias_sample(1, c(0, 2, 4))errored with'x' must be an array of at least two dimensions(rowSums()/ncol()on a dimensionless vector).mad_sample(predicted = c(0, 2, 4))errored withinvalid 'times' argument(nrow()of a vector isNULL).Fix: a new internal helper
ensure_sample_matrix()converts a vector of samples into a 1xN matrix after input assertion. It is used inbias_sample(),ae_median_sample(),se_mean_sample()andmad_sample(), and replaces the two existing inline copies of the same idiom incrps_sample()andpit_histogram_sample(). Matrix input is unchanged (results are bit-identical). The PR also corrects the integer bias formula in thebias_sample()docs, which statedP_t(x_t + 1)instead ofP_t(x_t - 1)(the code was correct: for a perfect integer forecast the code gives bias 0, while the documented formula would give -1).Tests were written first and failed against the unfixed code:
After the fix, the full test suite passes (0 failed, 922 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.