Skip to content

TransformedMessage.logpdf/pdf omit the transform Jacobian (base-space density at physical coordinates) #1498

Open

Description

@Jammy2211

Found while implementing the #1497 property sweep (priors/messages census follow-up; hubs #1330/#1331). Same failure shape as #1331-04: a generic-interface density path is silently wrong while the direct paths are right.

The finding

TransformedMessage inherits MessageInterface.logpdf (natural-parameter path). Its overrides forward natural_parameters, calc_log_base_measure and to_canonical_form to the base message — to_canonical_form is @transform-decorated, so the physical input is mapped to base coordinates — but no log_det change-of-variables term is ever added. The result: logpdf(x) at a physical x returns the base-space density at the mapped point, not the physical density. Meanwhile TransformedMessage.factor(x) does it correctly (base.logpdf(transform(x)) + log_det).

Reproduction on main @ 7d4d931 (numbers exact):

import numpy as np
from scipy.integrate import quad
import autofit as af

m = af.UniformPrior(0.0, 1.0).message      # TransformedMessage(UniformNormalMessage, LinearShift)

m.logpdf(np.asarray(0.7))                  # -1.05644  — physical density is 1.0, expect 0.0
m.factor(np.asarray(0.7))                  #  0.0      — correct

quad(lambda x: float(m.pdf(np.asarray(x))), 0.0, 1.0)   # 0.282095 = 1/(2*sqrt(pi)), not 1.0
quad(lambda x: float(np.exp(m.factor(np.asarray(x)))), 0.0, 1.0)  # 1.000000

For LogUniformPrior(0.01, 100) the generic-path pdf integrates to 15.83. Every TransformedMessage-wrapped prior (Uniform, LogUniform, LogGaussian) is affected; plain NormalMessage / TruncatedNormalMessage priors are not.

Doc contradiction

