Skip to content

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 #8635

Open

Description

@SohamDutta02

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.

Activity

  1. welcome commented on Nov 15, 2026

    @welcome

    Welcome Banner]
    🎉 Welcome to PyMC! 🎉 We're really excited to have your input into the project! 💖

    If you haven't done so already, please make sure you check out our Contributing Guidelines and Code of Conduct.

  2. Dikshant-Neupane commented on Nov 21, 2026

    @Dikshant-Neupane

    Hi, I would like to take this one if you are not planning to open a PR yourself. I have read the issue and the relevant code in pymc/logprob/order.py, and I can open a PR with the fix and a shape assertion in tests/logprob/test_order.py. Please let me know.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions