From 81653cde564c0979856b094afb6243810fcfdaab Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Tue, 17 Nov 2026 11:11:45 +0100 Subject: [PATCH] Reject improper `Uniform` and `LogUniform` distributions `Uniform` accepted any limits, e.g., `Uniform(0, inf)`, whose pdf is 0, whose cdf is nan, and which fails only when sampled, with numpy's `OverflowError: Range exceeds valid bounds`. `Uniform` now requires finite limits with `low < high` (on the scale of the underlying uniform distribution), and `LogUniform` requires `0 < low < high < inf`. In `petab.v1`, this affects start point sampling for parameters with infinite bounds or with `lowerBound=0` and log-scaled `parameterScaleUniform` priors. These already failed during sampling; they now fail when the `Prior` is created. Estimated parameters with `lowerBound == upperBound` and no prior can no longer be sampled. In `petab.v2`, `Parameter.prior_dist` reports the original prior parameters and bounds if a `uniform` prior does not overlap the parameter bounds. Co-Authored-By: Claude Opus 5.5 --- changelog.d/+uniform-proper.breaking.md | 9 +++++++++ petab/v1/distributions.py | 14 ++++++++++++- petab/v2/core.py | 6 ++++++ tests/v1/test_distributions.py | 27 +++++++++++++++++++++++++ tests/v2/test_lint.py | 26 ++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 changelog.d/+uniform-proper.breaking.md diff --git a/changelog.d/+uniform-proper.breaking.md b/changelog.d/+uniform-proper.breaking.md new file mode 100644 index 00000000..1fb2e950 --- /dev/null +++ b/changelog.d/+uniform-proper.breaking.md @@ -0,0 +1,9 @@ +`petab.v1.distributions.Uniform` requires finite limits with +`low < high`, and `LogUniform` requires `0 < low < high < inf`. +Both raise `ValueError` otherwise. In `petab.v1`, start point sampling +for parameters with infinite bounds, or with `lowerBound=0` and +log-scaled `parameterScaleUniform` priors, now fails when creating the +`Prior` instead of when sampling. Start points can no longer be sampled +for estimated parameters with `lowerBound == upperBound` and no prior +(v1 and v2) +([#604](https://github.com/PEtab-dev/libpetab-python/pull/604)) diff --git a/petab/v1/distributions.py b/petab/v1/distributions.py index 84475a33..e992dcab 100644 --- a/petab/v1/distributions.py +++ b/petab/v1/distributions.py @@ -381,6 +381,8 @@ def scale(self) -> float: class Uniform(Distribution): """A (log-)uniform distribution. + The bounds must be finite with ``low < high``. + :param low: The lower bound of the distribution. :param high: The upper bound of the distribution. :param log: If ``True``, the distribution is transformed to a log-uniform @@ -406,6 +408,11 @@ def __init__( from scipy.stats import uniform except ImportError as e: raise ImportError(_SCIPY_IMPORT_ERROR) from e + if not (np.isfinite(low) and np.isfinite(high) and low < high): + raise ValueError( + "Uniform distribution requires finite limits with low < high, " + f"but got low={low}, high={high}." + ) self._dist = uniform self._low = low self._high = high @@ -432,7 +439,7 @@ class LogUniform(Distribution): A random variable is log-uniformly distributed between ``low`` and ``high`` if its logarithm is uniformly distributed between ``log(low)`` and - ``log(high)``. + ``log(high)``. The bounds must satisfy ``0 < low < high < inf``. :param low: The lower bound of the distribution. :param high: The upper bound of the distribution. @@ -445,6 +452,11 @@ def __init__( high: float, trunc: tuple[float, float] | None = None, ): + if not (0 < low < high < np.inf): + raise ValueError( + "Log-uniform distribution requires 0 < low < high < inf, " + f"but got low={low}, high={high}." + ) try: from scipy.stats import uniform except ImportError as e: diff --git a/petab/v2/core.py b/petab/v2/core.py index 0948ea9a..a315cb1b 100644 --- a/petab/v2/core.py +++ b/petab/v2/core.py @@ -1209,6 +1209,12 @@ def prior_dist(self) -> Distribution | None: # `Uniform.__init__` does not accept the `trunc` parameter low = max(self.prior_parameters[0], self.lb) high = min(self.prior_parameters[1], self.ub) + if low >= high: + raise ValueError( + f"Uniform prior parameters {self.prior_parameters} and " + f"bounds [{self.lb}, {self.ub}] of parameter `{self.id}' " + "do not define a non-empty interval." + ) return cls(low, high) if cls == LogUniform: diff --git a/tests/v1/test_distributions.py b/tests/v1/test_distributions.py index 2c78a6ae..07c65fcb 100644 --- a/tests/v1/test_distributions.py +++ b/tests/v1/test_distributions.py @@ -148,3 +148,30 @@ def test_trunc_sequence(): assert d._trunc == (-1, 1) assert d.trunc_low == -1 assert d.trunc_high == 1 + + +@pytest.mark.parametrize( + "low, high", + [ + (0, np.inf), + (-np.inf, 0), + (-np.inf, np.inf), + (np.nan, 1), + (1, 1), + (2, 1), + ], +) +@pytest.mark.parametrize("log", [False, True, 10]) +def test_uniform_invalid_limits(low, high, log): + """Uniform distributions require finite limits with low < high.""" + with pytest.raises(ValueError, match="finite limits"): + Uniform(low, high, log=log) + + +@pytest.mark.parametrize( + "low, high", [(0, 1), (-1, 1), (1, np.inf), (np.nan, 1), (1, 1), (2, 1)] +) +def test_log_uniform_invalid_limits(low, high): + """Log-uniform distributions require 0 < low < high < inf.""" + with pytest.raises(ValueError, match="0 < low < high < inf"): + LogUniform(low, high) diff --git a/tests/v2/test_lint.py b/tests/v2/test_lint.py index f0405daf..ac2b3b49 100644 --- a/tests/v2/test_lint.py +++ b/tests/v2/test_lint.py @@ -3,6 +3,7 @@ from copy import deepcopy import pysb +import pytest from petab.v2 import Problem from petab.v2.C import TIME_SYMBOL @@ -264,3 +265,28 @@ def test_check_unused_placeholders(): assert "`obs_c`: noise placeholders `sigma` are not used" in issue.message assert "offset" not in issue.message assert "Consider removing them." in issue.message + + +@pytest.mark.parametrize( + "prior_parameters, lb, ub, hint", + [ + ([0, float("inf")], 0, float("inf"), "finite limits"), + ([0, 1], 2, 3, "do not define a non-empty interval"), + ], +) +def test_check_prior_distribution_invalid_uniform( + prior_parameters, lb, ub, hint +): + """Invalid uniform priors are reported with an informative hint.""" + problem = Problem() + problem.add_parameter( + "p1", + estimate=True, + lb=lb, + ub=ub, + prior_dist="uniform", + prior_pars=prior_parameters, + ) + assert (error := CheckPriorDistribution().run(problem)) is not None + assert "`p1` are invalid" in error.message + assert hint in error.message