该算法的预期抛硬币次数是多少?
假设我有一枚有偏差的硬币。翻转时,出现正面的概率为 4/5。
为了假装公平的抛掷,我采用了以下算法来模拟这种情况。假设 True
代表头部,False
代表尾部。
P(doUnfairFlip() = 0) = 0.8
和
P(doUnfairFlip() = 1) = 0.2
def fakeFairToss():
flip1 = 0
flip2 = 0
while (flip1 == flip2):
flip1 = doUnfairFlip()
flip2 = doUnfairFlip()
return (True if (flip1 == 0) else False)
它利用了这样一个事实,即在两次抛硬币后,获得正面-反面或反面-正面的可能性相同。
每次运行此函数时,我应该预期该有偏差的硬币会翻转多少次?
Suppose I have a biased coin. When flipped, the probability of yielding heads is 4/5.
To fake a fair toss, I pursue the following algorithm that models this situation. Suppose True
models heads, and False
represents tails.
P(doUnfairFlip() = 0) = 0.8
and
P(doUnfairFlip() = 1) = 0.2
def fakeFairToss():
flip1 = 0
flip2 = 0
while (flip1 == flip2):
flip1 = doUnfairFlip()
flip2 = doUnfairFlip()
return (True if (flip1 == 0) else False)
It makes use of the fact that one is equally likely to get a heads-tails or a tails-heads after two coin flips.
How many individual flips of this biased coin should I expect every time this function runs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设
doUnfairFlip()
中的样本为 IID。我们可以将这种情况视为无限的迭代序列,偶尔会被函数返回“打断”,而不是考虑每个函数调用的循环迭代。请注意,当相等失败时,函数返回恰好发生,
100 - 68 = 32%
的时间。现在,我们可以将这种情况识别为离散泊松过程,其中
lambda = 0.32.相应分布的平均值也是 lambda:我们预计每次循环迭代大约有 0.32 次函数调用,或者每次调用有 1.0 / 0.32 = 3.125 次迭代,或每次调用
6.25
调用doUnfairFlip()
。The odds of equality are
1/5^2 + 4/5^2 = 17/25 = 68%
, assuming samples fromdoUnfairFlip()
are IID.Instead of thinking of loop iterations per function invocation, we can look at the situation as a unbounded sequence of iterations occasionally "punctuated" by function returns. Notice a function return occurs precisely when equality fails,
100 - 68 = 32%
of the time.We can now identify the situation as a discrete Poisson process, with
lambda = 0.32
. The mean of corresponding distribution is alsolambda
: we can expect about0.32
function invocations per loop iteration, or1.0 / 0.32 = 3.125
iterations per invocation, or6.25
calls todoUnfairFlip()
per invocation.如果您所说的是,
那么满足循环条件的概率为
当 p = 0.68(即 3.125)时,您将执行循环与预期失败的次数相同的次数。因此,您应该期望运行循环 3.125 次,并总共调用 rand() 6.25 次。
If what you're saying is that
Then the probability of meeting the loop condition is
You will execute the loop as many times as the expected number of times to get a failure when p = 0.68, which is 3.125. So you should expect to run the loop 3.125 times, and call rand() a total of 6.25 times.
我认为大约运行了 1.5 次 while 循环,或者 3 次调用 rand()。但我的数学可能是错的。
I think its about 1.5 runs of the while loop, or 3 calls to rand(). My math may be wrong though.