darts icon indicating copy to clipboard operation
darts copied to clipboard

Consistent zero-denominator handling for percentage and range metrics

Open mahimn01 opened this issue 5 months ago • 0 comments

Context

PR #3122 added a zero_division parameter to wmape, ope, arre, marre, and coefficient_of_variation, mirroring the scaled-metric work in #3059 (mase, msse, rmsse). Review feedback (thanks @jakubchlapek) converged on a cleaner, consistent design than the initial NaN-on-zero approach. This issue tracks completing that design across the percentage/range metric family.

Agreed design

A single shared helper handles every "divide a numerator by a possibly-zero denominator" case, distinguishing two sub-cases exactly as the existing _safe_scaled_divide already does:

  • 0/0 (numerator 0 and denominator 0) → the metric's best score (a perfect forecast on a degenerate series): 0.0 for percentage-error metrics, 1.0 for scaled-error metrics ("on par with naive"). The fill value is a helper parameter.
  • x/0 (numerator ≠ 0, denominator 0) → np.nan. Filling with the best score here would mask a real miss.
  • zero_division="raise" preserves the legacy ValueError.

Rationale: the current PR fills every zero-denominator entry with NaN, which incorrectly scores a genuinely perfect forecast (e.g. ope on a series summing to zero with an identical prediction; arre/marre on a constant series predicted perfectly) as NaN instead of 0.0. sape/smape already encode the 0/0 → 0.0 convention, confirming it.

Scope

  • Aggregate-denominator (this PR, #3122): wmape, ope, arre, marre, coefficient_of_variation — switch to the unified helper with best_score=0.0. (coefficient_of_variation is RMSE/mean, so 0.0 = perfect forecast — proposed, pending confirmation since its denominator can be negative.)
  • Already aligned: sape/smape already fill 0/0 → 0.0 inline and have no x/0 case (the symmetric denominator is zero only when both actual and prediction are zero) — they need only the explicit zero_division knob for the "raise" option.
  • Element-wise denominator (deliberate / possibly follow-up): ape, mape still hard-raise if any y_t == 0; their denominator is per-timestep, so they need element-wise handling, and relaxing MAPE's default is a larger behavioral change.

Implementation

Collapse _safe_pct_divide into _safe_scaled_divide (one helper, configurable 0/0 fill, with the warn/raise message parameterized so the scaled callers keep their existing strings), update the callers, add a 0/0 best-score test case, and fold the ope negative-sum test into test_ope(). Tracked by #3122.

mahimn01 avatar Jun 09 '26 18:06 mahimn01