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
+14-43Lines changed: 14 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,8 +112,8 @@ AbstractMCMC.chainsstack
112
112
113
113
To make it a bit easier to interact with some arbitrary sampler state, we encourage implementations of `AbstractSampler` to implement the following methods:
114
114
```@docs
115
-
AbstractMCMC.realize
116
-
AbstractMCMC.realize!!
115
+
AbstractMCMC.getparams
116
+
AbstractMCMC.setparams!!
117
117
```
118
118
and optionally
119
119
```@docs
@@ -125,7 +125,7 @@ These methods can also be useful for implementing samplers which wraps some inne
125
125
126
126
In a `MixtureSampler` we need two things:
127
127
-`components`: collection of samplers.
128
-
-`weights`: collection of weights representing the probability of chosing the corresponding sampler.
128
+
-`weights`: collection of weights representing the probability of choosing the corresponding sampler.
To implement the state, we need to keep track of a couple of things:
138
138
-`index`: the index of the sampler used in this `step`.
139
-
-`transition`: the transition resulting from this `step`.
140
139
-`states`: the current states of _all_ the components.
141
140
Two aspects of this might seem a bit strange:
142
141
1. We need to keep track of the states of _all_ components rather than just the state for the sampler we used previously.
@@ -146,11 +145,9 @@ The reason for (1) is that lots of samplers keep track of more than just the pre
146
145
147
146
For (2) the reason is similar: some samplers might keep track of the variables _in the state_ differently, e.g. you might have a sampler which is _independent_ of the current realizations and the state is simply `nothing`.
148
147
149
-
Hence, we need the `transition`, which should always contain the realizations, to make sure we can resume from the same point in the space in the next `step`.
where ``\mathcal{K}_i`` denotes the i-th kernel/sampler, and ``w_i`` denotes the weight/probability of choosing the i-th sampler.
165
-
[`AbstractMCMC.updatestate!!`](@ref) comes into play in defining/computing ``\mathcal{K}_i(\cdot \mid X_{t - 1})`` since ``X_{t - 1}`` could be coming from a different sampler.
162
+
[`AbstractMCMC.getparams`](@ref) and [`AbstractMCMC.setparams!!`](@ref) comes into play in defining/computing ``\mathcal{K}_i(\cdot \mid X_{t - 1})`` since ``X_{t - 1}`` could be coming from a different sampler.
166
163
167
164
If we let `state` be the current `MixtureState`, `i` the current component, and `i_prev` is the previous component we sampled from, then this translates into the following piece of code:
168
165
169
166
```julia
170
167
# Update the corresponding state, i.e. `state.states[i]`, using
171
168
# the state and transition from the previous iteration.
@@ -239,20 +238,14 @@ function AbstractMCMC.step(rng, model::AbstractMCMC.AbstractModel, sampler::Mixt
239
238
# Extract states.
240
239
states =map(last, transitions_and_states)
241
240
# Create new `MixtureState`.
242
-
state =MixtureState(i, transition, states)
241
+
state =MixtureState(i, states)
243
242
244
243
return transition, state
245
244
end
246
245
```
247
246
248
-
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 `realize` and `realize!!`:
247
+
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 `getparams` and `setparams!!`.
249
248
250
-
```julia
251
-
function AbstractMCMC.updatestate!!(model, ::AdvancedMH.Transition, state_prev::AdvancedMH.Transition)
252
-
# Let's `deepcopy` just to be certain.
253
-
returndeepcopy(state_prev)
254
-
end
255
-
```
256
249
257
250
To use `MixtureSampler` with two samplers `sampler1` and `sampler2` from `AdvancedMH.jl` as components, we'd simply do
258
251
@@ -263,25 +256,3 @@ while ...
263
256
transition, state = AbstractMCMC.step(rng, model, sampler, state)
264
257
end
265
258
```
266
-
267
-
As a final note, there is one potential issue we haven't really addressed in the above implementation: a lot of samplers have their own implementations of `AbstractMCMC.AbstractModel` which means that we would also have to ensure that all the different samplers we are using would be compatible with the same model. A very easy way to fix this would be to just add a struct called `ManyModels` supporting `getindex`, e.g. `models[i]` would return the i-th `model`:
0 commit comments