Python code of binomial distribution
"python code to play with binomial distribution"
Binomial Distribution
import numpy as np
import matplotlib.pyplot as plt
def C_nk (n, k):
# get C(n,k)
res = 1
for i in range(n, n-k,-1):
res *= i
for i in range(k,0,-1):
res /= i
return res
def binomProb(n, k, p):
return C_nk(n,k) * p**k * (1-p)**(n-k)
# n = 10, P(yes) = 0.3; P(no) = 0.7
n = 10
p = 0.3
x = np.arange(n+1)
y = []
for k in x:
y.append(binomProb(n,k,p))
# Draw the Binomial probability distribution
plt.bar(x,y)
plt.show()
import scipy.stats as sps
# all k>2 probability
n = 10
p = 0.3
k = np.arange(n + 1)
PX = sps.binom.pmf(k, n, p)
# 二项分布累计分布
print(sum(PX[2:]))
Bernoulli Distribution
X~B(n,p), when n=1, Binomial distribution becomes Bernoulli Distribution.
伯努利分布、两点分布、0-1分布这三种分布是同一个分布的不同名称,又都是二项分布在n=1时的特例。
概率质量函数(probability mass function,简称PMF)
累积分布函数(cumulative distribution function, 简称CDF)
离散概率分布(Discrete probability distribution)