BUG: measurable matmul logp triples the density of B @ (x + b)
Maintainers usually reply within 3 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 56/100
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
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pymc-devs/pymc
-
BUG: ZarrTrace fails in init_trace for models with datetime coordinates (object dtype coord array)Possibly taken @vladosskins178 claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 3 days
-
BUG: DEMetropolis/DEMetropolisZ crash when vars is a subset of the model variablesPossibly taken @Souravrajvi0 claimed this 8 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 3 days
-
BUG: logp of pt.max/pt.min over a multi-dimensional i.i.d. variable has shape x.shape[1:] instead of being a scalarPossibly taken @saifmodan2006 claimed this 11 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
pymc-devs/pymc#8635 · 2 comments ·
Maintainers usually reply within 3 days
-
ENH: permit cachetools >= 7May be free again @shupandee claimed this 9 days ago, and no pull request is open. Openfeature request
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
pymc-devs/pymc#8633 · 1 comment ·
Maintainers usually reply within 3 days
-
DOC: Document how to call `DifferentialEquation` in a model (`y0`, `theta`, output shape)Possibly taken @saheli56 claimed this 16 days ago. Opendocs
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
pymc-devs/pymc#8616 · 3 comments ·
Maintainers usually reply within 3 days
Similar issues
-
triage
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
kornia/kornia-rs#1416 · 1 comment ·
Maintainers usually reply within 2 days
-
ui
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
PolicyEngine/ai-inequality#64 ·
Maintainers usually reply within 1 day
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over timePossibly taken @PabloRoque claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
pymc-devs/pymc-extras#839 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day