Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the pandas equivalent of R's qnorm()

I am moving some code from R to Anaconda Python. The R code uses qnorm, documented as "quantile function for the normal distribution with mean equal to mean and standard deviation equal to sd."

The call and parameters are:

   qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
   p           vector of probabilities.
   mean        vector of means.
   sd          vector of standard deviations.
   log.p       logical; if TRUE, probabilities p are given as log(p).
   lower.tail  logical; if TRUE (default), probabilities are
                            P[X≤x] otherwise, P[X].

I don't see any equivalent in pandas.Series. Have I missed it, is it on another object, or is there some equivalent in another library?

like image 607
verisimilidude Avatar asked Jan 03 '23 16:01

verisimilidude


2 Answers

A lot of this equivalent functionality is found in scipy.stats. In this case, you're looking for scipy.stats.norm.ppf.

qnorm(p, mean = 0, sd = 1) is equivalent to scipy.stats.norm.ppf(q, loc=0, scale=1).


import scipy.stats as st

>>> st.norm.ppf([0.01, 0.99])
array([-2.32634787,  2.32634787])

>>> st.norm.ppf([0.01, 0.99], loc=10, scale=0.1)
array([  9.76736521,  10.23263479])
like image 107
miradulo Avatar answered Jan 09 '23 19:01

miradulo


Just to expand @miradulo answer. If you want to get also qnorm(lower.tail=FALSE) you can just multiply by -1:

In R:

qnorm(0.8, lower.tail = F)
-0.8416212

In python

from scipy.stats import norm

norm.ppf(0.8) * -1
-0.8416212
like image 38
Pau Avatar answered Jan 09 '23 18:01

Pau