Created
September 5, 2023 11:40
-
-
Save willtebbutt/15608af78d08d53befbb8a0bcbd74a90 to your computer and use it in GitHub Desktop.
Acceptance rate drops off as dimension increases, despite simple density.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Pkg | |
Pkg.activate(; temp=true) | |
pkg"add AdvancedHMC, LogDensityProblems, LinearAlgebra, Plots, Random, Zygote, Statistics" | |
using AdvancedHMC, LogDensityProblems, LinearAlgebra, Plots, Random, Zygote, Statistics | |
# Define the target distribution using the `LogDensityProblem` interface | |
struct LogTargetDensity | |
dim::Int | |
end | |
LogDensityProblems.logdensity(::LogTargetDensity, θ) = -sum(abs2, θ) / 2 | |
LogDensityProblems.dimension(p::LogTargetDensity) = p.dim | |
function LogDensityProblems.capabilities(::Type{LogTargetDensity}) | |
return LogDensityProblems.LogDensityOrder{1}() | |
end | |
function hmc_problem() | |
Ds = [1, 5, 10, 25, 50, 100, 200, 300, 400, 500] | |
acceptance_rates = map(Ds) do D | |
rng = Xoshiro(123456) | |
hamiltonian = Hamiltonian(DiagEuclideanMetric(D), LogTargetDensity(D), Zygote) | |
kernel = HMCKernel(Trajectory{EndPointTS}(Leapfrog(0.8), FixedNSteps(10))) | |
initial_θ = randn(rng, D) | |
adaptor = NoAdaptation() | |
samples, stats = sample(rng, hamiltonian, kernel, initial_θ, 5_000, adaptor, 0); | |
average_acceptance_rate = mean(map(s -> s.acceptance_rate, stats)) | |
return average_acceptance_rate | |
end | |
savefig( | |
plot( | |
Ds, acceptance_rates; | |
xaxis=:log10, label="", xlabel="dim", ylabel="acceptance rate", | |
), | |
"acceptance_rates.png", | |
) | |
end | |
hmc_problem() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should produce something like this