Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-270

Metropolis Acceptance Ratio

Problem

Metropolis-Hastings acceptance probability for a proposal from x to y: min(1, (pi_y * q_xy) / (pi_x * q_yx)), where pi are target densities and q are proposal densities. Return 0.0 when any density is nonpositive, and 1.0 when the ratio reaches or exceeds 1.

Hint

The Hastings correction keeps detailed balance by comparing the reverse proposal density.

Test cases

case 1
in: [1,2,1,1]
expected: 1
case 2
in: [2,1,1,1]
expected: 0.5
case 3
in: [1,1,0.5,2]
expected: 0.25
case 4
in: [1,1,1,1]
expected: 1
case 5
in: [0,1,1,1]
expected: 0
Starter codePython
def metropolis_acceptance_ratio(pi_x, pi_y, q_xy, q_yx):
    # Your code here
    pass

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