Search code examples
pythonnumpydata-sciencehypothesis-testscipy.stats

norm.ppf vs norm.cdf in python's scipy.stats


so i have pasted my complete code for your reference, i want to know what's the use of ppf and cdf here? can you explain it? i did some research and found out that ppf(percent point function) is an inverse of CDF(comulative distribution function) if they really are, shouldn't this code work if i replaced ppf and cdf as 1/cdf and 1/ppf respectively?

please explain this to me, the difference between the two. and how to and when to use which

this is, btw, hypothesis testing. and sorry for so many comments, just a habit of explaining everything for my future reference.(do point me out if any of my comments is wrong regarding the same)

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, "\n\n")

#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 )

Solution

  • The .cdf() function calculates the probability for a given normal distribution value, while the .ppf() function calculates the normal distribution value for which a given probability is the required value. These are inverse of each other in this particular sense.

    To illustrate this calculation, check the below sample code.

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

    enter image description here

    This image with the code above should make it clear for you.

    Thanks!