条件骰子
Conditional Die Rolls
第 1 小问
题目详情
Dan 掷骰子,直到获得 。鉴于他“没有”看到 ,Dan 掷骰子的预期次数是多少?
Dan rolls a dice until he gets a . Given that he did not see a , what is the expected number of times Dan rolled his die?
解析
如果 Dan 没有看到 ,则意味着条件是 在 之前出现。当掷一个公平的骰子时,这种情况会发生一半,因此该情况的概率为 。
给定条件下滚动 的概率为 。因为卷是彼此独立的,所以该分布是 的几何分布,结果是: (请注意,如果他没有看到 ,则滚动 的概率不是 。这表明 Dan 滚动 和 的可能性相同,这不可能是真的,因为你知道 出现在 之前,而其他数字不一定是这种情况)
import random
roll_dice = lambda: random.randint(1,6)
sequence_lengths = []
num_iters = 10_000
for i in range(num_iters):
sequence = []
while(True):
sequence.append(roll_dice())
if sequence[-1] == 5:
break
elif sequence[-1] == 6:
sequence_lengths.append(len(sequence))
break
print(sum(sequence_lengths)/len(sequence_lengths))Original Explanation
If Dan did not see a , that means the condition is came before . When rolling a fair dice, this will occur half the time, thus the probability of the condition is .
The probability of rolling a given the condition is . Because rolls are independent of each other, this distribution is geometric with resulting in:
(Note if he does not see the probability of rolling a is not . That suggests Dan is equally likely to roll a and which can not be true since you know comes before which is not necessarily the case with the other numbers)
import random
roll_dice = lambda: random.randint(1,6)
sequence_lengths = []
num_iters = 10_000
for i in range(num_iters):
sequence = []
while(True):
sequence.append(roll_dice())
if sequence[-1] == 5:
break
elif sequence[-1] == 6:
sequence_lengths.append(len(sequence))
break
print(sum(sequence_lengths)/len(sequence_lengths))第 2 小问
题目详情
Dan 掷骰子,直到获得 。鉴于他看到了 ,Dan 掷骰子的预期次数是多少?
Dan rolls a dice until he gets a . Given that he saw a , what is the expected number of times Dan rolled his die?
解析
这与第 1 部分的情况相反,其中第一个 在第一个 之前出现。使用第 1 部分,我们知道找到预期的卷数,看到第一个 是 。
一旦我们看到了第一个,就没有更多的条件了。当没有条件时,滚动 的概率就是 ,因此我们期望在看到第一个 之前滚动骰子 次。要消除这种情况,平均需要掷 次,一旦这种情况消失,我们预计会掷 次,因此 Dan 预计会掷骰子 次。
import random
roll_dice = lambda: random.randint(1,6)
sequence_lengths = []
num_iters = 10_000
for i in range(num_iters):
sequence = []
while(True):
sequence.append(roll_dice())
if sequence[-1] == 6:
if 5 in sequence:
sequence_lengths.append(len(sequence))
break
print(sum(sequence_lengths)/len(sequence_lengths))Original Explanation
This is the opposite case as Part 1 where the first is seen before the first . Using Part 1, we know find the expected number of rolls to see the first is .
Once we have seen the first , there is no more condition. When there is no condition, the probability of rolling a is simply so we expect to roll a die times before seeing our first . To get rid of the condition will take on average rolls and once the condition is gone we expect to roll times therefore Dan can expect to roll the die times.
import random
roll_dice = lambda: random.randint(1,6)
sequence_lengths = []
num_iters = 10_000
for i in range(num_iters):
sequence = []
while(True):
sequence.append(roll_dice())
if sequence[-1] == 6:
if 5 in sequence:
sequence_lengths.append(len(sequence))
break
print(sum(sequence_lengths)/len(sequence_lengths))第 3 小问
题目详情
Dan 掷骰子,直到获得 。鉴于他只看到偶数,丹掷骰子的预期次数是多少?
Dan rolls a dice until he gets a . Given that he saw only even numbers, what is the expected number of times Dan rolled his die?
解析
仅看到偶数意味着 出现在 和 之前。 总数中第一个出现的单个数字的出现概率为 ,因此该条件的概率为 。
使用与第 1 部分类似的逻辑,我们得到在给定的滚动中将 滚动为 的概率,因此:
import random
roll_dice = lambda: random.randint(1,6)
sequence_lengths = []
num_iters = 10_000
for i in range(num_iters):
sequence = []
while(True):
sequence.append(roll_dice())
if sequence[-1] == 1:
break
elif sequence[-1] == 3:
break
elif sequence[-1] == 5:
break
elif sequence[-1] == 6:
sequence_lengths.append(len(sequence))
break
print(sum(sequence_lengths)/len(sequence_lengths))Original Explanation
Seeing only even numbers means came before and . A single number coming first among total numbers has probability of occuring, thus the probability of the condition is .
Using similar logic to Part 1 we get the probability of rolling a in a given roll to be , thus:
import random
roll_dice = lambda: random.randint(1,6)
sequence_lengths = []
num_iters = 10_000
for i in range(num_iters):
sequence = []
while(True):
sequence.append(roll_dice())
if sequence[-1] == 1:
break
elif sequence[-1] == 3:
break
elif sequence[-1] == 5:
break
elif sequence[-1] == 6:
sequence_lengths.append(len(sequence))
break
print(sum(sequence_lengths)/len(sequence_lengths))