BUG: `TruncatedRV` fallback support point computes half-interval width instead of midpoint, producing out-of-bounds initial points and `-inf` logp
Maintainers usually reply within 3 days
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
- Domain
- backend, machine-learning
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:
- 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]$).
- 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 withlogp = -inf. model.debug()reports that the variable evaluates to a non-finitelogp = -infat the initial point.- MCMC sampling fails or begins at an invalid point with
nanacceptance 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
mainbranch, commitbc3904be4) - 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:
- In
pymc/distributions/truncated.py(around line 394), replace:
with:(upper - lower) / 2, # lower and upper are finite
(and ensure discrete RVs round or floor the midpoint to maintain integer dtype, e.g. using(lower + upper) / 2, # lower and upper are finitept.roundorpt.maximum(pt.floor((lower + upper) / 2.0), lower)). - In
tests/distributions/test_truncated.py(line 440), update the test expectation for(icdf_normal(3, 1), -1, 2, ...)fromnp.full((2,), 3 / 2)tonp.full((2,), 1 / 2)(or0.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
- 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
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
meetecho/juturna_nodes#43 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
TigreGotico/text2spoken#726 ·
Maintainers usually reply within 1 day
-
bug dependencies
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
nolte/claude-shared#822 ·
Maintainers usually reply within 1 day
-
`RequestHandler.build_request_kwargs` drops empty request bodies such as `{}` or `""`Possibly taken @abhirajadhikary06 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
typesense/typesense-python#170 ·
Maintainers usually reply within 1 day
-
claude-subscription-directsdk: tool calls with raw control characters in string values are droppedOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
NousResearch/hermes-official-plugins#538 ·
Maintainers usually reply within 3 days