Unfortuantely, *at the moment* (but this will change soon) you have to make use of the underlying `step`

interface to be able to resume a chain.

```
julia> using Turing, Random
julia> @model function gdemo(xs)
# Assumptions
ĻĀ² ~ InverseGamma(2, 3)
Ī¼ ~ Normal(0, āĻĀ²)
# Observations
for i = 1:length(xs)
xs[i] ~ Normal(Ī¼, āĻĀ²)
end
end
gdemo (generic function with 2 methods)
julia> # Set up.
xs = randn(100);
julia> model = gdemo(xs);
julia> # Sampler.
alg = NUTS(0.65);
julia> kwargs = (nadapts=50,);
julia> num_samples = 100;
julia> ### The following two methods are equivalent ###
## Using `sample` ##
rng = MersenneTwister(42);
julia> chain = sample(rng, model, alg, num_samples; kwargs...)
ā Info: Found initial step size
ā Ļµ = 0.4
Sampling 100%|āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā| Time: 0:00:00
Chains MCMC chain (100Ć14Ć1 Array{Float64, 3}):
Iterations = 51:1:150
Number of chains = 1
Samples per chain = 100
Wall duration = 1.12 seconds
Compute duration = 1.12 seconds
parameters = ĻĀ², Ī¼
internals = lp, n_steps, is_accept, acceptance_rate, log_density, hamiltonian_energy, hamiltonian_energy_error, max_hamiltonian_energy_error, tree_depth, numerical_error, step_size, nom_step_size
Summary Statistics
parameters mean std mcse ess_bulk ess_tail rhat ess_per_sec
Symbol Float64 Float64 Float64 Float64 Float64 Float64 Float64
ĻĀ² 1.1090 0.1728 0.0591 10.8467 51.5563 1.1136 9.6587
Ī¼ -0.1753 0.1030 0.0126 66.7404 78.3393 0.9940 59.4304
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
ĻĀ² 0.8499 0.9980 1.1052 1.2155 1.4554
Ī¼ -0.3526 -0.2485 -0.1774 -0.1074 0.0239
julia> ## Using the iterator-interface ##
rng = MersenneTwister(42);
julia> spl = DynamicPPL.Sampler(alg);
julia> nadapts = 50;
julia> # Create an iterator we can just step through.
it = AbstractMCMC.Stepper(rng, model, spl, kwargs);
julia> # Initial sample and state.
transition, state = iterate(it);
ā Info: Found initial step size
ā Ļµ = 0.4
julia> # Simple container to hold the samples.
transitions = [];
julia> # Simple condition that says we only want `num_samples` samples.
condition(spls) = length(spls) < num_samples
condition (generic function with 1 method)
julia> # Sample until `condition` is no longer satisfied
while condition(transitions)
# For an iterator we pass in the previous `state` as the second argument
transition, state = iterate(it, state)
# Save `transition` if we're not adapting anymore
if state.i > nadapts
push!(transitions, transition)
end
end
julia> length(transitions), state.i, state.i == length(transitions) + nadapts
(100, 150, true)
julia> # Finally, if you want to convert the vector of `transitions` into a
# `MCMCChains.Chains` like is typically done:
chain = AbstractMCMC.bundle_samples(
map(identity, transitions), # trick to concretize the eltype of `transitions`
model,
spl,
state,
MCMCChains.Chains
)
Chains MCMC chain (100Ć14Ć1 Array{Float64, 3}):
Iterations = 1:1:100
Number of chains = 1
Samples per chain = 100
parameters = ĻĀ², Ī¼
internals = lp, n_steps, is_accept, acceptance_rate, log_density, hamiltonian_energy, hamiltonian_energy_error, max_hamiltonian_energy_error, tree_depth, numerical_error, step_size, nom_step_size
Summary Statistics
parameters mean std mcse ess_bulk ess_tail rhat ess_per_sec
Symbol Float64 Float64 Float64 Float64 Float64 Float64 Missing
ĻĀ² 1.1090 0.1728 0.0591 10.8467 51.5563 1.1136 missing
Ī¼ -0.1753 0.1030 0.0126 66.7404 78.3393 0.9940 missing
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
ĻĀ² 0.8499 0.9980 1.1052 1.2155 1.4554
Ī¼ -0.3526 -0.2485 -0.1774 -0.1074 0.0239
```

(this example is from Simple example of using NUTS with the new iterator interface in AbstractMCMC.jl available using Turing.jl > 0.15. Ā· GitHub)

In here you can just serialize the `state`

, and then when you want continue from a given `state`

, you can just load the `state`

from disk, skip the first line of calling `iterate(it)`

and instead use the `state`

you just loaded:)