Commit eacdcd7
fix: Richardson-extrapolate LensCalc numpy Hessian for JAX-path parity
The numpy path of LensCalc.hessian_from used a 2-point central finite
difference with buffer=0.01, giving O(h^2) ≈ 1e-4 truncation error. The
JAX path uses exact jax.jacfwd autodiff. At near-critical points this
~1e-4 Hessian error was amplified by the near-zero Jacobian determinant
into ~5e-4 relative errors in magnifications, producing ~0.1%
log-likelihood drift in the point-source full-pipeline JIT.
Closes the gap generically with Richardson extrapolation on the
existing stencil: evaluate _hessian_via_finite_difference at h and h/2,
combine as (4 * H(h/2) - H(h)) / 3. This cancels the leading O(h^2)
term, giving O(h^4) ≈ 1e-8 — float64 parity with the JAX path.
Cost is 2x the deflection evaluations (4 → 8 per grid); negligible
compared to a non-linear search iteration. No mass-profile API change.
Tests:
- New: test__hessian_from__np_richardson_matches_jax_jacfwd_to_float64
(asserts rtol=1e-8 across all four Hessian components)
- New: test__magnification_2d_via_hessian_from__np_jnp_agree_to_float64
(asserts rtol=1e-7 — one bit of float64 round-off from 1/det_A)
- Updated hardcoded expected values in test__hessian_from__*__correct_values
and test__magnification_2d_via_hessian_from to the Richardson-accurate
numbers (previous values carried O(h^2) truncation in their least
significant figures).1 parent 854e70e commit eacdcd7
2 files changed
Lines changed: 68 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
359 | 359 | | |
360 | 360 | | |
361 | 361 | | |
362 | | - | |
363 | | - | |
364 | | - | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
365 | 367 | | |
366 | 368 | | |
367 | 369 | | |
| |||
377 | 379 | | |
378 | 380 | | |
379 | 381 | | |
380 | | - | |
| 382 | + | |
381 | 383 | | |
382 | 384 | | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
383 | 398 | | |
384 | 399 | | |
385 | 400 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
155 | | - | |
156 | | - | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
157 | 200 | | |
158 | 201 | | |
159 | 202 | | |
| |||
0 commit comments