Skip to content

Commit 90946f3

Browse files
committed
Implement proper truncation for prior distributions
Currently, when sampled startpoints are outside the bounds, their value is set to the upper/lower bounds. This may put too much probability mass on the bounds. With these changes, we properly sample from the respective truncated distributions.
1 parent a272255 commit 90946f3

File tree

2 files changed

+238
-78
lines changed

2 files changed

+238
-78
lines changed

doc/example/distributions.ipynb

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
" if ax is None:\n",
4343
" fig, ax = plt.subplots()\n",
4444
"\n",
45-
" sample = distr.sample(10000)\n",
45+
" sample = distr.sample(20_000)\n",
4646
"\n",
4747
" # pdf\n",
4848
" xmin = min(sample.min(), distr.lb_scaled if distr.bounds is not None else sample.min())\n",
@@ -102,9 +102,8 @@
102102
"cell_type": "code",
103103
"source": [
104104
"plot(Normal(10, 2, transformation=LIN))\n",
105-
"plot(Normal(10, 2, transformation=LOG))\n",
106-
"# Note that the log-normal is different from the log-transformed normal distribution:\n",
107-
"plot(LogNormal(10, 2, transformation=LIN))"
105+
"# Note that log-transformed normal distribution is different from the log-normal distribution\n",
106+
"plot(Normal(10, 2, transformation=LOG))"
108107
],
109108
"id": "f6192c226f179ef9",
110109
"outputs": [],
@@ -120,11 +119,13 @@
120119
"metadata": {},
121120
"cell_type": "code",
122121
"source": [
123-
"plot(Uniform(0, 1, transformation=LOG10))\n",
124-
"plot(ParameterScaleUniform(0, 1, transformation=LOG10))\n",
122+
"# different, because transformation!=LIN\n",
123+
"plot(Uniform(1e-16, 1, transformation=LOG10))\n",
124+
"plot(ParameterScaleUniform(1e-16, 1, transformation=LOG10))\n",
125125
"\n",
126-
"plot(Uniform(0, 1, transformation=LIN))\n",
127-
"plot(ParameterScaleUniform(0, 1, transformation=LIN))\n"
126+
"# same, because transformation=LIN\n",
127+
"plot(Uniform(1e-16, 1, transformation=LIN))\n",
128+
"plot(ParameterScaleUniform(1e-16, 1, transformation=LIN))\n"
128129
],
129130
"id": "5ca940bc24312fc6",
130131
"outputs": [],
@@ -133,15 +134,18 @@
133134
{
134135
"metadata": {},
135136
"cell_type": "markdown",
136-
"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):",
137+
"source": "The given distributions are truncated at the bounds defined in the parameter table:",
137138
"id": "b1a8b17d765db826"
138139
},
139140
{
140141
"metadata": {},
141142
"cell_type": "code",
142143
"source": [
143-
"plot(Normal(0, 1, bounds=(-4, 4))) # negligible clipping-bias at 4 sigma\n",
144-
"plot(Uniform(0, 1, bounds=(0.1, 0.9))) # significant clipping-bias"
144+
"plot(Normal(0, 1, bounds=(-2, 2)))\n",
145+
"plot(Uniform(0, 1, bounds=(0.1, 0.9)))\n",
146+
"plot(Uniform(1e-8, 1, bounds=(0.1, 0.9), transformation=LOG10))\n",
147+
"plot(Laplace(0, 1, bounds=(-0.5, 0.5)))\n",
148+
"plot(ParameterScaleUniform(-3, 1, bounds=(1e-2, 1), transformation=LOG10))\n"
145149
],
146150
"id": "4ac42b1eed759bdd",
147151
"outputs": [],
@@ -156,21 +160,34 @@
156160
{
157161
"metadata": {},
158162
"cell_type": "code",
159-
"source": [
160-
"plot(Normal(10, 1, bounds=(6, 14), transformation=\"log10\"))\n",
161-
"plot(ParameterScaleNormal(10, 1, bounds=(10**6, 10**14), transformation=\"log10\"))\n"
162-
],
163+
"source": "plot(Normal(10, 1, bounds=(6, 14), transformation=\"log10\"))",
163164
"id": "581e1ac431860419",
164165
"outputs": [],
165166
"execution_count": null
166167
},
167168
{
168169
"metadata": {},
169170
"cell_type": "code",
170-
"source": "",
171+
"source": "plot(ParameterScaleNormal(10, 1, bounds=(10**6, 10**14), transformation=\"log10\"))",
172+
"id": "99202ecb47706a68",
173+
"outputs": [],
174+
"execution_count": null
175+
},
176+
{
177+
"metadata": {},
178+
"cell_type": "code",
179+
"source": "plot(LogLaplace(1, 0.5, bounds=(0.5, 8)))",
171180
"id": "802a64be56a6c94f",
172181
"outputs": [],
173182
"execution_count": null
183+
},
184+
{
185+
"metadata": {},
186+
"cell_type": "code",
187+
"source": "plot(LogNormal(2, 1, bounds=(0.5, 8)))",
188+
"id": "7820e93ab9b2fb47",
189+
"outputs": [],
190+
"execution_count": null
174191
}
175192
],
176193
"metadata": {

0 commit comments

Comments
 (0)