Skip to content
DeepForgeby svx
All problems
HardProbabilitypr-134

Gibbs Sweep (Seeded)

Problem

One Gibbs sweep for a two-variable binary model with joint table joint, where joint[x][y] is the joint probability. Starting from x = start (0 or 1), sample y from P(Y | X = x), then resample x from P(X | Y = y) using random.Random(seed). Return the new state as [x, y].

Hint

Normalize each conditional by the appropriate row or column sum.

Test cases

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


def gibbs_sweep(joint, start, seed):
    # Your code here
    pass

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