Skip to content

Natural logarithm of the probability mass function (PMF) for a binomial distribution.

License

Notifications You must be signed in to change notification settings

stdlib-js/stats-base-dists-binomial-logpmf

 
 

Repository files navigation

About stdlib...

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!

Logarithm of Probability Mass Function

NPM version Build Status Coverage Status

Evaluate the natural logarithm of the probability mass function (PMF) for a binomial distribution.

The probability mass function (PMF) for a binomial random variable is

$$f(x;n,p)=P(X=x;n,p)=\begin{cases} \textstyle {n \choose x}\, p^x (1-p)^{n-x} & \text{ for } x = 0,1,2,\ldots \\ 0 & \text{ otherwise} \end{cases}$$

where n is the number of trials and 0 <= p <= 1 is the success probability.

Usage

To use in Observable,

logpmf = require( 'https://cdn.jsdelivr.net/gh/stdlib-js/stats-base-dists-binomial-logpmf@umd/browser.js' )

To vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:

var logpmf = require( 'path/to/vendor/umd/stats-base-dists-binomial-logpmf/index.js' )

To include the bundle in a webpage,

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/stats-base-dists-binomial-logpmf@umd/browser.js"></script>

If no recognized module system is present, access bundle contents via the global scope:

<script type="text/javascript">
(function () {
    window.logpmf;
})();
</script>

logpmf( x, n, p )

Evaluates the natural logarithm of the probability mass function (PMF) for a binomial distribution with number of trials n and success probability p.

var y = logpmf( 3.0, 20, 0.2 );
// returns ~-1.583

y = logpmf( 21.0, 20, 0.2 );
// returns -Infinity

y = logpmf( 5.0, 10, 0.4 );
// returns ~-1.606

y = logpmf( 0.0, 10, 0.4 );
// returns ~-5.108

If provided NaN as any argument, the function returns NaN.

var y = logpmf( NaN, 20, 0.5 );
// returns NaN

y = logpmf( 0.0, NaN, 0.5 );
// returns NaN

y = logpmf( 0.0, 20, NaN );
// returns NaN

If provided a number of trials n which is not a nonnegative integer, the function returns NaN.

var y = logpmf( 2.0, 1.5, 0.5 );
// returns NaN

y = logpmf( 2.0, -2.0, 0.5 );
// returns NaN

If provided a success probability p outside of [0,1], the function returns NaN.

var y = logpmf( 2.0, 20, -1.0 );
// returns NaN

y = logpmf( 2.0, 20, 1.5 );
// returns NaN

logpmf.factory( n, p )

Returns a function for evaluating the probability mass function (PMF) of a binomial distribution with number of trials n and success probability p.

var mylogpmf = logpmf.factory( 10, 0.5 );

var y = mylogpmf( 3.0 );
// returns ~-2.144

y = mylogpmf( 5.0 );
// returns ~-1.402

Examples

<!DOCTYPE html>
<html lang="en">
<body>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/math-base-special-round@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/stats-base-dists-binomial-logpmf@umd/browser.js"></script>
<script type="text/javascript">
(function () {

var i;
var n;
var p;
var x;
var y;

for ( i = 0; i < 10; i++ ) {
    x = round( randu() * 20.0 );
    n = round( randu() * 100.0 );
    p = randu();
    y = logpmf( x, n, p );
    console.log( 'x: %d, n: %d, p: %d, ln(P(X = x;n,p)): %d', x, n, p.toFixed( 4 ), y.toFixed( 4 ) );
}

})();
</script>
</body>
</html>

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.