Skip to content
DeepForgeby svx
All problems
EasyProbabilitypr-308

HMC Acceptance Probability

Problem

Hamiltonian Monte Carlo accepts a proposal with probability min(1, exp(H0 - H1)), where the Hamiltonian is H = U + p^2 / (2 * mass). Given the potential energies and momenta before and after a step, return the acceptance probability. Return 0.0 when mass is nonpositive.

Hint

Accept whenever the total energy did not increase; otherwise exponentiate the energy increase.

Test cases

case 1
in: [1,0,1,0,1]
expected: 1
case 2
in: [0,0,1,0,1]
expected: 0.36787944117144233
case 3
in: [0,1,0,1,1]
expected: 1
case 4
in: [0,0,0,1,1]
expected: 0.6065306597126334
case 5
in: [0,0,0,0,0]
expected: 0
Starter codePython
import math


def hmc_acceptance_probability(energy0, momentum0, energy1, momentum1, mass):
    # Your code here
    pass

5 test cases · run your code against them in the editor