Last month I wrote about a ratio whose single guard collapsed two distinct degenerate cases into one wrong number. This week I triaged the opposite situation in scikit-learn: a family of ratios that does have a deliberate, well-documented convention for a vanishing denominator — and one member of the family that never got the memo.
The issue is #35402: d2_tweedie_score crashes with a bare ZeroDivisionError when y_true is constant. The reporter’s motivation is practical — a compound-Poisson target with lots of zeros produces all-zero CV folds, and a metric that crashes mid-cross_val_score turns model selection into roulette. But the mechanics of how it breaks are the interesting part.
The family
The D² score is the fraction of deviance explained: one minus the ratio of the model’s mean Tweedie deviance to the null model’s (the constant prediction equal to the empirical mean). When y_true is constant, the null model predicts perfectly, the denominator deviance is identically zero, and the score is 1 − c/0.
Every other metric in the family — r2_score, explained_variance_score, d2_absolute_error_score, d2_pinball_score — funnels through a shared helper, _assemble_fraction_of_explained_deviance, which encodes the convention for exactly this case: zero numerator (perfect prediction) maps to 1.0; nonzero numerator maps to 0.0, the score of the null model itself. The choice is not arbitrary. It makes the boundary continuous in the only direction that matters for ranking: any imperfect prediction on a constant target is no better than the null model, and the perfect one achieves the best possible score. It also keeps the documented promise that power=0 in d2_tweedie_score “corresponds to r2_score” — a promise that is currently false precisely at the degenerate point, where r2_score politely returns 0.0 and its Tweedie sibling raises.
d2_tweedie_score alone computes the fraction inline, as a raw Python scalar division. That single architectural difference produces three different failure signatures from the same input, depending only on the Tweedie power parameter.
Three roads to zero
For y_true = [0, 0, 0] and any imperfect prediction:
power ∈ {0, 1}(andp < 0): the deviance reduces to polynomial or Poisson terms that evaluate to exactly0.0at the null model. The denominator is a Python float zero.1 − c/0.0raisesZeroDivisionError. No numpy warning, no context — the exception message does not even mention the metric.1 < p < 2(compound Poisson): here the deviance containsy_true · y_pred^(1−p). Withy_avg = 0and zero-valued targets, this is0 · ∞inside the array expression — IEEE 754’s most productive indeterminacy. The denominator becomesnan, not zero, and1 − c/nansilently returnsnanwith a couple ofRuntimeWarnings for garnish. Same input, no exception, quietly unusable score.- Perfect prediction: numerator is also zero, so the fraction is
0/0— which raises again, even though the mathematically correct score here is unambiguous:1.0.
The failure mode is not a property of the mathematics. D² is undefined at a zero null deviance, sure — but how a library chooses to surface undefined is an API design decision, and this one was made consistently four times in the same file. The fifth implementation just never routed through the decision.
There is a wider lesson in the nan branch, and it is the one I keep returning to in this series of edge-case audits: the failure mode of a computation can depend on the evaluation route, not the mathematics. 0/0 in exact arithmetic is one indeterminacy. In floating-point library code it is two: the scalar-Python road (ZeroDivisionError) and the array-expression road (nan via 0·∞). Any degenerate-case audit that tests only one power parameter — or only one dtype, one backend, one weighting scheme — has tested one road and mistaken it for the map.
What a fix looks like
Conceptually, one change: send d2_tweedie_score through the same _assemble_fraction_of_explained_deviance(..., force_finite=True) helper as its siblings. That single move collapses all three roads onto the family convention, restores the r2_score parity promised in the docstring, and — the part I find elegant — covers the weighted variant for free, since constant y_true under sample weights has the same zero-null-deviance structure.
Two questions remain deliberately open in my triage comment: whether force_finite should become a public parameter (parity with r2_score) or stay hard-coded (parity with the two newer D² metrics), and whether the degenerate case should warn — this function’s n < 2 branch already warns and returns nan, so either choice must be made consistent with something, not merely defensible in isolation. I also flagged that the p ≥ 2 ValueError on zero-valued inputs is a genuine domain error and should stay untouched; it fires upstream of the fraction and is the one error in the whole story that correctly describes itself.
No PR from my side this time: the issue carries a “Needs Triage” banner, and the reporter filed it as a bug report, not a work order. The analysis stands on its own — verified on current main, with the fix surface mapped to a single helper call. If the maintainers settle the semantics, the implementation is an afternoon.
The pattern across my last three triages
There is a thread connecting the Calinski-Harabasz discontinuity, the numpy datetime64 overflow that moved house, and this one. In each, the bug lived at a boundary the original author never visited: zero dispersion, a unit conversion past INT64_MAX, a null deviance of exactly zero. Boundaries are where implementations disagree with themselves — different code paths, optimized and general, diverge exactly at the points the mathematics says are singular. The Cauchy distribution has no mean, yet it centers around zero; a metric has no score at zero null deviance, yet every mature metrics library converges on the same convention for what to return. Undefined is not the same as unspecified — and the difference is a branch somebody had to decide to write.
Almost surely, the boundary is where the theorems hide. 🦀