We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Cumulative Distribution Function
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
[Binomial][binomial-distribution] distribution [cumulative distribution function][cdf].
n
is the number of trials and p
is the success probability. The CDF can be equivalently expressed as
where I
is the [lower regularized incomplete beta function][incomplete-beta].
bash
npm install @stdlib/stats-base-dists-binomial-cdf
javascript
var cdf = require( '@stdlib/stats-base-dists-binomial-cdf' );
#### cdf( x, n, p )
Evaluates the [cumulative distribution function][cdf] for a [binomial][binomial-distribution] distribution with number of trials n
and success probability p
.
javascript
var y = cdf( 3.0, 20, 0.2 );
// returns ~0.411
y = cdf( 21.0, 20, 0.2 );
// returns 1.0
y = cdf( 5.0, 10, 0.4 );
// returns ~0.834
y = cdf( 0.0, 10, 0.4 );
// returns ~0.006
If provided NaN
as any argument, the function returns NaN
.
javascript
var y = cdf( NaN, 20, 0.5 );
// returns NaN
y = cdf( 0.0, NaN, 0.5 );
// returns NaN
y = cdf( 0.0, 20, NaN );
// returns NaN
If provided a number of trials n
which is not a nonnegative integer, the function returns NaN
.
javascript
var y = cdf( 2.0, 1.5, 0.5 );
// returns NaN
y = cdf( 2.0, -2.0, 0.5 );
// returns NaN
If provided a success probability p
outside of [0,1]
, the function returns NaN
.
javascript
var y = cdf( 2.0, 20, -1.0 );
// returns NaN
y = cdf( 2.0, 20, 1.5 );
// returns NaN
#### cdf.factory( n, p )
Returns a function for evaluating the [cumulative distribution function][cdf] of a [binomial][binomial-distribution] distribution with number of trials n
and success probability p
.
javascript
var mycdf = cdf.factory( 10, 0.5 );
var y = mycdf( 3.0 );
// returns ~0.172
y = mycdf( 1.0 );
// returns ~0.011
javascript
var randu = require( '@stdlib/random-base-randu' );
var round = require( '@stdlib/math-base-special-round' );
var cdf = require( '@stdlib/stats-base-dists-binomial-cdf' );
var i;
var n;
var p;
var x;
var y;
for ( i = 0; i < 10; i++ ) {
x = randu() * 20.0;
n = round( randu() * 100.0 );
p = randu();
y = cdf( x, n, p );
console.log( 'x: %d, n: %d, p: %d, F(x;n,p): %d', x.toFixed( 4 ), n, p.toFixed( 4 ), y.toFixed( 4 ) );
}