在 Python 中将 Z 分数(Z 值,标准分数)转换为正态分布的 p 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3496656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Z-score (Z-value, standard score) to p-value for normal distribution in Python
提问by gotgenes
How does one convert a Z-scorefrom the Z-distribution (standard normal distribution, Gaussian distribution)to a p-value? I have yet to find the magical function in Scipy's statsmoduleto do this, but one must be there.
如何将Z 分数从Z 分布(标准正态分布,高斯分布)转换为p值?我还没有在Scipy 的stats模块中找到神奇的功能来做到这一点,但必须有一个。
采纳答案by Josef
I like the survival function (upper tail probability) of the normal distribution a bit better, because the function name is more informative:
我更喜欢正态分布的生存函数(上尾概率),因为函数名称的信息量更大:
p_values = scipy.stats.norm.sf(abs(z_scores)) #one-sided
p_values = scipy.stats.norm.sf(abs(z_scores))*2 #twosided
normal distribution "norm" is one of around 90 distributions in scipy.stats
正态分布“norm”是 scipy.stats 中大约 90 个分布之一
norm.sf also calls the corresponding function in scipy.special as in gotgenes example
norm.sf 也调用了 scipy.special 中的相应函数,就像在 gotgenes 示例中一样
small advantage of survival function, sf: numerical precision should better for quantiles close to 1 than using the cdf
生存函数的小优势,sf:对于接近 1 的分位数,数值精度应该比使用 cdf 更好
回答by gotgenes
Aha! I found it: scipy.special.ndtr! This also appears to be under scipy.stats.stats.zprobas well (which is just a pointer to ndtr).
啊哈!我找到了:scipy.special.ndtr!这似乎也在下scipy.stats.stats.zprob(这只是指向 的指针ndtr)。
Specifically, given a one-dimensional numpy.arrayinstance z_scores, one can obtain the p-values as
具体来说,给定一个一维numpy.array实例z_scores,可以得到 p 值如下
p_values = 1 - scipy.special.ndtr(z_scores)
or alternatively
或者
p_values = scipy.special.ndtr(-z_scores)
回答by Myles Baker
I think the cumulative distribution function (cdf) is preferred to the survivor function. The survivor function is defined as 1-cdf, and may communicate improperly the assumptions the language model uses for directional percentiles. Also, the percentage point function (ppf) is the inverse of the cdf, which is very convenient.
我认为累积分布函数(cdf)优于幸存者函数。幸存者函数被定义为 1-cdf,并且可能会不正确地传达语言模型用于方向百分位数的假设。还有,百分点函数(ppf)是cdf的倒数,非常方便。
>>> import scipy.stats as st
>>> st.norm.ppf(.95)
1.6448536269514722
>>> st.norm.cdf(1.64)
0.94949741652589625
回答by Arnaldo P. Figueira Figueira
From formula:
从公式:
import numpy as np
import scipy.special as scsp
def z2p(z):
"""From z-score return p-value."""
return 0.5 * (1 + scsp.erf(z / np.sqrt(2)))
回答by Vivek Gopalan
p_value = scipy.stats.norm.pdf(abs(z_score_max)) #one-sided test
p_value = scipy.stats.norm.pdf(abs(z_score_max))*2 # two - sided test
The probability density function (pdf) function in python yields values p-values that are drawn from a z-score table in a intro/AP stats book.
python 中的概率密度函数 (pdf) 函数产生值 p 值,这些值是从介绍/AP 统计书中的 z 分数表中提取的。
回答by Xavier Guihot
Starting Python 3.8, the standard library provides the NormalDistobject as part of the statisticsmodule.
开始Python 3.8,标准库提供NormalDist对象作为statistics模块的一部分。
It can be used to apply the inverse cumulative distribution function(inv_cdf, also known as the quantile functionor the percent-point function) and the cumulative distribution function(cdf):
它可用于应用逆累积分布函数(inv_cdf,也称为分位数函数或百分比函数)和累积分布函数(cdf):
NormalDist().inv_cdf(0.95)
# 1.6448536269514715
NormalDist().cdf(1.64)
# 0.9494974165258963

