Skip to content
Permalink
Browse files

Add percentile exclusive option

PR-URL: #226
Fixes: #224
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information...
arturaugusto authored and trevnorris committed Feb 24, 2018
1 parent 9e97c1b commit 55487b0b9bc0e3d727874727ce414e306c9af7d2
Showing with 30 additions and 9 deletions.
  1. +3 −1 doc/md/vector.md
  2. +6 −8 src/vector.js
  3. +21 −0 test/vector/percentile-test.js
@@ -803,11 +803,13 @@ documentation.

### percentile()

**percentile( dataArray, k )**
**percentile( dataArray, k, [exclusive] )**

Returns the k-th percentile of values in the `dataArray` range, where k is in the range 0..1, exclusive.
Passing true for the exclusive parameter excludes both endpoints of the range.

jStat.percentile([1, 2, 3, 4], 0.3) === 1.9;
jStat.percentile([1, 2, 3, 4], 0.3, true) === 1.5;

### percentileOfScore()

@@ -308,22 +308,20 @@ jStat.quantiles = function quantiles(arr, quantilesArray, alphap, betap) {
return quantileVals;
};

// Returns the k-th percentile of values in a range, where k is in the
// range 0..1, exclusive.
jStat.percentile = function percentile(arr, k) {
// Return the k-th percentile of values in a range, where k is in the range 0..1, inclusive.
// Passing true for the exclusive parameter excludes both endpoints of the range.
jStat.percentile = function percentile(arr, k, exclusive) {
var _arr = arr.slice().sort(ascNum);
var realIndex = k * (_arr.length - 1);
var realIndex = k * (_arr.length + (exclusive ? 1 : -1)) + (exclusive ? 0 : 1);
var index = parseInt(realIndex);
var frac = realIndex - index;

if (index + 1 < _arr.length) {
return _arr[index] * (1 - frac) + _arr[index + 1] * frac;
return _arr[index - 1] + frac * (_arr[index] - _arr[index - 1]);
} else {
return _arr[index];
return _arr[index - 1];
}
}


// The percentile rank of score in a given array. Returns the percentage
// of all values in the input array that are less than (kind='strict') or
// less or equal than (kind='weak') score. Default is weak.
@@ -13,8 +13,29 @@ suite.addBatch({
'30th percentile of the list in the range': function(jStat) {
assert.deepEqual(jStat.percentile([1, 2, 3, 4], 0.3), 1.9);
},
'30th percentile of the list in the range with exclusive flag true': function(jStat) {
assert.deepEqual(jStat.percentile([1, 2, 3, 4], 0.3, true), 1.5);
},
'30th percentile of the list in the range, unsorted': function(jStat) {
assert.deepEqual(jStat.percentile([3, 1, 4, 2], 0.3), 1.9);
},
'40th percentile of the list in the range with exclusive flag false': function(jStat) {
assert.epsilon(0.0000001, jStat.percentile([15, 20, 35, 40, 50], 0.4, false), 29);
},
'40th percentile of the list in the range with exclusive flag true': function(jStat) {
assert.epsilon(0.0000001, jStat.percentile([15, 20, 35, 40, 50], 0.4, true), 26);
},
'10th percentile of the list in the range with exclusive flag false': function(jStat) {
assert.epsilon(0.0000001, jStat.percentile([15, 20, 35, 40, 50], 0.1, false), 17);
},
'10th percentile of the list in the range with exclusive flag true': function(jStat) {
assert(isNaN(jStat.percentile([15, 20, 35, 40, 50], 0.1, true)));
},
'100th percentile of the list in the range with exclusive flag false': function(jStat) {
assert.epsilon(0.0000001, jStat.percentile([15, 20, 35, 40, 50], 1, false), 50);
},
'100th percentile of the list in the range with exclusive flag true': function(jStat) {
assert(isNaN(jStat.percentile([15, 20, 35, 40, 50], 1, true)));
}
}
});

0 comments on commit 55487b0

Please sign in to comment.
You can’t perform that action at this time.