UNPKG

811 BJavaScriptView Raw
1import numericSort from "./numeric_sort";
2import quantileRankSorted from "./quantile_rank_sorted";
3
4/**
5 * This function returns the quantile in which one would find the given value in
6 * the given array. It will copy and sort your array before each run, so
7 * if you know your array is already sorted, you should use `quantileRankSorted`
8 * instead.
9 *
10 * @param {Array<number>} x input
11 * @returns {number} value value
12 * @example
13 * quantileRank([4, 3, 1, 2], 3); // => 0.75
14 * quantileRank([4, 3, 2, 3, 1], 3); // => 0.7
15 * quantileRank([2, 4, 1, 3], 6); // => 1
16 * quantileRank([5, 3, 1, 2, 3], 4); // => 0.8
17 */
18function quantileRank(x, value) {
19 // Cloning and sorting the array
20 const sortedCopy = numericSort(x);
21
22 return quantileRankSorted(sortedCopy, value);
23}
24
25export default quantileRank;