Skip to content

Commit

Permalink
Change default adaptor for HMCDA (#338)
Browse files Browse the repository at this point in the history
* Update README.md

* Update abstractmcmc.jl

* Update README.md

* Update constructors.jl

* Update src/abstractmcmc.jl

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update abstractmcmc.jl

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
yebai and github-actions[bot] authored Jul 27, 2023
1 parent e302429 commit bb4b534
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Users can also use the `AbstractMCMC` interface to sample, which is also used in
In order to show how this is done let us start from our previous example where we defined a `LogTargetDensity`, `ℓπ`.

```julia
using AbstractMCMC, LogDensityProblemsAD
# Wrap the previous LogTargetDensity as LogDensityModel
# where ℓπ::LogTargetDensity
model = AdvancedHMC.LogDensityModel(LogDensityProblemsAD.ADgradient(Val(:ForwardDiff), ℓπ))
Expand All @@ -134,7 +135,7 @@ D = 10; initial_θ = rand(D)
n_samples, n_adapts, δ = 1_000, 2_000, 0.8
sampler = HMCSampler(kernel, metric, adaptor)

# Now just sample
# Now sample
samples = AbstractMCMC.sample(
model,
sampler,
Expand All @@ -152,8 +153,8 @@ In the previous examples, we built the sampler by manually specifying the integr
```julia
# HMC Sampler
# step size, number of leapfrog steps
n_leapfrogs, lf_integrator = 0.25, Leapfrog(0.1)
hmc = HMC(n_leapfrogs, integrator = lf_integrator)
n_leapfrog, lf_integrator = 25, Leapfrog(0.1)
hmc = HMC(n_leapfrog, integrator = lf_integrator)
```

Equivalent to:
Expand Down Expand Up @@ -204,7 +205,7 @@ In the previous examples, we built the sampler by manually specifying the integr
initial_ϵ = find_good_stepsize(hamiltonian, initial_θ)
integrator = Leapfrog(initial_ϵ)
kernel = HMCKernel(Trajectory{EndPointTS}(integrator, FixedIntegrationTime(λ)))
adaptor = StanHMCAdaptor(MassMatrixAdaptor(metric), StepSizeAdaptor(δ, integrator))
adaptor = StepSizeAdaptor(δ, initial_ϵ)
hmcda = HMCSampler(kernel, metric, adaptor)
```

Expand All @@ -217,14 +218,14 @@ This can be done as follows:
nuts = NUTS(δ, metric = :dense) #metric = DenseEuclideanMetric(D)
# Provide your own AbstractMetric
metric = DiagEuclideanMetric(10)
nuts = NUTS(n_adapt, δ, metric = metric)
nuts = NUTS(δ, metric = metric)

nuts = NUTS(δ, integrator = :leapfrog) #integrator = Leapfrog(ϵ) (Default!)
nuts = NUTS(δ, integrator = :jitteredleapfrog) #integrator = JitteredLeapfrog(ϵ, 0.1ϵ)
nuts = NUTS(δ, integrator = :temperedleapfrog) #integrator = TemperedLeapfrog(ϵ, 1.0)

# Provide your own AbstractIntegrator
integrator = JitteredLeapfrog(ϵ, 0.2ϵ)
integrator = JitteredLeapfrog(0.1, 0.2)
nuts = NUTS(δ, integrator = integrator)
```

Expand All @@ -237,7 +238,7 @@ A small working example can be found at `test/cuda.jl`.
## API and supported HMC algorithms

An important design goal of AdvancedHMC.jl is modularity; we would like to support algorithmic research on HMC.
This modularity means that different HMC variants can be easily constructed by composing various components, such as preconditioning metric (i.e., mass matrix), leapfrog integrators, trajectories (static or dynamic), and adaption schemes, etc.
This modularity means that different HMC variants can be easily constructed by composing various components, such as preconditioning metric (i.e., mass matrix), leapfrog integrators, trajectories (static or dynamic), adaption schemes, etc.
The minimal example above can be modified to suit particular inference problems by picking components from the list below.

### Hamiltonian mass matrix (`metric`)
Expand Down
10 changes: 5 additions & 5 deletions src/abstractmcmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,14 @@ end

#########

function make_adaptor(
spl::Union{NUTS,HMCDA},
metric::AbstractMetric,
integrator::AbstractIntegrator,
)
function make_adaptor(spl::NUTS, metric::AbstractMetric, integrator::AbstractIntegrator)
return StanHMCAdaptor(MassMatrixAdaptor(metric), StepSizeAdaptor(spl.δ, integrator))
end

function make_adaptor(spl::HMCDA, metric::AbstractMetric, integrator::AbstractIntegrator)
return StepSizeAdaptor(spl.δ, integrator)
end

function make_adaptor(spl::HMC, metric::AbstractMetric, integrator::AbstractIntegrator)
return NoAdaptation()
end
Expand Down
2 changes: 1 addition & 1 deletion test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ include("common.jl")
(
HMCDA(T(0.8), 1, integrator = Leapfrog(T(0.1))),
(
adaptor_type = StanHMCAdaptor,
adaptor_type = NesterovDualAveraging,
metric_type = DiagEuclideanMetric{T},
integrator_type = Leapfrog{T},
),
Expand Down

0 comments on commit bb4b534

Please sign in to comment.