pythonscipy.stats中的norm.ppf与norm.cdf

所以我已经粘贴了我的完整代码供您参考,我想知道这里的ppf和cdf有什么用?你能解释一下吗?我做了一些研究,发现 ppf(百分比点函数)是 CDF(累积分布函数)的倒数,如果它们真的是,如果我将 ppf 和 cdf 分别替换为 1/cdf 和 1/ppf,这段代码不应该工作?

请向我解释这一点,两者之间的区别。以及如何以及何时使用哪个

这是,顺便说一句,假设检验。很抱歉有这么多评论,只是为了我将来的参考而解释所有内容的习惯。(如果我的评论有任何错误,请指出我)

ball_bearing_radius = [2.99, 2.99, 2.70, 2.92, 2.88, 2.92, 2.82, 2.83, 3.06, 2.85]




import numpy as np

from math import sqrt
from scipy.stats import norm

# h1 : u != U_0
# h0 : u = u_0
#case study : ball bearing example, claim is that radius = 3, do hypothesis testing 
mu_0 = 3
sigma = 0.1

#collect sample
sample = ball_bearing_radius

#compute mean
mean = np.mean(sample)

#compute n
n = len(sample)

#compute test statistic
z = (mean - mu_0) /(sigma/sqrt(n))

#set alpha
a = 0.01

#-------------------------

#calculate the z_a/2, by using percent point function of the norm of scipy
#ppf = percent point function, inverse of CDF(comulative distribution function)
#also, CDF = pr(X<=x), i.e., probability to the left of the distribution

z_critical = norm.ppf(1-a/2)    #this returns a value for which the probab to the left is 0.975

p_value = 2*(1 - norm.cdf(np.abs(z)))

p_value = float("{:.4f}".format(p_value))


print('z : ',z)
print('nz_critical :', z_critical)
print('nmean :', mean, "nn")

#test the hypothesis

if (np.abs(z) > z_critical):
    print("nREJECT THE NULL HYPOTHESIS : n p-value = ", p_value, "n Alpha = ", a )

else:
    print("CANNOT REJECT THE NULL HYPOTHESIS. NOT ENOUGH EVIDENCE TO REJECT IT: n p-value = ", p_value, "n Alpha = ", a )

回答

.ppf()函数计算给定正态分布值的概率,而该.cdf()函数计算给定概率是所需值的正态分布值。在这个特殊的意义上,它们彼此相反。

为了说明这个计算,检查下面的示例代码。

from scipy.stats import norm
print(norm.ppf(0.95))
print(norm.cdf(1.6448536269514722))

带有上面代码的这张图片应该让你一目了然。

谢谢!


以上是pythonscipy.stats中的norm.ppf与norm.cdf的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>