1 回答
TA贡献1844条经验 获得超8个赞
您正在计算错误分布的方差。我们正在寻找的方差适用于你反复掷骰子的实验,每次计算正面的数量,并计算正面数量的方差。您正在代码中执行此操作,但是您正在对所有掷骰子的正面总数求和,然后为每个可能的骰子结果计算这些总和的方差。
这将给出正确的结果。我添加了一些评论,希望能澄清它:
import tensorflow as tf
import tensorflow_probability as tfp
import numpy as np
tf.enable_eager_execution()
# Simulate the outcome of 1000 dice rolls
probs = [1/6.] * 6
dices = tfp.distributions.Multinomial(total_count=1000, probs=probs)
n = dices.sample()
l = list(n.numpy().astype(int))
L = []
# Loop over 6 possible dice outcomes
for i in range(len(l)):
# Loop over the rolls for this dice outcome
for _ in range(l[i]):
# For each of the dice rolls,
# Flip a coin 2 or three times
num_tosses = 3 if (i + 1) % 2 != 0 else 2
tosses = tfp.distributions.Bernoulli(probs=0.5)
coin_flip_data = tosses.sample(num_tosses)
# And count the number of heads
num_heads = np.sum(coin_flip_data.numpy())
L += [num_heads]
np.var(L)
> 0.668999
添加回答
举报