1
1
"""
2
2
MetropolisHastings{D}
3
3
4
- `MetropolisHastings` has one field, `proposal`.
4
+ `MetropolisHastings` has one field, `proposal`.
5
5
`proposal` is a `Proposal`, `NamedTuple` of `Proposal`, or `Array{Proposal}` in the shape of your data.
6
6
For example, if you wanted the sampler to return a `NamedTuple` with shape
7
7
@@ -38,7 +38,7 @@ none is given, the initial parameters will be drawn from the sampler's proposals
38
38
- `param_names` is a vector of strings to be assigned to parameters. This is only
39
39
used if `chain_type=Chains`.
40
40
- `chain_type` is the type of chain you would like returned to you. Supported
41
- types are `chain_type=Chains` if `MCMCChains` is imported, or
41
+ types are `chain_type=Chains` if `MCMCChains` is imported, or
42
42
`chain_type=StructArray` if `StructArrays` is imported.
43
43
"""
44
44
struct MetropolisHastings{D} <: MHSampler
@@ -62,12 +62,12 @@ function propose(
62
62
return propose (rng, sampler. proposal, model, transition_prev. params)
63
63
end
64
64
65
- function transition (sampler:: MHSampler , model:: DensityModelOrLogDensityModel , params)
65
+ function transition (sampler:: MHSampler , model:: DensityModelOrLogDensityModel , params, accepted )
66
66
logdensity = AdvancedMH. logdensity (model, params)
67
- return transition (sampler, model, params, logdensity)
67
+ return transition (sampler, model, params, logdensity, accepted )
68
68
end
69
- function transition (sampler:: MHSampler , model:: DensityModelOrLogDensityModel , params, logdensity:: Real )
70
- return Transition (params, logdensity)
69
+ function transition (sampler:: MHSampler , model:: DensityModelOrLogDensityModel , params, logdensity:: Real , accepted )
70
+ return Transition (params, logdensity, accepted )
71
71
end
72
72
73
73
# Define the first sampling step.
@@ -81,7 +81,7 @@ function AbstractMCMC.step(
81
81
kwargs...
82
82
)
83
83
params = initial_params === nothing ? propose (rng, sampler, model) : initial_params
84
- transition = AdvancedMH. transition (sampler, model, params)
84
+ transition = AdvancedMH. transition (sampler, model, params, false )
85
85
return transition, transition
86
86
end
87
87
@@ -106,9 +106,11 @@ function AbstractMCMC.step(
106
106
107
107
# Decide whether to return the previous params or the new one.
108
108
transition = if - Random. randexp (rng) < logα
109
- AdvancedMH. transition (sampler, model, candidate, logdensity_candidate)
109
+ AdvancedMH. transition (sampler, model, candidate, logdensity_candidate, true )
110
110
else
111
- transition_prev
111
+ params = transition_prev. params
112
+ lp = transition_prev. lp
113
+ Transition (params, lp, false )
112
114
end
113
115
114
116
return transition, transition
0 commit comments