一. 随机变量
随机变量是一个从样本空间 Ω \Omega Ω到实数空间 R R R的函数,比如随机变量 X X X可以表示投骰子的点数。随机变量一般可以分为两类:
- 离散型随机变量:随机变量的取值为有限个。
- 连续型随机变量:随机变量的取值是连续的,有无限多个。
scipy.stat模块中包含了多种概率分布的随机变量,包含离散型随机变量和连续型随机变量。离散型随机变量的常见接口如下:
方法名 | 功能 |
---|---|
rvs | 生成该分布的随机序列 |
pmf | 概率质量函数 |
cdf | 累计概率分布函数 |
stats | 计算该分布的均值,方差,偏度,峰度。[Mean('m'), variance('v'), skew('s'), kurtosis('k')] |
连续型随机变量的常见接口如下:
方法名 | 功能 |
---|---|
rvs | 生成该分布的随机序列 |
概率密度函数 | |
cdf | 累计概率分布函数 |
stats | 计算该分布的均值,方差,偏度,峰度。[Mean('m'), variance('v'), skew('s'), kurtosis('k')] |
二. 常见离散分布
1. 二项分布
如果随机变量 X X X的分布律为 P ( X = k ) = C n k p k q n − k , k = 0 , 1 , . . . n , P(X=k) = C^k_np^kq^{n-k},k = 0,1,...n, P(X=k)=Cnkpkqn−k,k=0,1,...n,其中 p + q = 1 p + q = 1 p+q=1 ,则称 X X X服从参数为 n , p n,p n,p的二项分布,记为 X ∼ B ( n , p ) X \sim B(n,p) X∼B(n,p)。
- 期望: E ( X ) = n p E(X) = np E(X)=np
- 方差: D ( X ) = n p ( 1 − p ) D(X) = np(1 - p) D(X)=np(1−p)
-
画出不同参数下的二项分布, n , p n, p n,p分别为 ( 10 , 0.3 ) , ( 10 , 0.5 ) , ( 10 , 0.7 ) (10,0.3),(10,0.5),(10,0.7) (10,0.3),(10,0.5),(10,0.7)
pythonimport numpy as np from scipy.stats import binom import matplotlib.pyplot as plt plt.rcParams["font.family"] = "SimHei" # 设置字体 plt.rcParams["axes.unicode_minus"] = False # 正常显示负号 if __name__ == '__main__': fig, ax = plt.subplots(3, 1, figsize = (10, 10)) # 调整子图间距 fig.subplots_adjust(hspace = 0.5) params = [(10, 0.3), (10, 0.5), (10, 0.7)] for i in range(len(params)): n = params[i][0] p = params[i][1] x = np.arange(0, n + 1) y = binom(n, p).pmf(x) # 计算随机变量的期望,方差 mean, var = binom.stats(n, p, moments='mv') ax[i].scatter(x, y, color = 'blue', marker = 'o') ax[i].set_title('n = {}, p = {}'.format(n, p)) ax[i].set_xticks(x) ax[i].text(1, 0.2, '期望: {:.2f}\n方差: {:.2f}'.format(mean, var)) ax[i].grid() plt.show()
运行结果:
-
生成服从不同参数二项分布的随机数组(采样100000次),然后查看数组的频率分布
pythonimport numpy as np from scipy.stats import binom import matplotlib.pyplot as plt plt.rcParams["font.family"] = "SimHei" # 设置字体 plt.rcParams["axes.unicode_minus"] = False # 正常显示负号 if __name__ == '__main__': fig, ax = plt.subplots(3, 1, figsize = (10, 10)) # 调整子图间距 fig.subplots_adjust(hspace = 0.5) params = [(10, 0.3), (10, 0.5), (10, 0.7)] for i in range(len(params)): n = params[i][0] p = params[i][1] x = np.arange(0, 11) # 抽样10万次 sample = binom.rvs(n = n, p = p, size = 100000) print(sample) ax[i].hist(sample, color = 'blue', density=True, bins = 50) ax[i].set_title('n = {}, p = {}'.format(n, p)) ax[i].set_xticks(x) ax[i].grid() plt.show()
运行结果:
2. 几何分布
若随机变量 X X X的分布律为 P ( X = k ) = ( 1 − p ) k − 1 p , k = 1 , 2 , . . . , P(X = k) = (1 - p)^{k - 1}p,k = 1, 2, ..., P(X=k)=(1−p)k−1p,k=1,2,...,其中 0 < p < 1 0 < p < 1 0<p<1,则称 X X X服从参数为 p p p的几何分布,记为 X ∼ G e ( p ) X \sim Ge(p) X∼Ge(p)。
- 期望: E ( X ) = 1 p E(X) = \frac{1}{p} E(X)=p1
- 方差: D ( X ) = 1 − p p 2 D(X) = \frac{1 - p}{p^2} D(X)=p21−p
-
画出不同参数下的几何分布, p p p分别为 ( 0.3 , 0.5 , 0.7 ) (0.3,0.5,0.7) (0.3,0.5,0.7)
pythonimport numpy as np from scipy.stats import geom import matplotlib.pyplot as plt plt.rcParams["font.family"] = "SimHei" # 设置字体 plt.rcParams["axes.unicode_minus"] = False # 正常显示负号 if __name__ == '__main__': fig, ax = plt.subplots(3, 1, figsize = (10, 10)) # 调整子图间距 fig.subplots_adjust(hspace = 0.5) params = [0.3,0.5,0.7] for i in range(len(params)): p = params[i] x = np.arange(1, 15) y = geom(p = p).pmf(x) print(y) # 计算随机变量的期望,方差 mean, var = geom.stats(p = p, moments='mv') ax[i].scatter(x, y, color = 'blue', marker = 'o') ax[i].set_title('p = {}'.format(p)) ax[i].set_xticks(x) ax[i].text(5, 0.2, '期望: {:.2f}\n方差: {:.2f}'.format(mean, var)) ax[i].grid() plt.show()
运行结果:
-
生成服从不同参数几何分布的随机数组(采样100000次),然后查看数组的频率分布
pythonimport numpy as np from scipy.stats import geom import matplotlib.pyplot as plt plt.rcParams["font.family"] = "SimHei" # 设置字体 plt.rcParams["axes.unicode_minus"] = False # 正常显示负号 if __name__ == '__main__': fig, ax = plt.subplots(3, 1, figsize = (10, 10)) # 调整子图间距 fig.subplots_adjust(hspace = 0.5) params = [0.3, 0.5, 0.7] for i in range(len(params)): p = params[i] x = np.arange(0, 15) # 抽样 sample = geom.rvs(p = p, size = 100000) print(sample) ax[i].hist(sample, color = 'blue', density=True, bins = 50) ax[i].set_title('p = {}'.format(p)) ax[i].set_xlim(0,15) ax[i].set_xticks(x) ax[i].grid() plt.show()
运行结果:
3. 泊松分布
若随机变量 X X X的分布律为 P ( X = k ) = λ k k ! e − λ , k = 0 , 1 , 2... , P(X=k) = \frac{\lambda^k}{k!}e^{-\lambda},k = 0, 1, 2 ..., P(X=k)=k!λke−λ,k=0,1,2...,其中 λ > 0 , \lambda > 0, λ>0,则称 X X X服从参数为 λ \lambda λ的泊松分布,记为 X ∼ P ( λ ) X \sim P(\lambda) X∼P(λ)。
- 期望: E ( X ) = λ E(X) = \lambda E(X)=λ
- 方差: D ( X ) = λ D(X) = \lambda D(X)=λ
-
画出不同参数下的泊松分布, λ \lambda λ分别为 ( 2 , 6 , 8 ) (2,6,8) (2,6,8)
pythonimport numpy as np from scipy.stats import poisson import matplotlib.pyplot as plt plt.rcParams["font.family"] = "SimHei" # 设置字体 plt.rcParams["axes.unicode_minus"] = False # 正常显示负号 if __name__ == '__main__': fig, ax = plt.subplots(3, 1, figsize = (10, 10)) # 调整子图间距 fig.subplots_adjust(hspace = 0.5) params = [2,6,8] for i in range(len(params)): numda = params[i] x = np.arange(1, 15) y = poisson(numda).pmf(x) # 计算随机变量的期望,方差 mean, var = poisson.stats(numda, moments='mv') ax[i].scatter(x, y, color = 'blue', marker = 'o') ax[i].set_title('lambda = {}'.format(numda)) ax[i].set_xticks(x) ax[i].set_yticks([0, 0.1, 0.2, 0.3, 0.4]) ax[i].text(5, 0.2, '期望: {:.2f}\n方差: {:.2f}'.format(mean, var)) ax[i].grid() plt.show()
运行结果:
-
生成服从不同参数泊松分布的随机数组(采样100000次),然后查看数组的频率分布
pythonimport numpy as np from scipy.stats import poisson import matplotlib.pyplot as plt plt.rcParams["font.family"] = "SimHei" # 设置字体 plt.rcParams["axes.unicode_minus"] = False # 正常显示负号 if __name__ == '__main__': fig, ax = plt.subplots(3, 1, figsize = (10, 10)) # 调整子图间距 fig.subplots_adjust(hspace = 0.5) params = [2, 6, 8] for i in range(len(params)): numda = params[i] x = np.arange(0, 16) # 抽样 sample = poisson.rvs(numda, size = 1000000) print(sample) ax[i].hist(sample, color = 'blue', density=True, bins = 50) ax[i].set_title('lamdba = {}'.format(numda)) ax[i].set_xticks(x) ax[i].set_xlim(0, 16) ax[i].grid() plt.show()
运行结果:
三. 常见连续分布
1. 正太分布
若随机变量 X X X的概率密度函数为 f ( x ) = 1 2 π δ e − ( x − μ ) 2 2 δ 2 , ( − ∞ < x < + ∞ ) f(x) = \frac{1}{\sqrt{2\pi}\delta}e^{- \frac{(x - \mu)^2}{2\delta^2}},( -\infty< x < +\infty) f(x)=2π δ1e−2δ2(x−μ)2,(−∞<x<+∞),则称 X X X服从参数为 ( μ , δ 2 ) (\mu,\delta^2) (μ,δ2)的正太分布,记为 X ∼ N ( μ , δ 2 ) X \sim N(\mu,\delta^2) X∼N(μ,δ2)。当 μ = 0 , δ = 1 \mu =0,\delta = 1 μ=0,δ=1时称 X X X服从标准正太分布。
- 期望: E ( X ) = μ E(X) = \mu E(X)=μ
- 方差: D ( X ) = δ 2 D(X) = \delta^2 D(X)=δ2
-
画出不同参数下的正太分布, μ , δ \mu,\delta μ,δ分别为 ( 0 , 1 ) , ( 0 , 3 ) (0, 1), (0, 3) (0,1),(0,3)
pythonimport numpy as np from scipy.stats import norm import matplotlib.pyplot as plt plt.rcParams["font.family"] = "SimHei" # 设置字体 plt.rcParams["axes.unicode_minus"] = False # 正常显示负号 if __name__ == '__main__': fig, ax = plt.subplots(figsize=(10, 8)) params = [(0, 1, 'red'), (0, 3, 'blue')] x = np.linspace(-20, 20, 1000) for i in range(0, len(params)): loc = params[i][0] scale = params[i][1] color = params[i][2] mean, var = norm.stats(loc, scale, moments='mv') ax.plot(x, norm(loc = loc, scale = scale).pdf(x), color = color, label = 'loc={},scale={},均值={},方差={}'.format(loc, scale,mean,var)) ax.set_xticks(np.arange(-20, 21)) ax.grid() ax.legend() plt.show()
-
生成服从不同参数正太分布的随机数组(采样100000次),然后查看数组的频率分布
pythonimport numpy as np from scipy.stats import norm import matplotlib.pyplot as plt plt.rcParams["font.family"] = "SimHei" # 设置字体 plt.rcParams["axes.unicode_minus"] = False # 正常显示负号 if __name__ == '__main__': fig, ax = plt.subplots(2, 1, figsize=(10, 8)) params = [(0, 1, 'red'), (0, 3, 'blue')] x = np.linspace(-20, 20, 1000) # 采样 for i in range(0, len(params)): loc = params[i][0] scale = params[i][1] color = params[i][2] # 画出分布图 ax[i].plot(x, norm(loc = loc, scale = scale).pdf(x), color = color, label = 'loc={},scale={}'.format(loc, scale)) # 画出随机抽样的频率分布直方图 ax[i].hist(norm(loc = loc, scale = scale).rvs(size = 100000), density=True, bins = 100) ax[i].set_xticks(np.arange(-20, 21)) ax[i].grid() ax[i].legend() plt.show()
2. 指数分布
若随机变量 X X X的概率密度函数为 f ( x ) = { λ e − λ x x ≥ 0 0 x < 0 ( λ > 0 ) f(x) = \begin{cases} {\lambda}e^{-{\lambda}x} & x \ge 0\\0 & x < 0\end{cases} (\lambda > 0) f(x)={λe−λx0x≥0x<0(λ>0),则称 X X X服从参数为 λ \lambda λ的指数分布,记为 X ∼ E ( λ ) X \sim E(\lambda) X∼E(λ)。
- 期望: E ( X ) = 1 λ E(X) = \frac{1}{\lambda} E(X)=λ1
- 方差: D ( X ) = 1 λ 2 D(X) = \frac{1}{{\lambda}^2} D(X)=λ21
scipy中指数分布expon的参数传入 λ \lambda λ的倒数。
A common parameterization for expon is in terms of the rate parameter lambda, such that pdf = lambda * exp(-lambda * x). This parameterization corresponds to using scale = 1 / lambda.
-
画出不同参数下的指数分布, λ \lambda λ分别为 ( 0.5 , 1 , 1.5 ) (0.5,1,1.5) (0.5,1,1.5)
pythonimport numpy as np import matplotlib.pyplot as plt from scipy.stats import expon plt.rcParams["font.family"] = "SimHei" # 设置字体 plt.rcParams["axes.unicode_minus"] = False # 正常显示负号 if __name__ == '__main__': fig, ax = plt.subplots(figsize = (10, 8)) params = [(0.5, 'red'), (1, 'blue'), (1.5, 'green')] x = np.linspace(0, 15, 1000) for i in range(0, len(params)): numda = params[i][0] color = params[i][1] mean, var = expon.stats(loc = 0, scale = 1 / numda, moments='mv') ax.plot(x, expon(scale = 1 / numda).pdf(x), color = color, label = 'lambda = {:.2f}, 均值:{:.2f}, 方差: {:.4f}'.format(numda, mean, var)) ax.grid() ax.legend() plt.show()
-
生成服从不同参数指数分布的随机数组(采样100000次),然后查看数组的频率分布
pythonimport numpy as np import matplotlib.pyplot as plt from scipy.stats import expon plt.rcParams["font.family"] = "SimHei" # 设置字体 plt.rcParams["axes.unicode_minus"] = False # 正常显示负号 if __name__ == '__main__': fig, ax = plt.subplots(3, 1, figsize = (10, 8)) params = [(0.5, 'red'), (1, 'blue'), (1.5, 'green')] x = np.linspace(0, 15, 1000) # 采样 for i in range(0, len(params)): numda = params[i][0] color = params[i][1] ax[i].plot(x, expon(scale = 1/numda).pdf(x), color = color, label = 'lambda={}'.format(numda)) ax[i].hist(expon(scale = 1/numda).rvs(size = 10000), density=True, bins = 100) ax[i].set_xticks(np.arange(0, 15)) ax[i].set_xlim(0, 15) ax[i].grid() ax[i].legend() plt.show()