Skip to content

fix: natural_logpdf clamps a genuine -inf to -1.8e308 #1533

Closed

Description

@Jammy2211

Overview

AbstractMessage.natural_logpdf (autofit/messages/interface.py:98) reduces through

xp.nan_to_num(log_base + eta_t - log_partition, nan=-xp.inf)

The nan=-xp.inf is deliberate — an out-of-support NaN is zero density. But posinf/neginf are left at their defaults, and nan_to_num replaces a genuine -inf with negative float max. The call therefore does the opposite of its intent for exactly the inputs that already had the right answer:

value reaching the reduction intended actual
NaN -inf -inf ✅
-inf -inf -1.7976931348623157e+308 ❌

Measured on main @ 6e2d8c8

LogGaussianPrior(0.4, 1.3):

value message.logpdf why
-1.0 -inf log(-1) is NaN → nan=-inf applies
0.0 -1.7976931348623157e+308 log(0) is -inf → default neginf clamps

That asymmetry is the proof of mechanism, and the clamped value is exactly -sys.float_info.max.

UniformPrior and LogUniformPrior return -inf correctly outside their boxes, so this is not visible for every family — it needs an expression that reaches -inf rather than NaN.

Why it matters

-1.8e308 is finite, and isfinite is the test a lot of this codebase branches on:

  • optax.apply_if_finite — the mechanism autofit/non_linear/clipper.py is built around. That module's entire premise is that leaving the prior support makes the objective non-finite.
  • Two such terms summed overflow to -inf while one does not, so behaviour depended on how many parameters were out of support.

Plan

  • Pass neginf=-xp.inf, posinf=xp.inf so only NaN is replaced.
  • Cover both halves of the reduction, pin that in-support values are untouched, and assert the general property that no prior family reports a finite density off its support.

Origin

Loose end from #1532, noted in its Shipped comment and not filed at the time. PyAutoMind prompt: draft/bug/priors/natural_logpdf_clamps_neginf.md.

Deliberately out of scope

TruncatedGaussianPrior's message returns finite logpdf well outside its limits (-8.20 at -1.0 for a (0, 3) support). That is separate looseness in TruncatedNormalMessage, not this clamp — the prior-level log_prior_from_value is correct there, which is why the P6 property tests pass. Not addressed here, and the new general-property test deliberately excludes it rather than papering over it.

Activity

  1. Jammy2211 commented on Aug 27, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped.

    • PR fix: natural_logpdf clamps a genuine -inf to -1.8e308 #1534 MERGED as 5c391fd, head 1e3b1a0, +90/-1 over 2 files.
    • CI green on every leg: unittest (3.12) 1m47s of test time, unittest (3.13) 2m10s, unittest-nojax 58s, docs / docs-build 1m35s.
    • Full suite 2203 passed / 36 skipped; baseline measured on the branch point 6e2d8c8 with changes stashed: 2190 / 36.
    • Label pending-release — not released.

    What landed

    autofit/messages/interface.py — natural_logpdf now passes neginf=-xp.inf, posinf=xp.inf, so only NaN is replaced. A genuine -inf survives the reduction instead of becoming -1.7976931348623157e+308.

    test_autofit/messages/test_natural_logpdf_neginf.py — 13 tests covering both halves of the reduction (the -inf half that was broken, the NaN half that was correct and is now pinned so fixing one cannot break the other), in-support values across 8 points, and the general property over Uniform / LogUniform / LogGaussian that no family reports a finite density off its support. Verified by inversion: reverting the fix fails 2 of the 13.

    Still open, unfiled

    TruncatedGaussianPrior's message returns finite logpdf well outside its limits — -8.20 at -1.0 for a (0, 3) support. That is separate looseness in TruncatedNormalMessage, not this clamp, and the prior-level log_prior_from_value is correct there (which is why the P6 property tests pass). The new general-property test excludes it explicitly rather than asserting something weaker across all families. Worth its own task if the message-level API is ever trusted directly.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions