Skip to content

Commit 17cc347

Browse files
authored
Merge pull request #17 from devmotion/compat
Update to AbstractMCMC 0.4
2 parents 588f284 + bbd0ff5 commit 17cc347

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
99
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1010

1111
[compat]
12-
AbstractMCMC = "0.3"
12+
AbstractMCMC = "0.4"
1313
Distributions = "0.20, 0.21, 0.22, 0.23"
1414
Requires = "1.0"
1515
julia = "1"

src/AdvancedMH.jl

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
module AdvancedMH
22

33
# Import the relevant libraries.
4-
using AbstractMCMC
5-
using Random
6-
using Requires
4+
import AbstractMCMC
75
using Distributions
6+
using Requires
87

9-
# Import specific functions and types to use or overload.
10-
import AbstractMCMC: step!, AbstractSampler, AbstractTransition,
11-
transition_type, bundle_samples
8+
using Random
129

1310
# 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
1516

1617
# Abstract type for MH-style samplers.
17-
abstract type Metropolis <: AbstractSampler end
18+
abstract type Metropolis <: AbstractMCMC.AbstractSampler end
1819
abstract type ProposalStyle end
1920

2021
struct RandomWalk <: ProposalStyle end
@@ -34,47 +35,44 @@ l
3435
DensityModel
3536
```
3637
"""
37-
struct DensityModel{F<:Function} <: AbstractModel
38+
struct DensityModel{F<:Function} <: AbstractMCMC.AbstractModel
3839
logdensity :: F
3940
end
4041

4142
# Create a very basic Transition type, only stores the
4243
# 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}
4445
params :: T
4546
lp :: L
4647
end
4748

4849
# 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))
5351

5452
# Calculate the density of the model given some parameterization.
5553
logdensity(model::DensityModel, params) = model.logdensity(params)
5654
logdensity(model::DensityModel, t::Transition) = t.lp
5755

5856
# A basic chains constructor that works with the Transition struct we defined.
59-
function bundle_samples(
57+
function AbstractMCMC.bundle_samples(
6058
rng::AbstractRNG,
6159
model::DensityModel,
6260
s::Metropolis,
6361
N::Integer,
64-
ts::Vector{<:AbstractTransition},
62+
ts::Vector,
6563
chain_type::Type{Any};
6664
param_names=missing,
6765
kwargs...
6866
)
6967
return ts
7068
end
7169

72-
function bundle_samples(
70+
function AbstractMCMC.bundle_samples(
7371
rng::AbstractRNG,
7472
model::DensityModel,
7573
s::Metropolis,
7674
N::Integer,
77-
ts::Vector{<:AbstractTransition},
75+
ts::Vector,
7876
chain_type::Type{Vector{NamedTuple}};
7977
param_names=missing,
8078
kwargs...

src/mcmcchains-connect.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import .MCMCChains: Chains
22

33
# A basic chains constructor that works with the Transition struct we defined.
4-
function bundle_samples(
4+
function AbstractMCMC.bundle_samples(
55
rng::AbstractRNG,
66
model::DensityModel,
77
s::Metropolis,
88
N::Integer,
9-
ts::Vector{<:AbstractTransition},
9+
ts::Vector,
1010
chain_type::Type{Chains};
1111
param_names=missing,
1212
kwargs...

src/mh-core.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,12 @@ end
161161
# Define the first step! function, which is called at the
162162
# beginning of sampling. Return the initial parameter used
163163
# to define the sampler.
164-
function step!(
164+
function AbstractMCMC.step!(
165165
rng::AbstractRNG,
166166
model::DensityModel,
167167
spl::MetropolisHastings,
168-
N::Integer;
168+
N::Integer,
169+
::Nothing;
169170
init_params=nothing,
170171
kwargs...
171172
)
@@ -179,7 +180,7 @@ end
179180
# Define the other step functions. Returns a Transition containing
180181
# either a new proposal (if accepted) or the previous proposal
181182
# (if not accepted).
182-
function step!(
183+
function AbstractMCMC.step!(
183184
rng::AbstractRNG,
184185
model::DensityModel,
185186
spl::MetropolisHastings,

src/structarray-connect.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import .StructArrays: StructArray
22

33
# A basic chains constructor that works with the Transition struct we defined.
4-
function bundle_samples(
4+
function AbstractMCMC.bundle_samples(
55
rng::AbstractRNG,
66
model::DensityModel,
77
s::Metropolis,
88
N::Integer,
9-
ts::Vector{<:AbstractTransition},
9+
ts::Vector,
1010
chain_type::Type{StructArray};
1111
param_names=missing,
1212
kwargs...
1313
)
14-
return StructArray(bundle_samples(rng, model, s, N, ts, Vector{NamedTuple}; param_names=param_names, kwargs...))
14+
samples = AbstractMCMC.bundle_samples(rng, model, s, N, ts, Vector{NamedTuple};
15+
param_names=param_names, kwargs...)
16+
return StructArray(samples)
1517
end
1618

1719
AbstractMCMC.chainscat(c::StructArray, cs::StructArray...) = vcat(c, cs...)

0 commit comments

Comments
 (0)