compute-quantile

Computes a quantile for a numeric array.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
compute-quantile
1.0.18 years ago8 years agoMinified + gzip package size for compute-quantile in KB

Readme

Quantile
!NPM versionnpm-imagenpm-url !Build Statustravis-imagetravis-url !Coverage Statuscoveralls-imagecoveralls-url !Dependenciesdependencies-imagedependencies-url
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.

Installation

$ npm install compute-quantile

For use in the browser, use browserify.

Usage

To use the module,
var quantile = require( 'compute-quantile' );

quantile( arr, p, opts )

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 2

If 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 2

Examples

var 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.js

Notes

The 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).

Tests

Unit

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 test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

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-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,
$ make view-cov

License

MIT license.

Copyright

Copyright © 2014. Athan Reines.