pmf.js (2387B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); 24 var isNonNegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' ); 25 var ibetaDerivative = require( './ibeta_derivative.js' ); 26 27 28 // MAIN // 29 30 /** 31 * Evaluates the probability mass function (PMF) for a negative binomial distribution with number of successes until experiment is stopped `r` and success probability `p`. 32 * 33 * @param {number} x - input value 34 * @param {PositiveNumber} r - number of successes until experiment is stopped 35 * @param {Probability} p - success probability 36 * @returns {Probability} evaluated PMF 37 * 38 * @example 39 * var y = pmf( 5.0, 20.0, 0.8 ); 40 * // returns ~0.157 41 * 42 * @example 43 * var y = pmf( 21.0, 20.0, 0.5 ); 44 * // returns ~0.06 45 * 46 * @example 47 * var y = pmf( 5.0, 10.0, 0.4 ); 48 * // returns ~0.016 49 * 50 * @example 51 * var y = pmf( 0.0, 10.0, 0.9 ); 52 * // returns ~0.349 53 * 54 * @example 55 * var y = pmf( 21.0, 15.5, 0.5 ); 56 * // returns ~0.037 57 * 58 * @example 59 * var y = pmf( 5.0, 7.4, 0.4 ); 60 * // returns ~0.051 61 * 62 * @example 63 * var y = pmf( 2.0, 0.0, 0.5 ); 64 * // returns NaN 65 * 66 * @example 67 * var y = pmf( 2.0, -2.0, 0.5 ); 68 * // returns NaN 69 * 70 * @example 71 * var y = pmf( 2.0, 20, -1.0 ); 72 * // returns NaN 73 * 74 * @example 75 * var y = pmf( 2.0, 20, 1.5 ); 76 * // returns NaN 77 * 78 * @example 79 * var y = pmf( NaN, 20.0, 0.5 ); 80 * // returns NaN 81 * 82 * @example 83 * var y = pmf( 0.0, NaN, 0.5 ); 84 * // returns NaN 85 * 86 * @example 87 * var y = pmf( 0.0, 20.0, NaN ); 88 * // returns NaN 89 */ 90 function pmf( x, r, p ) { 91 if ( 92 isnan( x ) || 93 isnan( r ) || 94 isnan( p ) || 95 r <= 0.0 || 96 p <= 0.0 || 97 p > 1.0 98 ) { 99 return NaN; 100 } 101 if ( !isNonNegativeInteger( x ) || p === 0.0 ) { 102 return 0.0; 103 } 104 return ( p / ( r + x ) ) * ibetaDerivative( p, r, x + 1.0 ); 105 } 106 107 108 // EXPORTS // 109 110 module.exports = pmf;