-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compatibility with MCMCChains (#309)
* Added MCMCChains connection * Using bijector in test * Apply suggestions from code review Co-authored-by: Seth Axen <[email protected]> * Apply suggestions from code review * Update mcmcchains-connect.jl * Update src/mcmcchains-connect.jl Co-authored-by: Seth Axen <[email protected]> Co-authored-by: Seth Axen <[email protected]> Co-authored-by: Hong Ge <[email protected]>
- Loading branch information
1 parent
0f2e402
commit 1b3393d
Showing
6 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import .MCMCChains: Chains | ||
|
||
# A basic chains constructor that works with the Transition struct we defined. | ||
function AbstractMCMC.bundle_samples( | ||
ts::Vector{<:Transition}, | ||
model::AbstractMCMC.AbstractModel, | ||
sampler::AbstractMCMC.AbstractSampler, | ||
state, | ||
chain_type::Type{Chains}; | ||
discard_initial=0, | ||
thinning=1, | ||
param_names=missing, | ||
bijector=identity, | ||
kwargs... | ||
) | ||
# Turn all the transitions into a vector-of-vectors. | ||
t = ts[1] | ||
tstat = merge((; lp = t.z.ℓπ.value), stat(t)) | ||
tstat_names = collect(keys(tstat)) | ||
vals = [vcat(bijector(t.z.θ), t.z.ℓπ.value, collect(values(stat(t)))) for t in ts] | ||
|
||
# Check if we received any parameter names. | ||
if ismissing(param_names) | ||
param_names = [Symbol(:param_, i) for i in 1:length(keys(ts[1].z.θ))] | ||
else | ||
# Generate new array to be thread safe. | ||
param_names = Symbol.(param_names) | ||
end | ||
|
||
# Bundle everything up and return a Chains struct. | ||
return Chains( | ||
vals, vcat(param_names, tstat_names), (parameters = param_names, internals = tstat_names,); | ||
start=discard_initial + 1, thin=thinning, | ||
) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using ReTest, Random, AdvancedHMC, ForwardDiff, AbstractMCMC, MCMCChains | ||
using Statistics: mean | ||
include("common.jl") | ||
|
||
@testset "MCMCChains w/ gdemo" begin | ||
rng = MersenneTwister(0) | ||
|
||
n_samples = 5_000 | ||
n_adapts = 5_000 | ||
|
||
θ_init = randn(rng, 2) | ||
|
||
model = AdvancedHMC.LogDensityModel(LogDensityProblemsAD.ADgradient(Val(:ForwardDiff), ℓπ_gdemo)) | ||
init_eps = Leapfrog(1e-3) | ||
κ = NUTS(init_eps) | ||
metric = DiagEuclideanMetric(2) | ||
adaptor = StanHMCAdaptor(MassMatrixAdaptor(metric), StepSizeAdaptor(0.8, κ.τ.integrator)) | ||
|
||
samples = AbstractMCMC.sample( | ||
rng, model, κ, metric, adaptor, n_adapts + n_samples; | ||
nadapts = n_adapts, | ||
init_params = θ_init, | ||
chain_type = Chains, | ||
progress=false, | ||
bijector = invlink_gdemo, | ||
verbose=false | ||
); | ||
|
||
# Transform back to original space. | ||
# NOTE: We're not correcting for the `logabsdetjac` here since, but | ||
# we're only interested in the mean it doesn't matter. | ||
m_est = mean(samples[n_adapts + 1:end]) | ||
|
||
@test m_est[:,2] ≈ [49 / 24, 7 / 6] atol=RNDATOL | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters