R 基础知识 | 汇总统计数据 | 图形显示 | 概率基础知识 | 概率规则 | 条件概率 | 后验概率和贝叶斯 | 可靠性 | 离散分布介绍
概率密度函数
概率密度函数 (pdf) 定义为
P ( X = x ) = q x − 1 p ; x = 1 , 2 , 3 , … P(X=x)=q^{x-1} p ; \quad x=1,2,3, \ldots P(X=x)=qx−1p;x=1,2,3,…
这是在第 x 次试验中第一次成功的概率。
几何随机变量是离散的,因为它的值是整数,并且是无限的,因为从理论上讲,我们可以永远等待成功。
我们可以很容易地检查 ∑ x P ( X = x ) = 1 \sum_{x} P(X=x)=1 ∑xP(X=x)=1
∑ x P ( X = x ) = p + q p + q 2 p + q 3 p + ⋯ = p ( 1 + q + q 2 + q 3 + ⋯ ) = p × 1 1 − q = 1 \begin{aligned}\sum_{x} P(X=x) &=p+q p+q^{2} p+q^{3} p+\cdots \\&=p\left(1+q+q^{2}+q^{3}+\cdots\right)=p \times \frac{1}{1-q}=1\end{aligned} x∑P(X=x)=p+qp+q2p+q3p+⋯=p(1+q+q2+q3+⋯)=p×1−q1=1
记住比率 q < 1 q<1 q<1 的无限几何级数之和是
1 + q + q 2 + q 3 + ⋯ = 1 1 − q = 1 p 1+q+q^{2}+q^{3}+\cdots=\frac{1}{1-q}=\frac{1}{p} 1+q+q2+q3+⋯=1−q1=p1
R代码计算概率密度函数
R 中提供了函数来计算和绘制 pdf。 对于几何分布,我们在缩写名称 geom 前加上“d”,用于 pdf; 函数 dgeom 连同参数 p,即成功的概率,作为参数来计算概率。
例 3 中的结果,找到了第一个缺陷出现在第五个缺陷中的概率,可以在 R 中检查。
dgeom (x = 4, prob = 0.03)
或
dgeom (4, 0.03)
得出
0.02655878
如果我们绘制 pdf,就会出现更清晰的画面。 R代码
par (mfrow = c(2, 2))
x <- 0:4
plot(x+1, dgeom(x, prob = 0.95),
xlab = "X = Number of trials", ylab = "P(X=x)",
type = "h", main = "First workstation, p = 0.95",
font.main = 1)
x <- 0:9
plot(x+1, dgeom(x, prob = 0.5),
xlab = "X = Number of trials", ylab = "P(X=x)",
type = "h", main = "First head, p = 0.5",
font.main = 1)
x <- 0:19
plot(x+1, dgeom(x, prob = 0.2),
xlab = "X = Number of trials", ylab = "P(X=x)",
type = "h", main = "First defective, p = 0.2",
font.main = 1)
x <- seq(0, 400, 50)
plot(x+1, dgeom(x, prob = 0.01),
xlab = "X = Number of trials", ylab = "P(X=x)",
type = "h", main = "First bit in error,
p = 0.01", font.main = 1)
几何分布
import matplotlib.pyplot as plt
def probability_to_occur_at(attempt, probability):
return (1-p)**(attempt - 1) * probability
p = 0.2
attempt = 3
attempts_to_show = range(21)[1:]
print('probability that the event will occur on the 8th try: ', probability_to_occur_at(attempt, p))
plt.xlabel('trials')
plt.ylabel('probability')
barlist = plt.bar(attempts_to_show, height=[probability_to_occur_at(x, p) for x in attempts_to_show], tick_label=attempts_to_show)
barlist[attempt].set_color('r')
plt.show()
probability that the event will occur on the 8th try: 0.12800000000000003
从条形图中可以看出,尝试的最小值为 1,并且没有最大值。
详情参阅 - 亚图跨际