The composed_transform.py module docstring (added in #1334) states: "Evaluating logpdf(x) at a physical x runs the same three steps in reverse (physical → base) via _transform, accumulating the log-Jacobian log_det of each transform for the change of variables." The code does not accumulate it. Either the docstring describes the intended contract (then logpdf needs the log_det term) or logpdf is deliberately base-space for EP message arithmetic (then the docstring and pdf() are wrong/misleading).

What needs adjudication before a fix

  • Which callers consume TransformedMessage.logpdf/pdf vs factor? The EP factor-graph path uses factor/exp_factor (correct). If any EP projection / log_norm path evaluates logpdf on transformed messages at physical coordinates, it inherits this error.
  • logpdf_gradient returns the base log_likelihood with a Jacobian-corrected gradient — a third convention (gradient physical, value base-space) worth settling in the same pass.
  • Note value_for, cdf, log_prior_from_value and factor are all verified correct — user-facing sampling and MCMC/MLE log-priors are unaffected. The exposure is anything treating transformed_message.pdf() as a physical density.

The #1497 property tests assert the physical-density path via factor for transformed messages and cite this issue at the site; when this is resolved they can be tightened to assert logpdf directly.

Sequencing suggestion: adjudicate alongside the parked single-source-density design (census C1/C4, prompts 12/13) — this is a fourth density-convention divergence of exactly the kind that design exists to eliminate.

Activity

  1. Jammy2211 commented on Aug 19, 2026

    @Jammy2211
    CollaboratorAuthor

    Caller analysis (adjudication point 1) — 2026-08-19, main @ 21288bb

    Full inventory of logpdf/pdf/logpdf_gradient/factor call sites across the production autofit/ tree, numerically verified (UniformPrior(0,2): logpdf(1.3) = −0.993174 vs factor(1.3) = −0.693147 = log 0.5; ∫exp(logpdf) over the support = 0.564190 = 2/(2√π), ∫exp(factor) = 1.000000).

    Callers expecting a physical density (exposed to the bug)

    • Prior.logpdf — the public prior API. UniformPrior.logpdf (uniform.py:124) calls message.logpdf explicitly, and its boundary-epsilon nudge operates in physical coordinates, so the method unambiguously intends physical semantics; every other prior reaches message.logpdf via Prior.__getattr__. Uniform / LogUniform / LogGaussian users are silently handed base-space values.
    • MessageInterface.pdf (interface.py:45) — zero production callers; public-API surface only.
    • The non-linear search stack never touches message logpdf — sampling and MCMC/MLE go through value_for / log_prior_from_value / factor, all verified correct. Exposure is API-level, not search-level.

    EP-internal callers — all base-space, and mutually consistent

    MeanField.logpdf/logpdf_gradient (mean_field.py:294,300), reached from FactorApproximation.__call__/func_gradient (mean_field.py:631-633,647) by the Laplace optimiser; MeanField is itself a Factor wrapping _logpdf, so projection(mode) in the from_mode_covariance log_norm calibration (mean_field.py:404) and AbstractMessage.__call__/factor_jacobian (abstract.py:392,398) route the same way.

    This loop is coherent, not buggy: a likelihood factor is a density over data (measure-free in x), so fval(x) + Σ log q_base(T(x)) is exactly the base-space tilted log density at z = T(x). The Laplace mode it finds is the base-space tilted mode, and from_mode (transform_jac + jac.quad) projects mode and covariance into base space consistently. The missing log_det is the EP loop's working convention — and base-space Laplace is arguably the better-conditioned choice (unbounded support).

    The one genuine cross-convention seam — PriorFactor

    PriorFactor (declarative/factor/prior.py:23,62) wraps prior.factor — the physical density — as both its factor callable and its log_likelihood_function. Its tilted objective under the numerical optimiser is therefore

    log π_base(T(x)) + log_det(x) + Σ log q_base(T(x))
    

    — one x-dependent log_det(x) more than the coherent base-space tilted density (and one short of the coherent physical one). Because the #1337 seam strips the exact-update hooks (see the graphical/README.md roster row), declarative PriorFactors really do take this numerical path: prior-factor EP updates optimise a hybrid objective whose mode is neither the base-space nor the physical tilted mode.

    Correction to adjudication point 2

    logpdf_gradient is not a third convention. Its analytic gradient equals the central-difference derivative of logpdf exactly (−0.520142 both, at the probe point) — the jacobian multiplication is just the chain rule for the base-space composition base.logpdf(T(x)). So (logpdf, logpdf_gradient, numerical_logpdf_gradient_hessian) form one self-consistent base-space family and factor is the physical one: two conventions, not three.

    New finding

    Running the reproducer surfaced #1501: TransformedMessage.factor_gradient unpacks four values from _transform_det_jac, which returns three — crashes on first call, zero production callers.

    Proposed resolution (adjudication point 3 — for ratification, nothing actioned)

    A two-sided contract repair rather than a one-line logpdf patch:

    1. EP-internal: declare base-space logpdf the message contract (fixing the composed_transform.py module-docstring claim that logpdf accumulates the Jacobian) and make PriorFactor consume the base-space density (message.logpdf) instead of prior.factor, making the EP loop fully base-space coherent. This changes prior-factor updates (removes the spurious log_det) but preserves the deliberate-looking base-space Laplace everywhere else. Belongs with the design: unify the Prior/Message hierarchy around a single-source density #1500 design (Q2/Q3 there).
    2. Public API: Prior.logpdf and pdf promise a physical density and should route through factor (or be renamed/documented). Small, standalone, user-facing correctness fix.

    The other coherent option — adding log_det to logpdf itself so everything is physical — touches every EP path at once and shifts EP Laplace modes to physical-space MAPs; that is a design change that should only be taken inside the #1500 single-source-density decision.

    External verification requested

    This analysis is AI-generated. Before acting on it we'd value a check from someone with a probabilistic-programming background on two claims in particular: (a) that the likelihood-factor tilted objective as implemented equals the base-space tilted density (the change-of-variables argument above), and (b) that PriorFactor's use of the physical density really does leave its tilted objective one log_det(x) off either coherent convention.


    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