Skip to content

Commit d111f20

Browse files
committed
test: pin the variance clamps in move_var/move_std/move_corr
Reverting any of the clamps left the whole suite passing. Pins them on two series: a small head followed by a large near-constant tail at 1e8 + 1 for move_var/move_std, and at 123456789.123 for move_corr, whose variances only round negative — rather than to exactly 0.0 — on the latter.
1 parent 5a6171e commit d111f20

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

‎numbagg/test/test_moving.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,32 @@ def test_appending_doesnt_change_earlier_results(rs, window, offset):
374374
)
375375

376376

377+
def test_numerical_issues_degenerate_window_clamps():
378+
# A small head followed by a large near-constant tail: `_offset` averages the
379+
# head, so it doesn't fit the tail and the sums of squares still cancel to a
380+
# slightly negative number. Unclamped, that surfaces as a negative variance
381+
# from `move_var`, a NaN out of `sqrt` in `move_std`, and a series
382+
# anticorrelated with itself from `move_corr`. Nothing else in the suite
383+
# fails if any of those clamps is reverted, so pin them here.
384+
#
385+
# `1e8 + 1` rather than a round `1e8` because a round one leaves the offset
386+
# values exactly `0.0`, and the accumulators never go negative at all.
387+
a = np.concatenate([np.zeros(5), np.full(25, 1e8 + 1)])
388+
389+
np.testing.assert_array_equal(move_var(a, window=5)[9:], 0.0)
390+
np.testing.assert_array_equal(move_std(a, window=5)[9:], 0.0)
391+
392+
# `move_corr` needs its own series: on the one above its `var_a` and `var_b`
393+
# come out as exactly 0.0, so the pre-existing `var_a_var_b > 0` guard already
394+
# emits NaN and the clamp is inert. Here they round to -4.0 instead, their
395+
# product is +16.0, the guard passes, and the unclamped result is -1.0 — a
396+
# series anticorrelated with itself, on windows where `move_var` says 0.0.
397+
c = np.concatenate([np.zeros(5), np.full(25, 123456789.123)])
398+
399+
np.testing.assert_array_equal(move_var(c, window=5)[9:], 0.0)
400+
assert np.all(np.isnan(move_corr(c, c, window=5)[9:]))
401+
402+
377403
def slow_move_mean(a, window, min_count=None, axis=-1):
378404
"Slow move_mean for unaccelerated dtype"
379405
return move_func(np.nanmean, a, window, min_count, axis=axis)

0 commit comments

Comments
 (0)