Computes a quantile for a numeric array.
This module determines the nearest rank and returns the array value corresponding to that rank.
This module does not currently support multiple interpolation methods, and instead uses a standard elementary method for estimating the quantile.
$ npm install compute-quantileFor use in the browser, use browserify.
To use the module,
var quantile = require( 'compute-quantile' );Given a probability 0 <= p <= 1, computes the quantile for an input array.
var unsorted = [ 4, 3, 5, 1, 2 ];
var q = quantile( unsorted, 0.25 );
// returns 2If the input array is already sorted in ascending order, you can set the sorted option to true.
var sorted = [ 1, 2, 3, 4, 5 ];
var q = quantile( sorted, 0.25, { 'sorted': true } );
// returns 2var quantile = require( 'compute-quantile' );
// Simulate some data...
var data = new Array( 1000 );
for ( var i = 0, len = data.length; i < len; i++ ) {
data[ i ] = Math.round( Math.random()*1000 );
}
// Compute the p-quantile for p = 0.5 (median):
console.log( quantile( data, 0.5 ) );To run the example code from the top-level application directory,
$ node ./examples/index.jsThe probability p is equal to k / q, where q is the total number of quantiles and k is the k th quantile.
If the input array is not sorted in ascending order, the function is O( N log(N) ), where N is the input array length. If the array is sorted, the function is O(1).
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make testAll new feature development should have corresponding unit tests to validate correct functionality.
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-covIstanbul creates a ./reports/coverage directory. To access an HTML version of the report,
$ make view-covCopyright © 2014. Athan Reines.