Skip to content

fix: LogUniform NumPy log-prior returns -inf for value<=0 (emcee NaN crash) - #1329

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/emcee-loguniform-nan
Jul 8, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/emcee-loguniform-nan

Conversation

@Jammy2211

@Jammy2211 Jammy2211 commented Jul 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Emcee's stretch move proposes physical values that can go non-positive for a
LogUniform-priored parameter. LogUniformPrior.log_prior_from_value computed
-np.log(value) on the NumPy path, which is NaN for value <= 0; that NaN
propagated into the summed figure-of-merit (fom = log_likelihood + Σ log_prior,
fitness.py) and crashed the search with ValueError: Probability function returned NaN.

This returns -inf (zero density → the proposed move is rejected) for
value <= 0, using the "double where" pattern so no log of a non-positive value
is ever evaluated (avoiding a NumPy RuntimeWarning). Positive values are
unchanged — the NumPy path stays unnormalised and unbounded, returning
-log(value); the JAX path is untouched.

Surfaced by PyAutoHeart #27 release-fidelity validation (run 28784914443); fixes
the autofit_workspace cookbook legs that failed with the emcee NaN crash.

Closes #1328.

API Changes

Behaviour-only: LogUniformPrior.log_prior_from_value(value) now returns -inf
(was NaN/+inf) for value <= 0 on the NumPy path. No signatures, symbols, or
defaults change; positive-value behaviour is identical. The only direct callers are
the autofit_workspace_test prior parity/regression gates
(jax_assertions/priors_xp_dispatch.py, prior_correctness/emcee_gaussian_bias_check.py),
which exercise only positive, in-bounds values and are unaffected.
See full details below.

Test Plan

  • python -m pytest test_autofit/mapper/prior/ test_autofit/non_linear/ (918 + 250 pass)
  • New regression test__log_prior_from_value__non_positive_returns_neg_inf
  • Real emcee over a LogUniform model completes without "Probability function returned NaN"
Full API Changes (for automation & release notes)

Changed Behaviour

  • autofit.LogUniformPrior.log_prior_from_value(value) — NumPy path returns -inf for value <= 0 (previously NaN from -log of a non-positive value, +inf at value == 0). Positive values unchanged (-log(value), unnormalised/unbounded). JAX path unchanged.

Migration

  • None. Internal behaviour fix. Direct callers (autofit_workspace_test prior parity gates) exercise only positive, in-bounds values and are unaffected.

🤖 Generated with Claude Code

…crash)

Emcee's stretch move proposes physical values that can go non-positive for a
LogUniform parameter. The NumPy log_prior_from_value path computed -np.log(value),
which is NaN for value<=0; the NaN propagated into the summed figure-of-merit
(fom = log_likelihood + sum(log_prior)) and crashed the search with
"ValueError: Probability function returned NaN".

Return -inf (zero density -> the proposed move is rejected) for value<=0, using
the double-where pattern so no log of a non-positive value is evaluated (avoiding
a NumPy RuntimeWarning). Positive values are unchanged: the NumPy path stays
unnormalised and unbounded, returning -log(value). The JAX path is untouched.

Surfaced by PyAutoHeart #27 release-fidelity validation (run 28784914443).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Jul 8, 2026
Copilot AI review requested due to automatic review settings July 8, 2026 08:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an Emcee crash caused by LogUniformPrior.log_prior_from_value returning NaN on the NumPy path when proposed values are non-positive, by safely returning -inf instead (zero density → rejected move) without ever evaluating log on invalid inputs.

Changes:

  • Update LogUniformPrior.log_prior_from_value(..., xp=np) to return -inf for value <= 0 using a safe “double where” pattern to avoid NumPy RuntimeWarnings.
  • Add a regression test ensuring non-positive values return -inf and that no “invalid value in log” warning is emitted; confirm positive-value behaviour is unchanged.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
autofit/mapper/prior/log_uniform.py Makes NumPy log-prior robust to non-positive proposals by returning -inf without evaluating invalid logs.
test_autofit/mapper/prior/test_prior.py Adds regression coverage for the non-positive case and verifies warning-free evaluation plus unchanged positive behaviour.

@Jammy2211
Jammy2211 merged commit 0f26ff2 into main Jul 8, 2026
6 checks passed
@Jammy2211
Jammy2211 deleted the feature/emcee-loguniform-nan branch July 8, 2026 08:55
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 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: LogUniform/Uniform NumPy log-prior returns NaN out-of-support (emcee crash)

2 participants