From 799768fe80db62e1cac47d702bfa72c270457b9e Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Tue, 17 Nov 2026 15:26:23 +0100 Subject: [PATCH] v2: handle infinite parameter bounds (PEtab 2.0 erratum E1) PEtab 2.0.0 erratum E1 allows `lowerBound=-inf` and `upperBound=inf`. If any estimated parameter has a prior, estimated parameters with infinite bounds require an explicit proper prior, since the default `uniform(lowerBound, upperBound)` prior would be improper. * `Parameter.lb` must be finite or `-inf`, `Parameter.ub` finite or `inf`. Errors refer to the `lowerBound`/`upperBound` columns. * `CheckPriorDistribution` reports estimated parameters with infinite bounds and no prior if the problem has a MAP objective. * `Problem.get_startpoint_distributions` raises a `ValueError` naming estimated parameters with infinite or missing bounds and no prior, instead of returning improper uniform distributions. `get_priors` delegates to it for MAP problems. * `Problem.sample_parameter_startpoints` used `get_priors`, which is empty for maximum likelihood problems, and thus failed. It now uses `get_startpoint_distributions`, returns an `(n_starts, 0)` array if no parameters are estimated, and no longer accepts and ignores arbitrary keyword arguments. * Test that prior truncation at bounds matching the prior's domain is a no-op, and that one-sided infinite bounds still truncate. Closes #565. Co-Authored-By: Claude Opus 5.5 --- changelog.d/+v2-infinite-bounds.fix.md | 8 ++ changelog.d/+v2-sample-startpoints.fix.md | 4 + petab/v2/core.py | 86 ++++++++++--- petab/v2/lint.py | 12 ++ tests/v2/test_core.py | 148 ++++++++++++++++++++++ tests/v2/test_lint.py | 32 ++++- 6 files changed, 268 insertions(+), 22 deletions(-) create mode 100644 changelog.d/+v2-infinite-bounds.fix.md create mode 100644 changelog.d/+v2-sample-startpoints.fix.md diff --git a/changelog.d/+v2-infinite-bounds.fix.md b/changelog.d/+v2-infinite-bounds.fix.md new file mode 100644 index 00000000..dc6fd1e8 --- /dev/null +++ b/changelog.d/+v2-infinite-bounds.fix.md @@ -0,0 +1,8 @@ +Validate infinite parameter bounds according to PEtab 2.0 erratum E1: +`lowerBound=inf` and `upperBound=-inf` are rejected; if any estimated +parameter has a prior, estimated parameters with infinite bounds +require an explicit prior, which is now checked by `petablint`. +`Problem.get_priors` and `Problem.get_startpoint_distributions` raise a +`ValueError` naming estimated parameters with infinite bounds and no +prior +([#601](https://github.com/PEtab-dev/libpetab-python/pull/601)) diff --git a/changelog.d/+v2-sample-startpoints.fix.md b/changelog.d/+v2-sample-startpoints.fix.md new file mode 100644 index 00000000..32dd76cc --- /dev/null +++ b/changelog.d/+v2-sample-startpoints.fix.md @@ -0,0 +1,4 @@ +`petab.v2.Problem.sample_parameter_startpoints` no longer fails for +maximum likelihood problems or problems without estimated parameters, +and no longer accepts and ignores arbitrary keyword arguments +([#601](https://github.com/PEtab-dev/libpetab-python/pull/601)) diff --git a/petab/v2/core.py b/petab/v2/core.py index a315cb1b..597df4e6 100644 --- a/petab/v2/core.py +++ b/petab/v2/core.py @@ -87,18 +87,30 @@ logger = logging.getLogger(__name__) -def _is_finite_or_neg_inf(v: float, info: ValidationInfo) -> float: - if not np.isfinite(v) and v != -np.inf: +def _field_to_column(info: ValidationInfo) -> str: + """The PEtab column name for the validated field.""" + return {"lb": C.LOWER_BOUND, "ub": C.UPPER_BOUND}.get( + info.field_name, info.field_name + ) + + +def _is_finite_or_neg_inf( + v: float | None, info: ValidationInfo +) -> float | None: + if v is not None and not np.isfinite(v) and v != -np.inf: raise ValueError( - f"{info.field_name} value must be finite or -inf but got {v}" + f"{_field_to_column(info)} value must be finite or -inf " + f"but got {v}" ) return v -def _is_finite_or_pos_inf(v: float, info: ValidationInfo) -> float: - if not np.isfinite(v) and v != np.inf: +def _is_finite_or_pos_inf( + v: float | None, info: ValidationInfo +) -> float | None: + if v is not None and not np.isfinite(v) and v != np.inf: raise ValueError( - f"{info.field_name} value must be finite or inf but got {v}" + f"{_field_to_column(info)} value must be finite or inf but got {v}" ) return v @@ -1061,13 +1073,17 @@ class Parameter(BaseModel): alias=C.PARAMETER_NAME, default=None ) #: Lower bound. - lb: Annotated[float | None, BeforeValidator(_convert_nan_to_none)] = Field( - alias=C.LOWER_BOUND, default=None - ) + lb: Annotated[ + float | None, + BeforeValidator(_convert_nan_to_none), + AfterValidator(_is_finite_or_neg_inf), + ] = Field(alias=C.LOWER_BOUND, default=None) #: Upper bound. - ub: Annotated[float | None, BeforeValidator(_convert_nan_to_none)] = Field( - alias=C.UPPER_BOUND, default=None - ) + ub: Annotated[ + float | None, + BeforeValidator(_convert_nan_to_none), + AfterValidator(_is_finite_or_pos_inf), + ] = Field(alias=C.UPPER_BOUND, default=None) #: Nominal value. nominal_value: Annotated[ # PEtab SciML supports arrays via "array" nominal values @@ -2102,6 +2118,17 @@ def has_ml_objective(self) -> bool: """ return not self.has_map_objective + def _get_unbounded_ids_without_prior(self) -> list[str]: + """IDs of estimated parameters with infinite or missing bounds and + no prior.""" + return [ + p.id + for p in self.parameters + if p.estimate + and p.prior_distribution is None + and any(b is None or np.isinf(b) for b in (p.lb, p.ub)) + ] + def get_priors(self) -> dict[str, Distribution]: """Get prior distributions. @@ -2110,15 +2137,13 @@ def get_priors(self) -> dict[str, Distribution]: :returns: The prior distributions for the estimated parameters in case the problem has a MAP objective, an empty dictionary otherwise. + :raises ValueError: If an estimated parameter without prior has an + infinite or missing bound. """ if not self.has_map_objective: return {} - return { - p.id: p.prior_dist if p.prior_distribution else Uniform(p.lb, p.ub) - for p in self.parameters - if p.estimate - } + return self.get_startpoint_distributions() def get_startpoint_distributions(self) -> dict[str, Distribution]: """Get distributions for sampling startpoints. @@ -2130,17 +2155,36 @@ def get_startpoint_distributions(self) -> dict[str, Distribution]: :returns: Mapping of parameter IDs to distributions for sampling startpoints. + :raises ValueError: If an estimated parameter without prior has an + infinite or missing bound. """ + if unbounded := self._get_unbounded_ids_without_prior(): + raise ValueError( + "The default uniform distribution is undefined for estimated " + "parameters with infinite or missing bounds and no prior: " + f"{unbounded}." + ) + return { p.id: p.prior_dist if p.prior_distribution else Uniform(p.lb, p.ub) for p in self.parameters if p.estimate } - def sample_parameter_startpoints(self, n_starts: int = 100, **kwargs): - """Create 2D array with starting points for optimization""" - priors = self.get_priors() - return np.vstack([p.sample(n_starts) for p in priors.values()]).T + def sample_parameter_startpoints(self, n_starts: int = 100) -> np.ndarray: + """Create 2D array with starting points for optimization. + + :param n_starts: Number of starting points. + :returns: Array of shape ``(n_starts, n_estimated)`` with columns + ordered as :attr:`x_free_ids`. + :raises ValueError: See :meth:`get_startpoint_distributions`. + """ + distributions = self.get_startpoint_distributions() + if not distributions: + return np.empty((n_starts, 0)) + return np.vstack( + [d.sample(n_starts) for d in distributions.values()] + ).T def sample_parameter_startpoints_dict( self, n_starts: int = 100 diff --git a/petab/v2/lint.py b/petab/v2/lint.py index 3d90976a..3a995e66 100644 --- a/petab/v2/lint.py +++ b/petab/v2/lint.py @@ -898,6 +898,18 @@ class CheckPriorDistribution(ValidationTask): def run(self, problem: Problem) -> ValidationIssue | None: messages = [] + + # The default uniform(lowerBound, upperBound) prior is improper for + # infinite bounds + if problem.has_map_objective and ( + unbounded := problem._get_unbounded_ids_without_prior() + ): + messages.append( + "If any estimated parameter has a prior, all estimated " + "parameters with infinite bounds require an explicit proper " + f"prior, but the following parameters have none: {unbounded}." + ) + for parameter in problem.parameters: if parameter.prior_distribution is None: continue diff --git a/tests/v2/test_core.py b/tests/v2/test_core.py index 5509b9e4..faab2df9 100644 --- a/tests/v2/test_core.py +++ b/tests/v2/test_core.py @@ -11,6 +11,7 @@ from sympy.abc import x, y import petab.v2 as petab +from petab.v1.distributions import Normal from petab.v2 import C from petab.v2.C import ( CONDITION_ID, @@ -379,6 +380,17 @@ def test_parameter(): with pytest.raises(ValidationError, match="less than"): Parameter(id="k1", lb=2, ub=1) + # infinite bounds + Parameter(id="k1", lb=-np.inf, ub=np.inf) + Parameter(id="k1", lb="-inf", ub="inf") + with pytest.raises(ValidationError, match="lowerBound .* finite or -inf"): + Parameter(id="k1", lb=np.inf, ub=np.inf) + with pytest.raises(ValidationError, match="upperBound .* finite or inf"): + Parameter(id="k1", lb=-np.inf, ub=-np.inf) + p = Parameter(id="k1", lb=0, ub=1) + with pytest.raises(ValidationError, match="finite or inf"): + p.ub = -np.inf + assert Parameter( id="k1", estimate=True, lb=1, ub=2, prior_parameters=[1, 2] ).model_dump() == { @@ -741,6 +753,142 @@ def test_sample_startpoint_shape(): assert sp.shape == (n_starts, 2) +def test_sample_startpoints_ml(): + """Test startpoint sampling for maximum likelihood problems.""" + problem = Problem() + problem += Parameter(id="p1", estimate=True, lb=1, ub=2) + problem += Parameter(id="p2", estimate=True, lb=3, ub=4) + assert problem.has_ml_objective + + sp = problem.sample_parameter_startpoints(n_starts=10) + assert sp.shape == (10, 2) + assert np.all((sp[:, 0] >= 1) & (sp[:, 0] <= 2)) + assert np.all((sp[:, 1] >= 3) & (sp[:, 1] <= 4)) + + # infinite bounds require a prior for sampling startpoints + problem += Parameter(id="p3", estimate=True, lb=0, ub=np.inf) + with pytest.raises(ValueError, match=r"missing bounds.*\['p3'\]"): + problem.get_startpoint_distributions() + with pytest.raises(ValueError, match=r"missing bounds.*\['p3'\]"): + problem.sample_parameter_startpoints() + # ML problem: no priors + assert problem.get_priors() == {} + + problem["p3"].prior_distribution = C.LOG_NORMAL + problem["p3"].prior_parameters = [0, 1] + sp = problem.sample_parameter_startpoints(n_starts=10) + assert sp.shape == (10, 3) + assert np.all(sp[:, 2] > 0) + + # missing bounds (bypassing validation) are reported like infinite ones + problem += Parameter.model_construct(id="p4", estimate=True) + with pytest.raises(ValueError, match=r"missing bounds.*\['p4'\]"): + problem.get_startpoint_distributions() + + +def test_sample_startpoints_no_estimated_parameters(): + """Test startpoint sampling without estimated parameters.""" + problem = Problem() + problem += Parameter(id="p1", estimate=False, nominal_value=1) + assert problem.sample_parameter_startpoints(n_starts=5).shape == (5, 0) + + +def test_get_priors_infinite_bounds(): + """Test that infinite bounds without prior are rejected for MAP + problems.""" + problem = Problem() + problem += Parameter(id="p1", estimate=True, lb=-np.inf, ub=np.inf) + problem += Parameter( + id="p2", + estimate=True, + lb=1, + ub=2, + prior_distribution=C.NORMAL, + prior_parameters=[1.5, 1], + ) + assert problem.has_map_objective + with pytest.raises(ValueError, match=r"missing bounds.*\['p1'\]"): + problem.get_priors() + + problem["p1"].prior_distribution = C.NORMAL + problem["p1"].prior_parameters = [0, 1] + assert set(problem.get_priors()) == {"p1", "p2"} + + +@pytest.mark.parametrize( + "prior_distribution, prior_parameters, lb, ub", + [ + (C.NORMAL, [0, 1], -np.inf, np.inf), + (C.LOG_NORMAL, [0, 1], 0, np.inf), + (C.EXPONENTIAL, [2], 0, np.inf), + (C.GAMMA, [2, 1], 0, np.inf), + (C.CHI_SQUARED, [2], 0, np.inf), + (C.RAYLEIGH, [1], 0, np.inf), + (C.LOG_LAPLACE, [0, 1], 0, np.inf), + (C.CAUCHY, [0, 1], -np.inf, np.inf), + (C.LAPLACE, [0, 1], -np.inf, np.inf), + ], +) +def test_prior_truncation_noop_at_domain( + prior_distribution, prior_parameters, lb, ub +): + """Bounds matching the prior's domain do not truncate the prior.""" + truncated = Parameter( + id="p", + lb=lb, + ub=ub, + prior_distribution=prior_distribution, + prior_parameters=prior_parameters, + ).prior_dist + # reference: the same distribution without truncation + untruncated = type(truncated)( + *prior_parameters, + **({"log": True} if prior_distribution.startswith("log-") else {}), + ) + + x = np.array([0.1, 0.5, 1.0, 2.0, 5.0]) + np.testing.assert_allclose(truncated.pdf(x), untruncated.pdf(x)) + np.testing.assert_allclose(truncated.cdf(x), untruncated.cdf(x)) + assert np.all(truncated.pdf(x) > 0) + + +def test_prior_truncation_one_sided_infinite(): + """A finite bound truncates the prior also if the other is infinite.""" + truncated = Parameter( + id="p", + lb=1, + ub=np.inf, + prior_distribution=C.LOG_NORMAL, + prior_parameters=[0, 1], + ).prior_dist + untruncated = Parameter( + id="p", + lb=0, + ub=np.inf, + prior_distribution=C.LOG_NORMAL, + prior_parameters=[0, 1], + ).prior_dist + # median of LogNormal(0, 1) is 1, so truncation at 1 doubles the density + x = np.array([1.5, 2.0, 5.0]) + np.testing.assert_allclose(truncated.pdf(x), 2 * untruncated.pdf(x)) + assert truncated.pdf(0.5) == 0 + assert np.all(truncated.sample(100) >= 1) + + # mirrored: finite upper bound + truncated = Parameter( + id="p", + lb=-np.inf, + ub=0, + prior_distribution=C.NORMAL, + prior_parameters=[0, 1], + ).prior_dist + untruncated = Normal(0, 1) + x = np.array([-2.0, -1.0, -0.5]) + np.testing.assert_allclose(truncated.pdf(x), 2 * untruncated.pdf(x)) + assert truncated.pdf(0.5) == 0 + assert np.all(truncated.sample(100) <= 0) + + def test_problem_config_paths(): """Test handling of URLS and local paths in ProblemConfig.""" diff --git a/tests/v2/test_lint.py b/tests/v2/test_lint.py index ac2b3b49..ea6c44f2 100644 --- a/tests/v2/test_lint.py +++ b/tests/v2/test_lint.py @@ -6,7 +6,7 @@ import pytest from petab.v2 import Problem -from petab.v2.C import TIME_SYMBOL +from petab.v2.C import LOG_NORMAL, NORMAL, TIME_SYMBOL, UNIFORM from petab.v2.lint import * from petab.v2.lint import ( get_required_parameters_for_parameter_table, @@ -290,3 +290,33 @@ def test_check_prior_distribution_invalid_uniform( assert (error := CheckPriorDistribution().run(problem)) is not None assert "`p1` are invalid" in error.message assert hint in error.message + + +def test_check_prior_distribution_infinite_bounds(): + """Estimated parameters with infinite bounds need a prior if the problem + has a MAP objective.""" + problem = Problem() + problem.add_parameter("p1", estimate=True, lb=0, ub=float("inf")) + problem.add_parameter("p2", estimate=True, lb=0, ub=1) + # non-estimated parameters are irrelevant + problem.add_parameter("p3", estimate=False, nominal_value=1) + + check = CheckPriorDistribution() + # ML problem: infinite bounds are fine + assert check.run(problem) is None + + problem["p2"].prior_distribution = NORMAL + problem["p2"].prior_parameters = [0.5, 1] + assert (error := check.run(problem)) is not None + assert error.level == ValidationIssueSeverity.ERROR + assert "['p1']" in error.message + + problem["p1"].prior_distribution = LOG_NORMAL + problem["p1"].prior_parameters = [0, 1] + assert check.run(problem) is None + + # improper explicit prior + problem["p1"].prior_distribution = UNIFORM + problem["p1"].prior_parameters = [0, float("inf")] + assert (error := check.run(problem)) is not None + assert "`p1` are invalid" in error.message