Arzental
Arzental

Reputation: 103

Is it possible to get quantile by the exact value in python?

I get the quantile of the standard normal distribution (e.g. at 0.9 value). I use scipy.stats.norm.ppf(0.9, 0, 1) for it.

I need to find the corresponding quantile to this value in my custom data. So I have to round value I got from scipy.stats.norm.ppf(0.9, 0, 1) and then find its quantile. Is there any dedicated package function for it?

Upvotes: 0

Views: 1147

Answers (1)

ramzeek
ramzeek

Reputation: 2315

I think you are looking for numpy.quantile:

import numpy as np
from scipy import stats

mean = 0
std = 1
N = 1000
quantile = 0.9

dist = stats.norm(mean, std)
x = dist.rvs(size = N)

data_quantile = np.quantile(x, quantile)
dist_quantile = dist.ppf(quantile)

print(f'The 0.9th quantile of the dataset is {data_quantile}')
#The 0.9th quantile of the dataset is 1.2580295186126398
print(f'The 0.9th quantile of the actual distribution is {dist_quantile}')
#The 0.9th quantile of the actual distribution is 1.2815515655446004

EDIT

However, I may be misinterpreting and after re-reading I am wondering if you actually want to do this:

def get_effective_quantile(dataset, distribution, quantile):
    dist_quantile = distribution.ppf(quantile)
    effective_quantile = sum(dataset <= dist_quantile) / len(dataset)
    return(effective_quantile)

print(f'The effective quantile of {dist_quantile} in the dataset is {get_effective_quantile(x, dist, quantile)}')
#The effective quantile of 1.2815515655446004 in the dataset is 0.904

I am unaware of a package or function that does that, but the above function is pretty straightforward and seems simpler and more robust than what you are currently doing based on your description.

Upvotes: 1

Related Questions