@stdlib/stats-base-dists-binomial-mgf
v0.3.1
Published
Binomial distribution moment-generating function (MGF).
Readme
Moment-Generating Function
Binomial distribution moment-generating function (MGF).
The moment-generating function for a binomial random variable is
where the nonnegative integer n is the number of trials and 0 <= p <= 1 is the success probability.
Installation
npm install @stdlib/stats-base-dists-binomial-mgfUsage
var mgf = require( '@stdlib/stats-base-dists-binomial-mgf' );mgf( t, n, p )
Evaluates the moment-generating function for a binomial distribution with number of trials n and success probability p.
var y = mgf( 0.5, 20, 0.2 );
// returns ~11.471
y = mgf( 5.0, 20, 0.2 );
// returns ~4.798e29
y = mgf( 0.9, 10, 0.4 );
// returns ~99.338
y = mgf( 0.0, 10, 0.4 );
// returns 1.0If provided NaN as any argument, the function returns NaN.
var y = mgf( NaN, 20, 0.5 );
// returns NaN
y = mgf( 0.0, NaN, 0.5 );
// returns NaN
y = mgf( 0.0, 20, NaN );
// returns NaNIf provided a number of trials n which is not a nonnegative integer, the function returns NaN.
var y = mgf( 0.2, 1.5, 0.5 );
// returns NaN
y = mgf( 0.2, -2.0, 0.5 );
// returns NaNIf provided a success probability p outside of [0,1], the function returns NaN.
var y = mgf( 0.2, 20, -1.0 );
// returns NaN
y = mgf( 0.2, 20, 1.5 );
// returns NaNmgf.factory( n, p )
Returns a function for evaluating the moment-generating function of a binomial distribution with number of trials n and success probability p.
var myMGF = mgf.factory( 10, 0.5 );
var y = myMGF( 0.3 );
// returns ~5.013Examples
var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
var uniform = require( '@stdlib/random-array-uniform' );
var logEachMap = require( '@stdlib/console-log-each-map' );
var mgf = require( '@stdlib/stats-base-dists-binomial-mgf' );
var opts = {
'dtype': 'float64'
};
var t = discreteUniform( 10, 0, 5, opts );
var n = discreteUniform( 10, 0, 10, opts );
var p = uniform( 10, 0.0, 1.0, opts );
logEachMap( 't: %0.4f, n: %0.4f, p: %0.4f, M_X(t;n,p): %0.4f', t, n, p, mgf );C APIs
Usage
#include "stdlib/stats/base/dists/binomial/mgf.h"stdlib_base_dists_binomial_mgf( t, n, p )
Evaluates the moment-generating function for a binomial distribution with number of trials n and success probability p.
double out = stdlib_base_dists_binomial_mgf( 0.5, 20, 0.2 );
// returns ~11.471The function accepts the following arguments:
- t:
[in] doubleinput value. - n:
[in] int32_tnumber of trials. - p:
[in] doublesuccess probability.
double stdlib_base_dists_binomial_mgf( const double t, const int32_t n, const double p );Examples
#include "stdlib/stats/base/dists/binomial/mgf.h"
#include "stdlib/math/base/special/ceil.h"
#include <stdlib.h>
#include <stdio.h>
static double random_uniform( const double min, const double max ) {
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v * (max - min) );
}
int main( void ) {
int32_t n;
double p;
double t;
double y;
int i;
for ( i = 0; i < 25; i++ ) {
n = (int32_t)stdlib_base_ceil( random_uniform( 0, 100 ) );
p = random_uniform( 0, 1 );
t = random_uniform( 0, 5 );
y = stdlib_base_dists_binomial_mgf( t, n, p );
printf( "t: %lf, n: %d, p: %lf, M_X(t;n,p): %lf\n", t, n, p, y );
}
return 0;
}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
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
