使用张量流概率解决掷骰子和抛硬币的问题,方差是错误的

时间:2019-02-07 20:02:44

标签: python tensorflow statistics tensorflow-probability

我不擅长统计学,我想学习。所以,请忍受我。我在Quora中看到了this问题-基本上说明了以下问题-

  

如果结果是奇数,则掷骰子,然后掷硬币   被扔了3次。否则,如果结果为偶数,则为公平   硬币将被抛2次。在这两种情况下,均计为正面数。   获得的#个头的方差是多少?

我想使用Python和tf-probability解决它。这是我所做的-

import tensorflow as tf
import tensorflow_probability as tfp
import numpy as np
tf.enable_eager_execution()
probs = [1/6.] * 6

dices = tfp.distributions.Multinomial(total_count=1000, probs=probs)

n = dices.sample()

HEAD = 1
TAIL = 0
l = list(n.numpy())
heads_even = []
heads_odd = []
for i, nums in enumerate(l):
    mul_by = 3 if (i + 1) % 2 != 0 else 2
    tosses = tfp.distributions.Bernoulli(probs=0.5)
    coin_flip_data = tosses.sample(nums * mul_by)
    l2 = coin_flip_data.numpy()
    unique, counts = np.unique(l2, return_counts=True)
    head_tails = dict(zip(unique, counts))
    if (i + 1) % 2 != 0:
        heads_odd.append(head_tails[HEAD])
    else:
        heads_even.append(head_tails[HEAD])

total_heads = heads_odd + heads_even
final_nd_arr = np.array(total_heads)
print(final_nd_arr.var())

但是,final_nd_arr.var()当然离实际答案还很远(它是2089.805555555556),0.68(正如人们在Quora答案中所提到的)。

我无法找出我做错了什么。我该如何纠正我的错误?

任何指针都会有所帮助。提前谢谢。

---------编辑

要提供更多数据,

dices.sample() => array([169., 173., 149., 171., 175., 163.], dtype=float32)
heads_odd => [266, 210, 259]
heads_even => [176, 167, 145]
total_heads => [266, 210, 259, 176, 167, 145]

1 个答案:

答案 0 :(得分:2)

您正在计算错误分布的方差。我们正在寻找的方差适用于实验,您可以一遍又一遍地掷骰子,每次计数杆头数,然后计算杆头数的方差。您正在代码中执行此操作,但是您求和所有骰子掷骰的总人数,然后针对每个骰子可能的结果取这些和的方差。

这将给出正确的结果。我添加了一些希望可以澄清它的评论:

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