Skip to content
DeepForgeby svx
All problems
HardProbabilitypr-133

Metropolis One Step (Seeded)

Problem

One Metropolis step on states 0..k-1 with target distribution target and a uniform symmetric proposal. Propose j = int(rng.random() * k) with random.Random(seed) and accept with probability min(1, target[j] / target[state]). Return the new state as an integer.

Hint

The proposal is symmetric, so the acceptance ratio is just the target ratio.

Test cases

case 1
in: [0,[0.2,0.3,0.5],42]
expected: 1
case 2
in: [1,[0.2,0.3,0.5],7]
expected: 0
case 3
in: [2,[0.2,0.3,0.5],1]
expected: 2
case 4
in: [0,[0.2,0.3,0.5],0]
expected: 2
Starter codePython
import random


def metropolis_step(state, target, seed):
    # Your code here
    pass

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