ValueError: 概率不总和为1

huangapple go评论58阅读模式
英文:

ValueError: probabilities do not sum to 1

问题

我不明白为什么会出现以下数值错误,概率不会加起来等于1。这是由于我的联合分布吗?是NumPy错误吗?任何帮助都将不胜感激。

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]])

# 初始化X和Y的起始值
x = 0
y = 0

# 创建数组来存储追踪图
trace_x = []
trace_y = []

# 运行10,000次Gibbs采样迭代
for i in range(10000):
    # 根据当前的Y值随机采样X的新值
    x = np.random.choice([0, 1, 2], p=p[y])
    # 根据当前的X值随机采样Y的新值
    y = np.random.choice([0, 1, 2], p=p[:, x])
    # 将新值添加到追踪图中
    trace_x.append(x)
    trace_y.append(y)

# 绘制追踪图
plt.plot(trace_x)
plt.title('X的追踪图')
plt.xlabel('迭代')
plt.ylabel('值')
plt.show()

plt.plot(trace_y)
plt.title('Y的追踪图')
plt.xlabel('迭代')
plt.ylabel('值')
plt.show()
英文:

Don't understand why am getting the following value error probabilities do not sum to 1. Is this due to my joint distribution? a numpy error? any help greatly appreciated.

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()

答案1

得分: 1

You're providing P(X, Y=y) rather than P(X | Y=y). There's a scaling term missing because P(X | Y=y) = P(X, Y=y) / P(Y=y). In other words, the rows and columns of the joint distribution shouldn't add up to one. You need to normalize them yourself to get the conditional distributions.

# 给定当前的Y值,从中采样X的新值
py = np.sum(p[y])
x = np.random.choice([0, 1, 2], p=p[y] / py)
# 给定当前的X值,从中采样Y的新值
px = np.sum(p[:, x])
y = np.random.choice([0, 1, 2], p=p[:, x] / px)
英文:

You're providing P(X, Y=y) rather than P(X | Y=y). There's a scaling term missing because P(X | Y=y) = P(X, Y=y) / P(Y=y). In other words the rows and columns of the joint distribution shouldn't add to one. You need to normalize them yourself to get the conditional distributions.

# 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)

huangapple
  • 本文由 发表于 2023年5月8日 00:04:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定