jmparejaz
jmparejaz

Reputation: 162

Calculate inverse CDF from a sample of data

If i got a random sample data:

X=np.random.random(100)*100

and I need to get the value X_i with CDF = 34% or whatever. The only way that I am able to think right now is with inverse CDF. I thought that percentile would be equivalent but someone told me it is near but not exact.

Upvotes: 2

Views: 1730

Answers (1)

qbzenker
qbzenker

Reputation: 4652

This should give you the index of X where the cdf is 0.34:

X=np.random.random(100)*100
cdf_frac_to_find = 0.34
cdf = np.cumsum(X)/np.sum(X) #take the cumulative sum of x and normalize so that it's max value is 1
X_index = np.argmin(np.abs(cdf-cdf_pct_to_find))
X_index
#out: 32 -- note that this will likely change because you're generating random numbers for X.

Upvotes: 4

Related Questions