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

Use n_adapts instead of nadapts #375

Merged
merged 5 commits into from
Oct 4, 2024
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AdvancedHMC"
uuid = "0bf59076-c3b1-5ca4-86bd-e02cd72cde3d"
version = "0.6.1"
version = "0.6.2"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ samples = AbstractMCMC.sample(
model,
sampler,
n_adapts + n_samples;
nadapts = n_adapts,
n_adapts = n_adapts,
initial_params = initial_θ,
)
```
Expand Down
38 changes: 35 additions & 3 deletions src/abstractmcmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ function AbstractMCMC.sample(
callback = nothing,
kwargs...,
)
if haskey(kwargs, :nadapts)
throw(
ArgumentError(
"keyword argument `nadapts` is unsupported. Please use `n_adapts` to specify the number of adaptation steps.",
),
)
end

if callback === nothing
callback = HMCProgressCallback(N, progress = progress, verbose = verbose)
progress = false # don't use AMCMC's progress-funtionality
Expand Down Expand Up @@ -78,6 +86,13 @@ function AbstractMCMC.sample(
callback = nothing,
kwargs...,
)
if haskey(kwargs, :nadapts)
throw(
ArgumentError(
"keyword argument `nadapts` is unsupported. Please use `n_adapts` to specify the number of adaptation steps.",
),
)
end

if callback === nothing
callback = HMCProgressCallback(N, progress = progress, verbose = verbose)
Expand Down Expand Up @@ -144,6 +159,14 @@ function AbstractMCMC.step(
n_adapts::Int = 0,
kwargs...,
)
if haskey(kwargs, :nadapts)
throw(
ArgumentError(
"keyword argument `nadapts` is unsupported. Please use `n_adapts` to specify the number of adaptation steps.",
),
)
end

# Compute transition.
i = state.i + 1
t_old = state.transition
Expand Down Expand Up @@ -200,7 +223,16 @@ function HMCProgressCallback(n_samples; progress = true, verbose = false)
HMCProgressCallback(pm, progress, verbose, Ref(0), Ref(0))
end

function (cb::HMCProgressCallback)(rng, model, spl, t, state, i; nadapts = 0, kwargs...)
function (cb::HMCProgressCallback)(
rng,
model,
spl,
t,
state,
i;
n_adapts::Int = 0,
kwargs...,
)
progress = cb.progress
verbose = cb.verbose
pm = cb.pm
Expand Down Expand Up @@ -243,8 +275,8 @@ function (cb::HMCProgressCallback)(rng, model, spl, t, state, i; nadapts = 0, kw
),
)
# Report finish of adapation
elseif verbose && isadapted && i == nadapts
@info "Finished $nadapts adapation steps" adaptor κ.τ.integrator metric
elseif verbose && isadapted && i == n_adapts
@info "Finished $(n_adapts) adapation steps" adaptor κ.τ.integrator metric
end
end

Expand Down
24 changes: 24 additions & 0 deletions test/abstractmcmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ using Statistics: mean
verbose = false,
)

# Error if keyword argument `nadapts` is used
@test_throws ArgumentError AbstractMCMC.sample(
rng,
model,
nuts,
n_adapts + n_samples;
nadapts = n_adapts,
initial_params = θ_init,
progress = false,
verbose = false,
)
@test_throws ArgumentError AbstractMCMC.sample(
rng,
model,
nuts,
MCMCThreads(),
n_adapts + n_samples,
2;
nadapts = n_adapts,
initial_params = θ_init,
progress = false,
verbose = false,
)

# Transform back to original space.
# NOTE: We're not correcting for the `logabsdetjac` here since, but
# we're only interested in the mean it doesn't matter.
Expand Down
2 changes: 1 addition & 1 deletion test/mcmcchains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using Statistics: mean
model,
sampler,
n_adapts + n_samples;
nadapts = n_adapts,
n_adapts = n_adapts,
initial_params = θ_init,
chain_type = Chains,
progress = false,
Expand Down
Loading