@@ -11,23 +11,22 @@ phasepoint(
1111# Negative kinetic energy
1212# ! Eq (13) of Girolami & Calderhead (2011)
1313function neg_energy (
14- h:: Hamiltonian{<:DenseRiemannianMetric, <:GaussianKinetic} ,
14+ h:: Hamiltonian{<:DenseRiemannianMetric,<:GaussianKinetic} ,
1515 r:: T ,
1616 θ:: T ,
1717) where {T<: AbstractVecOrMat }
1818 G = h. metric. map (h. metric. G (θ))
1919 # Need to consider the normalizing term as it is no longer same for different θs
2020 logZ = 1 / 2 * (size (G, 1 ) * log (2 π) + logdet (G)) # it will be user's responsibility to make sure G is SPD and logdet(G) is defined
21-
2221 mul! (h. metric. _temp, inv (G), r)
2322 # ldiv!(h.metric._temp, cholesky(G), r) # https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.ldiv!
2423 return - logZ - dot (r, h. metric. _temp) / 2
2524end
2625
2726function neg_energy (
28- h:: Hamiltonian{<:DenseRiemannianMetric, <:AbstractRelativisticKinetic} ,
27+ h:: Hamiltonian{<:DenseRiemannianMetric,<:AbstractRelativisticKinetic} ,
2928 r:: T ,
30- θ:: T
29+ θ:: T ,
3130) where {T<: AbstractVecOrMat }
3231 G = h. metric. map (h. metric. G (θ))
3332 # Need to consider the normalizing term as it is no longer same for different θs
@@ -39,12 +38,12 @@ function neg_energy(
3938
4039 mul! (h. metric. _temp, inv (G), r)
4140 # ldiv!(h.metric._temp, cholesky(G), r) # https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.ldiv!
42- return - relativistic_energy (h. kinetic, r, h. metric. _temp) - logZ_partial
41+ return - relativistic_energy (h. kinetic, r, h. metric. _temp) - logZ_partial
4342end
4443
4544# QUES L31 of hamiltonian.jl now reads a bit weird (semantically)
4645function ∂H∂θ (
47- h:: Hamiltonian{<:DenseRiemannianMetric{T,<:IdentityMap}, <:GaussianKinetic} ,
46+ h:: Hamiltonian{<:DenseRiemannianMetric{T,<:IdentityMap},<:GaussianKinetic} ,
4847 θ:: AbstractVecOrMat{T} ,
4948 r:: AbstractVecOrMat{T} ,
5049) where {T}
@@ -68,7 +67,7 @@ function ∂H∂θ(
6867end
6968
7069function ∂H∂θ (
71- h:: Hamiltonian{<:DenseRiemannianMetric{T,<:IdentityMap}, <:AbstractRelativisticKinetic} ,
70+ h:: Hamiltonian{<:DenseRiemannianMetric{T,<:IdentityMap},<:AbstractRelativisticKinetic} ,
7271 θ:: AbstractVecOrMat{T} ,
7372 r:: AbstractVecOrMat{T} ,
7473) where {T}
@@ -130,7 +129,8 @@ function ∂H∂θ_cache(
130129 term_2 = Q * D * J * D * Q'
131130
132131 isdiag = ∂H∂θ isa AbstractMatrix
133- g = isdiag ?
132+ g =
133+ isdiag ?
134134 - (∂ℓπ∂θ - 1 / 2 * diag (term_1_cached * ∂H∂θ) + 1 / 2 * diag (term_2 * ∂H∂θ)) :
135135 - mapreduce (vcat, 1 : d) do i
136136 ∂H∂θᵢ = ∂H∂θ[:, :, i]
149149 r:: AbstractVecOrMat{T} ,
150150) where {T} = ∂H∂θ_cache (h, θ, r)
151151function ∂H∂θ_cache (
152- h:: Hamiltonian{<:DenseRiemannianMetric{T,<:SoftAbsMap},<:RelativisticKinetic} ,
153- θ:: AbstractVecOrMat{T} ,
152+ h:: Hamiltonian{<:DenseRiemannianMetric{T,<:SoftAbsMap},<:RelativisticKinetic} ,
153+ θ:: AbstractVecOrMat{T} ,
154154 r:: AbstractVecOrMat{T} ;
155155 return_cache = false ,
156156 cache = nothing ,
@@ -207,11 +207,16 @@ function ∂H∂θ_cache(
207207 # @assert false
208208
209209 isdiag = ∂H∂θ isa AbstractMatrix
210- g = isdiag ?
211- - (∂ℓπ∂θ - 1 / 2 * diag (term_1_cached * ∂H∂θ) + 1 / 2 * (1 / mass) * diag (term_2 * ∂H∂θ)) :
210+ g =
211+ isdiag ?
212+ - (
213+ ∂ℓπ∂θ - 1 / 2 * diag (term_1_cached * ∂H∂θ) +
214+ 1 / 2 * (1 / mass) * diag (term_2 * ∂H∂θ)
215+ ) :
212216 - mapreduce (vcat, 1 : d) do i
213- ∂H∂θᵢ = ∂H∂θ[:,:,i]
214- ∂ℓπ∂θ[i] - 1 / 2 * tr (term_1_cached * ∂H∂θᵢ) + 1 / 2 * (1 / mass) * tr (term_2 * ∂H∂θᵢ) # (v2) cache friendly
217+ ∂H∂θᵢ = ∂H∂θ[:, :, i]
218+ ∂ℓπ∂θ[i] - 1 / 2 * tr (term_1_cached * ∂H∂θᵢ) +
219+ 1 / 2 * (1 / mass) * tr (term_2 * ∂H∂θᵢ) # (v2) cache friendly
215220 end
216221
217222 dv = DualValue (ℓπ, g)
220225
221226# FIXME This implementation for dimensionwise is incorrect
222227function ∂H∂θ (
223- h:: Hamiltonian{<:DenseRiemannianMetric{T,<:SoftAbsMap},<:DimensionwiseRelativisticKinetic} ,
224- θ:: AbstractVecOrMat{T} ,
228+ h:: Hamiltonian {
229+ <: DenseRiemannianMetric{T,<:SoftAbsMap} ,
230+ <: DimensionwiseRelativisticKinetic ,
231+ },
232+ θ:: AbstractVecOrMat{T} ,
225233 r:: AbstractVecOrMat{T} ,
226234) where {T}
227235 ℓπ, ∂ℓπ∂θ = h.∂ℓπ∂θ (θ)
@@ -251,7 +259,7 @@ function ∂H∂θ(
251259
252260 term_2_cached = Q * D * J * D * Q'
253261 g = - mapreduce (vcat, 1 : d) do i
254- ∂H∂θᵢ = ∂H∂θ[:,:, i]
262+ ∂H∂θᵢ = ∂H∂θ[:, :, i]
255263 # ∂ℓπ∂θ[i] - 1 / 2 * tr(term_1_cached * ∂H∂θᵢ) + 1 / 2 * M' * (J .* (Q' * ∂H∂θᵢ * Q)) * M # (v1)
256264 # NOTE Some further optimization can be done here: cache the 1st product all together
257265 ∂ℓπ∂θ[i] - 1 / 2 * term_1_cached[i] * - tr (term_2_cached * ∂H∂θᵢ) # (v2) cache friendly
262270
263271# ! Eq (14) of Girolami & Calderhead (2011)
264272function ∂H∂r (
265- h:: Hamiltonian{<:DenseRiemannianMetric, <:GaussianKinetic} ,
273+ h:: Hamiltonian{<:DenseRiemannianMetric,<:GaussianKinetic} ,
266274 θ:: AbstractVecOrMat ,
267275 r:: AbstractVecOrMat ,
268276)
@@ -277,7 +285,11 @@ function ∂H∂r(
277285 return G \ r # NOTE it's actually pretty weird that ∂H∂θ returns DualValue but ∂H∂r doesn't
278286end
279287
280- function ∂H∂r (h:: Hamiltonian{<:DenseRiemannianMetric, <:AbstractRelativisticKinetic} , θ:: AbstractVecOrMat , r:: AbstractVecOrMat )
288+ function ∂H∂r (
289+ h:: Hamiltonian{<:DenseRiemannianMetric,<:AbstractRelativisticKinetic} ,
290+ θ:: AbstractVecOrMat ,
291+ r:: AbstractVecOrMat ,
292+ )
281293 M = h. metric. map (h. metric. G (θ))
282294 # M⁻¹ = inv(M)
283295 # cholM⁻¹ = cholesky(Symmetric(M⁻¹)).U
0 commit comments