|
11 | 11 | from sympy.abc import x, y |
12 | 12 |
|
13 | 13 | import petab.v2 as petab |
| 14 | +from petab.v1.distributions import Normal |
14 | 15 | from petab.v2 import C |
15 | 16 | from petab.v2.C import ( |
16 | 17 | CONDITION_ID, |
@@ -379,6 +380,17 @@ def test_parameter(): |
379 | 380 | with pytest.raises(ValidationError, match="less than"): |
380 | 381 | Parameter(id="k1", lb=2, ub=1) |
381 | 382 |
|
| 383 | + # infinite bounds |
| 384 | + Parameter(id="k1", lb=-np.inf, ub=np.inf) |
| 385 | + Parameter(id="k1", lb="-inf", ub="inf") |
| 386 | + with pytest.raises(ValidationError, match="lowerBound .* finite or -inf"): |
| 387 | + Parameter(id="k1", lb=np.inf, ub=np.inf) |
| 388 | + with pytest.raises(ValidationError, match="upperBound .* finite or inf"): |
| 389 | + Parameter(id="k1", lb=-np.inf, ub=-np.inf) |
| 390 | + p = Parameter(id="k1", lb=0, ub=1) |
| 391 | + with pytest.raises(ValidationError, match="finite or inf"): |
| 392 | + p.ub = -np.inf |
| 393 | + |
382 | 394 | assert Parameter( |
383 | 395 | id="k1", estimate=True, lb=1, ub=2, prior_parameters=[1, 2] |
384 | 396 | ).model_dump() == { |
@@ -741,6 +753,143 @@ def test_sample_startpoint_shape(): |
741 | 753 | assert sp.shape == (n_starts, 2) |
742 | 754 |
|
743 | 755 |
|
| 756 | +def test_sample_startpoints_ml(): |
| 757 | + """Test startpoint sampling for maximum likelihood problems.""" |
| 758 | + problem = Problem() |
| 759 | + problem += Parameter(id="p1", estimate=True, lb=1, ub=2) |
| 760 | + problem += Parameter(id="p2", estimate=True, lb=3, ub=4) |
| 761 | + assert problem.has_ml_objective |
| 762 | + |
| 763 | + sp = problem.sample_parameter_startpoints(n_starts=10) |
| 764 | + assert sp.shape == (10, 2) |
| 765 | + assert np.all((sp[:, 0] >= 1) & (sp[:, 0] <= 2)) |
| 766 | + assert np.all((sp[:, 1] >= 3) & (sp[:, 1] <= 4)) |
| 767 | + |
| 768 | + # infinite bounds require a prior for sampling startpoints |
| 769 | + problem += Parameter(id="p3", estimate=True, lb=0, ub=np.inf) |
| 770 | + with pytest.raises(ValueError, match=r"missing bounds.*\['p3'\]"): |
| 771 | + problem.get_startpoint_distributions() |
| 772 | + with pytest.raises(ValueError, match=r"missing bounds.*\['p3'\]"): |
| 773 | + problem.sample_parameter_startpoints() |
| 774 | + # ML problem: no priors |
| 775 | + assert problem.get_priors() == {} |
| 776 | + |
| 777 | + problem["p3"].prior_distribution = C.LOG_NORMAL |
| 778 | + problem["p3"].prior_parameters = [0, 1] |
| 779 | + sp = problem.sample_parameter_startpoints(n_starts=10) |
| 780 | + assert sp.shape == (10, 3) |
| 781 | + assert np.all(sp[:, 2] > 0) |
| 782 | + |
| 783 | + # missing bounds (bypassing validation) are reported like infinite ones |
| 784 | + problem += Parameter.model_construct(id="p4", estimate=True) |
| 785 | + with pytest.raises(ValueError, match=r"missing bounds.*\['p4'\]"): |
| 786 | + problem.get_startpoint_distributions() |
| 787 | + |
| 788 | + |
| 789 | +def test_sample_startpoints_no_estimated_parameters(): |
| 790 | + """Test startpoint sampling without estimated parameters.""" |
| 791 | + problem = Problem() |
| 792 | + problem += Parameter(id="p1", estimate=False, nominal_value=1) |
| 793 | + assert problem.sample_parameter_startpoints(n_starts=5).shape == (5, 0) |
| 794 | + |
| 795 | + |
| 796 | +def test_get_priors_infinite_bounds(): |
| 797 | + """Test that infinite bounds without prior are rejected for MAP |
| 798 | + problems.""" |
| 799 | + problem = Problem() |
| 800 | + problem += Parameter(id="p1", estimate=True, lb=-np.inf, ub=np.inf) |
| 801 | + problem += Parameter( |
| 802 | + id="p2", |
| 803 | + estimate=True, |
| 804 | + lb=1, |
| 805 | + ub=2, |
| 806 | + prior_distribution=C.NORMAL, |
| 807 | + prior_parameters=[1.5, 1], |
| 808 | + ) |
| 809 | + assert problem.has_map_objective |
| 810 | + with pytest.raises(ValueError, match=r"missing bounds.*\['p1'\]"): |
| 811 | + problem.get_priors() |
| 812 | + |
| 813 | + problem["p1"].prior_distribution = C.NORMAL |
| 814 | + problem["p1"].prior_parameters = [0, 1] |
| 815 | + assert set(problem.get_priors()) == {"p1", "p2"} |
| 816 | + |
| 817 | + |
| 818 | +@pytest.mark.parametrize( |
| 819 | + "prior_distribution, prior_parameters, lb, ub", |
| 820 | + [ |
| 821 | + (C.NORMAL, [0, 1], -np.inf, np.inf), |
| 822 | + (C.LOG_NORMAL, [0, 1], 0, np.inf), |
| 823 | + (C.EXPONENTIAL, [2], 0, np.inf), |
| 824 | + (C.GAMMA, [2, 1], 0, np.inf), |
| 825 | + (C.CHI_SQUARED, [2], 0, np.inf), |
| 826 | + (C.RAYLEIGH, [1], 0, np.inf), |
| 827 | + (C.LOG_LAPLACE, [0, 1], 0, np.inf), |
| 828 | + (C.CAUCHY, [0, 1], -np.inf, np.inf), |
| 829 | + (C.LAPLACE, [0, 1], -np.inf, np.inf), |
| 830 | + ], |
| 831 | +) |
| 832 | +def test_prior_truncation_noop_at_domain( |
| 833 | + prior_distribution, prior_parameters, lb, ub |
| 834 | +): |
| 835 | + """Bounds matching the prior's domain do not truncate the prior.""" |
| 836 | + truncated = Parameter( |
| 837 | + id="p", |
| 838 | + lb=lb, |
| 839 | + ub=ub, |
| 840 | + prior_distribution=prior_distribution, |
| 841 | + prior_parameters=prior_parameters, |
| 842 | + ).prior_dist |
| 843 | + # reference: the same distribution without truncation |
| 844 | + untruncated = type(truncated)( |
| 845 | + *prior_parameters, |
| 846 | + **({"log": True} if prior_distribution.startswith("log-") else {}), |
| 847 | + ) |
| 848 | + assert untruncated._trunc is None |
| 849 | + |
| 850 | + x = np.array([0.1, 0.5, 1.0, 2.0, 5.0]) |
| 851 | + np.testing.assert_allclose(truncated.pdf(x), untruncated.pdf(x)) |
| 852 | + np.testing.assert_allclose(truncated.cdf(x), untruncated.cdf(x)) |
| 853 | + assert np.all(truncated.pdf(x) > 0) |
| 854 | + |
| 855 | + |
| 856 | +def test_prior_truncation_one_sided_infinite(): |
| 857 | + """A finite bound truncates the prior also if the other is infinite.""" |
| 858 | + truncated = Parameter( |
| 859 | + id="p", |
| 860 | + lb=1, |
| 861 | + ub=np.inf, |
| 862 | + prior_distribution=C.LOG_NORMAL, |
| 863 | + prior_parameters=[0, 1], |
| 864 | + ).prior_dist |
| 865 | + untruncated = Parameter( |
| 866 | + id="p", |
| 867 | + lb=0, |
| 868 | + ub=np.inf, |
| 869 | + prior_distribution=C.LOG_NORMAL, |
| 870 | + prior_parameters=[0, 1], |
| 871 | + ).prior_dist |
| 872 | + # median of LogNormal(0, 1) is 1, so truncation at 1 doubles the density |
| 873 | + x = np.array([1.5, 2.0, 5.0]) |
| 874 | + np.testing.assert_allclose(truncated.pdf(x), 2 * untruncated.pdf(x)) |
| 875 | + assert truncated.pdf(0.5) == 0 |
| 876 | + assert np.all(truncated.sample(100) >= 1) |
| 877 | + |
| 878 | + # mirrored: finite upper bound |
| 879 | + truncated = Parameter( |
| 880 | + id="p", |
| 881 | + lb=-np.inf, |
| 882 | + ub=0, |
| 883 | + prior_distribution=C.NORMAL, |
| 884 | + prior_parameters=[0, 1], |
| 885 | + ).prior_dist |
| 886 | + untruncated = Normal(0, 1) |
| 887 | + x = np.array([-2.0, -1.0, -0.5]) |
| 888 | + np.testing.assert_allclose(truncated.pdf(x), 2 * untruncated.pdf(x)) |
| 889 | + assert truncated.pdf(0.5) == 0 |
| 890 | + assert np.all(truncated.sample(100) <= 0) |
| 891 | + |
| 892 | + |
744 | 893 | def test_problem_config_paths(): |
745 | 894 | """Test handling of URLS and local paths in ProblemConfig.""" |
746 | 895 |
|
|
0 commit comments