You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
println(" actual mean: $(mean(samples_untransformed))")
249
249
```
250
250
251
+
On top of that, we can also verify that we don't ever go out of bounds:
252
+
253
+
```{julia}
254
+
println("went out of bounds $n_oob_transformed/10000 times")
255
+
```
256
+
251
257
### Which one is better?
252
258
253
259
In the subsections above, we've seen two different methods of sampling from a constrained distribution:
@@ -257,9 +263,16 @@ In the subsections above, we've seen two different methods of sampling from a co
257
263
258
264
(Note that both of these methods are applicable to other samplers as well, such as Hamiltonian Monte Carlo.)
259
265
260
-
Of course, the question then becomes which one of these is better.
261
-
We might look at the sample means above to see which one is 'closer' to the expected mean, but that's not a very robust method because the sample mean is itself random.
262
-
What we could do is to perform both methods many times and see how reliable the sample mean is.
266
+
Of course, a natural question to then ask is which one of these is better!
267
+
268
+
One option might be look at the sample means above to see which one is 'closer' to the expected mean.
269
+
However, that's not a very robust method because the sample mean is itself random, and if we were to use a different random seed we might well reach a different conclusion.
270
+
271
+
Another possibility we could look at the number of times the sample was rejected.
272
+
Does a lower rejection rate (as in the transformed case) imply that the method is better?
273
+
As it happens, this might seem like an intuitive conclusion, but it's not necessarily the case: for example, the sampling in unconstrained space could be much less efficient, such that even though we're not _rejecting_ samples, the ones that we do get are overly correlated and thus not representative of the distribution.
274
+
275
+
A robust comparison would involve performing both methods many times and seeing how _reliable_ the sample mean is.
We can see from this small study that although both methods give us the correct mean (on average), the method with the transformation is more reliable, in that the variance is much lower!
288
301
302
+
::: {.callout-note}
303
+
Alternatively, we could also try to directly measure how correlated the samples are.
304
+
One way to do this is to calculate the _effective sample size_ (ESS), which is described in [the Stan documentation](https://mc-stan.org/docs/reference-manual/analysis.html#effective-sample-size.section), and implemented in [MCMCChains.jl](https://github.com/TuringLang/MCMCChains.jl/).
305
+
A larger ESS implies that the samples are less correlated, and thus more representative of the underlying distribution:
306
+
307
+
```{julia}
308
+
using MCMCChains: Chains, ess
309
+
310
+
rejection = first(mh(logp, 10000, x -> x > 0))
311
+
transformation = f_inv(first(mh(logq, 10000, x -> true)))
0 commit comments