-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathpercentile.js
More file actions
21 lines (18 loc) · 905 Bytes
/
percentile.js
File metadata and controls
21 lines (18 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import isEmpty from 'underscore/modules/isEmpty';
import sortBy from 'underscore/modules/sortBy.js';
// Return the percentile value of the numeric elements from the collection
//Ex : 50th,75th,99th etc.
//https://en.wikipedia.org/wiki/Percentile
export default function percentile(collection, percentile) {
if (isEmpty(collection)) return 0;
if (typeof percentile !== 'number') throw new TypeError('Percentile must be a number between 0 - 100');
if (percentile <= 0) return collection[0];
if (percentile >= 100) return collection[collection.length - 1];
collection = sortBy(collection);
var index = (percentile/100) * (collection.length - 1),
lowerIndex = Math.floor(index),
upperIndex = lowerIndex + 1,
weight = index % 1;
if (upperIndex >= collection.length) return collection[lowerIndex];
return collection[lowerIndex] * (1 - weight) + collection[upperIndex] * weight;
}