Skip to content

Commit a940716

Browse files
dweindlclaude
andcommitted
Treat trunc=[-inf, inf] as no truncation in distributions
`Distribution.__init__` only recognized the tuple `(-inf, inf)` as "no truncation", but `petab.v2.Parameter.prior_dist` passes the bounds as a list. Such distributions were therefore handled as truncated, e.g., sampled via the inverse CDF instead of directly. Convert `trunc` to a tuple before the comparison, so any sequence is accepted and stored consistently. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b45a01c commit a940716

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Distributions with truncation limits `(-inf, inf)` given as a list instead
2+
of a tuple, as created by `petab.v2.Parameter.prior_dist` for unbounded
3+
parameters, are no longer treated as truncated
4+
([#603](https://github.com/PEtab-dev/libpetab-python/pull/603))

‎petab/v1/distributions.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Distribution(abc.ABC):
5151
If ``False``, no transformation is applied.
5252
:param trunc: The truncation points (lower, upper) of the distribution
5353
or ``None`` if the distribution is not truncated.
54+
``(-inf, inf)`` is equivalent to ``None``.
5455
If the distribution is log-scaled, the truncation limits are expected
5556
to be on the same log scale.
5657
"""
@@ -64,8 +65,10 @@ def __init__(
6465
if log is True:
6566
log = np.exp(1)
6667

67-
if trunc == (-np.inf, np.inf):
68-
trunc = None
68+
if trunc is not None:
69+
trunc = tuple(trunc)
70+
if trunc == (-np.inf, np.inf):
71+
trunc = None
6972

7073
if trunc is not None and trunc[0] >= trunc[1]:
7174
raise ValueError(

‎tests/v1/test_distributions.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,19 @@ def test_log_uniform():
132132
assert dist.pdf(1) > 0
133133
assert dist.pdf(2) > 0
134134
assert dist.pdf(3) == 0
135+
136+
137+
@pytest.mark.parametrize("trunc", [(-np.inf, np.inf), [-np.inf, np.inf]])
138+
def test_infinite_truncation_is_no_truncation(trunc):
139+
"""Truncation at (-inf, inf) is equivalent to no truncation."""
140+
d = Normal(0, 1, trunc=trunc)
141+
assert d._trunc is None
142+
assert repr(d) == repr(Normal(0, 1))
143+
144+
145+
def test_trunc_sequence():
146+
"""Truncation limits may be given as any sequence."""
147+
d = Normal(0, 1, trunc=[-1, 1])
148+
assert d._trunc == (-1, 1)
149+
assert d.trunc_low == -1
150+
assert d.trunc_high == 1

0 commit comments

Comments
 (0)