Skip to content

Commit 1913cbc

Browse files
author
AoifeHughes
committed
added requested docstrings
1 parent cabe73f commit 1913cbc

File tree

1 file changed

+76
-3
lines changed

1 file changed

+76
-3
lines changed

src/mcmc/hmc.jl

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,46 @@ end
8282

8383
DynamicPPL.initialsampler(::Sampler{<:Hamiltonian}) = SampleFromUniform()
8484

85-
# Handle setting `nadapts` and `discard_initial`
85+
"""
86+
sample(
87+
rng::AbstractRNG,
88+
model::DynamicPPL.Model,
89+
sampler::Sampler{<:AdaptiveHamiltonian},
90+
N::Integer;
91+
nadapts=sampler.alg.n_adapts,
92+
discard_adapt=true,
93+
discard_initial=-1,
94+
kwargs...
95+
)
96+
97+
Sample from `model` using an adaptive Hamiltonian sampler (NUTS or HMCDA).
98+
99+
This method handles adaptation and warm-up for adaptive Hamiltonian samplers.
100+
101+
# Keyword Arguments
102+
103+
- `nadapts::Int`: Number of adaptation steps. During these steps, the sampler adapts its
104+
step size and mass matrix. Defaults to the sampler's `n_adapts` value. If set to `-1`
105+
(the default for convenience constructors like `NUTS()`), automatically becomes
106+
`min(1000, N ÷ 2)`.
107+
108+
- `discard_adapt::Bool`: Whether to discard the adaptation samples from the returned chain.
109+
Defaults to `true`. When `true`, the adaptation samples are not included in the final results,
110+
as they may not be from the target distribution whilst the sampler is still adapting.
111+
112+
- `discard_initial::Int`: Number of initial samples to discard from the chain. Defaults to `-1`
113+
(automatic). When `-1`, this becomes `nadapts` if `discard_adapt` is `true`, otherwise `0`.
114+
Use this to manually specify how many initial samples to discard.
115+
116+
- `initial_params`: Initial parameter values for sampling. See `DynamicPPL.initialstep` for details.
117+
118+
Additional keyword arguments are passed to the underlying sampling implementation.
119+
120+
# Note
121+
122+
When resuming from a previous run using `resume_from`, adaptation is disabled
123+
(`nadapts=0`, `discard_adapt=false`, `discard_initial=0`).
124+
"""
86125
function AbstractMCMC.sample(
87126
rng::AbstractRNG,
88127
model::DynamicPPL.Model,
@@ -175,6 +214,41 @@ function find_initial_params(
175214
)
176215
end
177216

217+
"""
218+
initialstep(
219+
rng::AbstractRNG,
220+
model::AbstractModel,
221+
spl::Sampler{<:Hamiltonian},
222+
vi::AbstractVarInfo;
223+
initial_params=nothing,
224+
nadapts=0,
225+
verbose::Bool=true,
226+
kwargs...
227+
)
228+
229+
Perform the initial step for Hamiltonian Monte Carlo sampling.
230+
231+
This function initialises the Hamiltonian, finds a suitable step size (if not provided),
232+
and performs the first sampling step.
233+
234+
# Keyword Arguments
235+
236+
- `initial_params`: Initial parameter values to use for sampling. If `nothing` (the default),
237+
parameters are resampled from the prior until valid initial values with finite log probability
238+
and gradient are found. If provided, these values are used directly without validation.
239+
Must be in the same format as the model's parameters.
240+
241+
- `nadapts::Int`: Number of adaptation steps to be performed. Used internally to set up adaptation.
242+
Defaults to `0`.
243+
244+
- `verbose::Bool`: Whether to print informative messages (e.g., the automatically determined step size).
245+
Defaults to `true`.
246+
247+
# Note
248+
249+
If automatic initial parameter search fails after many attempts, an error is raised with
250+
suggestions for how to proceed. Consider providing explicit `initial_params` if this occurs.
251+
"""
178252
function DynamicPPL.initialstep(
179253
rng::AbstractRNG,
180254
model::AbstractModel,
@@ -389,7 +463,7 @@ NUTS() # Use default NUTS configuration.
389463
NUTS(1000, 0.65) # Use 1000 adaption steps, and target accept ratio 0.65.
390464
```
391465
392-
Arguments:
466+
# Arguments
393467
394468
- `n_adapts::Int` : The number of samples to use with adaptation.
395469
- `δ::Float64` : Target acceptance rate for dual averaging.
@@ -398,7 +472,6 @@ Arguments:
398472
- `init_ϵ::Float64` : Initial step size; 0 means automatically searching using a heuristic procedure.
399473
- `adtype::ADTypes.AbstractADType` : The automatic differentiation (AD) backend.
400474
If not specified, `ForwardDiff` is used, with its `chunksize` automatically determined.
401-
402475
"""
403476
struct NUTS{AD,metricT<:AHMC.AbstractMetric} <: AdaptiveHamiltonian
404477
n_adapts::Int # number of samples with adaption for ϵ

0 commit comments

Comments
 (0)