Skip to content

Commit

Permalink
Convinience constructors (#325)
Browse files Browse the repository at this point in the history
* Lab

* first draft

* cleaning Lab a lil

* Coded interface in

* Coded interface in

* working on no glue code from the other end

* trying stuff

* no glue code abstract mcmc

* AbstractMCMC working

* constructors + moving stuff to init_step

* Scaling things

* axing hmc

* HMC + HMCDA

* return sampler to master

* getmodel

* small step forward

* big step forward

* huge step forward

* fixed constructors

* constructors reworked

* linking

* constructors for tors PR

* constructors for tors PR

* no sampling needed anymore

* no Dynamic PPL

* convinience constructors consensus

* kwargs --> args

* bring back sample

* Update src/abstractmcmc.jl

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

* Update src/constructors.jl

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

* Update src/constructors.jl

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

* Update src/abstractmcmc.jl

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

* Update src/constructors.jl

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

* Update src/constructors.jl

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

* Update src/constructors.jl

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

* Update src/constructors.jl

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

* Update src/constructors.jl

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

* Update src/constructors.jl

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

* Update src/constructors.jl

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

* Update src/constructors.jl

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

* Update src/constructors.jl

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

* Update src/constructors.jl

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

* Apply suggestions from code review

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

* format

* fixing tests

* legacy sample

* load order

* formatting locally

* HMCSampler

* Taking in some of David s advince

* HMCSampler outside of Sample

* formatted + some new tests

* make functions not exported

* remove mentions to old constructors

* formatting

* save rng in state

* demo test

* specific tests for constructors

* remove mention of old constructors in tests

* integrator definition missing from test

* LeapFrong --> leapfrong

* leapfrog --> leapfrong(0.0) for correct type in default

* better typing

* bug

* dummy integrator

* bug

* forgot to change old sample signature

* bug

* forgot old sample signature

* retest is broken

* docs

* readme

* rm Lab

* bump vers

* move docs to file

* remove sample

* Apply Tor's suggestions

Co-authored-by: Tor Erlend Fjelde <[email protected]>

* move exports

* Fields in docs

* docs

* no rng in state

* metric type and integration method can be symbols

* Apply suggestions from code review

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

* kwargs

* Apply suggestions from code review

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

* bug

* format

* David s latest

* Apply suggestions from code review

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

* Update test/sampler.jl

Co-authored-by: Tor Erlend Fjelde <[email protected]>

* Apply suggestions from code review

Co-authored-by: Tor Erlend Fjelde <[email protected]>

* Tor s latest

* 0 means

* bug + float32 init test

* disentangle steps

* return of kappa

* more tests

* Apply suggestions from code review

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

* no kwdef on HMCSampler

* better get type + get_step_size

* format

* bug

* metric tweak

* integrator tweaks

* not needed

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tor Erlend Fjelde <[email protected]>
  • Loading branch information
3 people authored Jul 20, 2023
1 parent 4b2bee0 commit a7edfa9
Show file tree
Hide file tree
Showing 15 changed files with 504 additions and 169 deletions.
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.4.6"
version = "0.5.0"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ If you are interested in using AdvancedHMC.jl through a probabilistic programmin
- We presented a poster for AdvancedHMC.jl at [StanCon 2019](https://mc-stan.org/events/stancon2019Cambridge/) in Cambridge, UK. ([pdf](https://github.com/TuringLang/AdvancedHMC.jl/files/3730367/StanCon-AHMC.pdf))

**API CHANGES**
- [v0.5.0] **Breaking!** Convinience constructors for common samplers changed to:
- `HMC(init_ϵ::Float64=init_ϵ, n_leapfrog::Int=n_leapfrog)`
- `NUTS(n_adapts::Int=n_adapts, δ::Float64=δ)`
- `HMCDA(n_adapts::Int=n_adapts, δ::Float64=δ, λ::Float64=λ)`
- [v0.2.22] Three functions are renamed.
- `Preconditioner(metric::AbstractMetric)` -> `MassMatrixAdaptor(metric)` and
- `NesterovDualAveraging(δ, integrator::AbstractIntegrator)` -> `StepSizeAdaptor(δ, integrator)`
Expand Down
9 changes: 4 additions & 5 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ Documentation for AdvancedHMC.jl
## Structs
```@docs
ClassicNoUTurn
HMCSampler
HMC
NUTS
HMCDA
```

## Functions

```@docs
sample
```

## Index

```@index
```
43 changes: 7 additions & 36 deletions src/AdvancedHMC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,6 @@ export Trajectory,

# Useful defaults

struct NUTS{TS,TC} end

"""
$(SIGNATURES)
Convenient constructor for the no-U-turn sampler (NUTS).
This falls back to `HMCKernel(Trajectory{TS}(int, TC(args...; kwargs...)))` where
- `TS<:Union{MultinomialTS, SliceTS}` is the type for trajectory sampler
- `TC<:Union{ClassicNoUTurn, GeneralisedNoUTurn, StrictGeneralisedNoUTurn}` is the type for termination criterion.
See [`ClassicNoUTurn`](@ref), [`GeneralisedNoUTurn`](@ref) and [`StrictGeneralisedNoUTurn`](@ref) for details in parameters.
"""
NUTS{TS,TC}(int::AbstractIntegrator, args...; kwargs...) where {TS,TC} =
HMCKernel(Trajectory{TS}(int, TC(args...; kwargs...)))
NUTS(int::AbstractIntegrator, args...; kwargs...) =
HMCKernel(Trajectory{MultinomialTS}(int, GeneralisedNoUTurn(args...; kwargs...)))
NUTS::AbstractScalarOrVec{<:Real}) =
HMCKernel(Trajectory{MultinomialTS}(Leapfrog(ϵ), GeneralisedNoUTurn()))

export NUTS

# Deprecations for trajectory.jl

abstract type AbstractTrajectory end
Expand All @@ -103,25 +81,14 @@ struct StaticTrajectory{TS} end
Trajectory{EndPointTS}(Leapfrog(ϵ), FixedNSteps(L)),
)

