具有线性插值的 Numpy 百分位数 - 错误值?

Numpy percentiles with linear interpolation - wrong value?

百分位数的线性插值公式为:

linear: i + (j - i) * fraction, where fraction is the fractional part of the index surrounded by i and j.

假设我有这个包含 16 个观察值的列表:

test = [0, 1, 5, 5, 5, 6, 6, 7, 7, 8, 11, 12, 21, 23, 23, 24]

我将它作为一个 numpy 数组传递,并使用线性插值计算第 85 个百分位数。

np_test = np.asarray(test)
np.percentile(np_test, 85, interpolation = 'linear')

我得到的结果是22.5。但是,我认为这是不正确的。第 85 个百分位数的指数为 .85 * 16 = 13.6。因此,小数部分是 .6。 第 13 个值是 21,所以 i = 21。第 14 个值是 23,所以 j = 23。线性公式应得出:

21 + (23 - 21) * .6 = 21 + 2 * .6 = 21 + 1.2 = 22.2

正确答案是22.2。为什么我得到的是 22.5?

len(test)是16但是最后一个元素和第一个元素的距离少1,即d=16-1=15-0=15。因此,第 85 个百分位数的指数为 d*0.85 = 15*0.85 = 12.75test[12] = 21test[13] = 23。因此,对小数部分使用线性插值,我们得到:21 + 0.75 * (23 - 21) = 22.5。正确答案是 22.5.

来自numpy.percentile()文档的注释部分:

Given a vector V of length N, the q-th percentile of V is the value q/100 of the way from the mimumum to the maximum in in a sorted copy of V.

我认为这里的关键是"the way from the minimum to the maximum"。假设我们从 1 到 16 对元素进行编号。那么第一个元素的 "position" 是 1,test 中最后一个元素的 "position"(沿着 "coordinate axis of indices")是16. 因此距离16-1=15.