-
Notifications
You must be signed in to change notification settings - Fork 0
Abstract mcmc rwm #5
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
Open
AHA-HH
wants to merge
10
commits into
main
Choose a base branch
from
abstract-mcmc-rwm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bf026d2
start building initial rwm using interface
JordanSimba 3278238
steps and sample functions
AHA-HH 43dc28d
error around float64 with logdensity function
AHA-HH b3a8320
ignore vscode file.
JordanSimba f8c92ec
remove .vscode.
JordanSimba 0a6f699
Attempt to get step for RandomWalk Metropolis in tests.
JordanSimba 36aa2f6
buggy tests, reimplemented in a scratch file, sampler bug
AHA-HH 5fb6e86
1d random walk with AbstractMCMC working
AHA-HH c4e389c
multivariate random walk done, needs more testing
AHA-HH 0affb08
Update function with Matt's suggestions, tests updated correspondingly
AHA-HH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4,3 +4,6 @@ | |
| /Manifest*.toml | ||
| /docs/Manifest*.toml | ||
| /docs/build/ | ||
|
|
||
|
|
||
| **/.vscode | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| module Arianna | ||
|
|
||
| # Write your package code here. | ||
| include("RandomWalk.jl") | ||
| using .RandomWalk | ||
|
|
||
| end |
This file contains hidden or 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,78 @@ | ||
| module RandomWalk | ||
|
|
||
| using AbstractMCMC | ||
| using Random | ||
| using Distributions | ||
| using LogDensityProblems | ||
|
|
||
| # Step 1 — Model | ||
| struct DistributionModel <: AbstractMCMC.AbstractModel | ||
| dist::Distribution | ||
| end | ||
|
|
||
| LogDensityProblems.logdensity(model::DistributionModel, x) = | ||
| logpdf(model.dist, x) | ||
|
|
||
| LogDensityProblems.dimension(model::DistributionModel) = length(mean(model.dist)) | ||
|
|
||
|
|
||
| # Step 2 - Sampler | ||
| struct RWSampler <: AbstractMCMC.AbstractSampler | ||
| position::Vector{Float64} | ||
| stepsize::Float64 | ||
| end | ||
|
|
||
| # Step 3 - Random Walk Metropolis Step | ||
| function AbstractMCMC.step( | ||
| rng::AbstractRNG, | ||
| model::DistributionModel, | ||
| sampler::RWSampler | ||
| ) | ||
|
|
||
| proposal = sampler.position .+ sampler.stepsize .* randn(rng, length(sampler.position)) | ||
|
|
||
| logp_current = LogDensityProblems.logdensity(model, sampler.position) | ||
| logp_proposal = LogDensityProblems.logdensity(model, proposal) | ||
|
|
||
| log_accept_ratio = logp_proposal - logp_current | ||
|
|
||
| if log(rand(rng)) < log_accept_ratio | ||
| new_position = proposal | ||
| else | ||
| new_position = sampler.position | ||
| end | ||
|
|
||
| return RWSampler(new_position, sampler.stepsize), logp_current | ||
| end | ||
|
|
||
| function AbstractMCMC.step(rng::AbstractRNG, | ||
| model::DistributionModel, | ||
| sampler::RWSampler, | ||
| _weight::Float64) | ||
|
|
||
| # Multiple dispatch, ignore the weight and call the usual step | ||
| return AbstractMCMC.step(rng, model, sampler) | ||
| end | ||
|
|
||
| # Step 4 - Sampling Interface | ||
| function AbstractMCMC.sample( | ||
| rng::AbstractRNG, | ||
| model::DistributionModel, | ||
| sampler::RWSampler, | ||
| n_samples::Integer, | ||
| kwargs... | ||
| ) | ||
|
|
||
| d = length(sampler.position) | ||
| samples = Matrix{Float64}(undef, n_samples, d) | ||
|
|
||
| current_sampler = sampler | ||
|
|
||
| for i in 1:n_samples | ||
| current_sampler, logp = AbstractMCMC.step(rng, model, current_sampler) | ||
| samples[i, :] = current_sampler.position | ||
| end | ||
|
|
||
| return samples | ||
| end | ||
| end | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,51 @@ | ||
| using Arianna | ||
| using Test | ||
| using Arianna | ||
| import Arianna.RandomWalk | ||
| using Distributions | ||
| using LogDensityProblems | ||
| using Random | ||
| using AbstractMCMC | ||
| using Plots | ||
| using LinearAlgebra | ||
|
|
||
| @testset "Multidimensional Random Walk" begin | ||
| rng = Random.default_rng() | ||
|
|
||
| # 2D Gaussian | ||
| dist = MvNormal([0.0, 0.0], I(2)) | ||
| model = RandomWalk.DistributionModel(dist) | ||
|
|
||
| # Initial sampler | ||
| s0 = RandomWalk.RWSampler([0.0, 0.0], 0.1) | ||
| @test s0.position isa Vector{Float64} | ||
| @test length(s0.position) == 2 | ||
|
|
||
| # One step | ||
| s1, logp = AbstractMCMC.step(rng, model, s0) | ||
| @test s1.position isa Vector{Float64} | ||
| @test length(s1.position) == 2 | ||
| @test logp isa Float64 | ||
|
|
||
| @testset "Arianna.jl" begin | ||
| # Write your tests here. | ||
| # Multiple samples | ||
| samples = AbstractMCMC.sample(rng, model, s0, 100) | ||
| @test samples isa Matrix{Float64} | ||
| @test size(samples) == (100, 2) | ||
|
|
||
| # # Plot each dimension separately | ||
| # x = samples[:,1] | ||
| # y = samples[:,2] | ||
| # t = 1:size(samples,1) | ||
|
|
||
| # p = plot3d(x, y, t, | ||
| # xlabel = "x₁", | ||
| # ylabel = "x₂", | ||
| # zlabel = "Step", | ||
| # title = "3D Random Walk Trajectory", | ||
| # linealpha = 5, | ||
| # legend = false) | ||
|
|
||
| # savefig("trace_mv.png") | ||
| end | ||
|
|
||
|
|
||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this method does not quite match the interface expected by
AbstractMCMC.stepfunction. Specificallystepmethods with two signatures need to be definedand
with former (without
stateargument) called on the first step when there is no existing state and the second on all subsequent steps, and in both cases the function returning a 2-tuple with the first entrysamplecorresponding to the values being traced / sampled and the second entrystatethe current chain state.I think something more like
would be more what we want for the case where we are updating an existing state, with we here assuming
sample == state, that is we are recording the full state on each iteration (and the state is some form of vector like object).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Matt, just reviewing the comments; your suggestion makes a lot of sense, I just wanted to know if the first step function where we have no initial state should have 3 arguments as the initial state is not defined yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AHA-HH - yep exactly, implementing a method for
AbstractMCMC.stepwithout astateargument that dispatches on your sampler type is intended for defining how chain is initialised. Here that could just be initialising state vector as independent samples from standard normal distribution of correct dimension (or any other arbitrary initial distribution).