|
21 | 21 | _compute_regularization, |
22 | 22 | _fw_step, |
23 | 23 | _sc_weight_fw, |
| 24 | + _sc_weight_fw_numpy, |
24 | 25 | _sparsify, |
25 | 26 | _sum_normalize, |
26 | 27 | compute_sdid_estimator, |
@@ -207,6 +208,58 @@ def test_intercept_centering(self): |
207 | 208 | # They should be different because centering matters |
208 | 209 | assert not np.allclose(lam_intercept, lam_no_intercept, atol=1e-3) |
209 | 210 |
|
| 211 | + def test_fw_warns_on_nonconvergence(self): |
| 212 | + """Silent-failure audit axis B: _sc_weight_fw_numpy must warn when max_iter exhausts.""" |
| 213 | + rng = np.random.default_rng(42) |
| 214 | + Y = rng.standard_normal((15, 7)) # (N, T0+1) with T0=6 |
| 215 | + |
| 216 | + with pytest.warns(UserWarning, match="did not converge"): |
| 217 | + _sc_weight_fw_numpy(Y, zeta=0.1, max_iter=1, min_decrease=1e-12) |
| 218 | + |
| 219 | + def test_fw_no_warning_on_convergence(self): |
| 220 | + """Silent-failure audit axis B: no warning on well-conditioned convergent input.""" |
| 221 | + rng = np.random.default_rng(42) |
| 222 | + Y = rng.standard_normal((15, 7)) |
| 223 | + |
| 224 | + with warnings.catch_warnings(record=True) as w: |
| 225 | + warnings.simplefilter("always") |
| 226 | + _sc_weight_fw_numpy(Y, zeta=0.1, max_iter=10000, min_decrease=1e-3) |
| 227 | + assert not any("did not converge" in str(x.message) for x in w) |
| 228 | + |
| 229 | + def test_fw_wrapper_warns_on_nonconvergence_without_rust(self): |
| 230 | + """Silent-failure audit axis B: public _sc_weight_fw wrapper must route |
| 231 | + warnings through even when called via the dispatcher with the Rust |
| 232 | + backend disabled. Pins the contract against refactors that would |
| 233 | + bypass the numpy path.""" |
| 234 | + rng = np.random.default_rng(42) |
| 235 | + Y = rng.standard_normal((15, 7)) |
| 236 | + |
| 237 | + with patch("diff_diff.utils.HAS_RUST_BACKEND", False): |
| 238 | + with pytest.warns(UserWarning, match="did not converge"): |
| 239 | + _sc_weight_fw(Y, zeta=0.1, max_iter=1, min_decrease=1e-12) |
| 240 | + |
| 241 | + def test_fw_wrapper_no_warning_on_convergence_without_rust(self): |
| 242 | + """Silent-failure audit axis B: wrapper-level negative control.""" |
| 243 | + rng = np.random.default_rng(42) |
| 244 | + Y = rng.standard_normal((15, 7)) |
| 245 | + |
| 246 | + with patch("diff_diff.utils.HAS_RUST_BACKEND", False): |
| 247 | + with warnings.catch_warnings(record=True) as w: |
| 248 | + warnings.simplefilter("always") |
| 249 | + _sc_weight_fw(Y, zeta=0.1, max_iter=10000, min_decrease=1e-3) |
| 250 | + assert not any("did not converge" in str(x.message) for x in w) |
| 251 | + |
| 252 | + def test_fw_max_iter_zero_warns(self): |
| 253 | + """Silent-failure audit axis B: max_iter=0 produces the uniform init |
| 254 | + without iterating, which cannot converge by construction. The warning |
| 255 | + must fire (consistent with the convention: if we exited the loop |
| 256 | + without hitting the tolerance gate, we signal). Pins this contract.""" |
| 257 | + Y = np.random.default_rng(0).standard_normal((5, 4)) |
| 258 | + |
| 259 | + with patch("diff_diff.utils.HAS_RUST_BACKEND", False): |
| 260 | + with pytest.warns(UserWarning, match="did not converge"): |
| 261 | + _sc_weight_fw(Y, zeta=0.1, max_iter=0) |
| 262 | + |
210 | 263 |
|
211 | 264 | class TestSparsify: |
212 | 265 | """Verify sparsification behavior.""" |
|
0 commit comments