Skip to content

test: density-form log-prior gates + Emcee/LBFGS bias regression scripts - #27

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/log-prior-sign-convention
May 15, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/log-prior-sign-convention

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Adds permanent regression coverage for the log_prior_from_value sign convention shipping concurrently in PyAutoLabs/PyAutoFit#1269 (closes PyAutoLabs/PyAutoFit#1266).

This PR is paired with the PyAutoFit fix: PyAutoFit changes the prior bodies, this PR pins the new behaviour as long-running regression gates. Without these gates a future contributor could re-flip the sign during a refactor and the bug would re-emerge silently — empirically the bug went undetected for 4 years because the only test pinning the values rubber-stamped the wrong answer.

Scripts added

scripts/prior_correctness/emcee_gaussian_bias_check.py

Drives emcee.EnsembleSampler directly via Fitness.call_wrap on a flat-likelihood + GaussianPrior(mean=5, sigma=1) setup. Asserts:

  • The chain stays finite (no overflows from the runaway positive-quadratic potential that the bug produced).
  • Posterior sample mean = 5.0 ± 0.2.
  • Posterior sample std ∈ (0.7, 1.3).

Empirically: Emcee diverged to ~10^146 under the cost-form bug; this script catches that regression. Also includes a LogGaussianPrior(mean=0, sigma=0.5) finiteness check covering the log-Gaussian path.

scripts/prior_correctness/lbfgs_gaussian_bias_check.py

Drives scipy.optimize.minimize(method="L-BFGS-B") via Fitness.__call__ with convert_to_chi_squared=True on the same setup. Asserts:

  • L-BFGS-B converges (res.success == True).
  • Converged value = 5.0 ± 0.01.
  • res.fun ≈ 0.0 (chi_squared at the MAP).

Empirically: L-BFGS-B diverged to 8e143 under the cost-form bug.

Scripts updated

scripts/jax_assertions/priors_xp_dispatch.py

Adds four new value-pinned assertions complementing the existing NumPy↔JAX parity checks:

  • assert_log_prior_density_convention_gaussian() — pins log p at the mean (0.0) and at 1σ / 3σ (−0.5 / −4.5) for GaussianPrior(5, 1). Sign-direction regression gate.
  • assert_log_prior_density_convention_log_uniform() — pins log p at value=1 (0.0) and value=e (−1.0) for LogUniformPrior(0.01, 100). Catches the pre-#1266 1/value Jacobian-gradient bug.
  • assert_log_prior_density_convention_log_gaussian() — pins log p at value=0 (−inf, out of support) and value=1 (0.0, the mode) for LogGaussianPrior(0, 1).
  • assert_log_prior_density_jax_matches_numpy_signed() — checks both NumPy and JAX paths return < 0 at |z|=2σ for GaussianPrior(0, 1) and that they match. Catches partial sign-flip regressions.

The existing 24 NumPy↔JAX parity assertions still pass under the new convention (parity assertions are convention-invariant).

API Changes

None — pure test/regression script additions.

Test Plan

  • python scripts/jax_assertions/priors_xp_dispatch.py — all 28 assertions pass against the corrected PyAutoFit branch (was 24 in the prior assertions, +4 new density-form gates)
  • python scripts/prior_correctness/emcee_gaussian_bias_check.py — passes
  • python scripts/prior_correctness/lbfgs_gaussian_bias_check.py — passes
  • python scripts/jax_assertions/fitness_dispatch.py — still passes (unrelated, regression-checked)
  • python scripts/jax_assertions/nested.py — still passes
  • python scripts/jax_assertions/enable_pytrees.py — still passes
  • python scripts/jax_assertions/pytrees.py — still passes
  • python scripts/graphical/ep.py — full EP loop runs to completion (EP unaffected by the bug, verified)
Full API Changes (for automation & release notes)

None — internal test workspace addition only. New scripts and assertion functions surface no public API.

🤖 Generated with Claude Code

Adds permanent regression coverage for the log_prior_from_value sign
convention (PyAutoLabs/PyAutoFit#1266 — fix shipping concurrently on the
matching feature/log-prior-sign-convention branch in PyAutoFit).

scripts/prior_correctness/emcee_gaussian_bias_check.py
  Drives emcee.EnsembleSampler directly via Fitness.call_wrap on a
  flat-likelihood + GaussianPrior(mean=5, sigma=1) setup. Asserts the chain
  stays finite and samples cluster around 5.0 ± 0.2 with std in (0.7, 1.3).
  Promoted from a /tmp research script used to empirically confirm the bug
  (Emcee diverged to ~10^146 under the cost-form convention before the fix).
  Also includes a LogGaussianPrior(mean=0, sigma=0.5) finiteness check.

scripts/prior_correctness/lbfgs_gaussian_bias_check.py
  Drives scipy.optimize.minimize(method="L-BFGS-B") via Fitness.__call__
  with convert_to_chi_squared=True on the same setup. Asserts L-BFGS-B
  converges to 5.0 ± 0.01 (was diverging to 8e143 under cost convention).

scripts/jax_assertions/priors_xp_dispatch.py
  Adds four new assert_log_prior_density_convention_* gates that pin specific
  numerical values for the density convention:
    - GaussianPrior log p at mean / 1σ / 3σ
    - LogUniformPrior log p at 1.0 / e (drops the -log(log(b/a)) constant
      to match UniformPrior's -log(b-a) convention)
    - LogGaussianPrior log p at 0.0 (out-of-support → -inf) and 1.0 (mode)
    - NumPy ↔ JAX parity at a known < 0 density-form point (catches partial
      sign-flip regressions where one xp path is fixed and the other isn't)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label May 15, 2026
@Jammy2211
Jammy2211 merged commit 202bbcf into main May 15, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/log-prior-sign-convention branch May 15, 2026 13:03
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: log_prior_from_value sign-convention bug across Prior subclasses

1 participant