d3.quantile似乎错误地计算了Q1 [英] d3.quantile seems to be calculating Q1 incorrectly

查看:36
本文介绍了d3.quantile似乎错误地计算了Q1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我给 d3.quantile 提供了一个由24个数字组成的排序数组,并要求它计算第一个四分位数的值.由于可以将数组平均分为6个值的四组,因此我的假设是结果将是arr [5]和arr [6]的平均值,但这不是我得到的.

I'm giving a sorted array of 24 numbers to d3.quantile and asking it to calculate the first quartile value. Since the array can be split evenly into four groups of 6 values, my assumption was that the result would be the mean of arr[5] and arr[6], but that's not what I got.

var arr = [89.7, 93.2, 94, 94.3, 94.5, 95.4, 95.9, 96.1, 96.4, 96.5, 96.9, 96.9, 97.3, 97.6, 97.6, 97.6, 97.8, 98.3, 98.3, 98.4, 98.5, 98.5, 98.6, 98.6];
var myAssumption = (arr[5] + arr[6]) / 2;   // 95.65
var d3Result = d3.quantile(arr, 0.25);      // 95.775

d3分位数函数是否使用一些更复杂的算法?这篇Wikipedia文章列出了几个选项,但是我不确定正在使用哪个选项(或为什么一种算法优于另一种算法.

Does the d3 quantile function use some more complex algorithm? This Wikipedia article lists several options, but I'm not sure which is being used (or why one algorithm is preferable to another).

推荐答案

结果不正确,这是预期值.

The result is not incorrect, that's the expected value.

如果您查看链接的 Wikipedia 页面,则会看到" type 列中的"R-7"(写为"R-7,Excel,SciPy-(1,1),Maple-6" ).这就是 d3.quantile()函数使用的算法.

If you look at that Wikipedia page you linked, you'll see "R-7" in the type column (it's written "R-7, Excel, SciPy-(1,1), Maple-6"). That's the algorithm used by d3.quantile() function.

看看 d3.quantile()的源代码:

export default function(values, p, valueof) {
    if (valueof == null) valueof = number;
    if (!(n = values.length)) return;
    if ((p = +p) <= 0 || n < 2) return +valueof(values[0], 0, values);
    if (p >= 1) return +valueof(values[n - 1], n - 1, values);
    var n,
        i = (n - 1) * p,
        i0 = Math.floor(i),
        value0 = +valueof(values[i0], i0, values),
        value1 = +valueof(values[i0 + 1], i0 + 1, values);
    return value0 + (value1 - value0) * (i - i0);
}

因此,在您的情况下,我们将:

So, in your case, we'll have:

i = (24 - 1) * 0.25
//   ^--- the length of the array

哪个给了我们 5.75 (以及 5 作为 Math.floor(i)).

Which gives us 5.75 (and 5 as Math.floor(i)).

然后返回的值(该函数中的 value0 +(value1-value0)*(i-i0))将是:

Then the returned value (which is value0 + (value1 - value0) * (i - i0) in the function) will be:

95.4 + (95.9 - 95.4) * (5.75 - 5)

这为我们提供了您所看到的结果:

And that gives us the result you're seeing:

95.775

这是正在运行的演示:

var arr = [89.7, 93.2, 94, 94.3, 94.5, 95.4, 95.9, 96.1, 96.4, 96.5, 96.9, 96.9, 97.3, 97.6, 97.6, 97.6, 97.8, 98.3, 98.3, 98.4, 98.5, 98.5, 98.6, 98.6];
var d3Result = d3.quantile(arr, 0.25);
console.log(d3Result)

<script src="https://d3js.org/d3.v4.min.js"></script>

这篇关于d3.quantile似乎错误地计算了Q1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