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