Skip to content

Commit 57868da

Browse files
committed
Add section on reproducibility
1 parent 3f5aeb0 commit 57868da

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

usage/sampling-options/index.qmd

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ Turing.setprogress!(false); # or true
5757

5858
For NUTS in particular, you can also specify `verbose=false` to disable the "Found initial step size" info message.
5959

60+
## Ensuring sampling reproducibility
61+
62+
Like many other Julia functions, a `Random.AbstractRNG` object can be passed as the first argument to `sample()` to ensure reproducibility of results.
63+
64+
```{julia}
65+
using Random
66+
chn1 = sample(Xoshiro(468), demo_model(), MH(), 5);
67+
chn2 = sample(Xoshiro(468), demo_model(), MH(), 5);
68+
(chn1[:x] == chn2[:x], chn1[:y] == chn2[:y])
69+
```
70+
71+
Alternatively, you can set the global RNG using `Random.seed!()`, although we recommend this less as it modifies global state.
72+
73+
```{julia}
74+
Random.seed!(468)
75+
chn3 = sample(demo_model(), MH(), 5);
76+
Random.seed!(468)
77+
chn4 = sample(demo_model(), MH(), 5);
78+
(chn3[:x] == chn4[:x], chn3[:y] == chn4[:y])
79+
```
80+
81+
::: {.callout-note}
82+
The outputs of pseudorandom number generators in the standard `Random` library are not guaranteed to be the same across different Julia versions or platforms.
83+
If you require absolute reproducibility, you should use [the StableRNGs.jl package](https://github.com/JuliaRandom/StableRNGs.jl).
84+
:::
85+
6086
## Switching the output type
6187

6288
By default, the results of MCMC sampling are bundled up in an `MCMCChains.Chains` object.
@@ -145,7 +171,6 @@ This is useful to, for example, perform sampling in batches, or to inspect inter
145171
Firstly, the previous chain _must_ have been run using the `save_state=true` argument.
146172

147173
```{julia}
148-
using Random
149174
rng = Xoshiro(468)
150175
151176
chn1 = sample(rng, demo_model(), MH(), 5; save_state=true);

0 commit comments

Comments
 (0)