time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

logpmf.js (2065B)


      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 factorialln = require( '@stdlib/math/base/special/factorialln' );
     25 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     26 var ln = require( '@stdlib/math/base/special/ln' );
     27 var NINF = require( '@stdlib/constants/float64/ninf' );
     28 var PINF = require( '@stdlib/constants/float64/pinf' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Evaluates the natural logarithm of the probability mass function (PMF) for a Poisson distribution with mean parameter `lambda` at a value `x`.
     35 *
     36 * @param {number} x - input value
     37 * @param {NonNegativeNumber} lambda - mean parameter
     38 * @returns {number} evaluated logPMF
     39 *
     40 * @example
     41 * var y = logpmf( 4.0, 3.0 );
     42 * // returns ~-1.784
     43 *
     44 * @example
     45 * var y = logpmf( 1.0, 3.0 );
     46 * // returns ~-1.901
     47 *
     48 * @example
     49 * var y = logpmf( -1.0, 2.0 );
     50 * // returns -Infinity
     51 *
     52 * @example
     53 * var y = logpmf( 0.0, NaN );
     54 * // returns NaN
     55 *
     56 * @example
     57 * var y = logpmf( NaN, 0.5 );
     58 * // returns NaN
     59 *
     60 * @example
     61 * // Invalid mean parameter:
     62 * var y = logpmf( 2.0, -0.5 );
     63 * // returns NaN
     64 */
     65 function logpmf( x, lambda ) {
     66 	if ( isnan( x ) || isnan( lambda ) || lambda < 0.0 ) {
     67 		return NaN;
     68 	}
     69 	if ( lambda === 0.0 ) {
     70 		return ( x === 0.0 ) ? 0.0 : NINF;
     71 	}
     72 	if ( isNonNegativeInteger( x ) && x !== PINF ) {
     73 		return ( x * ln( lambda ) ) - lambda - factorialln( x );
     74 	}
     75 	return NINF;
     76 }
     77 
     78 
     79 // EXPORTS //
     80 
     81 module.exports = logpmf;