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
Update logp in varinfo when external samplers are used (#2616)
* Update logp in varinfo when external samplers are used
* Add tests
* More tests
* Bump patch, changelog
* Only do one extra model evaluation instead of two
* Don't run the same test 13 times
* Fix tests
* Simplify interface
* isapprox for floats...
* Update comment / changelog
Copy file name to clipboardExpand all lines: src/mcmc/external_sampler.jl
+64-21Lines changed: 64 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,23 @@ The `Unconstrained` type-parameter is to indicate whether the sampler requires u
7
7
8
8
# Fields
9
9
$(TYPEDFIELDS)
10
+
11
+
# Turing.jl's interface for external samplers
12
+
13
+
When implementing a new `MySampler <: AbstractSampler`,
14
+
`MySampler` must first and foremost conform to the `AbstractMCMC` interface to work with Turing.jl's `externalsampler` function.
15
+
In particular, it must implement:
16
+
17
+
- `AbstractMCMC.step` (the main function for taking a step in MCMC sampling; this is documented in AbstractMCMC.jl)
18
+
- `AbstractMCMC.getparams(::DynamicPPL.Model, external_state)`: How to extract the parameters from the state returned by your sampler (i.e., the second return value of `step`).
19
+
20
+
There are a few more optional functions which you can implement to improve the integration with Turing.jl:
21
+
22
+
- `Turing.Inference.isgibbscomponent(::MySampler)`: If you want your sampler to function as a component in Turing's Gibbs sampler, you should make this evaluate to `true`.
23
+
24
+
- `Turing.Inference.requires_unconstrained_space(::MySampler)`: If your sampler requires unconstrained space, you should return `true`. This tells Turing to perform linking on the VarInfo before evaluation, and ensures that the parameter values passed to your sampler will always be in unconstrained (Euclidean) space.
25
+
26
+
- `Turing.Inference.getlogp_external(external_transition, external_state)`: Tell Turing how to extract the log probability density associated with this transition (and state). If you do not specify these, Turing will simply re-evaluate the model with the parameters obtained from `getparams`, which can be inefficient. It is therefore recommended to store the log probability density in either the transition or the state (or both) and override this method.
0 commit comments