Skip to content

Commit 433b594

Browse files
igerberclaude
andcommitted
Make uniform-kernel PSD warning test deterministic and assert message
Address P3 documentation/test finding from CI Codex review of PR #411 re-rerun. The original `test_uniform_kernel_negative_eigenvalue_warns` constructed a "numerically borderline" setup with random distances and ran the path under `warnings.catch_warnings()` but never asserted that the warning was emitted — the comment even noted that the warning "may or may not fire depending on numerical condition". That left the uniform-kernel PSD-warning behavior less tightly regression-tested than the Bartlett path (which has the assertion). Rewrite the test to monkey-patch `_uniform_kernel` with an aggressively indefinite kernel (same pattern as `test_indefinite_meat_warning_fires_for_bartlett`) and assert the `UserWarning` surfaces with `kernel='uniform'` in the message. The warning behavior is now deterministic and locked. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 0ed64e1 commit 433b594

1 file changed

Lines changed: 49 additions & 22 deletions

File tree

tests/test_conley_vcov.py

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -391,32 +391,59 @@ def test_shape_matches_bread(self, small_ols_with_coords):
391391
assert vcov.shape == (k, k)
392392

393393
def test_uniform_kernel_negative_eigenvalue_warns(self):
394-
"""Construct a degenerate setup that produces a uniform-kernel
395-
meat with a small negative eigenvalue. Verifies the PSD-warning
396-
path. The setup uses two clusters of identical-coordinate points so
397-
the uniform-kernel meat reduces to a known structure that is
398-
numerically borderline."""
394+
"""The uniform kernel is documented as not PSD-guaranteed (Conley
395+
1999 footnote 11): its spectral window is negative in regions, so
396+
the resulting meat can be indefinite. Force the indefinite path
397+
deterministically by monkey-patching ``_uniform_kernel`` to return
398+
a kernel matrix with aggressive negative off-diagonals (mirroring
399+
the bartlett warning test below), and assert the warning surfaces
400+
with kernel='uniform' in the message.
401+
"""
402+
from diff_diff import conley as conley_mod
403+
399404
rng = np.random.default_rng(seed=1)
400-
n = 30
401-
# Mix of identical-coord pairs; uniform kernel sums full pairs
402-
coords = np.repeat(rng.uniform(0, 1, size=(n // 2, 2)), 2, axis=0)
405+
n = 6
406+
coords = rng.uniform(0, 1, size=(n, 2))
403407
X = np.column_stack([np.ones(n), rng.standard_normal(n)])
404-
eps = rng.standard_normal(n)
408+
eps = np.ones(n)
405409
bread = X.T @ X
406-
# No assertion on the exact meat — only that the PSD path is
407-
# exercised. The warning may or may not fire depending on numerical
408-
# condition; this test mainly ensures the code path runs without error.
409-
with warnings.catch_warnings():
410-
warnings.simplefilter("always")
411-
_compute_conley_vcov(
412-
X,
413-
eps,
414-
coords,
415-
cutoff=10.0,
416-
metric="euclidean",
417-
kernel="uniform",
418-
bread_matrix=bread,
410+
411+
original = conley_mod._uniform_kernel
412+
413+
def _indefinite(u: np.ndarray) -> np.ndarray:
414+
base = np.eye(u.shape[0])
415+
for i in range(u.shape[0]):
416+
for j in range(u.shape[0]):
417+
if i != j:
418+
base[i, j] = -10.0
419+
return base
420+
421+
try:
422+
conley_mod._uniform_kernel = _indefinite
423+
with warnings.catch_warnings(record=True) as w:
424+
warnings.simplefilter("always")
425+
conley_mod._compute_conley_vcov(
426+
X,
427+
eps,
428+
coords,
429+
cutoff=10.0,
430+
metric="euclidean",
431+
kernel="uniform",
432+
bread_matrix=bread,
433+
)
434+
psd_warnings = [
435+
msg
436+
for msg in w
437+
if issubclass(msg.category, UserWarning)
438+
and "uniform" in str(msg.message)
439+
and "negative eigenvalue" in str(msg.message)
440+
]
441+
assert len(psd_warnings) >= 1, (
442+
f"Expected a UserWarning naming kernel='uniform' and "
443+
f"'negative eigenvalue'; got {[str(m.message) for m in w]}"
419444
)
445+
finally:
446+
conley_mod._uniform_kernel = original
420447

421448
def test_indefinite_meat_warning_fires_for_bartlett(self):
422449
"""Both kernels (radial 1-D bartlett and uniform) are practitioner

0 commit comments

Comments
 (0)