@@ -8,7 +8,7 @@ Static Metropolis-Hastings. Proposes only from the prior distribution.
8
8
9
9
Fields:
10
10
11
- - `init_θ ` is the vector form of the parameters needed for the likelihood function.
11
+ - `init_params ` is the vector form of the parameters needed for the likelihood function.
12
12
- `proposal` is a distribution.
13
13
14
14
Example:
@@ -17,21 +17,59 @@ Example:
17
17
RWMH([0.0, 0.0], MvNormal(x, 1.0))
18
18
````
19
19
"""
20
- StaticMH (init_theta:: Real , proposal = Normal (init_theta, 1 )) = MetropolisHastings (Static (), init_theta, proposal)
21
- StaticMH (init_theta:: Vector{<:Real} , proposal = MvNormal (init_theta, 1 )) = MetropolisHastings (Static (), init_theta, proposal)
20
+ StaticMH (init_theta, proposal) = MetropolisHastings (Static (), proposal, init_theta)
21
+ function StaticMH (init_theta:: Vector , proposal)
22
+ if proposal isa Vector
23
+ # Verify that there are proposal distributions for each parameter.
24
+ length (proposal) == length (init_theta) ||
25
+ throw (" The number of proposal distributions must match the number of parameters." )
26
+ end
27
+
28
+ return MetropolisHastings (Static (), proposal, init_theta)
29
+ end
22
30
23
31
# Define a function that makes a basic proposal depending on a univariate
24
32
# parameterization or a multivariate parameterization.
25
- propose (spl:: MetropolisHastings{Static} , model:: DensityModel , θ:: Real ) = Transition (model, rand (spl. proposal))
26
- propose (spl:: MetropolisHastings{Static} , model:: DensityModel , θ:: Vector{<:Real} ) = Transition (model, rand (spl. proposal))
33
+ propose (spl:: MetropolisHastings{Static, <:Distribution} , model:: DensityModel , params:: Transition ) = Transition (model, rand (spl. proposal))
34
+ function propose (spl:: MetropolisHastings{Static, <:AbstractArray} , model:: DensityModel , params:: Transition )
35
+ props = map (rand, spl. proposal)
36
+ return Transition (model, props)
37
+ end
38
+ function propose (spl:: MetropolisHastings{Static, <:NamedTuple} , model:: DensityModel , params:: Transition )
39
+ return Transition (model, _propose (spl. proposal))
40
+ end
41
+ @generated function _propose (proposals:: NamedTuple{names} ) where {names}
42
+ expr = Expr (:tuple )
43
+ map (names) do f
44
+ push! (expr. args, Expr (:(= ), f, :(rand (proposals.$ f)) ))
45
+ end
46
+ return expr
47
+ end
27
48
28
49
"""
29
- q(θ ::Real, dist::Sampleable)
30
- q(θ ::Vector{<:Real}, dist::Sampleable)
50
+ q(params ::Real, dist::Sampleable)
51
+ q(params ::Vector{<:Real}, dist::Sampleable)
31
52
q(t1::Transition, dist::Sampleable)
32
53
33
- Calculates the probability `q(θ | θcond )`, using the proposal distribution `spl.proposal`.
54
+ Calculates the probability `q(params | paramscond )`, using the proposal distribution `spl.proposal`.
34
55
"""
35
- q (spl:: MetropolisHastings{Static} , θ:: Real , θcond:: Real ) = logpdf (spl. proposal, θ)
36
- q (spl:: MetropolisHastings{Static} , θ:: Vector{<:Real} , θcond:: Vector{<:Real} ) = logpdf (spl. proposal, θ)
56
+ function q (spl:: MetropolisHastings{Static, <:Distribution} , t:: Transition , t_cond:: Transition )
57
+ return logpdf (spl. proposal, t. params)
58
+ end
59
+
60
+ function q (spl:: MetropolisHastings{Static, <:AbstractArray} , t:: Transition , t_cond:: Transition )
61
+ return sum (map (x -> logpdf (x[1 ], x[2 ]), zip (spl. proposal, t. params)))
62
+ end
63
+
64
+ function q (spl:: MetropolisHastings{Static, <:NamedTuple} , t:: Transition , t_cond:: Transition )
65
+ total = 0.0
66
+ for p in keys (t. params)
67
+ if length (t. params[p]) == 1
68
+ total += logpdf (spl. proposal[p], t. params[p][1 ])
69
+ else
70
+ total += logpdf (spl. proposal[p], t. params[p])
71
+ end
72
+ end
73
+ return total
74
+ end
37
75
0 commit comments