BUG: logp of pt.max/pt.min over a multi-dimensional i.i.d. variable has shape x.shape[1:] instead of being a scalar
Maintainers usually reply within 3 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
Research direction
Start in pymc/logprob/order.py, reading max_logprob and max_logprob_discrete; the broadcast followed by [0] only drops the first axis. Replace that indexing with [(0,) * base_rv.ndim] in all four places, then run tests/logprob/test_order.py. Done means pm.logp(pt.max(...)) and pm.logp(pt.min(...)) are scalar for 2-D and 3-D bases, and the max/min tests assert the resulting shape rather than relying on broadcasting.
Written by the indexing model from the issue text.
Description
Describe the issue:
pt.max(x) and pt.min(x) reduced over every axis of an i.i.d. variable are scalars, but when x has two or more dimensions the derived logp has shape x.shape[1:], with the correct value repeated in every entry. Inside a model those entries are summed, so the observation is counted prod(x.shape[1:]) times: 3x for a (2, 3) base, 12x for (2, 3, 4). This affects continuous and discrete max, and min as well. A 1-D base gives the correct scalar.
The cause is in pymc/logprob/order.py. max_logprob and max_logprob_discrete broadcast the value to the base variable's shape and then take [0] of the result (lines 148–149 and 168–169), which only removes the first axis:
bcast_value = pt.broadcast_to(value, base_rv_shape)
logprob = _logprob_helper(base_rv, bcast_value)[0]
logcdf = _logcdf_helper(base_rv, bcast_value)[0]
The existing tests don't catch this. test_max_logprob and test_min_logprob include (3, 4) cases, but they compare a scalar against the (4,)-shaped result with np.testing.assert_allclose, which broadcasts, so they pass.
Indexing every axis instead, i.e. [(0,) * base_rv.ndim] in those four places, fixes it. The logp becomes a scalar, model logps match the analytic order-statistic density, and tests/logprob/test_order.py still passes. Adding a shape assertion to the max/min tests would cover the regression. I'm happy to open a PR.
Reproduceable code example:
import numpy as np
import pymc as pm
import pytensor.tensor as pt
from scipy import stats
x = pm.Normal.dist(0.0, 1.0, size=(2, 3))
m = pt.max(x)
print(m.ndim)
print(pm.logp(m, 0.3).eval())
n = 6
print(np.log(n) + stats.norm.logpdf(0.3) + (n - 1) * stats.norm.logcdf(0.3)) # -1.5792298719190243
with pm.Model() as model:
pm.CustomDist("m", dist=lambda size: pt.max(pm.Normal.dist(0.0, 1.0, size=(2, 3))), observed=0.3)
print(model.compile_logp()({}))
print(pm.logp(pt.min(pm.Normal.dist(0.0, 1.0, size=(2, 3))), 0.3).eval().shape)
print(pm.logp(pt.max(pm.Poisson.dist(2.0, size=(2, 3))), 2).eval().shape)
print(pm.logp(pt.max(pm.Normal.dist(0.0, 1.0, size=(2, 3, 4))), 0.3).eval().shape)
Error message:
No error is raised, the logp is silently wrong
PyMC version information:
PyMC: 6.4.0 (pip) and main @ bc3904b
PyTensor: 3.3.8
Python: 3.13
OS: Linux
Installed via: pip (release) and pip install -e . (main)
Context for the issue:
Any model whose likelihood uses the max or min of a multi-dimensional batch of i.i.d. draws gets that likelihood raised to the power prod(x.shape[1:]), so the posterior is overconfident without any warning. It looks like a shape problem rather than a deliberate restriction, since a 1-D base is handled correctly.
- 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
-
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
-
Model.table(): Allow truncating of parameter list in distributionsPossibly taken @Imad2702 claimed this 17 days ago. Openbeginner friendly
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
pymc-devs/pymc#8609 · 1 comment ·
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