Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a mutable struct instead #13

Merged
merged 2 commits into from
Jul 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/SampleChainsDynamicHMC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using TupleVectors:chainvec

export dynamichmc

@concrete struct DynamicHMCChain{T} <: AbstractChain{T}
@concrete mutable struct DynamicHMCChain{T} <: AbstractChain{T}
samples # :: AbstractVector{T}
logq # log-density for distribution the sample was drawn from
info # Per-sample metadata, type depends on sampler used
Expand All @@ -35,7 +35,7 @@ function DynamicHMCChain(t::TransformVariables.TransformTuple, Q::DynamicHMC.Eva
meta = steps
transform = t

return DynamicHMCChain{T}(samples, logq, info, meta, zeroarr(Q), transform)
return DynamicHMCChain{T}(samples, logq, info, meta, Q, transform)
end

TupleVectors.summarize(ch::DynamicHMCChain) = summarize(samples(ch))
Expand All @@ -51,8 +51,8 @@ function SampleChains.pushsample!(chain::DynamicHMCChain, Q::DynamicHMC.Evaluate
end

function SampleChains.step!(chain::DynamicHMCChain)
Q, tree_stats = DynamicHMC.mcmc_next_step(getfield(chain, :meta), getfield(chain, :state)[])
getfield(chain, :state)[] = Q
Q, tree_stats = DynamicHMC.mcmc_next_step(getfield(chain, :meta), getfield(chain, :state))
setfield!(chain, :state, Q)
return Q, tree_stats
end

Expand Down Expand Up @@ -158,11 +158,5 @@ function SampleChains.sample!(chain::DynamicHMCChain, n::Int=1000)
return chain
end

# I have no idea if this makes sense. But it's kind of cool so I wanted to try it
function zeroarr(x::T) where {T}
a = Array{T,0}(undef)
a[] = x
return a
end

end