如果 Pr(boys)=0.51,则 5 个婴儿中正好有 3 个男孩的概率

Probability of having exactly 3 boys out of 5 babies if Pr(boys)=0.51

假设所有出生的婴儿中有 51% 是男性,并且所有出生都是独立的。我如何计算 R 中正好有 3 个男孩(5 个婴儿中)的概率?我可以算术求解,但在 R 中我还没有找到一个简单的函数。

dbinom(x, n, p) 给出在 n 次试验中 x 次成功的二项式概率质量函数,给定成功概率 p。如果您在终端中键入 ?dbinom,则会给出详细信息。

前提是我没有误解你的问题,在你的情况下,假设有一个男孩的概率为 0.51,那么在 5 children 中恰好看到 3 个男孩的概率是

dbinom(3, 5, prob = 0.51)
#[1] 0.3184951

或者您可能问的是在 5 children 中观察到 ≥3 个男孩的概率?

pbinom(2, 5, prob = 0.51, lower.tail = F)
#[1] 0.518745

(注意这里的 2 不是 typo/mistake,因为 pbinomlower.tail = F 给出 Pr(X > x))。


或者在 5 个 children 中看到 ≤3 个男孩的概率?

pbinom(3, 5, prob = 0.51)
#[1] 0.7997501

更新

我们可以比较手动使用sumdbinom计算二项式累积分布的性能与使用pbinom

x <- 5000
n <- 10000
library(microbenchmark)
res <- microbenchmark(
    sum_mass = sum(dbinom(x:n, n, prob = 0.51)),
    cum_dist = pbinom(x - 1, n, prob = 0.51, lower.tail = F))
#Unit: microseconds
#     expr     min       lq      mean   median       uq      max neval
# sum_mass 736.276 773.8825 832.58220 797.3500 843.2485 1668.000   100
# cum_dist   2.136   2.7845   7.77986   4.0005   7.1765  252.872   100