logpmf.js (2715B)
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 isNonNegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' ); 24 var binomcoefln = require( '@stdlib/math/base/special/binomcoefln' ); 25 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 26 var log1p = require( '@stdlib/math/base/special/log1p' ); 27 var ln = require( '@stdlib/math/base/special/ln' ); 28 var NINF = require( '@stdlib/constants/float64/ninf' ); 29 var PINF = require( '@stdlib/constants/float64/pinf' ); 30 31 32 // MAIN // 33 34 /** 35 * Evaluates the natural logarithm of the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p` at a value `x`. 36 * 37 * @param {number} x - input value 38 * @param {NonNegativeInteger} n - number of trials 39 * @param {Probability} p - success probability 40 * @returns {number} evaluated logPMF 41 * 42 * @example 43 * var y = logpmf( 3.0, 20, 0.2 ); 44 * // returns ~-1.583 45 * 46 * @example 47 * var y = logpmf( 21.0, 20, 0.2 ); 48 * // returns -Infinity 49 * 50 * @example 51 * var y = logpmf( 5.0, 10, 0.4 ); 52 * // returns ~-1.606 53 * 54 * @example 55 * var y = logpmf( 0.0, 10, 0.4 ); 56 * // returns ~-5.108 57 * 58 * @example 59 * var y = logpmf( NaN, 20, 0.5 ); 60 * // returns NaN 61 * 62 * @example 63 * var y = logpmf( 0.0, NaN, 0.5 ); 64 * // returns NaN 65 * 66 * @example 67 * var y = logpmf( 0.0, 20, NaN ); 68 * // returns NaN 69 * 70 * @example 71 * var y = logpmf( 2.0, 1.5, 0.5 ); 72 * // returns NaN 73 * 74 * @example 75 * var y = logpmf( 2.0, -2.0, 0.5 ); 76 * // returns NaN 77 * 78 * @example 79 * var y = logpmf( 2.0, 20, -1.0 ); 80 * // returns NaN 81 * 82 * @example 83 * var y = logpmf( 2.0, 20, 1.5 ); 84 * // returns NaN 85 */ 86 function logpmf( x, n, p ) { 87 var out; 88 if ( 89 isnan( x ) || 90 isnan( n ) || 91 isnan( p ) || 92 p < 0.0 || 93 p > 1.0 || 94 !isNonNegativeInteger( n ) || 95 n === PINF 96 ) { 97 return NaN; 98 } 99 if ( isNonNegativeInteger( x ) ) { 100 if ( x > n ) { 101 return NINF; 102 } 103 if ( p === 0.0 ) { 104 return ( x === 0 ) ? 0.0 : NINF; 105 } 106 if ( p === 1.0 ) { 107 return ( x === n ) ? 0.0 : NINF; 108 } 109 out = binomcoefln( n, x ); 110 out += (x * ln( p )) + (( n - x ) * log1p( -p )); 111 return out; 112 } 113 return NINF; 114 } 115 116 117 // EXPORTS // 118 119 module.exports = logpmf;