Skip to content

Commit

Permalink
Make integer constraint flexible (#126)
Browse files Browse the repository at this point in the history
* add a note

* fix type def
  • Loading branch information
xukai92 committed Nov 4, 2019
1 parent dff94e3 commit b3aa67d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/integrator.jl
Expand Up @@ -2,13 +2,17 @@
#### Numerical methods for simulating Hamiltonian trajectory.
####

# TODO: The type `<:Tuple{Integer,Bool}` is introduced to address
# https://github.com/TuringLang/Turing.jl/pull/941#issuecomment-549191813
# We might want to simplify it to `Tuple{Int,Bool}` when we figured out
# why the it behaves unexpected on Windos 32.

abstract type AbstractIntegrator end

abstract type AbstractLeapfrog{T} <: AbstractIntegrator end

jitter(::AbstractRNG, ::AbstractLeapfrog, ϵ) = ϵ
temper(lf::AbstractLeapfrog, r, ::NamedTuple{(:i, :is_half),Tuple{Int,Bool}}, ::Int) = r
temper(lf::AbstractLeapfrog, r, ::NamedTuple{(:i, :is_half),<:Tuple{Integer,Bool}}, ::Int) = r

function step(
rng::AbstractRNG,
Expand Down Expand Up @@ -79,13 +83,13 @@ function Base.show(io::IO, l::TemperedLeapfrog)
end

"""
temper(lf::TemperedLeapfrog, r, step::NamedTuple{(:i, :is_half),Tuple{Int,Bool}}, n_steps::Int)
temper(lf::TemperedLeapfrog, r, step::NamedTuple{(:i, :is_half),<:Tuple{Integer,Bool}}, n_steps::Int)
Tempering step. `step` is a named tuple with
- `i` being the current leapfrog iteration and
- `is_half` indicating whether or not it's (the first) half momentum/tempering step
"""
function temper(lf::TemperedLeapfrog, r, step::NamedTuple{(:i, :is_half),Tuple{Int,Bool}}, n_steps::Int)
function temper(lf::TemperedLeapfrog, r, step::NamedTuple{(:i, :is_half),<:Tuple{Integer,Bool}}, n_steps::Int)
i_temper = 2(step.i - 1) + 1 + step.is_half # counter for half temper steps
return i_temper <= n_steps ? r * sqrt(lf.α) : r / sqrt(lf.α)
end

0 comments on commit b3aa67d

Please sign in to comment.