Open In App

How to Find a P-Value from a Z-Score in Python?

The p-value in statistics is the likelihood of getting outcomes at least as significant as the observed results of a statistical hypothesis test, given the null hypothesis is true. The p-value, rather than rejection points, is used to determine the least level of significance at which the null hypothesis is rejected. A lower p-value indicates that the alternative hypothesis has more evidence supporting it, if a p-value is high alternative hypothesis is rejected and the null hypothesis is rejected. In this article let’s demonstrate how to find a p-value from a z-score in python.

We use scipy.stats.norm.sf() function for calculating p-value from z-score. 



Syntax:

scipy.stats.norm.sf(Z_value)



if z_value is less than zero then we use :

scipy.stats.norm.sf(abs(Z_value))

Method 1: Left tailed or Lower tailed test

In distribution, the lower tail includes the lowest values. Because the lowest values on a number line are on the left, the lowest group of numbers will always show on the left when graphing any distribution on a Coordinate plane. z- value is generally negative for the left tailed test. we use abs() function for it.

Example: 




# importing packages
import scipy.stats
  
# finding p-value
p_value = scipy.stats.norm.sf(abs(-0.67))
print('p value is : ' + str(p_value))

Output:

p value is : 0.25142889509531013

In case we choose the level of significance or alpha value as 0.05. the p-value is more than the alpha value (0.05) so the alternative hypothesis is rejected and the null hypothesis is accepted. 

Method 2: Right tailed or upper tailed test

 A right-tailed test or upper test is the inequality that is pointing to the right.

Example:




# importing packages
import scipy.stats
  
# finding p-value
p_value = scipy.stats.norm.sf(abs(1.67))
print('p value is : ' + str(p_value))

Output:

p value is : 0.04745968180294733

In case we choose the level of significance or alpha value as 0.05. the p-value is less than the alpha value (0.05) so the alternative hypothesis is accepted and null hypothesis is rejected.

Method 3: Two-tailed tests

In statistics, a two-tailed test is a procedure that uses a two-sided critical area of a distribution to determine if a sample is larger than or less than a given range of values.

Example:




# importing packages
import scipy.stats
  
# finding p-value
p_value = scipy.stats.norm.sf(abs(1.8))*2
print('p value is : ' + str(p_value))

Output:

p value is : 0.07186063822585158

In case we choose the level of significance or alpha value as 0.05. the p-value is more than the alpha value (0.05) so the alternative hypothesis is rejected and the null hypothesis is accepted.


Article Tags :