📜  SciPy – 统计显着性检验

📅  最后修改于: 2022-05-13 01:55:09.274000             🧑  作者: Mango

SciPy – 统计显着性检验

在概率和统计中,我们倾向于计算很多不同的数字,工作并没有就此结束,统计解释这些数字以了解该数字对手头特定问题的重要性是非常重要的。在统计学中,很少有技术可以评估这些数字的重要性。在本文中,让我们讨论Python包支持的一些统计显着性测试。

一些突出和广泛使用的统计显着性检验如下,'

  • 假设检验——单尾检验和双尾检验
  • T检验
  • Kolmogorov Smirnoff 检验

描述性统计术语

均值、中位数、众数、方差和标准差通常被称为中心性和散布的度量。它必须是任何统计数据分析的第一步。

偏度和峰度是测试正态性的两个测试。如果 Skwness 为 0,峰度为 3,则表示数据的分布是完全正态(对称)的。如果我们断定分布是正态的,我们可以推断出很多关于分布的其他参数。

如果偏度值为负,则数据为左偏,如果值为正,则数据为右偏。同样,正峰度表示重尾分布,反之亦然

Python3
import numpy as np
from scipy.stats import describe
 
v = np.random.normal(size=100)
result = describe(v)
 
print(result)


Python3
from scipy import stats
 
sales_with_chipotle = [13.4, 10.9, 11.2, 11.8,
                       14, 15.3, 14.2, 12.6,
                       17, 16.2, 16.5, 15.7]
 
sales_without_chipotle = [12, 11.7, 10.7, 11.2,
                          14.8, 14.4, 13.9, 13.7,
                          16.9, 16, 15.6, 16]
 
t_value, p_value = stats.ttest_ind(sales_with_chipotle,
                                   sales_without_chipotle)
 
print('Test statistic is %f' % float("{:.6f}".format(t_value)))
print('p-value for two tailed test is %f' % p_value)
 
alpha = 0.05
if p_value <= alpha:
 
    print('Conclusion', 'n', 'Since p-value(=%f)' % p_value,
          '<', 'alpha(=%.2f)' % alpha,
          '''We reject the null hypothesis H0. \
          So we conclude that the \
          Mean sales is affected by \
          adding chipotle sauce to the best\
          selling ingredient i.e., μ1 = μ2 at \
          %.2f level of significance.''' % alpha)
 
else:
 
    print('Conclusion', 'n', 'Since p-value(=%f)' % p_value,
          '>', 'alpha(=%.2f)' % alpha,
          'We fail to reject the null \
          hypothesis H0, which signifies \
          the Mean of sales is not affected\
          by adding chipotle sauce.')


Python3
import numpy as np
from scipy.stats import kstest
 
v = np.random.uniform(size=1000)
 
res = kstest(v, 'uniform')
 
print(res)
       
alpha = 0.05
 
if res[1] > alpha:
    print('We fail to reject the null hypothesis because \
    the p-value {res[1]} > alpha, which means the\
    distribution follows the uniform distribution')
 
else:
    print('We  reject the null hypothesis because\
    the p-value {res[1]} < alpha, which means the\
    distribution doesn"t follows the uniform distribution')


输出:

假设检验

假设检验是一种统计检验,它使用样本数据得出关于总体参数的结论。假设检验是通过定义实验和检验假设的统计显着性来进行的。

假设检验的参数包括 p 值和 alpha 值(指定显着性水平)。 p 值衡量的是获得比您从实验中获得的值更极端的值的概率。 Alpha,即显着性水平,是当实际上它为真时,你会犯错误拒绝原假设的概率。如果 p 值 <= alpha,我们拒绝原假设并说数据具有统计显着性。否则,我们接受原假设。

假设检验有两种类型:

  • 一尾测试
  • 双尾测试

一尾测试

单尾检验是一种统计检验,其中分布的临界区域是一侧的,因此它大于或小于某个值,但不能同时存在。

双尾测试

双尾检验是一种方法,其中分布的临界区域是两侧的,并测试样本是否大于或小于某个值范围。

现在,让我们使用假设检验的概念来演示 t 检验。

T检验

T 检验用于确定研究中两组的平均值之间是否存在显着差异。此检验假设数据遵循 t 分布,这是正态分布的轻微变化。

示例:让我们尝试测试以下假设。例如,一家名为“X”的公司希望测试平均销售额是否会受到在其最畅销菜肴中添加墨西哥辣椒酱的影响。现在,让我们为这个问题构建一个假设。

此问题的置信区间选择为 95%,因此 alpha 值 = 0.05。我们知道如果 p <= alpha 拒绝原假设。我们利用 scipy 中的 ttest_ind()函数进行 t 检验,如下所示。

Python3

from scipy import stats
 
sales_with_chipotle = [13.4, 10.9, 11.2, 11.8,
                       14, 15.3, 14.2, 12.6,
                       17, 16.2, 16.5, 15.7]
 
sales_without_chipotle = [12, 11.7, 10.7, 11.2,
                          14.8, 14.4, 13.9, 13.7,
                          16.9, 16, 15.6, 16]
 
t_value, p_value = stats.ttest_ind(sales_with_chipotle,
                                   sales_without_chipotle)
 
print('Test statistic is %f' % float("{:.6f}".format(t_value)))
print('p-value for two tailed test is %f' % p_value)
 
alpha = 0.05
if p_value <= alpha:
 
    print('Conclusion', 'n', 'Since p-value(=%f)' % p_value,
          '<', 'alpha(=%.2f)' % alpha,
          '''We reject the null hypothesis H0. \
          So we conclude that the \
          Mean sales is affected by \
          adding chipotle sauce to the best\
          selling ingredient i.e., μ1 = μ2 at \
          %.2f level of significance.''' % alpha)
 
else:
 
    print('Conclusion', 'n', 'Since p-value(=%f)' % p_value,
          '>', 'alpha(=%.2f)' % alpha,
          'We fail to reject the null \
          hypothesis H0, which signifies \
          the Mean of sales is not affected\
          by adding chipotle sauce.')

输出:

Kolmogorov Smirnoff 检验

Kolmogorov-Smirnov 检验用于检验一组数据来自相似分布的原假设。 KS 检验 执行一个样本或两个样本的拟合优度检验。单样本检验将样本的基本分布 F(x) 与给定分布 G(x) 进行比较。双样本检验比较两个独立样本的基本分布。这两个测试仅对连续分布有效。

示例:在这里,我们使用随机函数生成了遵循均匀分布的 1000 个值的样本。然后我们导入 kstest()函数来测试随机函数是否正确生成均匀分布。让我们选择 alpha 值为 0.05

p 值是使用 kstest函数返回的测试统计信息计算的。如果 pvalue> alpha,我们不能拒绝原假设,否则接受备择假设

Python3

import numpy as np
from scipy.stats import kstest
 
v = np.random.uniform(size=1000)
 
res = kstest(v, 'uniform')
 
print(res)
       
alpha = 0.05
 
if res[1] > alpha:
    print('We fail to reject the null hypothesis because \
    the p-value {res[1]} > alpha, which means the\
    distribution follows the uniform distribution')
 
else:
    print('We  reject the null hypothesis because\
    the p-value {res[1]} < alpha, which means the\
    distribution doesn"t follows the uniform distribution')

输出: