d3.quantile seems to be calculating Q1 incorrectly

左心房为你撑大大i 提交于 2021-01-28 08:06:40

问题


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

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).


回答1:


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

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.

Have a look at the source code of 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

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

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

Here is the running demo:

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>


来源:https://stackoverflow.com/questions/46270604/d3-quantile-seems-to-be-calculating-q1-incorrectly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!