几何分布(Geometric distribution)是离散型概率分布。其中一种定义为:在 n n n次伯努利试验中,试验 k k k次才得到第一次成功的机率。详细地说,是:前 k − 1 k-1 k−1次皆失败,第 k k k次成功的概率,每次实验中成功的概率 p p p保持不变。几何分布是帕斯卡分布当 r = 1 r=1 r=1时的特例。
P ( k ) = p ( 1 − p ) k − 1 , k = 1 , 2 , . . . P(k) = p(1 - p)^{k - 1} , k = 1, 2,... P(k)=p(1−p)k−1,k=1,2,...
F ( k ) = 1 − ( 1 − p ) k , k = 1 , 2 , . . . F(k) = 1 - (1 - p)^k,k = 1, 2, ... F(k)=1−(1−p)k,k=1,2,...
E ( k ) = 1 p E(k) = \frac{1}{p} E(k)=p1
V ( k ) = 1 − p p 2 V(k) = \frac{1 - p}{p^2} V(k)=p21−p
当变量 x ′ x' x′定义为实验第一次成功时失败的次数, x ′ = k − 1 x' = k - 1 x′=k−1:
P ( x ′ ) = p ( 1 − p ) x ′ P(x') = p(1 - p)^{x'} P(x′)=p(1−p)x′
F ( x ′ ) = 1 − ( 1 − p ) x ′ + 1 F(x') = 1 - (1 - p)^{x'+1} F(x′)=1−(1−p)x′+1
E ( x ′ ) = E ( x ) − 1 = ( 1 − p ) / p E(x') = E(x) - 1 = (1 - p)/p E(x′)=E(x)−1=(1−p)/p
V ( x ′ ) = V ( x ) = ( 1 − p ) / p 2 V(x') = V(x) = (1 - p)/p^2 V(x′)=V(x)=(1−p)/p2
生成几何分布的随机变量 k k k:
例:假设一个实验成功的概率为 p = 0.2 p = 0.2 p=0.2,随机几何变量 x x x为该实验第一次成功是尝试的次数,生成一个随机几何变量:
import numpy as np
import matplotlib.pyplot as plt
def generate_geometric(p=0.1):
u = np.random.uniform(0, 1)
x = int(np.log(1 - u)/ np.log(1 - p)) + 1
return x