@stdlib/stats-base-dists-binomial-pmf 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/NOTICE CHANGED
@@ -1 +1 @@
1
- Copyright (c) 2016-2024 The Stdlib Authors.
1
+ Copyright (c) 2016-2026 The Stdlib Authors.
package/README.md CHANGED
@@ -148,22 +148,117 @@ y = mypmf( 5.0 );
148
148
  <!-- eslint no-undef: "error" -->
149
149
 
150
150
  ```javascript
151
- var randu = require( '@stdlib/random-base-randu' );
152
- var round = require( '@stdlib/math-base-special-round' );
151
+ var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
152
+ var uniform = require( '@stdlib/random-array-uniform' );
153
+ var logEachMap = require( '@stdlib/console-log-each-map' );
153
154
  var pmf = require( '@stdlib/stats-base-dists-binomial-pmf' );
154
155
 
155
- var i;
156
- var n;
157
- var p;
158
- var x;
159
- var y;
160
-
161
- for ( i = 0; i < 10; i++ ) {
162
- x = round( randu() * 20.0 );
163
- n = round( randu() * 100.0 );
164
- p = randu();
165
- y = pmf( x, n, p );
166
- console.log( 'x: %d, n: %d, p: %d, P(X = x;n,p): %d', x, n, p.toFixed( 4 ), y.toFixed( 4 ) );
156
+ var opts = {
157
+ 'dtype': 'float64'
158
+ };
159
+ var x = discreteUniform( 10, 0, 20, opts );
160
+ var n = discreteUniform( 10, 0, 100, opts );
161
+ var p = uniform( 10, 0.0, 1.0, opts );
162
+
163
+ logEachMap( 'x: %0.4f, n: %0.4f, p: %0.4f, P(X = x;n,p): %0.4f', x, n, p, pmf );
164
+ ```
165
+
166
+ </section>
167
+
168
+ <!-- /.examples -->
169
+
170
+ <!-- C interface documentation. -->
171
+
172
+ * * *
173
+
174
+ <section class="c">
175
+
176
+ ## C APIs
177
+
178
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
179
+
180
+ <section class="intro">
181
+
182
+ </section>
183
+
184
+ <!-- /.intro -->
185
+
186
+ <!-- C usage documentation. -->
187
+
188
+ <section class="usage">
189
+
190
+ ### Usage
191
+
192
+ ```c
193
+ #include "stdlib/stats/base/dists/binomial/pmf.h"
194
+ ```
195
+
196
+ #### stdlib_base_dists_binomial_pmf( x, n, p )
197
+
198
+ Evaluates the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p` at a value `x`.
199
+
200
+ ```c
201
+ double out = stdlib_base_dists_binomial_pmf( 3.0, 20, 0.2 );
202
+ // returns ~0.205
203
+ ```
204
+
205
+ The function accepts the following arguments:
206
+
207
+ - **x**: `[in] double` input value.
208
+ - **n**: `[in] int32_t` number of trials.
209
+ - **p**: `[in] double` success probability.
210
+
211
+ ```c
212
+ double stdlib_base_dists_binomial_pmf( const double x, const int32_t n, const double p );
213
+ ```
214
+
215
+ </section>
216
+
217
+ <!-- /.usage -->
218
+
219
+ <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
220
+
221
+ <section class="notes">
222
+
223
+ </section>
224
+
225
+ <!-- /.notes -->
226
+
227
+ <!-- C API usage examples. -->
228
+
229
+ <section class="examples">
230
+
231
+ ### Examples
232
+
233
+ ```c
234
+ #include "stdlib/stats/base/dists/binomial/pmf.h"
235
+ #include <stdlib.h>
236
+ #include <stdio.h>
237
+ #include <stdint.h>
238
+
239
+ static double random_uniform( const double min, const double max ) {
240
+ double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
241
+ return min + ( v*(max-min) );
242
+ }
243
+
244
+ static int32_t random_int( const int32_t min, const int32_t max ) {
245
+ return min + (int32_t)( random_uniform( 0.0, 1.0 ) * ( max - min + 1 ) );
246
+ }
247
+
248
+ int main( void ) {
249
+ int32_t n;
250
+ double p;
251
+ double x;
252
+ double y;
253
+ int i;
254
+
255
+ for ( i = 0; i < 25; i++ ) {
256
+ n = random_int( 1, 20 );
257
+ x = random_uniform( 0.0, (double)n );
258
+ p = random_uniform( 0.0, 1.0 );
259
+ y = stdlib_base_dists_binomial_pmf( x, n, p );
260
+ printf( "x: %.4f, n: %d, p: %.4f, P(X = x;n,p): %.4f\n", x, n, p, y );
261
+ }
167
262
  }
