|
| 1 | +function rand_angles(rng::AbstractRNG, dim) |
| 2 | + rand(rng, dim - 1) .* vcat(fill(π, dim - 2), 2 * π) |
| 3 | +end |
| 4 | + |
| 5 | +"Special case of `polar2spherical` with dimension equal 2" |
| 6 | +polar2cartesian(θ, d) = d * [cos(θ), sin(θ)] |
| 7 | + |
| 8 | +# ref: https://en.wikipedia.org/wiki/N-sphere#Spherical_coordinates |
| 9 | +function polar2spherical(θs, d) |
| 10 | + cos_lst, sin_lst = cos.(θs), sin.(θs) |
| 11 | + suffixed_cos_lst = vcat(cos_lst, 1) # [cos(θ[1]), cos(θ[2]), ..., cos(θ[d-1]), 1] |
| 12 | + prefixed_cumprod_sin_lst = vcat(1, cumprod(sin_lst)) # [1, sin(θ[1]), sin(θ[1]) * sin(θ[2]), ..., sin(θ[1]) * ... * sin(θ[d-1])] |
| 13 | + return d * prefixed_cumprod_sin_lst .* suffixed_cos_lst |
| 14 | +end |
| 15 | + |
| 16 | +momentum_mode(m, c) = sqrt((1 / c^2 + sqrt(1 / c^2 + 4 * m^2)) / 2) # mode of the momentum distribution |
| 17 | + |
| 18 | +function _rand( |
| 19 | + rng::AbstractRNG, |
| 20 | + metric::UnitEuclideanMetric{T}, |
| 21 | + kinetic::RelativisticKinetic{T}, |
| 22 | +) where {T} |
| 23 | + densityfunc = x -> exp(-relativistic_energy(kinetic, [x])) * x |
| 24 | + mm = momentum_mode(kinetic.m, kinetic.c) |
| 25 | + sampler = RejectionSampler(densityfunc, (0.0, Inf), (mm / 2, mm * 2); max_segments = 5) |
| 26 | + sz = size(metric) |
| 27 | + θs = rand_angles(rng, prod(sz)) |
| 28 | + d = only(run_sampler!(rng, sampler, 1)) |
| 29 | + r = polar2spherical(θs, d * rand(rng, [-1, +1])) # TODO Double check if +/- is needed |
| 30 | + r = reshape(r, sz) |
| 31 | + return r |
| 32 | +end |
| 33 | + |
| 34 | +# TODO Support AbstractVector{<:AbstractRNG} |
| 35 | +# FIXME Unit-test this using slice sampler or HMC sampler |
| 36 | +function _rand( |
| 37 | + rng::AbstractRNG, |
| 38 | + metric::UnitEuclideanMetric{T}, |
| 39 | + kinetic::DimensionwiseRelativisticKinetic{T}, |
| 40 | +) where {T} |
| 41 | + h_temp = Hamiltonian(metric, kinetic, identity, identity) |
| 42 | + densityfunc = x -> exp(neg_energy(h_temp, [x], [x])) |
| 43 | + sampler = RejectionSampler(densityfunc, (-Inf, Inf); max_segments = 5) |
| 44 | + sz = size(metric) |
| 45 | + r = run_sampler!(rng, sampler, prod(sz)) |
| 46 | + r = reshape(r, sz) |
| 47 | + return r |
| 48 | +end |
| 49 | + |
| 50 | +# TODO Support AbstractVector{<:AbstractRNG} |
| 51 | +function _rand( |
| 52 | + rng::AbstractRNG, |
| 53 | + metric::DiagEuclideanMetric{T}, |
| 54 | + kinetic::AbstractRelativisticKinetic{T}, |
| 55 | +) where {T} |
| 56 | + r = _rand(rng, UnitEuclideanMetric(size(metric)), kinetic) |
| 57 | + # p' = A p where A = sqrtM |
| 58 | + r ./= metric.sqrtM⁻¹ |
| 59 | + return r |
| 60 | +end |
| 61 | +# TODO Support AbstractVector{<:AbstractRNG} |
| 62 | +function _rand( |
| 63 | + rng::AbstractRNG, |
| 64 | + metric::DenseEuclideanMetric{T}, |
| 65 | + kinetic::AbstractRelativisticKinetic{T}, |
| 66 | +) where {T} |
| 67 | + r = _rand(rng, UnitEuclideanMetric(size(metric)), kinetic) |
| 68 | + # p' = A p where A = cholM |
| 69 | + ldiv!(metric.cholM⁻¹, r) |
| 70 | + return r |
| 71 | +end |
0 commit comments