Skip to content

Commit c974b8c

Browse files
authored
Unbounded priors (#103)
* Add test for unbounded priors * Update test case 0024 description
1 parent bce739a commit c974b8c

File tree

11 files changed

+185
-2
lines changed

11 files changed

+185
-2
lines changed

petabtests/cases/v2.0.0/sbml/0024/0024.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585

8686
case = PetabV2TestCase.from_problem(
8787
id=24,
88-
brief="Prior distributions.",
88+
brief="Truncated prior distributions.",
8989
description=DESCRIPTION,
9090
model=sbml_file,
9191
problem=problem,
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from inspect import cleandoc
2+
from numpy import inf
3+
4+
from petab.v2.C import *
5+
from petab.v2 import Problem
6+
from petabtests import (
7+
PetabV2TestCase,
8+
antimony_to_sbml_str,
9+
)
10+
from pathlib import Path
11+
from petab.v2 import PriorDistribution
12+
13+
DESCRIPTION = cleandoc(r"""
14+
## Objective
15+
16+
This case tests different non-truncated prior distributions, as well as
17+
implicit uniform priors and fixed parameters.
18+
19+
## Model
20+
21+
A simple model with all constant parameters.
22+
""")
23+
24+
# problem --------------------------------------------------------------------
25+
26+
priors = [
27+
(PriorDistribution.UNIFORM, (2, 8), 2, 8),
28+
(PriorDistribution.NORMAL, (4, 2), -inf, inf),
29+
(PriorDistribution.LOG_NORMAL, (5, 2), 0.0, inf),
30+
(PriorDistribution.CAUCHY, (3, 5), -inf, inf),
31+
(PriorDistribution.CHI_SQUARED, (4), 0.0, inf),
32+
(PriorDistribution.EXPONENTIAL, (3), 0.0, inf),
33+
(PriorDistribution.GAMMA, (3, 5), 0, inf),
34+
(PriorDistribution.LAPLACE, (3, 5), -inf, inf),
35+
(PriorDistribution.LOG_LAPLACE, (3, 5), 0, inf),
36+
(PriorDistribution.LOG_UNIFORM, (3, 5), 3, 5),
37+
(PriorDistribution.RAYLEIGH, (3), 0, inf),
38+
]
39+
40+
tested_prior_distrs = {pd for pd, _, _, _ in priors}
41+
untested_distrs = [
42+
pd.value for pd in PriorDistribution if pd not in tested_prior_distrs
43+
]
44+
if untested_distrs:
45+
print("Untested prior distributions:", untested_distrs)
46+
47+
sbml_file = Path(__file__).parent / "_model.xml"
48+
49+
parameters = "\n".join(
50+
f"p_{prior_type.value.replace('-', '_')} = 5;" for prior_type, _, _, _ in priors
51+
)
52+
ant_model = f"""
53+
model petab_test_0025
54+
{parameters}
55+
end
56+
"""
57+
sbml_file.write_text(antimony_to_sbml_str(ant_model))
58+
59+
problem = Problem()
60+
for prior_type, prior_pars, support_lb, support_ub in priors:
61+
problem.add_parameter(
62+
f"p_{prior_type.value.replace('-', '_')}",
63+
estimate=True,
64+
lb=support_lb,
65+
ub=support_ub,
66+
nominal_value=5,
67+
prior_distribution=prior_type,
68+
prior_parameters=prior_pars,
69+
)
70+
# implicit uniform prior
71+
problem.add_parameter("p1", estimate=True, nominal_value=1, lb=0, ub=2)
72+
# fixed, i.e., no prior
73+
problem.add_parameter("p_fixed", estimate=False, nominal_value=1)
74+
# we need some observable and measurement
75+
problem.add_observable("obs_p1", "p1", noise_formula="p_fixed")
76+
problem.add_measurement("obs_p1", experiment_id="", time=0, measurement=1)
77+
78+
# solutions ------------------------------------------------------------------
79+
80+
simulation_df = problem.measurement_df.copy(deep=True).rename(
81+
columns={MEASUREMENT: SIMULATION}
82+
)
83+
simulation_df[SIMULATION] = [
84+
1,
85+
]
86+
87+
case = PetabV2TestCase.from_problem(
88+
id=25,
89+
brief="Non-truncated prior distributions.",
90+
description=DESCRIPTION,
91+
model=sbml_file,
92+
problem=problem,
93+
simulation_df=simulation_df,
94+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# PEtab test case 0025
2+
3+
## Objective
4+
5+
This case tests different non-truncated prior distributions, as well as
6+
implicit uniform priors and fixed parameters.
7+
8+
## Model
9+
10+
A simple model with all constant parameters.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
condition_files: []
2+
experiment_files: []
3+
format_version: 2.0.0
4+
measurement_files:
5+
- _measurements.tsv
6+
model_files:
7+
model_0:
8+
language: sbml
9+
location: _model.xml
10+
observable_files:
11+
- _observables.tsv
12+
parameter_files:
13+
- _parameters.tsv
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
chi2: 0.0
2+
llh: -0.91893853320467
3+
log_prior:
4+
p1: -0.69314718055995
5+
p_cauchy: -2.90258780340177
6+
p_chisquare: -2.27685644868579
7+
p_exponential: -2.76527895533478
8+
p_gamma: -3.30258509299405
9+
p_laplace: -2.70258509299405
10+
p_log_laplace: -4.19013542294133
11+
p_log_normal: -4.65851253490362
12+
p_log_uniform: -0.93771092034198
13+
p_normal: -1.73708571376462
14+
p_rayleigh: -1.97667555379101
15+
p_uniform: -1.79175946922805
16+
simulation_files:
17+
- _simulations.tsv
18+
tol_chi2: 0.001
19+
tol_llh: 0.001
20+
tol_log_prior: 1.0e-14
21+
tol_simulations: 0.001
22+
tol_unnorm_log_posterior: 0.001
23+
unnorm_log_posterior: -30.85385872214566
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
modelId observableId experimentId time measurement observableParameters noiseParameters
2+
obs_p1 0.0 1.0
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Created by libAntimony version v3.1.1 with libSBML version 5.20.5. -->
3+
<sbml xmlns="http://www.sbml.org/sbml/level3/version2/core" level="3" version="2">
4+
<model metaid="petab_test_0025" id="petab_test_0025">
5+
<listOfParameters>
6+
<parameter id="p_uniform" value="5" constant="true"/>
7+
<parameter id="p_normal" value="5" constant="true"/>
8+
<parameter id="p_log_normal" value="5" constant="true"/>
9+
<parameter id="p_cauchy" value="5" constant="true"/>
10+
<parameter id="p_chisquare" value="5" constant="true"/>
11+
<parameter id="p_exponential" value="5" constant="true"/>
12+
<parameter id="p_gamma" value="5" constant="true"/>
13+
<parameter id="p_laplace" value="5" constant="true"/>
14+
<parameter id="p_log_laplace" value="5" constant="true"/>
15+
<parameter id="p_log_uniform" value="5" constant="true"/>
16+
<parameter id="p_rayleigh" value="5" constant="true"/>
17+
</listOfParameters>
18+
</model>
19+
</sbml>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
observableId observableName observableFormula noiseFormula noiseDistribution observablePlaceholders noisePlaceholders
2+
obs_p1 p1 p_fixed normal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
parameterId lowerBound upperBound nominalValue estimate priorDistribution priorParameters
2+
p_uniform 2.0 8.0 5.0 true uniform 2.0;8.0
3+
p_normal -inf inf 5.0 true normal 4.0;2.0
4+
p_log_normal 0.0 inf 5.0 true log-normal 5.0;2.0
5+
p_cauchy -inf inf 5.0 true cauchy 3.0;5.0
6+
p_chisquare 0.0 inf 5.0 true chisquare 4.0
7+
p_exponential 0.0 inf 5.0 true exponential 3.0
8+
p_gamma 0.0 inf 5.0 true gamma 3.0;5.0
9+
p_laplace -inf inf 5.0 true laplace 3.0;5.0
10+
p_log_laplace 0.0 inf 5.0 true log-laplace 3.0;5.0
11+
p_log_uniform 3.0 5.0 5.0 true log-uniform 3.0;5.0
12+
p_rayleigh 0.0 inf 5.0 true rayleigh 3.0
13+
p1 0.0 2.0 1.0 true
14+
p_fixed 1.0 false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
modelId observableId experimentId time simulation observableParameters noiseParameters
2+
obs_p1 0.0 1

0 commit comments

Comments
 (0)