time-to-botec

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

factory.js (2651B)


      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 isnan = require( '@stdlib/math/base/assert/is-nan' );
     25 var constantFunction = require( '@stdlib/utils/constant-function' );
     26 var exp = require( '@stdlib/math/base/special/exp' );
     27 var fln = require( '@stdlib/math/base/special/factorialln' );
     28 var max = require( '@stdlib/math/base/special/max' );
     29 var min = require( '@stdlib/math/base/special/min' );
     30 var PINF = require( '@stdlib/constants/float64/pinf' );
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Returns a function for evaluating the probability mass function (PMF) for a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n`.
     37 *
     38 * @param {NonNegativeInteger} N - population size
     39 * @param {NonNegativeInteger} K - subpopulation size
     40 * @param {NonNegativeInteger} n - number of draws
     41 * @returns {Function} PMF
     42 *
     43 * @example
     44 * var mypmf = factory( 30, 20, 5 );
     45 * var y = mypmf( 4.0 );
     46 * // returns ~0.34
     47 *
     48 * y = mypmf( 1.0 );
     49 * // returns ~0.029
     50 */
     51 function factory( N, K, n ) {
     52 	var maxs;
     53 	var mins;
     54 	if (
     55 		isnan( N ) ||
     56 		isnan( K ) ||
     57 		isnan( n ) ||
     58 		!isNonNegativeInteger( N ) ||
     59 		!isNonNegativeInteger( K ) ||
     60 		!isNonNegativeInteger( n ) ||
     61 		N === PINF ||
     62 		K === PINF ||
     63 		K > N ||
     64 		n > N
     65 	) {
     66 		return constantFunction( NaN );
     67 	}
     68 
     69 	mins = max( 0, n + K - N );
     70 	maxs = min( K, n );
     71 	return pmf;
     72 
     73 	/**
     74 	* Evaluates the probability mass function (PMF) for a hypergeometric distribution.
     75 	*
     76 	* @private
     77 	* @param {number} x - input value
     78 	* @returns {Probability} evaluated PMF
     79 	*/
     80 	function pmf( x ) {
     81 		var ldenom;
     82 		var lnum;
     83 		var lpmf;
     84 		if ( isnan( x ) ) {
     85 			return NaN;
     86 		}
     87 		if (
     88 			isNonNegativeInteger( x ) &&
     89 			mins <= x &&
     90 			x <= maxs
     91 		) {
     92 			lnum = fln( n ) + fln( K ) + fln( N - n ) + fln( N - K );
     93 			ldenom = fln( N ) + fln( x ) + fln( n - x );
     94 			ldenom += fln( K - x ) + fln( N - K + x - n );
     95 			lpmf = lnum - ldenom;
     96 			return exp( lpmf );
     97 		}
     98 		return 0.0;
     99 	}
    100 }
    101 
    102 
    103 // EXPORTS //
    104 
    105 module.exports = factory;