我有numpy,statsmodel,pandas和scipy(我认为)
如何计算p值的z得分,反之亦然?
例如,如果我的p值为0.95,我应该得到1.96作为回报。
我在scipy中看到了一些函数,但它们只在数组上运行z-test。
答案 0 :(得分:89)
>>> import scipy.stats as st
>>> st.norm.ppf(.95)
1.6448536269514722
>>> st.norm.cdf(1.64)
0.94949741652589625
正如其他用户所说,Python默认计算左/下尾概率。如果要确定包含95%分布的密度点,则必须采用另一种方法:
>>>st.norm.ppf(.975)
1.959963984540054
>>>st.norm.ppf(.025)
-1.960063984540054
答案 1 :(得分:7)
从Python 3.8
开始,标准库提供NormalDist
对象作为statistics
模块的一部分。
它可以用来获取zscore
,法线曲线下x%的区域位于此from statistics import NormalDist
NormalDist().inv_cdf((1 + 0.95) / 2.)
# 1.9599639845400536
NormalDist().cdf(1.9599639845400536) * 2 - 1
# 0.95
(忽略两条尾巴)。
使用标准正态分布上的inv_cdf
(逆累积分布函数)和cdf
(累积分布函数),我们可以从彼此获得反之亦然:
data = json.dumps(json_lines)
'(1 + 0.95)/ 2'的说明。公式可以在此wikipedia部分中找到。
答案 2 :(得分:0)
如果你对T-test感兴趣,可以做类似的:
总结:如果样本量大于 30,则 z 分布和 t 分布几乎相同,可以使用任何一种。如果有总体标准差且样本量大于 30,则可以用总体标准差代替样本标准差使用 t 分布。
测试 统计 |
查找 表格 |
查找 值 |
关键 值 |
正常 分布 |
人口 标准 偏差(sigma) |
样本 大小 |
---|---|---|---|---|---|---|
z-statistics | z-table | z-score | z-critical 是特定置信水平下的 z-score | 是 | 已知 | > 30 |
t-statistics | t-table | t-score | t-critical 是特定置信水平的 t-score | 是 | 未知 | <30 |
Python 百分比函数用于计算特定置信度的临界值:
= stats.norm.ppf(1 - alpha) (use alpha = alpha/2 for two-sided)
= stats.t.ppf(alpha/numOfTails, ddof)
import numpy as np
from scipy import stats
# alpha to critical
alpha = 0.05
n_sided = 2 # 2-sided test
z_crit = stats.norm.ppf(1-alpha/n_sided)
print(z_crit) # 1.959963984540054
# critical to alpha
alpha = stats.norm.sf(z_crit) * n_sided
print(alpha) # 0.05