人工智能/模式识别/机器学习精华专题量化分析自然语言处理 (让机器更懂你)

评估一元边缘分布的正态性

2019-06-16  本文已影响1人  readilen

1 检查是否对称

一般来说,统计量较小的时候使用点图,n较大的时候使用直方图,可以揭示一元分布的一个尾部比另一个长的多的情况.
例子

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt  
from scipy import stats
from matplotlib import style

mu, sigma = 0, 0.
s = np.random.normal(mu, sigma, 1000)
f, ax = plt.subplots(figsize=(10, 7))
plt.scatter(x=range(len(s)), y=s, c='r')
plt.xlim(0,500)
plt.show()
散点图
sns.distplot(s)
直方图

是不是很对称啊
我们熟悉一下更多的正态分布样图

style.use('fivethirtyeight')
mu_params = [-1, 0, 1]
sd_params = [0.5, 1, 1.5]
x = np.linspace(-7, 7, 100)
f, ax = plt.subplots(len(mu_params), len(sd_params), sharex=True, sharey=True, figsize=(12,8))
for i in range(3):
    for j in range(3):
        mu = mu_params[i]
        sd = sd_params[j]
        y = stats.norm(mu, sd).pdf(x)
        ax[i, j].plot(x, y)
        ax[i, j].plot(0,0, label='mu={:3.2f}\nsigma={:3.2f}'.format(mu,sd), alpha=0)
        ax[i, j].legend(fontsize=10)
ax[2,1].set_xlabel('x', fontsize=16)
ax[1,0].set_ylabel('pdf(x)', fontsize=16)
plt.suptitle('Gaussian PDF', fontsize=16)
plt.tight_layout()
plt.show()
更多分布图

2 区间检查

一元正态分布属于区间(μ-σ,μ+σ)内的取值概率为0.683,
属于区间(μ-2σ,μ+2σ)内的取值概率为0.954
在(μ-3σ,μ+3σ)内的取值概率为0.997

sigma = 2
list(map(lambda x: stats.norm.cdf(x*sigma,loc=0,scale=sigma) - stats.norm.cdf(-x*sigma,loc=0,scale=sigma), range(1, 6)))

[0.6826894921370859,
 0.9544997361036416,
 0.9973002039367398,
 0.9999366575163338,
 0.9999994266968562]

3 QQ图

QQ图(Quantile Quantile Plot)有两个作用,1检查一组数据是否服从同一分布,2检查两个分布是否服从同一分布
QQ图原理是比较两组数据的累计分布函数来判断两组数据是否是服从同一分布,所以第一步我们应该做两组数据的累计分布。首先,作为对比我们看一下标准正太分布的累计分布图

x = np.linspace(-5, 5, 100)
y = stats.norm.cdf(x, 0, 1)
plt.plot(x, y)
正态分布累计图

然后,绘制目标数据(这里使用随机生成的数据集)的累计分布函数图

x = np.random.normal(0, 1, 100)
sorted_ = np.sort(x)
yvals = np.arange(len(sorted_))/float(len(sorted_))
plt.plot(sorted_, yvals)
实际的累计图

直观上对比,目标累计分布函数图和标准正太累计分布函数图差异不大,事实是不是这样呢?最后我们就可以做qq图做对比。

stats.probplot(x, dist="norm", plot=plt)
plt.show()
image.png

除了正态分布,我们还可以检测t分布(自动度较小)

nsample = 100
plt.figure(figsize=(10, 8))
x = stats.t.rvs(3, size=nsample)
res = stats.probplot(x, plot=plt)
t分布

检测t分布(自动度较大)

nsample = 100
plt.figure(figsize=(10, 8))
x = stats.t.rvs(25, size=nsample)
res = stats.probplot(x, plot=plt)
t分布(自动度较大)

两种正态分布的混合

nsample = 100
plt.figure(figsize=(10, 8))
x = stats.norm.rvs(loc=[0,5], scale=[1,1.5],size=(nsample,2)).ravel()
res = stats.probplot(x, plot=plt)
两种正态分布的混合

loggamma分布

plt.figure(figsize=(10, 8))
x = stats.loggamma.rvs(c=2.5, size=500)
stats.probplot(x, dist=stats.loggamma, sparams=(2.5,), plot=plt)
loggamma分布
上一篇 下一篇

猜你喜欢

热点阅读