Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
keywords = ["markov chain monte carlo", "probabilistic programming"]
license = "MIT"
desc = "A lightweight interface for common MCMC methods."
version = "5.17.0"
version = "5.17.1"

[deps]
BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66"
Expand Down
5 changes: 3 additions & 2 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ Common keyword arguments for regular and parallel sampling are:
- `discard_initial` (default: `num_warmup`): number of initial samples that are discarded. Note that
if `discard_initial < num_warmup`, warm-up samples will also be included in the resulting samples.
- `thinning` (default: `1`): factor by which to thin samples.
- `initial_state` (default: `nothing`): if `initial_state !== nothing`, the first call to [`AbstractMCMC.step`](@ref)
is passed `initial_state` as the `state` argument.
- `initial_state` (default: `nothing`): if `initial_state !== nothing`, sampling resumes from `initial_state`:
the first call to [`AbstractMCMC.step_warmup`](@ref), or to [`AbstractMCMC.step`](@ref) if `num_warmup == 0`,
is passed `initial_state` as the `state` argument, and no initialisation step is taken.

!!! info
The common keyword arguments `progress`, `chain_type`, and `callback` are not supported by the iterator [`AbstractMCMC.steps`](@ref).
Expand Down
4 changes: 4 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# History

## 5.17.1

- Chains resumed from `initial_state` now take exactly `num_warmup` warm-up steps. Previously they took `num_warmup + 1`, including one when `num_warmup = 0` ([#220](https://github.com/TuringLang/AbstractMCMC.jl/issues/220)).

## 5.17.0

- Add `SamplingOutput` to retain draws, iteration indices, timing, and optional sampler states in a common container that chain packages can convert to their own formats ([#218](https://github.com/TuringLang/AbstractMCMC.jl/pull/218)).
32 changes: 28 additions & 4 deletions src/sample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,14 @@ function _filter_initial_params_kwarg(kwargs)
end

# Dispatch to step or step_warmup based on the number of steps run so far. `nsteps` is the
# number of steps taken so far (prior to calling this function).
function _step_or_step_warmup(nsteps::Int, num_warmup::Int, args...; kwargs...)
sample, state = if nsteps <= num_warmup
# number of steps taken so far (prior to calling this function). The first step of a fresh
# chain only initialises it, so it is followed by `num_warmup` warm-up steps. A chain resumed
# from `initial_state` (`resumed == true`) has no initialisation step, so its first
# `num_warmup` steps are the warm-up steps.
function _step_or_step_warmup(
nsteps::Int, num_warmup::Int, resumed::Bool, args...; kwargs...
)
sample, state = if nsteps + resumed <= num_warmup
step_warmup(args...; num_warmup=num_warmup, kwargs...)
else
step(args...; kwargs...)
Expand Down Expand Up @@ -215,6 +220,8 @@ function mcmcsample(
# Number of steps taken so far. This is incremented by every call to
# `_step_or_step_warmup`.
nsteps = 0
# Whether the chain is resumed from `initial_state`, i.e. has no initialisation step.
resumed = initial_state !== nothing
# Number of steps that have been saved so far.
nsteps_kept = 0

Expand All @@ -223,6 +230,7 @@ function mcmcsample(
nsteps, sample, state = _step_or_step_warmup(
nsteps,
num_warmup,
resumed,
rng,
model,
sampler,
Expand Down Expand Up @@ -255,6 +263,7 @@ function mcmcsample(
nsteps, sample, state = _step_or_step_warmup(
nsteps,
num_warmup,
resumed,
rng,
model,
sampler,
Expand Down Expand Up @@ -296,6 +305,7 @@ function mcmcsample(
nsteps, sample, state = _step_or_step_warmup(
nsteps,
num_warmup,
resumed,
rng,
model,
sampler,
Expand All @@ -315,6 +325,7 @@ function mcmcsample(
nsteps, sample, state = _step_or_step_warmup(
nsteps,
num_warmup,
resumed,
rng,
model,
sampler,
Expand Down Expand Up @@ -406,6 +417,8 @@ function mcmcsample(
# Number of steps taken so far. This is incremented by every call to
# `_step_or_step_warmup`.
nsteps = 0
# Whether the chain is resumed from `initial_state`, i.e. has no initialisation step.
resumed = initial_state !== nothing
# Number of steps that have been saved so far.
nsteps_kept = 0

Expand All @@ -414,6 +427,7 @@ function mcmcsample(
nsteps, sample, state = _step_or_step_warmup(
nsteps,
num_warmup,
resumed,
rng,
model,
sampler,
Expand All @@ -429,7 +443,15 @@ function mcmcsample(
discard_sample = j < discard_initial
# Obtain the next sample and state.
nsteps, sample, state = _step_or_step_warmup(
nsteps, num_warmup, rng, model, sampler, state; discard_sample, kwargs...
nsteps,
num_warmup,
resumed,
rng,
model,
sampler,
state;
discard_sample,
kwargs...,
)
end

Expand Down Expand Up @@ -464,6 +486,7 @@ function mcmcsample(
nsteps, sample, state = _step_or_step_warmup(
nsteps,
num_warmup,
resumed,
rng,
model,
sampler,
Expand All @@ -477,6 +500,7 @@ function mcmcsample(
nsteps, sample, state = _step_or_step_warmup(
nsteps,
num_warmup,
resumed,
rng,
model,
sampler,
Expand Down
42 changes: 42 additions & 0 deletions test/sample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,48 @@
end
end

@testset "Warm-up when resuming from `initial_state`" begin
# The state counts (warm-up, regular) transitions. The initial step takes neither, so
# fresh and resumed chains should both take exactly `num_warmup` warm-up transitions.
struct CountModel <: AbstractMCMC.AbstractModel end
struct CountSampler <: AbstractMCMC.AbstractSampler end
AbstractMCMC.step(rng, ::CountModel, ::CountSampler; kwargs...) = (0, 0), (0, 0)
function AbstractMCMC.step(
rng, ::CountModel, ::CountSampler, state::Tuple{Int,Int}; kwargs...
)
new_state = (state[1], state[2] + 1)
return new_state, new_state
end
function AbstractMCMC.step_warmup(
rng, ::CountModel, ::CountSampler, state::Tuple{Int,Int}; kwargs...
)
new_state = (state[1] + 1, state[2])
return new_state, new_state
end

N = 6
isdone(rng, model, sampler, samples, state, iteration; kwargs...) = iteration > N
@testset "num_warmup=$num_warmup, discard_initial=$discard_initial" for num_warmup in
(0, 1, 3),
discard_initial in (0, 2)

kwargs = (; num_warmup, discard_initial, progress=false)
fresh = last(sample(CountModel(), CountSampler(), N; kwargs...))
@test fresh[1] == num_warmup

resumed = last(
sample(CountModel(), CountSampler(), N; initial_state=fresh, kwargs...)
)
@test resumed[1] - fresh[1] == num_warmup

# The same for the method that samples until `isdone` returns `true`.
resumed = last(
sample(CountModel(), CountSampler(), isdone; initial_state=fresh, kwargs...)
)
@test resumed[1] - fresh[1] == num_warmup
end
end

@testset "Sample without predetermined N" begin
Random.seed!(1234)
chain = sample(MyModel(), MySampler())
Expand Down
Loading