简体   繁体   English

如何从python中的样本中获得最可能的68%?

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

Suppose I have the following sample of 100,000 points drawn from the chi-square distribution.假设我从卡方分布中抽取了以下 100,000 个点的样本。

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.我想获得概率最高的样本的 68%。 Or, in general how to get the N% of the samples with maximum probability?或者,通常如何以最大概率获得 N% 的样本? Note that when N tends to zero we would get the mode/maxima/maximum likelihood point.请注意,当 N 趋于零时,我们将获得众数/最大值/最大似然点。 Please help.请帮忙。 PS 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. PS我不是在寻找分位数/百分位数,如果分布/直方图不对称,它不会给出最高概率的样本部分。

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.我能想到的最简单的解决方案是拟合卡方分布,评估每个样本的密度,并取前k样本,其中k是数据集的第N'th部分。

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM