|
1 | 1 | module AdvancedMH
|
2 | 2 |
|
3 | 3 | # Import the relevant libraries.
|
4 |
| -using AbstractMCMC |
5 |
| -using Random |
6 |
| -using Requires |
| 4 | +import AbstractMCMC |
7 | 5 | using Distributions
|
| 6 | +using Requires |
8 | 7 |
|
9 |
| -# Import specific functions and types to use or overload. |
10 |
| -import AbstractMCMC: step!, AbstractSampler, AbstractTransition, |
11 |
| - transition_type, bundle_samples |
| 8 | +using Random |
12 | 9 |
|
13 | 10 | # Exports
|
14 |
| -export MetropolisHastings, DensityModel, sample, psample, RWMH, StaticMH, Proposal, Static, RandomWalk |
| 11 | +export MetropolisHastings, DensityModel, RWMH, StaticMH, Proposal, Static, RandomWalk |
| 12 | + |
| 13 | +# Reexports |
| 14 | +using AbstractMCMC: sample, psample |
| 15 | +export sample, psample |
15 | 16 |
|
16 | 17 | # Abstract type for MH-style samplers.
|
17 |
| -abstract type Metropolis <: AbstractSampler end |
| 18 | +abstract type Metropolis <: AbstractMCMC.AbstractSampler end |
18 | 19 | abstract type ProposalStyle end
|
19 | 20 |
|
20 | 21 | struct RandomWalk <: ProposalStyle end
|
|
34 | 35 | DensityModel
|
35 | 36 | ```
|
36 | 37 | """
|
37 |
| -struct DensityModel{F<:Function} <: AbstractModel |
| 38 | +struct DensityModel{F<:Function} <: AbstractMCMC.AbstractModel |
38 | 39 | logdensity :: F
|
39 | 40 | end
|
40 | 41 |
|
41 | 42 | # Create a very basic Transition type, only stores the
|
42 | 43 | # parameter draws and the log probability of the draw.
|
43 |
| -struct Transition{T<:Union{Vector, Real, NamedTuple}, L<:Real} <: AbstractTransition |
| 44 | +struct Transition{T<:Union{Vector, Real, NamedTuple}, L<:Real} |
44 | 45 | params :: T
|
45 | 46 | lp :: L
|
46 | 47 | end
|
47 | 48 |
|
48 | 49 | # Store the new draw and its log density.
|
49 |
| -Transition(model::M, params::T) where {M<:DensityModel, T} = Transition(params, logdensity(model, params)) |
50 |
| - |
51 |
| -# Tell the interface what transition type we would like to use. |
52 |
| -transition_type(model::DensityModel, spl::Metropolis) = typeof(Transition(spl.init_params, logdensity(model, spl.init_params))) |
| 50 | +Transition(model::DensityModel, params) = Transition(params, logdensity(model, params)) |
53 | 51 |
|
54 | 52 | # Calculate the density of the model given some parameterization.
|
55 | 53 | logdensity(model::DensityModel, params) = model.logdensity(params)
|
56 | 54 | logdensity(model::DensityModel, t::Transition) = t.lp
|
57 | 55 |
|
58 | 56 | # A basic chains constructor that works with the Transition struct we defined.
|
59 |
| -function bundle_samples( |
| 57 | +function AbstractMCMC.bundle_samples( |
60 | 58 | rng::AbstractRNG,
|
61 | 59 | model::DensityModel,
|
62 | 60 | s::Metropolis,
|
63 | 61 | N::Integer,
|
64 |
| - ts::Vector{<:AbstractTransition}, |
| 62 | + ts::Vector, |
65 | 63 | chain_type::Type{Any};
|
66 | 64 | param_names=missing,
|
67 | 65 | kwargs...
|
68 | 66 | )
|
69 | 67 | return ts
|
70 | 68 | end
|
71 | 69 |
|
72 |
| -function bundle_samples( |
| 70 | +function AbstractMCMC.bundle_samples( |
73 | 71 | rng::AbstractRNG,
|
74 | 72 | model::DensityModel,
|
75 | 73 | s::Metropolis,
|
76 | 74 | N::Integer,
|
77 |
| - ts::Vector{<:AbstractTransition}, |
| 75 | + ts::Vector, |
78 | 76 | chain_type::Type{Vector{NamedTuple}};
|
79 | 77 | param_names=missing,
|
80 | 78 | kwargs...
|
|
0 commit comments