168
263
  ```
169
264
 
@@ -171,6 +266,10 @@ for ( i = 0; i < 10; i++ ) {
171
266
 
172
267
  <!-- /.examples -->
173
268
 
269
+ </section>
270
+
271
+ <!-- /.c -->
272
+
174
273
  <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
175
274
 
176
275
  <section class="related">
@@ -205,7 +304,7 @@ See [LICENSE][stdlib-license].
205
304
 
206
305
  ## Copyright
207
306
 
208
- Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
307
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
209
308
 
210
309
  </section>
211
310
 
@@ -218,8 +317,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
218
317
  [npm-image]: http://img.shields.io/npm/v/@stdlib/stats-base-dists-binomial-pmf.svg
219
318
  [npm-url]: https://npmjs.org/package/@stdlib/stats-base-dists-binomial-pmf
220
319
 
221
- [test-image]: https://github.com/stdlib-js/stats-base-dists-binomial-pmf/actions/workflows/test.yml/badge.svg?branch=v0.2.2
222
- [test-url]: https://github.com/stdlib-js/stats-base-dists-binomial-pmf/actions/workflows/test.yml?query=branch:v0.2.2
320
+ [test-image]: https://github.com/stdlib-js/stats-base-dists-binomial-pmf/actions/workflows/test.yml/badge.svg?branch=v0.3.0
321
+ [test-url]: https://github.com/stdlib-js/stats-base-dists-binomial-pmf/actions/workflows/test.yml?query=branch:v0.3.0
223
322
 
224
323
  [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/stats-base-dists-binomial-pmf/main.svg
225
324
  [coverage-url]: https://codecov.io/github/stdlib-js/stats-base-dists-binomial-pmf?branch=main
@@ -231,8 +330,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
231
330
 
232
331
  -->
233
332
 
234
- [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
235
- [chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
333
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
334
+ [chat-url]: https://stdlib.zulipchat.com
236
335
 
237
336
  [stdlib]: https://github.com/stdlib-js/stdlib
238
337
 
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../lib/main.js", "../lib/factory.js", "../lib/index.js"],
4
- "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isNonNegativeInteger = require( '@stdlib/math-base-assert-is-nonnegative-integer' );\nvar binomcoefln = require( '@stdlib/math-base-special-binomcoefln' );\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar log1p = require( '@stdlib/math-base-special-log1p' );\nvar exp = require( '@stdlib/math-base-special-exp' );\nvar ln = require( '@stdlib/math-base-special-ln' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\n\n\n// MAIN //\n\n/**\n* Evaluates the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p` at a value `x`.\n*\n* @param {number} x - input value\n* @param {NonNegativeInteger} n - number of trials\n* @param {Probability} p - success probability\n* @returns {Probability} evaluated PMF\n*\n* @example\n* var y = pmf( 3.0, 20, 0.2 );\n* // returns ~0.205\n*\n* @example\n* var y = pmf( 21.0, 20, 0.2 );\n* // returns 0.0\n*\n* @example\n* var y = pmf( 5.0, 10, 0.4 );\n* // returns ~0.201\n*\n* @example\n* var y = pmf( 0.0, 10, 0.4 );\n* // returns ~0.006\n*\n* @example\n* var y = pmf( NaN, 20, 0.5 );\n* // returns NaN\n*\n* @example\n* var y = pmf( 0.0, NaN, 0.5 );\n* // returns NaN\n*\n* @example\n* var y = pmf( 0.0, 20, NaN );\n* // returns NaN\n*\n* @example\n* var y = pmf( 2.0, 1.5, 0.5 );\n* // returns NaN\n*\n* @example\n* var y = pmf( 2.0, -2.0, 0.5 );\n* // returns NaN\n*\n* @example\n* var y = pmf( 2.0, 20, -1.0 );\n* // returns NaN\n*\n* @example\n* var y = pmf( 2.0, 20, 1.5 );\n* // returns NaN\n*/\nfunction pmf( x, n, p ) {\n\tvar lnl;\n\tif (\n\t\tisnan( x ) ||\n\t\tisnan( n ) ||\n\t\tisnan( p ) ||\n\t\tp < 0.0 ||\n\t\tp > 1.0 ||\n\t\t!isNonNegativeInteger( n ) ||\n\t\tn === PINF\n\t) {\n\t\treturn NaN;\n\t}\n\tif ( isNonNegativeInteger( x ) ) {\n\t\tif ( x > n ) {\n\t\t\treturn 0.0;\n\t\t}\n\t\tif ( p === 0.0 ) {\n\t\t\treturn ( x === 0 ) ? 1.0 : 0.0;\n\t\t}\n\t\tif ( p === 1.0 ) {\n\t\t\treturn ( x === n ) ? 1.0 : 0.0;\n\t\t}\n\t\tlnl = binomcoefln( n, x );\n\t\tlnl += (x * ln( p )) + (( n - x ) * log1p( -p ));\n\t\treturn exp( lnl );\n\t}\n\treturn 0.0;\n}\n\n\n// EXPORTS //\n\nmodule.exports = pmf;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isNonNegativeInteger = require( '@stdlib/math-base-assert-is-nonnegative-integer' );\nvar constantFunction = require( '@stdlib/utils-constant-function' );\nvar binomcoefln = require( '@stdlib/math-base-special-binomcoefln' );\nvar degenerate = require( '@stdlib/stats-base-dists-degenerate-pmf' ).factory;\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar log1p = require( '@stdlib/math-base-special-log1p' );\nvar exp = require( '@stdlib/math-base-special-exp' );\nvar ln = require( '@stdlib/math-base-special-ln' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\n\n\n// MAIN //\n\n/**\n* Returns a function for evaluating the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p`.\n*\n* @param {NonNegativeInteger} n - number of trials\n* @param {Probability} p - success probability\n* @returns {Function} PMF\n*\n* @example\n* var pmf = factory( 10, 0.5 );\n* var y = pmf( 3.0 );\n* // returns ~0.117\n*\n* y = pmf( 5.0 );\n* // returns ~0.246\n*/\nfunction factory( n, p ) {\n\tif (\n\t\tisnan( n ) ||\n\t\tisnan( p ) ||\n\t\t!isNonNegativeInteger( n ) ||\n\t\tn === PINF ||\n\t\tp < 0.0 ||\n\t\tp > 1.0\n\t) {\n\t\treturn constantFunction( NaN );\n\t}\n\tif ( p === 0.0 || n === 0 ) {\n\t\treturn degenerate( 0.0 );\n\t}\n\tif ( p === 1.0 ) {\n\t\treturn degenerate( n );\n\t}\n\treturn pmf;\n\n\t/**\n\t* Evaluates the probability mass function (PMF) for a binomial distribution.\n\t*\n\t* @private\n\t* @param {number} x - input value\n\t* @returns {Probability} evaluated PMF\n\t*\n\t* @example\n\t* var y = pmf( 2.0 );\n\t* // returns <number>\n\t*/\n\tfunction pmf( x ) {\n\t\tvar lnl;\n\t\tif ( isnan( x ) ) {\n\t\t\treturn NaN;\n\t\t}\n\t\tif ( isNonNegativeInteger( x ) ) {\n\t\t\tif ( x > n ) {\n\t\t\t\treturn 0.0;\n\t\t\t}\n\t\t\tlnl = binomcoefln( n, x );\n\t\t\tlnl += (x * ln( p )) + ((n - x) * log1p( -p ));\n\t\t\treturn exp( lnl );\n\t\t}\n\t\treturn 0.0;\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Binomial distribution probability mass function (PMF).\n*\n* @module @stdlib/stats-base-dists-binomial-pmf\n*\n* @example\n* var pmf = require( '@stdlib/stats-base-dists-binomial-pmf' );\n*\n* var y = pmf( 3.0, 20, 0.2 );\n* // returns ~0.205\n*\n* y = pmf( 21.0, 20, 0.2 );\n* // returns 0.0\n*\n* y = pmf( 5.0, 10, 0.4 );\n* // returns ~0.201\n*\n* y = pmf( 0.0, 10, 0.4 );\n* // returns ~0.06\n*\n* @example\n* var factory = require( '@stdlib/stats-base-dists-binomial-pmf' ).factory;\n*\n* var pmf = factory( 10, 0.5 );\n*\n* var y = pmf( 3.0 );\n* // returns ~0.117\n*\n* y = pmf( 5.0 );\n* // returns ~0.246\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
4
+ "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isNonNegativeInteger = require( '@stdlib/math-base-assert-is-nonnegative-integer' );\nvar binomcoefln = require( '@stdlib/math-base-special-binomcoefln' );\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar log1p = require( '@stdlib/math-base-special-log1p' );\nvar exp = require( '@stdlib/math-base-special-exp' );\nvar ln = require( '@stdlib/math-base-special-ln' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\n\n\n// MAIN //\n\n/**\n* Evaluates the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p` at a value `x`.\n*\n* @param {number} x - input value\n* @param {NonNegativeInteger} n - number of trials\n* @param {Probability} p - success probability\n* @returns {Probability} evaluated PMF\n*\n* @example\n* var y = pmf( 3.0, 20, 0.2 );\n* // returns ~0.205\n*\n* @example\n* var y = pmf( 21.0, 20, 0.2 );\n* // returns 0.0\n*\n* @example\n* var y = pmf( 5.0, 10, 0.4 );\n* // returns ~0.201\n*\n* @example\n* var y = pmf( 0.0, 10, 0.4 );\n* // returns ~0.006\n*\n* @example\n* var y = pmf( NaN, 20, 0.5 );\n* // returns NaN\n*\n* @example\n* var y = pmf( 0.0, NaN, 0.5 );\n* // returns NaN\n*\n* @example\n* var y = pmf( 0.0, 20, NaN );\n* // returns NaN\n*\n* @example\n* var y = pmf( 2.0, 1.5, 0.5 );\n* // returns NaN\n*\n* @example\n* var y = pmf( 2.0, -2.0, 0.5 );\n* // returns NaN\n*\n* @example\n* var y = pmf( 2.0, 20, -1.0 );\n* // returns NaN\n*\n* @example\n* var y = pmf( 2.0, 20, 1.5 );\n* // returns NaN\n*/\nfunction pmf( x, n, p ) {\n\tvar lnl;\n\tif (\n\t\tisnan( x ) ||\n\t\tisnan( n ) ||\n\t\tisnan( p ) ||\n\t\tp < 0.0 ||\n\t\tp > 1.0 ||\n\t\t!isNonNegativeInteger( n ) ||\n\t\tn === PINF\n\t) {\n\t\treturn NaN;\n\t}\n\tif ( isNonNegativeInteger( x ) ) {\n\t\tif ( x > n ) {\n\t\t\treturn 0.0;\n\t\t}\n\t\tif ( p === 0.0 ) {\n\t\t\treturn ( x === 0 ) ? 1.0 : 0.0;\n\t\t}\n\t\tif ( p === 1.0 ) {\n\t\t\treturn ( x === n ) ? 1.0 : 0.0;\n\t\t}\n\t\tlnl = binomcoefln( n, x );\n\t\tlnl += (x * ln( p )) + (( n - x ) * log1p( -p ));\n\t\treturn exp( lnl );\n\t}\n\treturn 0.0;\n}\n\n\n// EXPORTS //\n\nmodule.exports = pmf;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isNonNegativeInteger = require( '@stdlib/math-base-assert-is-nonnegative-integer' );\nvar constantFunction = require( '@stdlib/utils-constant-function' );\nvar binomcoefln = require( '@stdlib/math-base-special-binomcoefln' );\nvar degenerate = require( '@stdlib/stats-base-dists-degenerate-pmf' ).factory;\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar log1p = require( '@stdlib/math-base-special-log1p' );\nvar exp = require( '@stdlib/math-base-special-exp' );\nvar ln = require( '@stdlib/math-base-special-ln' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\n\n\n// MAIN //\n\n/**\n* Returns a function for evaluating the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p`.\n*\n* @param {NonNegativeInteger} n - number of trials\n* @param {Probability} p - success probability\n* @returns {Function} PMF\n*\n* @example\n* var pmf = factory( 10, 0.5 );\n* var y = pmf( 3.0 );\n* // returns ~0.117\n*\n* y = pmf( 5.0 );\n* // returns ~0.246\n*/\nfunction factory( n, p ) {\n\tif (\n\t\tisnan( n ) ||\n\t\tisnan( p ) ||\n\t\t!isNonNegativeInteger( n ) ||\n\t\tn === PINF ||\n\t\tp < 0.0 ||\n\t\tp > 1.0\n\t) {\n\t\treturn constantFunction( NaN );\n\t}\n\tif ( p === 0.0 || n === 0 ) {\n\t\treturn degenerate( 0.0 );\n\t}\n\tif ( p === 1.0 ) {\n\t\treturn degenerate( n );\n\t}\n\treturn pmf;\n\n\t/**\n\t* Evaluates the probability mass function (PMF) for a binomial distribution.\n\t*\n\t* @private\n\t* @param {number} x - input value\n\t* @returns {Probability} evaluated PMF\n\t*\n\t* @example\n\t* var y = pmf( 2.0 );\n\t* // returns <number>\n\t*/\n\tfunction pmf( x ) {\n\t\tvar lnl;\n\t\tif ( isnan( x ) ) {\n\t\t\treturn NaN;\n\t\t}\n\t\tif ( isNonNegativeInteger( x ) ) {\n\t\t\tif ( x > n ) {\n\t\t\t\treturn 0.0;\n\t\t\t}\n\t\t\tlnl = binomcoefln( n, x );\n\t\t\tlnl += (x * ln( p )) + ((n - x) * log1p( -p ));\n\t\t\treturn exp( lnl );\n\t\t}\n\t\treturn 0.0;\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Binomial distribution probability mass function (PMF).\n*\n* @module @stdlib/stats-base-dists-binomial-pmf\n*\n* @example\n* var pmf = require( '@stdlib/stats-base-dists-binomial-pmf' );\n*\n* var y = pmf( 3.0, 20, 0.2 );\n* // returns ~0.205\n*\n* y = pmf( 21.0, 20, 0.2 );\n* // returns 0.0\n*\n* y = pmf( 5.0, 10, 0.4 );\n* // returns ~0.201\n*\n* y = pmf( 0.0, 10, 0.4 );\n* // returns ~0.006\n*\n* @example\n* var factory = require( '@stdlib/stats-base-dists-binomial-pmf' ).factory;\n*\n* var pmf = factory( 10, 0.5 );\n*\n* var y = pmf( 3.0 );\n* // returns ~0.117\n*\n* y = pmf( 5.0 );\n* // returns ~0.246\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
5
5
  "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAuB,QAAS,iDAAkD,EAClFC,EAAc,QAAS,uCAAwC,EAC/DC,EAAQ,QAAS,iCAAkC,EACnDC,EAAQ,QAAS,iCAAkC,EACnDC,EAAM,QAAS,+BAAgC,EAC/CC,EAAK,QAAS,8BAA+B,EAC7CC,EAAO,QAAS,gCAAiC,EAyDrD,SAASC,EAAKC,EAAGC,EAAGC,EAAI,CACvB,IAAIC,EACJ,OACCT,EAAOM,CAAE,GACTN,EAAOO,CAAE,GACTP,EAAOQ,CAAE,GACTA,EAAI,GACJA,EAAI,GACJ,CAACV,EAAsBS,CAAE,GACzBA,IAAMH,EAEC,IAEHN,EAAsBQ,CAAE,EACvBA,EAAIC,EACD,EAEHC,IAAM,EACDF,IAAM,EAAM,EAAM,EAEvBE,IAAM,EACDF,IAAMC,EAAM,EAAM,GAE5BE,EAAMV,EAAaQ,EAAGD,CAAE,EACxBG,GAAQH,EAAIH,EAAIK,CAAE,GAAQD,EAAID,GAAML,EAAO,CAACO,CAAE,EACvCN,EAAKO,CAAI,GAEV,CACR,CAKAZ,EAAO,QAAUQ,ICtHjB,IAAAK,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAuB,QAAS,iDAAkD,EAClFC,EAAmB,QAAS,iCAAkC,EAC9DC,EAAc,QAAS,uCAAwC,EAC/DC,EAAa,QAAS,yCAA0C,EAAE,QAClEC,EAAQ,QAAS,iCAAkC,EACnDC,EAAQ,QAAS,iCAAkC,EACnDC,EAAM,QAAS,+BAAgC,EAC/CC,EAAK,QAAS,8BAA+B,EAC7CC,EAAO,QAAS,gCAAiC,EAoBrD,SAASC,EAASC,EAAGC,EAAI,CACxB,GACCP,EAAOM,CAAE,GACTN,EAAOO,CAAE,GACT,CAACX,EAAsBU,CAAE,GACzBA,IAAMF,GACNG,EAAI,GACJA,EAAI,EAEJ,OAAOV,EAAkB,GAAI,EAE9B,GAAKU,IAAM,GAAOD,IAAM,EACvB,OAAOP,EAAY,CAAI,EAExB,GAAKQ,IAAM,EACV,OAAOR,EAAYO,CAAE,EAEtB,OAAOE,EAaP,SAASA,EAAKC,EAAI,CACjB,IAAIC,EACJ,OAAKV,EAAOS,CAAE,EACN,IAEHb,EAAsBa,CAAE,EACvBA,EAAIH,EACD,GAERI,EAAMZ,EAAaQ,EAAGG,CAAE,EACxBC,GAAQD,EAAIN,EAAII,CAAE,GAAOD,EAAIG,GAAKR,EAAO,CAACM,CAAE,EACrCL,EAAKQ,CAAI,GAEV,CACR,CACD,CAKAf,EAAO,QAAUU,IC9CjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD",
