Repository navigation
193: Add sd_sample() and iqr_sample() sharpness metrics - #1240
Draft
nikosbosse wants to merge 4 commits into
Draft
nikosbosse wants to merge 4 commits into
nikosbosse wants to merge 4 commits into
Conversation
Add the standard deviation and the interquartile range of the predictive samples as additional sharpness/dispersion measures for sample-based forecasts, mirroring mad_sample(). They are not added to the default metrics of get_metrics.forecast_sample(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1240 +/- ##
=======================================
Coverage 99.18% 99.18%
=======================================
Files 41 41
Lines 2333 2341 +8
=======================================
+ Hits 2314 2322 +8
Misses 19 19 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
- Document that iqr_sample() errors on missing samples unless na.rm = TRUE (sd_sample()/mad_sample() return NA) and pin it in a test. - Document that sd_sample() is NA for a single sample. - Replace tautological/weak assertions with literal expected values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
CLAUDE:
Description
This PR closes #193.
It adds two dispersion (sharpness) metrics for sample-based forecasts. Both sit next to
mad_sample()inR/metrics-sample.R:sd_sample(): row-wise standard deviation of the predictive samples (...passed tostats::sd()).iqr_sample(): row-wise interquartile range of the predictive samples (...passed tostats::IQR(), e.g.type).Both use the same interface as
mad_sample():observedis ignored, input is validated withassert_input_sample(), and vector input is handled viaensure_sample_matrix(). Each has its own Rd page tagged@keywords metric, so pkgdown lists them with the other lower-level scoring functions. The docs cover the NA edge cases:iqr_sample()errors on missing samples unlessna.rm = TRUEis passed.sd_sample()andmad_sample()returnNAinstead; this difference comes fromstats::IQR().sd_sample()returnsNAfor a single sample.mad_sample()gets a@seealsopointing to the new functions. The scoring-rules vignette mentions them briefly, and this PR also fixes a missing space there. A NEWS item is added under "New features".Maintainer decision:
sd_sample()andiqr_sample()are deliberately not added to the defaults ofget_metrics.forecast_sample(), because that would change the default output ofscore(). Users add them explicitly, e.g.score(forecast, metrics = c(get_metrics(forecast), list(sd = sd_sample, iqr = iqr_sample))). The closed bot PR #1103 did add them to the defaults; happy to switch if you prefer that.NAMESPACE / Rd: I verified with roxygen2 8.1.0, the version in
Config/roxygen2/version:roxygen2::roxygenise()on this branch produces no diff in NAMESPACE, DESCRIPTION or any Rd file.Implementation: a judge agent compared two independent drafts and picked draft A as the base.
Checklist
My PR is based on a package issue and I have explicitly linked it.
I have included the target issue or issues in the PR title as follows: issue-number: PR title
I have tested my changes locally. Full
devtools::test()passes:FAIL 0 | WARN 4 | SKIP 0 | PASS 1059(the 4 warnings were already there before this PR).I have added or updated unit tests where necessary. New tests in
tests/testthat/test-metrics-sample.Rcover:sqrt(c(12.5, 7.5)), IQR =c(2, 5), IQR withtype = 6=5.5observedis ignored...is passed on (na.rm,type)iqr_sample()errors withoutna.rm,sd_sample()returnsNAscore()get_metrics(); this guards the design decision and is not fail-before evidenceVerified fail-before/pass-after: with
R/metrics-sample.RandNAMESPACEreverted tomain,devtools::test(filter = "metrics-sample")fails because the functions are not found. With the change, there are 0 failures.I have updated the documentation if required.
I have built the package locally and run rebuilt docs using roxygen2. Rebuilt with roxygen2 8.1.0 (matching the repo); no unrelated churn.
My code follows the established coding standards and I have run
lintr::lint_package()to check for style issues introduced by my changes. Ranlintr::lint()on the changed R and test files. The test file has no lints. InR/metrics-sample.Rthe only lints are spuriousobject_usage_linter"no visible global function" warnings, which also appear for unchanged code when the package is not installed. None are in the new functions.I have added a news item linked to this PR.
I have reviewed CI checks for this PR and addressed them as far as I am able. CI is running on the latest push.
Review
An independent adversarial agent reviewed this PR and approved it with minor comments and nits. All points were addressed as follows.
Changed in response:
iqr_sample()NA handling differs fromsd_sample()/mad_sample(): this is now documented iniqr_sample()'s@param ...and pinned by a test (expect_error(iqr_sample(predicted = rbind(c(1, 2, 3, 4, NA))), "missing values")).expect_identical(sd_sample(...), apply(predicted, 1, sd)); thesqrt(c(12.5, 7.5))literal already pins the value.isTRUE(all.equal(...))check oftypewith literal values:2for the default and5.5fortype = 6.sd_sample()'s@returnsnow says the result isNAfor a single sample, or for missing samples withoutna.rm = TRUE.Rebutted:
sd_sample/iqr_sample/mad_sampleinto a shareddispersion_sample(predicted, fun, ...)helper: not applied. The shared body is three short lines that already delegate to the shared helpersassert_input_sample()andensure_sample_matrix(). A further wrapper would add indirection and would touchmad_sample(), which is outside the scope of this PR.mad_sample(): I did not changemad_sample(), to keep the diff minimal. The new functions return theapply()result directly instead of assigning it and callingreturn(). The behaviour is the same....thatsd/IQRdon't accept (e.g.sample_id) error with "unused argument": no change. This matches the existing behaviour ofmad_sample()and is not a regression, as the reviewer also noted.🤖 Generated with Claude Code