Skip to content

193: Add sd_sample() and iqr_sample() sharpness metrics - #1240

Draft
nikosbosse wants to merge 4 commits into
mainfrom
issue-193-sharpness-metrics-sample
Draft

nikosbosse wants to merge 4 commits into
mainfrom
issue-193-sharpness-metrics-sample

Conversation

@nikosbosse

@nikosbosse nikosbosse commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

CLAUDE:

Description

This PR closes #193.

It adds two dispersion (sharpness) metrics for sample-based forecasts. Both sit next to mad_sample() in R/metrics-sample.R:

  • sd_sample(): row-wise standard deviation of the predictive samples (... passed to stats::sd()).
  • iqr_sample(): row-wise interquartile range of the predictive samples (... passed to stats::IQR(), e.g. type).

Both use the same interface as mad_sample(): observed is ignored, input is validated with assert_input_sample(), and vector input is handled via ensure_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 unless na.rm = TRUE is passed. sd_sample() and mad_sample() return NA instead; this difference comes from stats::IQR().
  • sd_sample() returns NA for a single sample.

mad_sample() gets a @seealso pointing 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() and iqr_sample() are deliberately not added to the defaults of get_metrics.forecast_sample(), because that would change the default output of score(). 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.R cover:

    • exact values: sd = sqrt(c(12.5, 7.5)), IQR = c(2, 5), IQR with type = 6 = 5.5
    • observed is ignored
    • ... is passed on (na.rm, type)
    • NA behaviour: iqr_sample() errors without na.rm, sd_sample() returns NA
    • input validation
    • vector input gives the same result as a 1-row matrix
    • use inside score()
    • neither metric is in the default get_metrics(); this guards the design decision and is not fail-before evidence

    Verified fail-before/pass-after: with R/metrics-sample.R and NAMESPACE reverted to main, 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. Ran lintr::lint() on the changed R and test files. The test file has no lints. In R/metrics-sample.R the only lints are spurious object_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 from sd_sample()/mad_sample(): this is now documented in iqr_sample()'s @param ... and pinned by a test (expect_error(iqr_sample(predicted = rbind(c(1, 2, 3, 4, NA))), "missing values")).
  • Weak or tautological assertions:
    • Dropped expect_identical(sd_sample(...), apply(predicted, 1, sd)); the sqrt(c(12.5, 7.5)) literal already pins the value.
    • Replaced the isTRUE(all.equal(...)) check of type with literal values: 2 for the default and 5.5 for type = 6.
    • Kept the "not in default metrics" assertion as a design guard, and no longer count it as fail-before evidence.
  • Undocumented single-sample edge case: sd_sample()'s @returns now says the result is NA for a single sample, or for missing samples without na.rm = TRUE.
  • Missing review note and CI status in the PR body: this section adds the note. The CI checkbox stays unticked until the matrix on the latest push is green.
  • NAMESPACE edited by hand, never checked with roxygen: I ran roxygen2 8.1.0, the version the repo actually declares (the reviewer assumed 7.3.3). It produces no diff in NAMESPACE or any other generated file.

Rebutted:

  • Factor sd_sample/iqr_sample/mad_sample into a shared dispersion_sample(predicted, fun, ...) helper: not applied. The shared body is three short lines that already delegate to the shared helpers assert_input_sample() and ensure_sample_matrix(). A further wrapper would add indirection and would touch mad_sample(), which is outside the scope of this PR.
  • Style differences from mad_sample(): I did not change mad_sample(), to keep the diff minimal. The new functions return the apply() result directly instead of assigning it and calling return(). The behaviour is the same.
  • Arguments forwarded via ... that sd/IQR don't accept (e.g. sample_id) error with "unused argument": no change. This matches the existing behaviour of mad_sample() and is not a regression, as the reviewer also noted.

🤖 Generated with Claude Code

nikosbosse and others added 3 commits November 8, 2026 13:07
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

codecov Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.18%. Comparing base (8b18f07) to head (af3e80a).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- 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

No deployments
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.

Add more metrics to assess sharpness of forecasts in a sample format

1 participant