views:

160

answers:

2

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.

+2  A: 

Aha! I found it: scipy.special.ndtr! This also appears to be under scipy.stats.stats.zprob as well (which is just a pointer to ndtr).

Specifically, given a one-dimensional numpy.array instance z_scores, one can obtain the p-values as

p_values = 1 - scipy.special.ndtr(z_scores)

or alternatively

p_values = scipy.special.ndtr(-z_scores)
gotgenes
Strange terminology, "Z-distribution" instead of "Normal curve". Z-score I'd probably call standard deviation in this context as well.
Nick T
Well, the Z-distribution == "standard normal distribution" == `N(0, 1)`. That said, your point is well taken. I have updated the question to reflect the various terminology for the same concepts.
gotgenes
+2  A: 

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(z_scores) #one-sided

p_values = scipy.stats.norm.sf(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