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
Copy file name to clipboardExpand all lines: docs/src/api.md
+22-9Lines changed: 22 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -171,13 +171,17 @@ function AbstractMCMC.step(rng, model::AbstractMCMC.AbstractModel, sampler::Mixt
171
171
)
172
172
173
173
# 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
179
182
else
180
-
state_inner
183
+
# Otherwise we just carry over the previous ones.
184
+
state.states[j]
181
185
end
182
186
end
183
187
@@ -200,7 +204,7 @@ function AbstractMCMC.step(rng, model::AbstractMCMC.AbstractModel, sampler::Mixt
200
204
# Sample the component to use this `step`.
201
205
i =rand(Categorical(sampler.weights))
202
206
# Extract the corresponding transition.
203
-
transition =first(transition_and_states[i])
207
+
transition =first(transitions_and_states[i])
204
208
# Extract states.
205
209
states =map(last, transitions_and_states)
206
210
# Create new `MixtureState`.
@@ -210,10 +214,19 @@ function AbstractMCMC.step(rng, model::AbstractMCMC.AbstractModel, sampler::Mixt
210
214
end
211
215
```
212
216
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
+
returndeepcopy(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
0 commit comments