Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.d/+distributions-trunc-sequence.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Distributions with truncation limits `(-inf, inf)` given as a list instead
of a tuple, as created by `petab.v2.Parameter.prior_dist` for unbounded
parameters, are no longer treated as truncated
([#603](https://github.com/PEtab-dev/libpetab-python/pull/603))
7 changes: 5 additions & 2 deletions petab/v1/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Distribution(abc.ABC):
If ``False``, no transformation is applied.
:param trunc: The truncation points (lower, upper) of the distribution
or ``None`` if the distribution is not truncated.
``(-inf, inf)`` is equivalent to ``None``.
If the distribution is log-scaled, the truncation limits are expected
to be on the same log scale.
"""
Expand All @@ -64,8 +65,10 @@ def __init__(
if log is True:
log = np.exp(1)

if trunc == (-np.inf, np.inf):
trunc = None
if trunc is not None:
trunc = tuple(trunc)
if trunc == (-np.inf, np.inf):
trunc = None

if trunc is not None and trunc[0] >= trunc[1]:
raise ValueError(
Expand Down
16 changes: 16 additions & 0 deletions tests/v1/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,19 @@ def test_log_uniform():
assert dist.pdf(1) > 0
assert dist.pdf(2) > 0
assert dist.pdf(3) == 0


@pytest.mark.parametrize("trunc", [(-np.inf, np.inf), [-np.inf, np.inf]])
def test_infinite_truncation_is_no_truncation(trunc):
"""Truncation at (-inf, inf) is equivalent to no truncation."""
d = Normal(0, 1, trunc=trunc)
assert d._trunc is None
assert repr(d) == repr(Normal(0, 1))


def test_trunc_sequence():
"""Truncation limits may be given as any sequence."""
d = Normal(0, 1, trunc=[-1, 1])
assert d._trunc == (-1, 1)
assert d.trunc_low == -1
assert d.trunc_high == 1
Loading