Skip to content

Instantly share code, notes, and snippets.

@willtebbutt
Created September 5, 2023 11:40
Show Gist options
  • Save willtebbutt/15608af78d08d53befbb8a0bcbd74a90 to your computer and use it in GitHub Desktop.
Save willtebbutt/15608af78d08d53befbb8a0bcbd74a90 to your computer and use it in GitHub Desktop.
Acceptance rate drops off as dimension increases, despite simple density.
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()
@willtebbutt
Copy link
Author

Should produce something like this
acceptance_rates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment