一般地,如果随机变量
X
X
X服从参数为
n
n
n和
p
p
p的二项分布,我们记
X
∼
b
(
n
,
p
)
X\sim b(n,p)
X∼b(n,p)或
X
∼
B
(
n
,
p
)
X\sim B(n,p)
X∼B(n,p)。n次试验中正好得到k次成功的概率由概率质量函数给出:
f
(
k
,
n
,
p
)
=
Pr
(
X
=
k
)
=
(
a
b
)
p
k
(
1
−
p
)
n
−
k
f(k,n,p) = \Pr(X = k) = \begin{pmatrix} a \\ b \end{pmatrix} p^{k}(1 - p)^{n-k}
f(k,n,p)=Pr(X=k)=(ab)pk(1−p)n−k 对于k = 0, 1, 2, …, n,其中
(
n
k
)
=
n
!
k
!
(
n
−
k
)
!
{n \choose k}={\frac {n!}{k!(n-k)!}}
(kn)=k!(n−k)!n!
是二项式系数(这就是二项分布的名称的由来),又记为
C
(
n
,
k
)
C(n, k)
C(n,k),
n
C
k
nC_k
nCk,或
n
C
k
^nC_k
nCk。该公式可以用以下方法理解:我们希望有k次成功
(
p
k
)
(p^k)
(pk)和
n
−
k
n − k
n−k次失败
(
1
−
p
)
n
−
k
(1 − p)^{n − k}
(1−p)n−k。然而,k次成功可以在n次试验的任何地方出现,而把
k
k
k次成功分布在
n
n
n次试验中共有
C
(
n
,
k
)
C(n, k)
C(n,k)个不同的方法。
numpy.random.binomial(n, p, size=None)Draw samples from a binomial distribution. 表示对一个二项分布进行采样, size 表示采样的次数, n 表示做了n 重伯努利试验, p 表示成功的概率,函数的返回值表示n 中成功的次数。
2. 泊松分布
泊松分布主要用于估计某个时间段某事件发生的概率。
numpy.random.poisson(lam=1.0, size=None)Draw samples from a Poisson distribution. 表示对一个泊松分布进行采样, size 表示采样的次数, lam 表示一个单位内发生事件的平均值,函数的返回值表示一个单位内事件发生的次数。
3. 超几何分布
在超几何分布中,各次实验不是独立的,各次实验成功的概率也不等。
numpy.random.hypergeometric(ngood, nbad, nsample, size=None)
D
r
a
w
s
a
m
p
l
e
s
f
r
o
m
a
H
y
p
e
r
g
e
o
m
e
t
r
i
c
d
i
s
t
r
i
b
u
t
i
o
n
.
Draw samples from a Hypergeometric distribution.
DrawsamplesfromaHypergeometricdistribution. 表示对一个超几何分布进行采样, size 表示采样的次数, ngood 表示总体中具有成功标志的元素个数, nbad 表示总体中不具有成功标志的元素个数, ngood+nbad 表示总体样本容量, nsample 表示抽取元素的次数(小于或等于总体样本容量),函数的返回值表示抽取nsample 个元素中具有成功标识 的元素个数。
二、连续型随机变量的分布
1. 均匀分布
均匀分布可以使用以下方法产生:
numpy.random.uniform(low=0.0, high=1.0, size=None)Draw samples from a uniform distribution. Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform .
numpy.random.randint(low, high=None, size=None, dtype='l')Return random integers from low (inclusive) to high (exclusive). Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high). If high is None (the default), then results are from [0, low).
numpy.random.shuffle(x)Modify a sequence in-place by shuffling its contents. This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same.
numpy.random.permutation(x)Randomly permute a sequence, or return a permuted range. If x is a multi-dimensional array, it is only shuffled along its first index.