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

feat: support position-dependent kinetic #369

Merged
merged 13 commits into from
Jul 10, 2024
15 changes: 12 additions & 3 deletions src/hamiltonian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ end
∂H∂r(h::Hamiltonian{<:DenseEuclideanMetric,<:GaussianKinetic}, r::AbstractVecOrMat) =
h.metric.M⁻¹ * r

# TODO make the order of θ and r consistent with neg_energy
# TODO add stricter types to block hamiltonian.jl#L37 from working on unknown metric/kinetic
xukai92 marked this conversation as resolved.
Show resolved Hide resolved
∂H∂θ(h::Hamiltonian, θ::AbstractVecOrMat, r::AbstractVecOrMat) = ∂H∂θ(h, θ)
xukai92 marked this conversation as resolved.
Show resolved Hide resolved
∂H∂r(h::Hamiltonian, θ::AbstractVecOrMat, r::AbstractVecOrMat) = ∂H∂r(h, r)

struct PhasePoint{T<:AbstractVecOrMat{<:AbstractFloat},V<:DualValue}
θ::T # Position variables / model parameters.
r::T # Momentum variables
Expand Down Expand Up @@ -156,7 +161,7 @@ phasepoint(
rng::Union{AbstractRNG,AbstractVector{<:AbstractRNG}},
θ::AbstractVecOrMat{T},
h::Hamiltonian,
) where {T<:Real} = phasepoint(h, θ, rand(rng, h.metric, h.kinetic))
) where {T<:Real} = phasepoint(h, θ, rand(rng, h.metric, h.kinetic, θ))

abstract type AbstractMomentumRefreshment end

Expand All @@ -168,7 +173,7 @@ refresh(
::FullMomentumRefreshment,
h::Hamiltonian,
z::PhasePoint,
) = phasepoint(h, z.θ, rand(rng, h.metric, h.kinetic))
) = phasepoint(h, z.θ, rand(rng, h.metric, h.kinetic, z.θ))

"""
$(TYPEDEF)
Expand Down Expand Up @@ -196,4 +201,8 @@ refresh(
ref::PartialMomentumRefreshment,
h::Hamiltonian,
z::PhasePoint,
) = phasepoint(h, z.θ, ref.α * z.r + sqrt(1 - ref.α^2) * rand(rng, h.metric, h.kinetic))
) = phasepoint(
h,
z.θ,
ref.α * z.r + sqrt(1 - ref.α^2) * rand(rng, h.metric, h.kinetic, z.θ),
)
8 changes: 5 additions & 3 deletions src/metric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ function _rand(
return r
end

# TODO The rand interface should be updated by rand from momentum distribution + optional affine transformation by metric
Base.rand(rng::AbstractRNG, metric::AbstractMetric, kinetic::AbstractKinetic) =
# TODO The rand interface should be updated as "rand from momentum distribution + optional affine transformation by metric"
xukai92 marked this conversation as resolved.
Show resolved Hide resolved
# ignore θ by default unless defined by the specific kinetic (i.e. not position-dependent)
xukai92 marked this conversation as resolved.
Show resolved Hide resolved
Base.rand(rng::AbstractRNG, metric::AbstractMetric, kinetic::AbstractKinetic, _θ) =
xukai92 marked this conversation as resolved.
Show resolved Hide resolved
_rand(rng, metric, kinetic) # this disambiguity is required by Random.rand
Base.rand(
rng::AbstractVector{<:AbstractRNG},
metric::AbstractMetric,
kinetic::AbstractKinetic,
_θ,
xukai92 marked this conversation as resolved.
Show resolved Hide resolved
) = _rand(rng, metric, kinetic)
Base.rand(metric::AbstractMetric, kinetic::AbstractKinetic) =
Base.rand(metric::AbstractMetric, kinetic::AbstractKinetic, _θ) =
xukai92 marked this conversation as resolved.
Show resolved Hide resolved
rand(GLOBAL_RNG, metric, kinetic)
Loading