Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Z-score (Z-value, standard score) to p-value for normal distribution in Python

How does one convert a Z-score from the Z-distribution (standard normal distribution, Gaussian distribution) to a p-value? I have yet to find the magical function in Scipy's stats module to do this, but one must be there.

like image 589
gotgenes Avatar asked Aug 16 '10 19:08

gotgenes


People also ask

How do you find p-value from z-score in Python?

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

How do you convert z-score to normal distribution?

z = (x – μ) / σ Assuming a normal distribution, your z score would be: z = (x – μ) / σ

How do you find p-value with z-score?

Test with two tails In a two-tailed hypothesis test, let's say we wish to calculate the p-value associated with a z-score of 1.24. We simply multiplied the one-tailed p-value by two to get the two-tailed p-value. 0.2149 is the p-value.


1 Answers

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.sf also calls the corresponding function in scipy.special as in gotgenes example

small advantage of survival function, sf: numerical precision should better for quantiles close to 1 than using the cdf

like image 128
Josef Avatar answered Oct 12 '22 21:10

Josef