https://github.com/willf/streaming-percentiles-js
JavaScript library with implementations of multiple streaming percentile algorithms
https://github.com/willf/streaming-percentiles-js
Last synced: 5 months ago
JSON representation
JavaScript library with implementations of multiple streaming percentile algorithms
- Host: GitHub
- URL: https://github.com/willf/streaming-percentiles-js
- Owner: willf
- License: mit
- Created: 2020-12-04T19:01:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2018-03-08T21:26:10.000Z (over 8 years ago)
- Last Synced: 2025-03-24T12:52:25.997Z (about 1 year ago)
- Homepage:
- Size: 137 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://stevenengelhardt.com/post-series/calculating-percentiles-on-streaming-data-2018/).
## Installing
If you use NPM, `npm install streaming-percentiles`. Otherwise,
download the [latest release
binaries](https://sengelha.github.io/streaming-percentiles-js/streamingPercentiles.v1.zip)
or the [latest release source
code](https://github.com/sengelha/streaming-percentiles-js/releases/latest).
You can also load directly from
[unpkg.com](https://unpkg.com/streaming-percentiles/).
For convenience, you can also use the [latest release
binaries](https://sengelha.github.io/streaming-percentiles-js/streamingPercentiles.v1.zip)
directly from a web browser:
```html
var gk = new streamingPercentiles.GK(0.1);
...
```
## Example
Here's a simple example on how to use the Greenwald-Khanna streaming
percentile algorithm:
```javascript
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:
```javascript
var sp = require('streaming-percentiles');
var gk = new sp.GK(0.1);
```
### *gk*.insert(*value*)
Logs the observation of a value.
Example:
```javascript
gk.insert(Math.random());
```
### *gk*.quantile(*phi*)
Compute the approximate quantile at *phi*. For example, the 95th
percentile corresponds to *phi* = 0.95.
Example:
```javascript
var p50 = gk.quantile(0.5);
```
## License
This project is licensed under the MIT License.