time-to-botec

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

quantile.js (2681B)


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