Skip to content

Commit 4ca57b0

Browse files
committed
improved and fixed some bugs in docs
1 parent f7b6096 commit 4ca57b0

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

docs/src/api.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,17 @@ function AbstractMCMC.step(rng, model::AbstractMCMC.AbstractModel, sampler::Mixt
171171
)
172172

173173
# Create the new states.
174-
# NOTE: A better approach would be to use `Setfield.@set state.states[i] = ...`
175-
# but to keep this demo self-contained, we don't.
176-
states_new = ntuple(1:length(state.states)) do j
177-
if j != i
178-
state.states[j]
174+
# NOTE: Code below will result in `states_new` begin a `Vector`.
175+
# If we wanted to allow usage of alternative containers, e.g. `Tuple`
176+
# it would be better to use something like `@set states[i] = state_current`
177+
# where `@set` is from Setfield.jl.
178+
states_new = map(1:length(state.states)) do j
179+
if j == i
180+
# Replace the i-th state with the new one.
181+
state_current
179182
else
180-
state_inner
183+
# Otherwise we just carry over the previous ones.
184+
state.states[j]
181185
end
182186
end
183187

@@ -200,7 +204,7 @@ function AbstractMCMC.step(rng, model::AbstractMCMC.AbstractModel, sampler::Mixt
200204
# Sample the component to use this `step`.
201205
i = rand(Categorical(sampler.weights))
202206
# Extract the corresponding transition.
203-
transition = first(transition_and_states[i])
207+
transition = first(transitions_and_states[i])
204208
# Extract states.
205209
states = map(last, transitions_and_states)
206210
# Create new `MixtureState`.
@@ -210,10 +214,19 @@ function AbstractMCMC.step(rng, model::AbstractMCMC.AbstractModel, sampler::Mixt
210214
end
211215
```
212216

213-
To use `MixtureSampler` with two samplers `sampler1` and `sampler2` as components, we'd simply do
217+
Suppose we then wanted to use this with some of the packages which implements AbstractMCMC.jl's interface, e.g. [`AdvancedMH.jl`](https://github.com/TuringLang/AdvancedMH.jl), then we'd simply have to implement `values` and `setvalues!!`:
218+
219+
```julia
220+
function AbstractMCMC.updatestate!!(::AdvancedMH.Transition, state_prev::AdvancedMH.Transition)
221+
# Let's `deepcopy` just to be certain.
222+
return deepcopy(state_prev)
223+
end
224+
```
225+
226+
To use `MixtureSampler` with two samplers `sampler1` and `sampler2` from `AdvancedMH.jl` as components, we'd simply do
214227

215228
```julia
216-
sampler = MixtureSampler((0.1, 0.9), (sampler1, sampler2))
229+
sampler = MixtureSampler([sampler1, sampler2], [0.1, 0.9])
217230
transition, state = AbstractMCMC.step(rng, model, sampler)
218231
while ...
219232
transition, state = AbstractMCMC.step(rng, model, sampler, state)

0 commit comments

Comments
 (0)