Skip to content

A Julia library for random-walk Metropolis-Hastings algorithms

License

Notifications You must be signed in to change notification settings

arzwa/AdvancedMH.jl

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AdvancedMH.jl

AdvancedMH.jl currently provides a robust implementation of random walk Metropolis-Hastings samplers.

Further development aims to provide a suite of adaptive Metropolis-Hastings implementations.

Usage

AdvancedMH works by accepting some log density function which is used to construct a DensityModel. The DensityModel is then used in a sample call.

# Import the package.
using AdvancedMH
using Distributions

# Generate a set of data from the posterior we want to estimate.
data = rand(Normal(0, 1), 30)

# Define the components of a basic model.
insupport(θ) = θ[2] >= 0
dist(θ) = Normal(θ[1], θ[2])
density(θ) = insupport(θ) ? sum(logpdf.(dist(θ), data)) : -Inf

# Construct a DensityModel.
model = DensityModel(density)

# Set up our sampler with initial parameters.
spl = MetropolisHastings([0.0, 0.0])

# Sample from the posterior.
chain = sample(model, spl, 100000; param_names=["μ", "σ"])

Output:

Object of type Chains, with data of type 100000×3×1 Array{Float64,3}

Iterations        = 1:100000
Thinning interval = 1
Chains            = 1
Samples per chain = 100000
internals         = lp
parameters        = μ, σ

2-element Array{MCMCChains.ChainDataFrame,1}

Summary Statistics

│ Row │ parameters │ mean      │ std      │ naive_se   │ mcse       │ ess     │ r_hat   │
│     │ Symbol     │ Float64   │ Float64  │ Float64    │ Float64    │ Any     │ Any     │
├─────┼────────────┼───────────┼──────────┼────────────┼────────────┼─────────┼─────────┤
│ 1   │ μ          │ 0.08341880.2414180.000763430.003410674693.041.00008 │
│ 2   │ σ          │ 1.331160.1841110.000582210.002587784965.831.00001 │

Quantiles

│ Row │ parameters │ 2.5%25.0%50.0%75.0%97.5%    │
│     │ Symbol     │ Float64   │ Float64    │ Float64   │ Float64  │ Float64  │
├─────┼────────────┼───────────┼────────────┼───────────┼──────────┼──────────┤
│ 1   │ μ          │ -0.393769-0.07711340.08016880.2411620.564331 │
│ 2   │ σ          │ 1.036851.20441.309921.436091.75745

Custom proposals

Custom proposal distributions can be specified by passing a distribution to MetropolisHastings:

# Set up our sampler with initial parameters.
spl = MetropolisHastings([0.0, 0.0], MvNormal(2, 0.5)) 

About

A Julia library for random-walk Metropolis-Hastings algorithms

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Julia 100.0%