JavaScript library with implementations of multiple streaming percentile algorithms
Clone or download
Latest commit fd9c0e9 Mar 9, 2018

README.md

Streaming Percentiles

This is a reusable JavaScript library with implementations of various percentile algorithms on streams of data. These algorithms all calculate only approximate percentiles, not exact percentiles.

For more on streaming percentiles, see Calculating Percentiles on Streaming Data.

Installing

If you use NPM, npm install streaming-percentiles. Otherwise, download the latest release binaries or the latest release source code. You can also load directly from unpkg.com.

For convenience, you can also use the latest release binaries directly from a web browser:

<script src="//sengelha.github.io/streaming-percentiles-js/streamingPercentiles.v1.min.js"></script>
<script>
var gk = new streamingPercentiles.GK(0.1);
...
</script>

Example

Here's a simple example on how to use the Greenwald-Khanna streaming percentile algorithm:

var sp = require('streaming-percentiles');

// epsilon is allowable error.  As epsilon becomes smaller, the
// accuracy of the approximations improves, but the class consumes
// more memory.
var epsilon = 0.1;
var gk = new sp.GK(epsilon);
for (var i = 0; i < 1000; ++i)
    gk.insert(Math.random());
var p50 = gk.quantile(0.5); // Approx. median
var p95 = gk.quantile(0.95); // Approx. 95th percentile

API Reference

class GK(epsilon)

Construct an object which implements the Greenwald-Khanna streaming percentile algorithm with allowable error epsilon.

Example:

var sp = require('streaming-percentiles');
var gk = new sp.GK(0.1);

gk.insert(value)

Logs the observation of a value.

Example:

gk.insert(Math.random());

gk.quantile(phi)

Compute the approximate quantile at phi. For example, the 95th percentile corresponds to phi = 0.95.

Example:

var p50 = gk.quantile(0.5);

License

This project is licensed under the MIT License.