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

Open Beginner friendly
#8,635 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@saifmodan2006 is already working on this.

Since Nov 16, 2026.

  • #8636 by @saifmodan2006 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

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

bug
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

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.