struct HMCDA{TS} end
@deprecate HMCDA{TS}(int::AbstractIntegrator, λ) where {TS} HMCKernel(
Trajectory{TS}(int, FixedIntegrationTime(λ)),
)
@deprecate HMCDA(int::AbstractIntegrator, λ) HMCKernel(
Trajectory{EndPointTS}(int, FixedIntegrationTime(λ)),
)
@deprecate HMCDA::AbstractScalarOrVec{<:Real}, λ) HMCKernel(
Trajectory{EndPointTS}(Leapfrog(ϵ), FixedIntegrationTime(λ)),
)

@deprecate find_good_eps find_good_stepsize

export StaticTrajectory, HMCDA, find_good_eps
export StaticTrajectory, find_good_eps

include("adaptation/Adaptation.jl")
using .Adaptation
import .Adaptation:
StepSizeAdaptor, MassMatrixAdaptor, StanHMCAdaptor, NesterovDualAveraging
StepSizeAdaptor, MassMatrixAdaptor, StanHMCAdaptor, NesterovDualAveraging, NoAdaptation

# Helpers for initializing adaptors via AHMC structs

Expand Down Expand Up @@ -161,13 +128,17 @@ export StepSizeAdaptor,
WelfordVar,
WelfordCov,
NaiveHMCAdaptor,
StanHMCAdaptor
StanHMCAdaptor,
NoAdaptation

include("diagnosis.jl")

include("sampler.jl")
export sample

include("constructors.jl")
export HMCSampler, HMC, NUTS, HMCDA

include("abstractmcmc.jl")

## Without explicit AD backend
Expand Down
Loading

0 comments on commit a7edfa9

Please sign in to comment.