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

MH Constructor #2037

Merged
merged 33 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
94bc38e
first draft
JaimeRZP Jul 6, 2023
26f5bac
abstractcontext + tests
JaimeRZP Jul 6, 2023
b31908f
bug
JaimeRZP Jul 6, 2023
af50188
externalsampler() in tests
JaimeRZP Jul 7, 2023
6725e4a
Name Tupple problems
JaimeRZP Jul 7, 2023
427bef4
moving stuff to DynamicPPL RP
JaimeRZP Jul 11, 2023
c022765
using new DynamicPPL PR
JaimeRZP Jul 14, 2023
9ea3550
mistakenly removed line
JaimeRZP Jul 14, 2023
bdf5110
specific constructors
JaimeRZP Jul 14, 2023
6025d70
Merge branch 'master' into MH_constructor
JaimeRZP Jul 17, 2023
4ab5939
no StaticMH RWMH
JaimeRZP Jul 17, 2023
74869f4
Bump bijectors compat (#2052)
yebai Jul 27, 2023
2e76333
Merge branch 'master' into MH_constructor
yebai Jul 27, 2023
17960f4
Merge branch 'master' into MH_constructor
yebai Jul 29, 2023
0176407
Bugfixes.
yebai Jul 30, 2023
f775f52
Add TODO.
yebai Jul 30, 2023
fb5612f
Update mh.jl
yebai Jul 30, 2023
0abebd7
Update Inference.jl
yebai Jul 30, 2023
3dae981
Removed obsolete exports.
yebai Jul 30, 2023
f792d73
removed unnecessary import of extract_priors
torfjelde Jul 31, 2023
b19abdc
added missing ) in MH tests
torfjelde Jul 31, 2023
7f42b53
fixed incorrect referneces to AdvancedMH in tests
torfjelde Jul 31, 2023
dfc5285
Merge branch 'master' into MH_constructor
torfjelde Jul 31, 2023
411d030
improve ESLogDensityFunction
torfjelde Jul 31, 2023
7dd122a
remove hardcoding of SimpleVarInfo
torfjelde Jul 31, 2023
5f0963e
added fixme comment
torfjelde Jul 31, 2023
70ddc23
minor style changes
torfjelde Jul 31, 2023
3fcb839
fixed issues with MH with RandomWalkProposal being used as an externa…
torfjelde Jul 31, 2023
47584f8
fixed accidental typo
torfjelde Jul 31, 2023
bdc87b9
move definitions of unflatten for NamedTuple
torfjelde Jul 31, 2023
ef27088
improved TODO
torfjelde Jul 31, 2023
045e2d0
Update Project.toml
yebai Aug 16, 2023
286a974
Merge branch 'master' into MH_constructor
yebai Aug 16, 2023
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
1 change: 0 additions & 1 deletion src/Turing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export @model, # modelling
Prior, # Sampling from the prior

MH, # classic sampling
RWMH,
Emcee,
ESS,
Gibbs,
Expand Down
18 changes: 18 additions & 0 deletions src/inference/Inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ Wrap a sampler so it can be used as an inference algorithm.
"""
externalsampler(sampler::AbstractSampler) = ExternalSampler(sampler)

"""
ESLogDensityFunction

A log density function for the External sampler.

"""
const ESLogDensityFunction{M<:Model,S<:Sampler{<:ExternalSampler},V<:AbstractVarInfo} = Turing.LogDensityFunction{V,M,<:DynamicPPL.DefaultContext}
function LogDensityProblems.logdensity(f::ESLogDensityFunction, x::NamedTuple)
return DynamicPPL.logjoint(f.model, DynamicPPL.unflatten(f.varinfo, x))
end

# TODO: make a nicer `set_namedtuple!` and move these functions to DynamicPPL.
function DynamicPPL.unflatten(vi::TypedVarInfo, θ::NamedTuple)
set_namedtuple!(deepcopy(vi), θ)
return vi
end
DynamicPPL.unflatten(vi::SimpleVarInfo, θ::NamedTuple) = SimpleVarInfo(θ, vi.logp, vi.transformation)

# Algorithm for sampling from the prior
struct Prior <: InferenceAlgorithm end

Expand Down
15 changes: 15 additions & 0 deletions src/inference/mh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ function MH(space...)
return MH{tuple(syms...), typeof(proposals)}(proposals)
end

# Some of the proposals require working in unconstrained space.
transform_maybe(proposal::AMH.Proposal) = proposal
function transform_maybe(proposal::AMH.RandomWalkProposal)
return AMH.RandomWalkProposal(Bijectors.transformed(proposal.proposal))
end

function MH(model::Model; proposal_type=AMH.StaticProposal)
priors = DynamicPPL.extract_priors(model)
props = Tuple([proposal_type(prop) for prop in values(priors)])
vars = Tuple(map(Symbol, collect(keys(priors))))
priors = map(transform_maybe, NamedTuple{vars}(props))
return AMH.MetropolisHastings(priors)
end

#####################
# Utility functions #
#####################
Expand Down Expand Up @@ -346,6 +360,7 @@ end
function should_link(varinfo, sampler, proposal::AdvancedMH.RandomWalkProposal)
return true
end
# FIXME: This won't be hit unless `vals` are all the exactly same concrete type of `AdvancedMH.RandomWalkProposal`!
function should_link(
varinfo,
sampler,
Expand Down
6 changes: 6 additions & 0 deletions test/inference/mh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

s4 = Gibbs(MH(:m), MH(:s))
c4 = sample(gdemo_default, s4, N)

s5 = externalsampler(MH(gdemo_default, proposal_type=AdvancedMH.RandomWalkProposal))
c5 = sample(gdemo_default, s5, N)

s6 = externalsampler(MH(gdemo_default, proposal_type=AdvancedMH.StaticProposal))
c6 = sample(gdemo_default, s6, N)
end
@numerical_testset "mh inference" begin
Random.seed!(125)
Expand Down