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
16 changes: 13 additions & 3 deletions src/hamiltonian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ end
∂H∂r(h::Hamiltonian{<:DenseEuclideanMetric,<:GaussianKinetic}, r::AbstractVecOrMat) =
h.metric.M⁻¹ * r

# TODO (kai) make the order of θ and r consistent with neg_energy
# TODO (kai) add stricter types to block hamiltonian.jl#L37 from working on unknown metric/kinetic
# The gradient of a position-dependent Hamiltonian system depends on both θ and r.
∂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 +162,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 +174,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 +202,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.θ),
)
18 changes: 17 additions & 1 deletion src/metric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function _rand(
return r
end

# TODO The rand interface should be updated by rand from momentum distribution + optional affine transformation by metric
# 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
Base.rand(rng::AbstractRNG, metric::AbstractMetric, kinetic::AbstractKinetic) =
_rand(rng, metric, kinetic) # this disambiguity is required by Random.rand
Base.rand(
Expand All @@ -139,3 +139,19 @@ Base.rand(
) = _rand(rng, metric, kinetic)
Base.rand(metric::AbstractMetric, kinetic::AbstractKinetic) =
rand(GLOBAL_RNG, metric, kinetic)

# ignore θ by default unless defined by the specific kinetic (i.e. not position-dependent)
Base.rand(
rng::AbstractRNG,
metric::AbstractMetric,
kinetic::AbstractKinetic,
θ::AbstractVecOrMat,
) = rand(rng, metric, kinetic) # this disambiguity is required by Random.rand
Base.rand(
rng::AbstractVector{<:AbstractRNG},
metric::AbstractMetric,
kinetic::AbstractKinetic,
θ::AbstractVecOrMat,
) = rand(rng, metric, kinetic)
Base.rand(metric::AbstractMetric, kinetic::AbstractKinetic, θ::AbstractVecOrMat) =
rand(metric, kinetic)
Loading