Skip to content

Add vectorized HMC docs #440

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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ makedocs(;
pages=[
"AdvancedHMC.jl" => "index.md",
"Get Started" => "get_started.md",
"Vectorized HMC" => "vectorized.md",
"Automatic Differentiation Backends" => "autodiff.md",
"Detailed API" => "api.md",
"Interfaces" => "interfaces.md",
Expand Down
2 changes: 1 addition & 1 deletion docs/src/get_started.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sampling from a multivariate Gaussian using NUTS
# [Sampling from a multivariate Gaussian using NUTS](@id get_started)

In this section, we demonstrate a minimal example of sampling from a multivariate Gaussian (10-dimensional) using the No U-Turn Sampler (NUTS). Below we describe the major components of the Hamiltonian system which are essential to sample using this approach:

Expand Down
28 changes: 28 additions & 0 deletions docs/src/vectorized.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Vectorized HMC Sampling

In this section, we explain how to easily employ vectorized Hamiltonian Monte Carlo with AdvancedHMC.jl. Let's continue with the previous example in [getting-started](@ref get_started), we want to sample a multivariate Gaussian (10-dimensional) with multiple chains, we can simply specify the number of chains in initial parameters, leapfrog integrator, and metric to tell AdvanceHMC.jl how many chains we want to sample. Here, the vectorized multivariate Gaussian log density problem come from [MCMCLogDensityProblems.jl](https://github.com/chalk-lab/MCMCLogDensityProblems.jl) which is a library of common log density target distributions designed for vectorized sampling.

```julia
using AdvancedHMC
using MCMCLogDensityProblems

D = 10
target = HighDimGaussian(D)
ℓπ(x) = logpdf(target, x)
∂ℓπ∂θ(x) = logpdf_grad(target, x)

n_chains = 5
θ_init = rand(D, n_chains)
ϵ = 0.1
lfi = Leapfrog(fill(ϵ, n_chains))
n_steps = 10
n_samples = 20_000
metric = DiagEuclideanMetric((D, n_chains))
τ = Trajectory{EndPointTS}(lfi, FixedNSteps(n_steps))
h = Hamiltonian(metric, ℓπ, ∂ℓπ∂θ)
samples, stats = sample(h, HMCKernel(τ), θ_init, n_samples; verbose=false)
```

!!! note

Vectorized sampling only support static HMC, which means samplers like `NUTS` should not be used for vectorized sampling for now.
Loading