@sgratzl/science
Version:
Scientific and statistical computing in JavaScript.
19 lines (15 loc) • 479 B
JavaScript
import ascending from '../core/ascending';
// Uses R's quantile algorithm type=7.
export default function quantiles(d, quantiles) {
d = d.slice().sort(ascending);
var n_1 = d.length - 1;
return quantiles.map(function(q) {
if (q === 0) return d[0];
else if (q === 1) return d[n_1];
var index = 1 + q * n_1,
lo = Math.floor(index),
h = index - lo,
a = d[lo - 1];
return h === 0 ? a : a + h * (d[lo] - a);
});
};