-
Notifications
You must be signed in to change notification settings - Fork 48
Consolidate RHMC Changes #481
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
base: qqy/NEW_RMHMC
Are you sure you want to change the base?
Changes from all commits
502949d
312b2d9
bd583e9
2020f2d
4fe83b9
dfcbb53
ce20e4b
a5373d9
d96c29c
7d4a86f
8f1ebc5
981932e
9cac513
061e096
9688206
d96d939
ca4d2bd
346a129
ae057b5
703ed09
36bf35d
187aa58
4dbe9fe
3de6002
069c6a8
b61ea01
4d847e2
473cc93
c7090ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -32,11 +32,15 @@ where `ϵ` is the step size of leapfrog integration. | |||||||||||||
| ### Adaptor (`adaptor`) | ||||||||||||||
|
|
||||||||||||||
| - Adapt the mass matrix `metric` of the Hamiltonian dynamics: `mma = MassMatrixAdaptor(metric)` | ||||||||||||||
|
|
||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
| + This is lowered to `UnitMassMatrix`, `WelfordVar` or `WelfordCov` based on the type of the mass matrix `metric` | ||||||||||||||
| + There is an experimental way to improve the *diagonal* mass matrix adaptation using gradient information (similar to [nutpie](https://github.com/pymc-devs/nutpie)), | ||||||||||||||
| currently to be initialized for a `metric` of type `DiagEuclideanMetric` | ||||||||||||||
| via `mma = AdvancedHMC.NutpieVar(size(metric); var=copy(metric.M⁻¹))` | ||||||||||||||
| until a new interface is introduced in an upcoming breaking release to specify the method of adaptation. | ||||||||||||||
|
Comment on lines
+38
to
+40
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| - Adapt the step size of the leapfrog integrator `integrator`: `ssa = StepSizeAdaptor(δ, integrator)` | ||||||||||||||
|
|
||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
| + It uses Nesterov's dual averaging with `δ` as the target acceptance rate. | ||||||||||||||
| - Combine the two above *naively*: `NaiveHMCAdaptor(mma, ssa)` | ||||||||||||||
| - Combine the first two using Stan's windowed adaptation: `StanHMCAdaptor(mma, ssa)` | ||||||||||||||
|
|
@@ -61,12 +65,12 @@ sample( | |||||||||||||
| Draw `n_samples` samples using the kernel `κ` under the Hamiltonian system `h` | ||||||||||||||
|
|
||||||||||||||
| - The randomness is controlled by `rng`. | ||||||||||||||
|
|
||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
| + If `rng` is not provided, the default random number generator (`Random.default_rng()`) will be used. | ||||||||||||||
|
|
||||||||||||||
| - The initial point is given by `θ`. | ||||||||||||||
| - The adaptor is set by `adaptor`, for which the default is no adaptation. | ||||||||||||||
|
|
||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
| + It will perform `n_adapts` steps of adaptation, for which the default is `1_000` or 10% of `n_samples`, whichever is lower. | ||||||||||||||
| - `drop_warmup` specifies whether to drop samples. | ||||||||||||||
| - `verbose` controls the verbosity. | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -59,12 +59,11 @@ export Hamiltonian | |||||||||||||||
|
|
||||||||||||||||
| include("integrator.jl") | ||||||||||||||||
| export Leapfrog, JitteredLeapfrog, TemperedLeapfrog | ||||||||||||||||
| include("riemannian/integrator.jl") | ||||||||||||||||
| export GeneralizedLeapfrog | ||||||||||||||||
|
|
||||||||||||||||
| include("riemannian/metric.jl") | ||||||||||||||||
| export IdentityMap, SoftAbsMap, DenseRiemannianMetric | ||||||||||||||||
|
|
||||||||||||||||
| export AbstractRiemannianMetric, DenseRiemannianMetric, IdentityMap, SoftAbsMap | ||||||||||||||||
| include("riemannian/integrator.jl") | ||||||||||||||||
| export GeneralizedLeapfrog, ImplicitMidpoint | ||||||||||||||||
| include("riemannian/hamiltonian.jl") | ||||||||||||||||
|
|
||||||||||||||||
| include("trajectory.jl") | ||||||||||||||||
|
|
@@ -89,7 +88,7 @@ export find_good_eps | |||||||||||||||
| include("adaptation/Adaptation.jl") | ||||||||||||||||
| using .Adaptation | ||||||||||||||||
| import .Adaptation: | ||||||||||||||||
| StepSizeAdaptor, MassMatrixAdaptor, StanHMCAdaptor, NesterovDualAveraging, NoAdaptation | ||||||||||||||||
| StepSizeAdaptor, MassMatrixAdaptor, StanHMCAdaptor, NesterovDualAveraging, NoAdaptation, PositionOrPhasePoint | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| # Helpers for initializing adaptors via AHMC structs | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -131,6 +130,7 @@ export StepSizeAdaptor, | |||||||||||||||
| MassMatrixAdaptor, | ||||||||||||||||
| UnitMassMatrix, | ||||||||||||||||
| WelfordVar, | ||||||||||||||||
| NutpieVar, | ||||||||||||||||
| WelfordCov, | ||||||||||||||||
| NaiveHMCAdaptor, | ||||||||||||||||
| StanHMCAdaptor, | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,13 +4,13 @@ export Adaptation | |||||
| using LinearAlgebra: LinearAlgebra | ||||||
| using Statistics: Statistics | ||||||
|
|
||||||
| using ..AdvancedHMC: AbstractScalarOrVec | ||||||
| using ..AdvancedHMC: AbstractScalarOrVec, PhasePoint | ||||||
| using DocStringExtensions | ||||||
|
|
||||||
| """ | ||||||
| $(TYPEDEF) | ||||||
|
|
||||||
| Abstract type for HMC adaptors. | ||||||
| Abstract type for HMC adaptors. | ||||||
| """ | ||||||
| abstract type AbstractAdaptor end | ||||||
| function getM⁻¹ end | ||||||
|
|
@@ -21,12 +21,17 @@ function initialize! end | |||||
| function finalize! end | ||||||
| export AbstractAdaptor, adapt!, initialize!, finalize!, reset!, getϵ, getM⁻¹ | ||||||
|
|
||||||
| get_position(x::PhasePoint) = x.θ | ||||||
| get_position(x::AbstractVecOrMat{<:AbstractFloat}) = x | ||||||
| const PositionOrPhasePoint = Union{AbstractVecOrMat{<:AbstractFloat}, PhasePoint} | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||
|
|
||||||
| struct NoAdaptation <: AbstractAdaptor end | ||||||
| export NoAdaptation | ||||||
| include("stepsize.jl") | ||||||
| export StepSizeAdaptor, NesterovDualAveraging | ||||||
|
|
||||||
| include("massmatrix.jl") | ||||||
| export MassMatrixAdaptor, UnitMassMatrix, WelfordVar, WelfordCov | ||||||
| export MassMatrixAdaptor, UnitMassMatrix, WelfordVar, NutpieVar, WelfordCov | ||||||
|
|
||||||
| ## | ||||||
| ## Composite adaptors | ||||||
|
|
@@ -47,18 +52,14 @@ getϵ(ca::NaiveHMCAdaptor) = getϵ(ca.ssa) | |||||
| # TODO: implement consensus adaptor | ||||||
| function adapt!( | ||||||
| nca::NaiveHMCAdaptor, | ||||||
| θ::AbstractVecOrMat{<:AbstractFloat}, | ||||||
| z_or_theta::PositionOrPhasePoint, | ||||||
| α::AbstractScalarOrVec{<:AbstractFloat}, | ||||||
| ) | ||||||
| adapt!(nca.ssa, θ, α) | ||||||
| adapt!(nca.pc, θ, α) | ||||||
| return nothing | ||||||
| end | ||||||
| function reset!(aca::NaiveHMCAdaptor) | ||||||
| reset!(aca.ssa) | ||||||
| reset!(aca.pc) | ||||||
| adapt!(nca.ssa, z_or_theta, α) | ||||||
| adapt!(nca.pc, z_or_theta, α) | ||||||
| return nothing | ||||||
| end | ||||||
|
|
||||||
| initialize!(adaptor::NaiveHMCAdaptor, n_adapts::Int) = nothing | ||||||
| finalize!(aca::NaiveHMCAdaptor) = finalize!(aca.ssa) | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