BUG: measurable matmul logp triples the density of B @ (x + b)

Open
#8,530 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
56/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
numpy, python
Domain
backend, databases

Research direction

Start by tracing the logprob_measurable_matmul reduction named in the report, then run the reproduction with B @ x and B @ (x + b) to compare the results. The issue does not identify a source file or test, so locate the rewrite and its existing tests before changing anything. Done means the shifted expression matches the stated multivariate-normal density without changing the unshifted result.

Written by the indexing model from the issue text.

Description

bug
Describe the issue:

pm.logp(B @ (x + b), y) for a vector MvNormal x returns -25.070 instead of the transformed-normal density -8.587. The same B @ x without the shift is correct (-4.9726 against -4.9726). The shift expands the vector to a column, and the measurable-matmul rewrite then sums the scalar MvNormal logp once per row, so the base density is counted three times. The Jacobian is subtracted once.

Not #6950. That enhancement asked for R @ MvNormal.dist(...) to have a logp. That form now matches scipy.stats.multivariate_normal. This is the shifted product, which is the expression a caller writes for an affine image, and it returns a finite density that is wrong by 16.5 nats.

Not #8513. That is CAR forward draws using inv(Q)**2. This is the logprob_measurable_matmul reduction, and pm.draw of the same expression is not involved.

Reproduceable code example:
import numpy as np
from scipy import stats
import pymc as pm

mu = np.array([0.2, -0.4, 0.7])
cov = np.array([[1.0, 0.2, 0.0], [0.2, 0.8, -0.1], [0.0, -0.1, 0.5]])
B = np.array([[1.3, 0.0, 0.2], [0.0, 0.9, -0.4], [0.2, 1.1, 0.7]])
b = np.array([0.2, 1.1, -0.4])
y = np.array([0.4, -0.2, 1.3])
x = pm.MvNormal.dist(mu, cov=cov)

print("B @ x expected", stats.multivariate_normal.logpdf(y, B @ mu, B @ cov @ B.T))
print("B @ x observed", float(pm.logp(B @ x, y).eval()))
print("B @ (x + b) expected", stats.multivariate_normal.logpdf(y, B @ (mu + b), B @ cov @ B.T))
print("B @ (x + b) observed", float(pm.logp(B @ (x + b), y).eval()))
Error message:

No exception. The log-density is finite and wrong.

PyMC version information:
PyMC: 6.3.2+21.gd1a761e84
PyTensor: 3.3.0
Python: 3.12.10
NumPy: 2.4.6
SciPy: 1.18.11
OS: macOS arm64
Install: editable checkout of d1a761e84174cb5d5427e6f0c52352ffe42aa14e
Context for the issue:

B @ (x + b) is a normal model for a linearly transformed latent. A sampler using this logp targets a density about e^16.5 times too small. B @ x being correct shows the rewrite fires; it applies the single-vector change of variable to the expanded product.

Expected behavior: pm.logp(B @ (x + b), y) equals the multivariate-normal log-density of y under mean B @ (mu + b) and covariance B @ cov @ B.T.

Dominant language
Python
Stars
9.9k
Forks
2.4k
Avg merge
4d 10h
Merged PRs (30d)
12

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pymc-devs/pymc

All issues in pymc-devs/pymc

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.