【概率统计】S05E03 离散型随机变量的分布与度量

# 3.离散型随机变量的分布列

0 1 2 其他
$P_X(x)$ 1/4 1/2 1/4 0

$P(X>0)=\sum_{x=1}^{2}{p_X(x)}=P_X(1)+P_X(2)=1/2+1/4=3/4$

# 5.常见的离散型随机变量及其分布

## 5.1.二项分布及二项随机变量

### 5.1.1.分布列及PMF图

$p_X(k)=P(X=k)=\begin{pmatrix} n\\ k \end{pmatrix}p^k(1-p)^{n-k}$

import numpy as np
from scipy.stats import binom
import matplotlib.pyplot as plt
import seaborn
seaborn.set()

k = np.arange(0, 11)

fig, ax = plt.subplots(2, 1)
fig.suptitle('binomial distribution PMF')
binom_rv_0 = binom(n=10, p=0.5)
binom_rv_1 = binom(n=10, p=0.25)

ax[0].plot(k, binom_rv_0.pmf(k), 'bo', ms=8)
ax[0].vlines(k, 0, binom_rv_0.pmf(k), colors='b', lw=5, alpha=0.5)
ax[0].set_title('n=10,p=0.5')

ax[1].plot(k, binom_rv_1.pmf(k), 'ro', ms=8)
ax[1].vlines(k, 0, binom_rv_1.pmf(k), colors='r', lw=5, alpha=0.5)
ax[1].set_title('n=10,p=0.25')

plt.show()

### 5.1.2.随机变量的模拟

import numpy as np
from scipy.stats import binom
import matplotlib.pyplot as plt
import seaborn
seaborn.set()

fig, ax = plt.subplots(2, 1)
fig.suptitle('binomial distribution RVS')
binom_rv_0 = binom(n=10, p=0.5)
binom_rv_1 = binom(n=10, p=0.25)

rvs_0 = binom_rv_0.rvs(size=100000)
rvs_1 = binom_rv_1.rvs(size=100000)

ax[0].hist(rvs_0, bins=11, normed=True)
ax[0].set_title('n=10,p=0.5')

ax[1].hist(rvs_1, bins=11, normed=True)
ax[1].set_title('n=10,p=0.25')

print('rvs_0:{}'.format(rvs_0))
print('rvs_1:{}'.format(rvs_1))
plt.show()

rvs_0:[7 4 7 ..., 7 4 6]
rvs_1:[2 0 3 ..., 5 4 3]

### 5.1.3.随机变量的数字特征

import numpy as np
from scipy.stats import binom

binom_rv = binom(n=10, p=0.25)
mean, var, skew, kurt = binom_rv.stats(moments='mvsk')

binom_rvs = binom_rv.rvs(size=100000)
E_sim = np.mean(binom_rvs)
S_sim = np.std(binom_rvs)
V_sim = S_sim * S_sim

print('mean={},var={}'.format(mean,var))
print('E_sim={},V_sim={}'.format(E_sim,V_sim))
print('E=np={},V=np(1-p)={}'.format(10 * 0.25,10 * 0.25 * 0.75))

mean=2.5,var=1.875
E_sim=2.50569,V_sim=1.8735076238999997
E=np=2.5,V=np(1-p)=1.875