Python z-score 的概率,反之亦然

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20864847/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:25:21  来源:igfitidea点击:

Probability to z-score and vice versa

pythonstatistics

提问by user3084006

How do I calculate the z scoreof a p-valueand vice versa?

我如何计算z scorea 的p-value,反之亦然?

For example if I have a p-value of 0.95I should get 1.96in return.

例如,如果我有一个 p 值,0.95我应该得到1.96回报。

I saw some functions in scipy but they only run a z-test on an array.

我在 scipy 中看到了一些函数,但它们只对数组运行 z-test。

I have access to numpy, statsmodel, pandas, and scipy (I think).

我可以访问 numpy、statsmodel、pandas 和 scipy(我认为)。

采纳答案by Myles Baker

>>> import scipy.stats as st
>>> st.norm.ppf(.95)
1.6448536269514722
>>> st.norm.cdf(1.64)
0.94949741652589625

Default Python Probabilities

默认 Python 概率

As other users noted, Python calculates left/lower-tail probabilities by default. If you want to determine the density points where 95% of the distribution is included, you have to take another approach:

正如其他用户所指出的,Python 默认计算左/下尾概率。如果要确定包含 95% 分布的密度点,则必须采用另一种方法:

>>>st.norm.ppf(.975)
1.959963984540054
>>>st.norm.ppf(.025)
-1.960063984540054

Density between two points

两点间的密度

回答by Xavier Guihot

Starting in Python 3.8, the standard library provides the NormalDistobject as part of the statisticsmodule.

从 开始Python 3.8,标准库将NormalDist对象作为statistics模块的一部分提供。

It can be used to get the zscorefor which x% of the area under a normal curve lies (ignoring both tails).

它可用于获取zscore正态曲线下面积的 x%(忽略两条尾巴)。

We can obtain one from the other and vice versa using the inv_cdf(inverse cumulative distribution function) and the cdf(cumulative distribution function) on the standard normal distribution:

我们可以使用标准正态分布上的inv_cdf(逆累积分布函数)和cdf(累积分布函数)从另一个中获得一个,反之亦然:

NormalDist().inv_cdf((1 + 0.95) / 2.)
# 1.9599639845400536
NormalDist().cdf(1.9599639845400536) * 2 - 1
# 0.95

An explanation for the '(1 + 0.95) / 2.' formula can be found in this wikipediasection.

'(1 + 0.95) / 2.' 的解释 公式可以在这个维基百科部分找到。