6
6
  "names": ["require_main", "__commonJSMin", "exports", "module", "isNonNegativeInteger", "binomcoefln", "isnan", "log1p", "exp", "ln", "PINF", "pmf", "x", "n", "p", "lnl", "require_factory", "__commonJSMin", "exports", "module", "isNonNegativeInteger", "constantFunction", "binomcoefln", "degenerate", "isnan", "log1p", "exp", "ln", "PINF", "factory", "n", "p", "pmf", "x", "lnl", "setReadOnly", "main", "factory"]
7
7
  }
@@ -126,7 +126,7 @@ interface PMF {
126
126
  * // returns ~0.201
127
127
  *
128
128
  * y = pmf( 0.0, 10, 0.4 );
129
- * // returns ~0.06
129
+ * // returns ~0.006
130
130
  *
131
131
  * var mypmf = pmf.factory( 10, 0.5 );
132
132
  *
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #ifndef STDLIB_STATS_BASE_DISTS_BINOMIAL_PMF_H
20
+ #define STDLIB_STATS_BASE_DISTS_BINOMIAL_PMF_H
21
+
22
+ #include <stdint.h>
23
+
24
+ /*
25
+ * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
26
+ */
27
+ #ifdef __cplusplus
28
+ extern "C" {
29
+ #endif
30
+
31
+ /**
32
+ * Evaluates the probability mass function (PMF) for a binomial distribution.
33
+ */
34
+ double stdlib_base_dists_binomial_pmf( const double x, const int32_t n, const double p );
35
+
36
+ #ifdef __cplusplus
37
+ }
38
+ #endif
39
+
40
+ #endif // !STDLIB_STATS_BASE_DISTS_BINOMIAL_PMF_H
package/lib/index.js CHANGED
@@ -36,7 +36,7 @@
36
36
  * // returns ~0.201
37
37
  *
38
38
  * y = pmf( 0.0, 10, 0.4 );
39
- * // returns ~0.06
39
+ * // returns ~0.006
40
40
  *
41
41
  * @example
42
42
  * var factory = require( '@stdlib/stats-base-dists-binomial-pmf' ).factory;
package/lib/native.js ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var addon = require( './../src/addon.node' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Evaluates the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p` at a value `x`.
30
+ *
31
+ * @private
32
+ * @param {number} x - input value
33
+ * @param {NonNegativeInteger} n - number of trials
34
+ * @param {Probability} p - success probability
35
+ * @returns {Probability} evaluated PMF
36
+ *
37
+ * @example
38
+ * var y = pmf( 3.0, 20, 0.2 );
39
+ * // returns ~0.205
40
+ *
41
+ * @example
42
+ * var y = pmf( 21.0, 20, 0.2 );
43
+ * // returns 0.0
44
+ *
45
+ * @example
46
+ * var y = pmf( 5.0, 10, 0.4 );
47
+ * // returns ~0.201
48
+ *
49
+ * @example
50
+ * var y = pmf( 0.0, 10, 0.4 );
51
+ * // returns ~0.006
52
+ *
53
+ * @example
54
+ * var y = pmf( NaN, 20, 0.5 );
55
+ * // returns NaN
56
+ *
57
+ * @example
58
+ * var y = pmf( 0.0, 20, NaN );
59
+ * // returns NaN
60
+ */
61
+ function pmf( x, n, p ) {
62
+ return addon( x, n, p );
63
+ }
64
+
65
+
66
+ // EXPORTS //
67
+
68
+ module.exports = pmf;
package/manifest.json ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "options": {
3
+ "task": "build",
4
+ "wasm": false
5
+ },
6
+ "fields": [
7
+ {
8
+ "field": "src",
9
+ "resolve": true,
10
+ "relative": true
11
+ },
12
+ {
13
+ "field": "include",
14
+ "resolve": true,
15
+ "relative": true
16
+ },
17
+ {
18
+ "field": "libraries",
19
+ "resolve": false,
20
+ "relative": false
21
+ },
22
+ {
23
+ "field": "libpath",
24
+ "resolve": true,
25
+ "relative": false
26
+ }
27
+ ],
28
+ "confs": [
29
+ {
30
+ "task": "build",
31
+ "wasm": false,
32
+ "src": [
33
+ "./src/main.c"
34
+ ],
35
+ "include": [
36
+ "./include"
37
+ ],
38
+ "libraries": [],
39
+ "libpath": [],
40
+ "dependencies": [
41
+ "@stdlib/math-base-napi-ternary",
42
+ "@stdlib/math-base-special-binomcoefln",
43
+ "@stdlib/math-base-assert-is-nan",
44
+ "@stdlib/math-base-special-exp",
45
+ "@stdlib/math-base-special-log1p",
46
+ "@stdlib/math-base-special-ln",
47
+ "@stdlib/math-base-assert-is-nonnegative-integer"
48
+ ]
49
+ },
50
+ {
51
+ "task": "benchmark",
52
+ "wasm": false,
53
+ "src": [
54
+ "./src/main.c"
55
+ ],
56
+ "include": [
57
+ "./include"
58
+ ],
59
+ "libraries": [],
60
+ "libpath": [],
61
+ "dependencies": [
62
+ "@stdlib/math-base-special-binomcoefln",
63
+ "@stdlib/math-base-assert-is-nan",
64
+ "@stdlib/math-base-special-exp",
65
+ "@stdlib/math-base-special-log1p",
66
+ "@stdlib/math-base-special-ln",
67
+ "@stdlib/math-base-assert-is-nonnegative-integer"
68
+ ]
69
+ },
70
+ {
71
+ "task": "examples",
72
+ "wasm": false,
73
+ "src": [
74
+ "./src/main.c"
75
+ ],
76
+ "include": [
77
+ "./include"
78
+ ],
79
+ "libraries": [],
80
+ "libpath": [],
81
+ "dependencies": [
82
+ "@stdlib/math-base-special-binomcoefln",
83
+ "@stdlib/math-base-assert-is-nan",
84
+ "@stdlib/math-base-special-exp",
85
+ "@stdlib/math-base-special-log1p",
86
+ "@stdlib/math-base-special-ln",
87
+ "@stdlib/math-base-assert-is-nonnegative-integer"
88
+ ]
89
+ }
90
+ ]
91
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stdlib/stats-base-dists-binomial-pmf",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Binomial distribution probability mass function (PMF).",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
@@ -14,9 +14,12 @@
14
14
  }
15
15
  ],
16
16
  "main": "./lib",
17
+ "gypfile": false,
17
18
  "directories": {
18
19
  "doc": "./docs",
20
+ "include": "./include",
19
21
  "lib": "./lib",
22
+ "src": "./src",
20
23
  "dist": "./dist"
21
24
  },
22
25
  "types": "./docs/types",
@@ -32,14 +35,16 @@
32
35
  "dependencies": {
33
36
  "@stdlib/constants-float64-pinf": "^0.2.2",
34
37
  "@stdlib/math-base-assert-is-nan": "^0.2.2",
35
- "@stdlib/math-base-assert-is-nonnegative-integer": "^0.2.1",
36
- "@stdlib/math-base-special-binomcoefln": "^0.2.1",
38
+ "@stdlib/math-base-assert-is-nonnegative-integer": "^0.2.2",
39
+ "@stdlib/math-base-napi-ternary": "^0.3.1",
40
+ "@stdlib/math-base-special-binomcoefln": "^0.2.2",
37
41
  "@stdlib/math-base-special-exp": "^0.2.4",
38
42
  "@stdlib/math-base-special-ln": "^0.2.4",
39
43
  "@stdlib/math-base-special-log1p": "^0.2.3",
40
- "@stdlib/stats-base-dists-degenerate-pmf": "^0.2.2",
44
+ "@stdlib/stats-base-dists-degenerate-pmf": "^0.3.0",
41
45
  "@stdlib/utils-constant-function": "^0.2.2",
42
- "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2"
46
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
47
+ "@stdlib/utils-library-manifest": "^0.2.3"
43
48
  },
44
49
  "devDependencies": {},
45
50
  "engines": {
package/src/addon.c ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "stdlib/stats/base/dists/binomial/pmf.h"
20
+ #include "stdlib/math/base/napi/ternary.h"
21
+
22
+ STDLIB_MATH_BASE_NAPI_MODULE_DID_D( stdlib_base_dists_binomial_pmf )
package/src/main.c ADDED
@@ -0,0 +1,67 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "stdlib/stats/base/dists/binomial/pmf.h"
20
+ #include "stdlib/math/base/assert/is_nan.h"
21
+ #include "stdlib/math/base/assert/is_nonnegative_integer.h"
22
+ #include "stdlib/math/base/special/binomcoefln.h"
23
+ #include "stdlib/math/base/special/ln.h"
24
+ #include "stdlib/math/base/special/log1p.h"
25
+ #include "stdlib/math/base/special/exp.h"
26
+ #include <stdint.h>
27
+
28
+ /**
29
+ * Evaluates the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p` at a value `x`.
30
+ *
31
+ * @param x input value
32
+ * @param n number of trials
33
+ * @param p success probability
34
+ * @return evaluated PMF
35
+ *
36
+ * @example
37
+ * double y = stdlib_base_dists_binomial_pmf( 3.0, 20, 0.2 );
38
+ * // returns ~0.205
39
+ */
40
+ double stdlib_base_dists_binomial_pmf( const double x, const int32_t n, const double p ) {
41
+ double lnl;
42
+
43
+ if (
44
+ stdlib_base_is_nan( x ) ||
45
+ stdlib_base_is_nan( p ) ||
46
+ n < 0 ||
47
+ p < 0.0 ||
48
+ p > 1.0
49
+ ) {
50
+ return 0.0 / 0.0; // NaN
51
+ }
52
+ if ( !stdlib_base_is_nonnegative_integer( x ) ) {
53
+ return 0.0;
54
+ }
55
+ if ( x > n ) {
56
+ return 0.0;
57
+ }
58
+ if ( p == 0.0 ) {
59
+ return ( x == 0.0 ) ? 1.0 : 0.0;
60
+ }
61
+ if ( p == 1.0 ) {
62
+ return ( x == n ) ? 1.0 : 0.0;
63
+ }
64
+ lnl = stdlib_base_binomcoefln( n, x );
65
+ lnl += ( x * stdlib_base_ln( p ) ) + ( ( n - x ) * stdlib_base_log1p( -p ) );
66
+ return stdlib_base_exp( lnl );
67
+ }