简体   繁体   English

在 Julia 与 Python 中使用分位数时出现意外结果

[英]Unexpected result when using quantile in Julia vs Python

I've just started with Julia and I am trying to do some simple statistics.我刚从 Julia 开始,我正在尝试做一些简单的统计。

I'm using the StatsBase package and am trying to calculate quantiles.我正在使用 StatsBase package 并尝试计算分位数。

using StatsBase

lst = 1:10

print(nquantile(lst, 4))

and get并得到

[1.0, 3.25, 5.5, 7.75, 10.0]

Where I assume Q_1 = 3.25 and Q_2 = 7.75我假设 Q_1 = 3.25 和 Q_2 = 7.75

Running a similar code on python:在 python 上运行类似的代码:

from statistics import quantiles

lst = [_ for _ in range(1, 11)]
print(quantiles(lst))

yields:产量:

[2.75, 5.5, 8.25]

Where Q_1 = 2.75 and Q_3 = 8.25.其中 Q_1 = 2.75 和 Q_3 = 8.25。

According to my understanding of statistics, pythons results correspond to what the actual math is.根据我对统计学的理解,pythons的结果与实际数学是对应的。

So, What I am guessing is that the Julia variant is using some kind of gaussian distribution to find the quantiles.所以,我猜测的是 Julia 变体正在使用某种高斯分布来查找分位数。 If so, is there a way to make this follow uniform distribution?如果是这样,有没有办法使它遵循均匀分布?

There are many quantile definitions and Julia implements all options found in Hyndman, R.J and Fan, Y. (1996) Sample Quantiles in Statistical Packages", The American Statistician, Vol. 50, No. 4, pp. 361-365有许多分位数定义,Julia 实现了在Hyndman、R.J 和 Fan, Y. (1996) Sample Quantiles in Statistical Packages 中找到的所有选项”,美国统计学家,第 50 卷,第 354 页,

In order to get the Python equivalent do:为了获得 Python 等效项,请执行以下操作:

julia> quantile(1:10, (0:4)/4; alpha=0,beta=0)
5-element Vector{Float64}:
  1.0
  2.75
  5.5
  8.25
 10.0

Explanation (found in docs):说明(在文档中找到):

help?> nquantile
(...)
  Equivalent to quantile(x, [0:n]/n). 
(...)
help?> quantile

  quantile(itr, p; sorted=false, alpha::Real=1.0, beta::Real=alpha)
(...)
  By default (alpha = beta = 1), quantiles are computed via linear interpolation between the points ((k-1)/(n-1),
  v[k]), for k = 1:n where n = length(itr). This corresponds to Definition 7 of Hyndman and Fan (1996), and is the  same as the R and NumPy default.

(...)
    •  Def. 6: alpha=0, beta=0 (Excel PERCENTILE.EXC, Python default, Stata altdef)
(...)

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

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