Skip to content

Commit 5a6171e

Browse files
committed
fix: clamp move_corr's variances and pin the appending property
move_var and move_std clamp their variance at zero; move_corr computes var_a/var_b inline and guarded only their product, so two variances that both rounded slightly negative multiplied to a positive number and passed the guard. move_corr(a, a, window=5) on a series of zeros followed by a large constant returned -1.0 where move_var returns 0.0 on the same windows. Clamping each variance sends those to NaN and leaves well-conditioned data unchanged. The offset choice rests on appending to a series not changing results already emitted, but test_property.py is skipped at module level (an unconditional skip, so --run-nightly doesn't re-enable it), leaving that constraint unenforced. Pin it directly in test_moving.py.
1 parent 56ff8fc commit 5a6171e

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎numbagg/moving.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# rounding floor everywhere, not just where it's a good fit. The mean of the whole
3434
# series is the most accurate choice on most inputs, but it's ruled out here — it
3535
# depends on values that haven't been seen yet, which would break the guarantee
36-
# `test_moving_bigger_arrays_have_same_beginning` pins, that appending to a series
36+
# `test_appending_doesnt_change_earlier_results` pins, that appending to a series
3737
# doesn't change results that were already emitted. The first observation keeps
3838
# that guarantee but rests on `a[0]` being representative; when it isn't, the whole
3939
# series pays for it. So we average the first `window` observations: still a
@@ -336,8 +336,13 @@ def move_corr(a: T, b: T, window: int, min_count: int, out: T) -> None:
336336
count_reciprocal = 1.0 / count
337337
avg_a = asum * count_reciprocal
338338
avg_b = bsum * count_reciprocal
339-
var_a = asum_sq * count_reciprocal - avg_a**2
340-
var_b = bsum_sq * count_reciprocal - avg_b**2
339+
# Clamp each variance at zero, as `move_var` does — otherwise two
340+
# variances that round slightly negative multiply to a positive
341+
# product, pass the guard below, and give a meaningless denominator.
342+
# A degenerate window then reaches the `else` branch and emits NaN,
343+
# which is the right answer for a correlation with a constant series.
344+
var_a = max(asum_sq * count_reciprocal - avg_a**2, 0.0)
345+
var_b = max(bsum_sq * count_reciprocal - avg_b**2, 0.0)
341346
cov_ab = prodsum * count_reciprocal - avg_a * avg_b
342347
var_a_var_b = var_a * var_b
343348
if var_a_var_b > 0:

‎numbagg/test/test_moving.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,39 @@ def test_numerical_issues_vs_exact_reference(case, tol):
341341
assert cov_err < tol, f"move_cov: {cov_err:.2e}"
342342

343343

344+
@pytest.mark.parametrize("window", [5, 20])
345+
@pytest.mark.parametrize("offset", [0.0, 1e8])
346+
def test_appending_doesnt_change_earlier_results(rs, window, offset):
347+
# The offset each of these functions subtracts is a function of the whole
348+
# series, so it has to depend only on values that any result already depends
349+
# on — otherwise appending to a series would change results that were already
350+
# emitted. `_offset` averages the first `window` values for exactly this
351+
# reason, and the choice cost real accuracy relative to a full-series mean, so
352+
# pin the property it bought. `test_moving_bigger_arrays_have_same_beginning`
353+
# in `test_property.py` covers the same ground but is skipped at module level.
354+
#
355+
# Bit-exact, not approximate: a tolerance here would let a future offset that
356+
# peeks ahead through, which is the whole thing being ruled out.
357+
a = rs.standard_normal(40) + offset
358+
b = rs.standard_normal(40) + offset
359+
360+
for func in (move_var, move_std):
361+
for n in (window, window + 1, len(a) - 1):
362+
np.testing.assert_array_equal(
363+
func(a[:n], window=window),
364+
func(a, window=window)[:n],
365+
err_msg=f"{func.__name__}, n={n}",
366+
)
367+
368+
for func in (move_cov, move_corr):
369+
for n in (window, window + 1, len(a) - 1):
370+
np.testing.assert_array_equal(
371+
func(a[:n], b[:n], window=window),
372+
func(a, b, window=window)[:n],
373+
err_msg=f"{func.__name__}, n={n}",
374+
)
375+
376+
344377
def slow_move_mean(a, window, min_count=None, axis=-1):
345378
"Slow move_mean for unaccelerated dtype"
346379
return move_func(np.nanmean, a, window, min_count, axis=axis)

0 commit comments

Comments
 (0)