import numpy as np
import matplotlib.pyplot as plt
p = np.array([[10/66, 15/66, 3/66], [20/66, 12/66, 0], [6/66, 0, 0]])
# Initialize starting values for X and Y
x = 0
y = 0
# Create arrays to store trace plots
trace_x = []
trace_y = []
# Run 10,000 iterations of Gibbs sampling
for i in range(10000):
# Sample a new value for X given the current value of Y
x = np.random.choice([0, 1, 2], p=p[y])
# Sample a new value for Y given the current value of X
y = np.random.choice([0, 1, 2], p=p[:, x])
# Append the new values to the trace plots
trace_x.append(x)
trace_y.append(y)
# Plot the trace plots
plt.plot(trace_x)
plt.title('Trace plot for X')
plt.xlabel('Iteration')
plt.ylabel('Value')
plt.show()
plt.plot(trace_y)
plt.title('Trace plot for Y')
plt.xlabel('Iteration')
plt.ylabel('Value')
plt.show()
# Sample a new value for X given the current value of Y
py = np.sum(p[y])
x = np.random.choice([0, 1, 2], p=p[y] / py)
# Sample a new value for Y given the current value of X
px = np.sum(p[:, x])
y = np.random.choice([0, 1, 2], p=p[:, x] / px)