time-to-botec

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

factory.js (2507B)


      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 max = require( '@stdlib/math/base/special/max' );
     27 var min = require( '@stdlib/math/base/special/min' );
     28 var cdf = require( './../../../../../base/dists/hypergeometric/cdf' );
     29 var PINF = require( '@stdlib/constants/float64/pinf' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Returns a function for evaluating the quantile function for a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n`.
     36 *
     37 * @param {NonNegativeInteger} N - population size
     38 * @param {NonNegativeInteger} K - subpopulation size
     39 * @param {NonNegativeInteger} n - number of draws
     40 * @returns {Function} quantile function
     41 *
     42 * @example
     43 * var quantile = factory( 100, 20, 10 );
     44 * var y = quantile( 0.2 );
     45 * // returns 1
     46 *
     47 * y = quantile( 0.9 );
     48 * // returns 4
     49 */
     50 function factory( N, K, n ) {
     51 	if (
     52 		isnan( N ) ||
     53 		isnan( K ) ||
     54 		isnan( n ) ||
     55 		!isNonNegativeInteger( N ) ||
     56 		!isNonNegativeInteger( K ) ||
     57 		!isNonNegativeInteger( n ) ||
     58 		N === PINF ||
     59 		K === PINF ||
     60 		K > N ||
     61 		n > N
     62 	) {
     63 		return constantFunction( NaN );
     64 	}
     65 	return quantile;
     66 
     67 	/**
     68 	* Evaluates the quantile function for a hypergeometric distribution.
     69 	*
     70 	* @private
     71 	* @param {Probability} p - input value
     72 	* @returns {NonNegativeInteger} evaluated quantile function
     73 	*/
     74 	function quantile( p ) {
     75 		var prob;
     76 		var x;
     77 
     78 		if ( isnan( p ) || p < 0.0 || p > 1.0 ) {
     79 			return NaN;
     80 		}
     81 		if ( p === 0.0 ) {
     82 			return max( 0, n + K - N );
     83 		}
     84 		if ( p === 1.0 ) {
     85 			return min( n, K );
     86 		}
     87 		x = max( 0, n + K - N );
     88 		while ( true ) {
     89 			prob = cdf( x, N, K, n );
     90 			if ( prob > p ) {
     91 				break;
     92 			}
     93 			x += 1;
     94 		}
     95 		return x;
     96 	}
     97 }
     98 
     99 
    100 // EXPORTS //
    101 
    102 module.exports = factory;