该算法的预期抛硬币次数是多少?

发布于 2024-12-28 22:07:29 字数 493 浏览 3 评论 0原文

假设我有一枚有偏差的硬币。翻转时,出现正面的概率为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花辞树 2025-01-04 22:07:29

假设 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 from doUnfairFlip() 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 also lambda: we can expect about 0.32 function invocations per loop iteration, or 1.0 / 0.32 = 3.125 iterations per invocation, or 6.25 calls to doUnfairFlip() per invocation.

笑梦风尘 2025-01-04 22:07:29

如果您所说的是,

P(rand() % 2 = 0) = 0.8
P(rand() % 2 = 1) = 0.2

那么满足循环条件的概率为

0.8*0.8 + 0.2*0.2 = 0.68

当 p = 0.68(即 3.125)时,您将执行循环与预期失败的次数相同的次数。因此,您应该期望运行循环 3.125 次,并总共调用 rand() 6.25 次。

If what you're saying is that

P(rand() % 2 = 0) = 0.8
P(rand() % 2 = 1) = 0.2

Then the probability of meeting the loop condition is

0.8*0.8 + 0.2*0.2 = 0.68

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.

撑一把青伞 2025-01-04 22:07:29

我认为大约运行了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文