|
1 | 1 | { |
2 | 2 | "cells": [ |
3 | 3 | { |
4 | | - "metadata": {}, |
5 | 4 | "cell_type": "markdown", |
| 5 | + "id": "372289411a2aa7b3", |
| 6 | + "metadata": {}, |
6 | 7 | "source": [ |
7 | 8 | "# Prior distributions in PEtab\n", |
8 | 9 | "\n", |
|
18 | 19 | "* *Initialization priors* can be used as a hint for the optimization algorithm. They will not enter the objective function. They are specified in the `initializationPriorType` and `initializationPriorParameters` columns of the parameter table.\n", |
19 | 20 | "\n", |
20 | 21 | "\n" |
21 | | - ], |
22 | | - "id": "372289411a2aa7b3" |
| 22 | + ] |
23 | 23 | }, |
24 | 24 | { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "id": "initial_id", |
25 | 28 | "metadata": { |
26 | 29 | "collapsed": true |
27 | 30 | }, |
28 | | - "cell_type": "code", |
| 31 | + "outputs": [], |
29 | 32 | "source": [ |
30 | 33 | "import matplotlib.pyplot as plt\n", |
31 | 34 | "import numpy as np\n", |
|
45 | 48 | " sample = prior.sample(10000)\n", |
46 | 49 | "\n", |
47 | 50 | " # pdf\n", |
48 | | - " xmin = min(sample.min(), prior.lb_scaled if prior.bounds is not None else sample.min())\n", |
49 | | - " xmax = max(sample.max(), prior.ub_scaled if prior.bounds is not None else sample.max())\n", |
| 51 | + " xmin = min(\n", |
| 52 | + " sample.min(),\n", |
| 53 | + " prior.lb_scaled if prior.bounds is not None else sample.min(),\n", |
| 54 | + " )\n", |
| 55 | + " xmax = max(\n", |
| 56 | + " sample.max(),\n", |
| 57 | + " prior.ub_scaled if prior.bounds is not None else sample.max(),\n", |
| 58 | + " )\n", |
50 | 59 | " x = np.linspace(xmin, xmax, 500)\n", |
51 | 60 | " y = prior.pdf(x)\n", |
52 | | - " ax.plot(x, y, color='red', label='pdf')\n", |
| 61 | + " ax.plot(x, y, color=\"red\", label=\"pdf\")\n", |
53 | 62 | "\n", |
54 | | - " sns.histplot(sample, stat='density', ax=ax, label=\"sample\")\n", |
| 63 | + " sns.histplot(sample, stat=\"density\", ax=ax, label=\"sample\")\n", |
55 | 64 | "\n", |
56 | 65 | " # bounds\n", |
57 | 66 | " if prior.bounds is not None:\n", |
58 | 67 | " for bound in (prior.lb_scaled, prior.ub_scaled):\n", |
59 | 68 | " if bound is not None and np.isfinite(bound):\n", |
60 | | - " ax.axvline(bound, color='black', linestyle='--', label='bound')\n", |
| 69 | + " ax.axvline(bound, color=\"black\", linestyle=\"--\", label=\"bound\")\n", |
61 | 70 | "\n", |
62 | 71 | " ax.set_title(str(prior))\n", |
63 | | - " ax.set_xlabel('Parameter value on the parameter scale')\n", |
| 72 | + " ax.set_xlabel(\"Parameter value on the parameter scale\")\n", |
64 | 73 | " ax.grid(False)\n", |
65 | 74 | " handles, labels = ax.get_legend_handles_labels()\n", |
66 | | - " unique_labels = dict(zip(labels, handles))\n", |
| 75 | + " unique_labels = dict(zip(labels, handles, strict=False))\n", |
67 | 76 | " ax.legend(unique_labels.values(), unique_labels.keys())\n", |
68 | 77 | " plt.show()" |
69 | | - ], |
70 | | - "id": "initial_id", |
71 | | - "outputs": [], |
72 | | - "execution_count": null |
| 78 | + ] |
73 | 79 | }, |
74 | 80 | { |
75 | | - "metadata": {}, |
76 | 81 | "cell_type": "markdown", |
77 | | - "source": "The basic distributions are the uniform, normal, Laplace, log-normal, and log-laplace distributions:\n", |
78 | | - "id": "db36a4a93622ccb8" |
| 82 | + "id": "db36a4a93622ccb8", |
| 83 | + "metadata": {}, |
| 84 | + "source": "The basic distributions are the uniform, normal, Laplace, log-normal, and log-laplace distributions:\n" |
79 | 85 | }, |
80 | 86 | { |
81 | | - "metadata": {}, |
82 | 87 | "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "id": "4f09e50a3db06d9f", |
| 90 | + "metadata": {}, |
| 91 | + "outputs": [], |
83 | 92 | "source": [ |
84 | 93 | "plot(Prior(UNIFORM, (0, 1)))\n", |
85 | 94 | "plot(Prior(NORMAL, (0, 1)))\n", |
86 | 95 | "plot(Prior(LAPLACE, (0, 1)))\n", |
87 | 96 | "plot(Prior(LOG_NORMAL, (0, 1)))\n", |
88 | 97 | "plot(Prior(LOG_LAPLACE, (1, 0.5)))" |
89 | | - ], |
90 | | - "id": "4f09e50a3db06d9f", |
91 | | - "outputs": [], |
92 | | - "execution_count": null |
| 98 | + ] |
93 | 99 | }, |
94 | 100 | { |
95 | | - "metadata": {}, |
96 | 101 | "cell_type": "markdown", |
97 | | - "source": "If a parameter scale is specified (`parameterScale=lin|log|log10` not a `parameterScale*`-type distribution), the sample is transformed accordingly (but not the distribution parameters):\n", |
98 | | - "id": "dab4b2d1e0f312d8" |
| 102 | + "id": "dab4b2d1e0f312d8", |
| 103 | + "metadata": {}, |
| 104 | + "source": "If a parameter scale is specified (`parameterScale=lin|log|log10` not a `parameterScale*`-type distribution), the sample is transformed accordingly (but not the distribution parameters):\n" |
99 | 105 | }, |
100 | 106 | { |
101 | | - "metadata": {}, |
102 | 107 | "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "id": "f6192c226f179ef9", |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [], |
103 | 112 | "source": [ |
104 | 113 | "plot(Prior(NORMAL, (10, 2), transformation=LIN))\n", |
105 | 114 | "plot(Prior(NORMAL, (10, 2), transformation=LOG))\n", |
106 | 115 | "\n", |
107 | | - "# Note that the log-normal distribution is different from a log-transformed normal distribution:\n", |
| 116 | + "# Note that the log-normal distribution is different\n", |
| 117 | + "# from a log-transformed normal distribution:\n", |
108 | 118 | "plot(Prior(LOG_NORMAL, (10, 2), transformation=LIN))" |
109 | | - ], |
110 | | - "id": "f6192c226f179ef9", |
111 | | - "outputs": [], |
112 | | - "execution_count": null |
| 119 | + ] |
113 | 120 | }, |
114 | 121 | { |
115 | | - "metadata": {}, |
116 | 122 | "cell_type": "markdown", |
117 | | - "source": "On the log-transformed parameter scale, `Log*` and `parameterScale*` distributions are equivalent:", |
118 | | - "id": "4281ed48859e6431" |
| 123 | + "id": "4281ed48859e6431", |
| 124 | + "metadata": {}, |
| 125 | + "source": "On the log-transformed parameter scale, `Log*` and `parameterScale*` distributions are equivalent:" |
119 | 126 | }, |
120 | 127 | { |
121 | | - "metadata": {}, |
122 | 128 | "cell_type": "code", |
| 129 | + "execution_count": null, |
| 130 | + "id": "34c95268e8921070", |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
123 | 133 | "source": [ |
124 | 134 | "plot(Prior(LOG_NORMAL, (10, 2), transformation=LOG))\n", |
125 | 135 | "plot(Prior(PARAMETER_SCALE_NORMAL, (10, 2)))" |
126 | | - ], |
127 | | - "id": "34c95268e8921070", |
128 | | - "outputs": [], |
129 | | - "execution_count": null |
| 136 | + ] |
130 | 137 | }, |
131 | 138 | { |
132 | | - "metadata": {}, |
133 | 139 | "cell_type": "markdown", |
134 | | - "source": "Prior distributions can also be defined on the parameter scale by using the types `parameterScaleUniform`, `parameterScaleNormal` or `parameterScaleLaplace`. In these cases, 1) the distribution parameter are interpreted on the transformed parameter scale, and 2) a sample from the given distribution is used directly, without applying any transformation according to `parameterScale` (this implies, that for `parameterScale=lin`, there is no difference between `parameterScaleUniform` and `uniform`):", |
135 | | - "id": "263c9fd31156a4d5" |
| 140 | + "id": "263c9fd31156a4d5", |
| 141 | + "metadata": {}, |
| 142 | + "source": "Prior distributions can also be defined on the parameter scale by using the types `parameterScaleUniform`, `parameterScaleNormal` or `parameterScaleLaplace`. In these cases, 1) the distribution parameter are interpreted on the transformed parameter scale, and 2) a sample from the given distribution is used directly, without applying any transformation according to `parameterScale` (this implies, that for `parameterScale=lin`, there is no difference between `parameterScaleUniform` and `uniform`):" |
136 | 143 | }, |
137 | 144 | { |
138 | | - "metadata": {}, |
139 | 145 | "cell_type": "code", |
| 146 | + "execution_count": null, |
| 147 | + "id": "5ca940bc24312fc6", |
| 148 | + "metadata": {}, |
| 149 | + "outputs": [], |
140 | 150 | "source": [ |
141 | 151 | "plot(Prior(UNIFORM, (0.01, 2), transformation=LOG10))\n", |
142 | 152 | "plot(Prior(PARAMETER_SCALE_UNIFORM, (0.01, 2), transformation=LOG10))\n", |
143 | 153 | "\n", |
144 | 154 | "plot(Prior(UNIFORM, (0.01, 2), transformation=LIN))\n", |
145 | | - "plot(Prior(PARAMETER_SCALE_UNIFORM, (0.01, 2), transformation=LIN))\n" |
146 | | - ], |
147 | | - "id": "5ca940bc24312fc6", |
148 | | - "outputs": [], |
149 | | - "execution_count": null |
| 155 | + "plot(Prior(PARAMETER_SCALE_UNIFORM, (0.01, 2), transformation=LIN))" |
| 156 | + ] |
150 | 157 | }, |
151 | 158 | { |
152 | | - "metadata": {}, |
153 | 159 | "cell_type": "markdown", |
154 | | - "source": "To prevent the sampled parameters from exceeding the bounds, the sampled parameters are clipped to the bounds. The bounds are defined in the parameter table. Note that the current implementation does not support sampling from a truncated distribution. Instead, the samples are clipped to the bounds. This may introduce unwanted bias, and thus, should only be used with caution (i.e., the bounds should be chosen wide enough):", |
155 | | - "id": "b1a8b17d765db826" |
| 160 | + "id": "b1a8b17d765db826", |
| 161 | + "metadata": {}, |
| 162 | + "source": "To prevent the sampled parameters from exceeding the bounds, the sampled parameters are clipped to the bounds. The bounds are defined in the parameter table. Note that the current implementation does not support sampling from a truncated distribution. Instead, the samples are clipped to the bounds. This may introduce unwanted bias, and thus, should only be used with caution (i.e., the bounds should be chosen wide enough):" |
156 | 163 | }, |
157 | 164 | { |
158 | | - "metadata": {}, |
159 | 165 | "cell_type": "code", |
160 | | - "source": [ |
161 | | - "plot(Prior(NORMAL, (0, 1), bounds=(-4, 4))) # negligible clipping-bias at 4 sigma\n", |
162 | | - "plot(Prior(UNIFORM, (0, 1), bounds=(0.1, 0.9))) # significant clipping-bias" |
163 | | - ], |
| 166 | + "execution_count": null, |
164 | 167 | "id": "4ac42b1eed759bdd", |
| 168 | + "metadata": {}, |
165 | 169 | "outputs": [], |
166 | | - "execution_count": null |
| 170 | + "source": [ |
| 171 | + "plot(\n", |
| 172 | + " Prior(NORMAL, (0, 1), bounds=(-4, 4))\n", |
| 173 | + ") # negligible clipping-bias at 4 sigma\n", |
| 174 | + "plot(Prior(UNIFORM, (0, 1), bounds=(0.1, 0.9))) # significant clipping-bias" |
| 175 | + ] |
167 | 176 | }, |
168 | 177 | { |
169 | | - "metadata": {}, |
170 | 178 | "cell_type": "markdown", |
171 | | - "source": "Further distribution examples:", |
172 | | - "id": "45ffce1341483f24" |
| 179 | + "id": "45ffce1341483f24", |
| 180 | + "metadata": {}, |
| 181 | + "source": "Further distribution examples:" |
173 | 182 | }, |
174 | 183 | { |
175 | | - "metadata": {}, |
176 | 184 | "cell_type": "code", |
| 185 | + "execution_count": null, |
| 186 | + "id": "581e1ac431860419", |
| 187 | + "metadata": {}, |
| 188 | + "outputs": [], |
177 | 189 | "source": [ |
178 | 190 | "plot(Prior(NORMAL, (10, 1), bounds=(6, 14), transformation=\"log10\"))\n", |
179 | | - "plot(Prior(PARAMETER_SCALE_NORMAL, (10, 1), bounds=(10**6, 10**14), transformation=\"log10\"))\n", |
| 191 | + "plot(\n", |
| 192 | + " Prior(\n", |
| 193 | + " PARAMETER_SCALE_NORMAL,\n", |
| 194 | + " (10, 1),\n", |
| 195 | + " bounds=(10**6, 10**14),\n", |
| 196 | + " transformation=\"log10\",\n", |
| 197 | + " )\n", |
| 198 | + ")\n", |
180 | 199 | "plot(Prior(LAPLACE, (10, 2), bounds=(6, 14)))" |
181 | | - ], |
182 | | - "id": "581e1ac431860419", |
183 | | - "outputs": [], |
184 | | - "execution_count": null |
| 200 | + ] |
185 | 201 | } |
186 | 202 | ], |
187 | 203 | "metadata": { |
|
0 commit comments