How to get most probable 68% from a sample in python?

220 Views Asked by At

Suppose I have the following sample of 100,000 points drawn from the chi-square distribution.

x=np.random.chisquare(10,100000)

We plot the histogram which is asymmetric. Let us say the histogram represents the probability.

I want to get 68% of the sample having the highest probability. Or, in general how to get the N% of the samples with maximum probability? Note that when N tends to zero we would get the mode/maxima/maximum likelihood point. Please help. P.S. I am not looking for quantile/percentile which would not give the part of the sample with highest probability if the distribution/histogram is asymmetric.

1

There are 1 best solutions below

0
On

The most naive solution, I can think of, to your problem is to fit the chi-square distribution, evaluate the density over each sample, and take the top k samples where k is the N'th fraction of your dataset.

from math import floor
import numpy as np
from scipy.stats import chi2

N = 100000
k = int(floor(0.68 * N))

x = np.random.chisquare(10, N)

dist = chi2.fit(x)

top_k = x[np.argsort(chi2.pdf(x, *dist))][::-1][:k]