npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cumulative-distribution-function

v2.1.1

Published

create empirical cumulative distribution function from array of values

Downloads

388

Readme

cumulative-distribution-function

Build Status Coverage Status

Calculates statistical cumulative distribution function from data array of x values.

Installation

npm i cumulative-distribution-function -S

Dependencies

None. Suitable for usage on nodejs or on the browser, via browserify.

Initialization

const cdf = require('cumulative-distribution-function');

Usage

Pass a number data array as input. Do not pass strings that look like numbers. Invalid data may throw a TypeError

f = cdf(data) returns a function f, the empirical cumulative distribution function, a step function f(x) that counts the proportion of data less than or equal to the number input x.

The function returned by cdf(data) takes a number x and returns the proportion of values less than or equal to x.

New for v2.0: Passing string data (e.g."42") or other invalid, missing or empty data instead of numbers will throw a TypeError.

Pass clean data. Convert and clean up your data with something like:

const clean_data = raw_data
                    .map((v)=>(+v))
                    .filter((v)=>(isFinite(v)));

where the map function will convert stringified numbers (e.g. '123') to numbers; and the filter function will remove NaN's, +Infinity, and -Infinity.

The simple map/filter conversion above won't work for everybody.
For instance, +v converts '' or null to 0 which is kept, and undefined to NaN which is filtered out.

New for v2.1: Bisection loop limit

Error("cdf function exceeded 40 bisection iterations, aborting bisection loop")

The returned function f from f = cdf(data) will throw the above error instead of loop endlessly on invalid data. This helps in case an "attacker" (or simply poor code) changes the xs data array accessible at f.xs() to be string data or other invalid data. The limit of 40 bisections implies a data array size limit of roughly 2^40 or 10^12 entries. This limit is beyond 2021 browser and nodejs capabilities but is short enough to "fail quickly" in the case of an unusual failure in the bisection exit conditions.

Example

var mydata = [13,2,5,3,23,7,11,13,19,23];
var mycdf  = cdf(mydata); // cdf(mydata) returns a **function**, so mycdf is a **function**
mycdf(-5)  // 0.0 because all mydata are greater than -5
mycdf(2)  // 0.1 because 1 of 10 mydata are less than or equal to 2
mycdf(3)  // 0.2 because 2 of 10 mydata are less than or equal to 3
mycdf(13) // 0.7 because 7 of 10 mydata are less than or equal to 13
mycdf(19) // 0.8 because 8 of 10 mydata are less than or equal to 19
mycdf(20) // 0.8 because 8 of 10 mydata are less than or equal to 20
mycdf(25) // 1.0 because all mydata are less than or equal to 25
mycdf.xs() // returns [2,    3,  5,  7, 11, 13, 19, 23] from sorted, unique mydata
mycdf.ps() // returns [0.1,0.2,0.3,0.4,0.5,0.7,0.8,1.0] from corresponding cumulative proportions

Missing or Empty Data

Will throw TypeError

Tests

Use mocha framework.

Copyright

Copyright 2016- Paul Brewer, Economic and Financial Technology Consulting LLC

License

MIT