time-to-botec

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

pmf.js (1787B)


      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 isInteger = require( '@stdlib/math/base/assert/is-integer' );
     24 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Evaluates the probability mass function (PMF) for a discrete uniform distribution with minimum support `a` and maximum support `b` at a value `x`.
     31 *
     32 * @param {number} x - input value
     33 * @param {integer} a - minimum support
     34 * @param {integer} b - maximum support
     35 * @returns {number} evaluated PMF
     36 *
     37 * @example
     38 * var y = pmf( 2.0, 0, 4 );
     39 * // returns ~0.2
     40 *
     41 * @example
     42 * var y = pmf( 5.0, 0, 4 );
     43 * // returns 0.0
     44 *
     45 * @example
     46 * var y = pmf( 2, 0, 8 );
     47 * // returns ~0.111
     48 *
     49 * @example
     50 * var y = pmf( NaN, 0, 1 );
     51 * // returns NaN
     52 *
     53 * @example
     54 * var y = pmf( 0.0, NaN, 1 );
     55 * // returns NaN
     56 *
     57 * @example
     58 * var y = pmf( 0.0, 0, NaN );
     59 * // returns NaN
     60 *
     61 * @example
     62 * var y = pmf( 2.0, 3, 1 );
     63 * // returns NaN
     64 */
     65 function pmf( x, a, b ) {
     66 	if (
     67 		isnan( x ) ||
     68 		isnan( a ) ||
     69 		isnan( b ) ||
     70 		!isInteger( a ) ||
     71 		!isInteger( b ) ||
     72 		a > b
     73 	) {
     74 		return NaN;
     75 	}
     76 	if ( x < a || x > b || !isInteger( x ) ) {
     77 		return 0.0;
     78 	}
     79 	return 1.0 / ( b - a + 1.0 );
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = pmf;