what's the inverse of the quantile function on a pandas Series? what's the inverse of the quantile function on a pandas Series? python python

what's the inverse of the quantile function on a pandas Series?


I had the same question as you did! I found an easy way of getting the inverse of quantile using scipy.

#libs requiredfrom scipy import statsimport pandas as pdimport numpy as np#generate ramdom data with same seed (to be reproducible)np.random.seed(seed=1)df = pd.DataFrame(np.random.uniform(0,1,(10)), columns=['a'])#quantile functionx = df.quantile(0.5)[0]#inverse of quantilestats.percentileofscore(df['a'],x)


Sorting can be expensive, if you look for a single value I'd guess you'd be better of computing it with:

s = pd.Series(np.random.uniform(size=1000))( s < 0.7 ).astype(int).mean() # =0.7ish

There's probably a way to avoid the int(bool) shenanigan.


Mathematically speaking, you're trying to find the CDF or return the probability of s being smaller than or equal to a value or quantile of q:

F(q) = Pr[s <= q]

One can use numpy and try this one-line code:

np.mean(s.to_numpy() <= q)