BUG: `TruncatedRV` fallback support point computes half-interval width instead of midpoint, producing out-of-bounds initial points and `-inf` logp

Open
#8,639 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@vishalmore90 is already working on this.

Since Nov 16, 2026.

  • #8640 by @vishalmore90 — open

Assessment

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

Research direction

Read pymc/distributions/truncated.py around truncated_support_point and the relevant cases in tests/distributions/test_truncated.py. Check how the fallback behaves when the base support point falls outside finite positive and negative bounds, including discrete variables. Done means the support point stays inside the truncation interval and the tests verify the corrected midpoint behavior; pull request #8640 is already open.

Written by the indexing model from the issue text.

Description

Description
Describe the issue:

In pymc/distributions/truncated.py, when computing the support_point for a TruncatedRV whose base distribution's support point lies outside the truncation interval and both bounds are finite, the fallback logic in truncated_support_point computes (upper - lower) / 2:

# pymc/distributions/truncated.py:392-406
fallback_support_point = pt.switch(
    pt.and_(pt.bitwise_not(pt.isinf(lower)), pt.bitwise_not(pt.isinf(upper))),
    (upper - lower) / 2,  # lower and upper are finite
    pt.switch(
        pt.isinf(upper),
        lower + 1,  # only lower is finite
        upper - 1,  # only upper is finite
    ),
)

(upper - lower) / 2 calculates half the width of the interval (the radius), rather than the midpoint (lower + upper) / 2 (or lower + (upper - lower) / 2).

As a result:

  1. Whenever $lower > 0$ and $upper < 3 \times lower$, the fallback support point is strictly less than the lower bound ($5.0 < 10.0$ for $[10, 20]$; $50.0 < 100.0$ for $[100, 200]$).
  2. Whenever $upper < 0$, the fallback support point is positive and strictly greater than the upper bound ($5.0 > -10.0$ for $[-20, -10]$).

In these cases, support_point returns a value completely outside the distribution's support. Consequently:

  • For truncated discrete distributions (which have no default transform, e.g., Truncated("x", Poisson.dist(mu=2), lower=10, upper=20)), or continuous truncated distributions where transformation is disabled (default_transform=None), model.initial_point() assigns an out-of-bounds point with logp = -inf.
  • model.debug() reports that the variable evaluates to a non-finite logp = -inf at the initial point.
  • MCMC sampling fails or begins at an invalid point with nan acceptance rates.

Furthermore, tests/distributions/test_truncated.py (line 440) currently codifies this bug: for lower = -1, upper = 2, the true midpoint is $(-1 + 2) / 2 = 0.5$, but the test asserts 3 / 2 ($1.5 = (2 - (-1)) / 2$), which only passed because $1.5$ happened to fall inside $[-1, 2]$.


Reproduceable code example:
import pymc as pm
from pymc.distributions.distribution import support_point

# 1. Truncated discrete variable in a model
with pm.Model() as m:
    x = pm.Truncated("x", pm.Poisson.dist(mu=2), lower=10, upper=20)

ip = m.initial_point()
print("Initial point:", ip)
print("Point logp:", m.point_logps(ip))

# 2. Direct support point check for continuous distributions
s_pos = pm.Truncated.dist(pm.StudentT.dist(nu=4, mu=0, sigma=1), lower=10, upper=20)
print("Support point for [10, 20]:", support_point(s_pos).eval())  # prints 5.0, which is < 10

s_neg = pm.Truncated.dist(pm.StudentT.dist(nu=4, mu=0, sigma=1), lower=-20, upper=-10)
print("Support point for [-20, -10]:", support_point(s_neg).eval())  # prints 5.0, which is > -10

Error message:
Initial point: {'x': array(5)}
Point logp: {'x': np.float64(-inf)}

Support point for [10, 20]: 5.0
Support point for [-20, -10]: 5.0

Running m.debug() on the model outputs:

point={'x': array(5)}

The variable x has the following parameters:
0: [] [id A] <Vector(int64, shape=(0,))>
1: 2 [id B] <Scalar(int8, shape=())>
2: 10 [id C] <Scalar(int8, shape=())>
3: 20 [id D] <Scalar(int8, shape=())>
The parameters evaluate to:
0: []
1: 2
2: 10
3: 20
Some of the values of variable x are associated with a non-finite logp:
 value = [5] -> logp = -inf

You can set `verbose=True` for more details

PyMC version information:
  • PyMC Version: 6.4.0+ (current main branch, commit bc3904be4)
  • PyTensor Version: 3.3.8
  • Python Version: 3.13
  • Operating system: Windows / Linux / macOS
  • How did you install PyMC/PyMC3: source / pip

Context for the issue:

Truncated distributions with strictly positive or strictly negative finite bounds are common in practice (e.g., modeling bounded counts, waiting times, or shifted phenomena). When the untruncated base distribution's natural support point does not lie in the truncated interval, PyMC should fall back to the interval's midpoint.

Because (upper - lower) / 2 was used instead of (lower + upper) / 2 (analogous to TruncatedNormal.support_point in pymc/distributions/continuous.py:710 and Uniform.support_point in continuous.py:303), the fallback point routinely lands outside the support, producing -inf log probability at the model's initial point and preventing sampling or optimization from starting cleanly.

Proposed Solution:
  1. In pymc/distributions/truncated.py (around line 394), replace:
    (upper - lower) / 2,  # lower and upper are finite
    
    with:
    (lower + upper) / 2,  # lower and upper are finite
    
    (and ensure discrete RVs round or floor the midpoint to maintain integer dtype, e.g. using pt.round or pt.maximum(pt.floor((lower + upper) / 2.0), lower)).
  2. In tests/distributions/test_truncated.py (line 440), update the test expectation for (icdf_normal(3, 1), -1, 2, ...) from np.full((2,), 3 / 2) to np.full((2,), 1 / 2) (or 0.5), and add test cases with strictly positive ([10, 20]) and strictly negative ([-20, -10]) bounds.
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.