time-to-botec

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

cdf.js (3247B)


      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 trunc = require( '@stdlib/math/base/special/trunc' );
     26 var max = require( '@stdlib/math/base/special/max' );
     27 var min = require( '@stdlib/math/base/special/min' );
     28 var pmf = require( './../../../../../base/dists/hypergeometric/pmf' );
     29 var PINF = require( '@stdlib/constants/float64/pinf' );
     30 var Float64Array = require( '@stdlib/array/float64' );
     31 var sum = require( './sum.js' );
     32 
     33 
     34 // MAIN //
     35 
     36 /**
     37 * Evaluates the cumulative distribution function (CDF) for a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n` at a value `x`.
     38 *
     39 * @param {number} x - input value
     40 * @param {NonNegativeInteger} N - population size
     41 * @param {NonNegativeInteger} K - subpopulation size
     42 * @param {NonNegativeInteger} n - number of draws
     43 * @returns {Probability} evaluated CDF
     44 *
     45 * @example
     46 * var y = cdf( 1.0, 8, 4, 2 );
     47 * // returns ~0.786
     48 *
     49 * @example
     50 * var y = cdf( 1.5, 8, 4, 2 );
     51 * // returns ~0.786
     52 *
     53 * @example
     54 * var y = cdf( 2.0, 8, 4, 2 );
     55 * // returns 1.0
     56 *
     57 * @example
     58 * var y = cdf( 0, 8, 4, 2 );
     59 * // returns ~0.214
     60 *
     61 * @example
     62 * var y = cdf( NaN, 10, 5, 2 );
     63 * // returns NaN
     64 *
     65 * @example
     66 * var y = cdf( 0.0, NaN, 5, 2 );
     67 * // returns NaN
     68 *
     69 * @example
     70 * var y = cdf( 0.0, 10, NaN, 2 );
     71 * // returns NaN
     72 *
     73 * @example
     74 * var y = cdf( 0.0, 10, 5, NaN );
     75 * // returns NaN
     76 *
     77 * @example
     78 * var y = cdf( 2.0, 10.5, 5, 2 );
     79 * // returns NaN
     80 *
     81 * @example
     82 * var y = cdf( 2.0, 10, 1.5, 2 );
     83 * // returns NaN
     84 *
     85 * @example
     86 * var y = cdf( 2.0, 10, 5, -2.0 );
     87 * // returns NaN
     88 *
     89 * @example
     90 * var y = cdf( 2.0, 10, 5, 12 );
     91 * // returns NaN
     92 *
     93 * @example
     94 * var y = cdf( 2.0, 8, 3, 9 );
     95 * // returns NaN
     96 */
     97 function cdf( x, N, K, n ) {
     98 	var denom;
     99 	var probs;
    100 	var num;
    101 	var ret;
    102 	var i;
    103 
    104 	if (
    105 		isnan( x ) ||
    106 		isnan( N ) ||
    107 		isnan( K ) ||
    108 		isnan( n ) ||
    109 		!isNonNegativeInteger( N ) ||
    110 		!isNonNegativeInteger( K ) ||
    111 		!isNonNegativeInteger( n ) ||
    112 		N === PINF ||
    113 		K === PINF ||
    114 		K > N ||
    115 		n > N
    116 	) {
    117 		return NaN;
    118 	}
    119 	x = trunc( x );
    120 	if ( x < max( 0, n+K-N ) ) {
    121 		return 0.0;
    122 	}
    123 	if ( x >= min( n, K ) ) {
    124 		return 1.0;
    125 	}
    126 
    127 	probs = new Float64Array( x+1 );
    128 	probs[ x ] = pmf( x, N, K, n );
    129 
    130 	/*
    131 	* Use recurrence relation:
    132 	*
    133 	*   (x+1)( N - K - (n-x-1))P(X=x+1)=(K-x)(n-x)P(X=x)
    134 	*/
    135 	for ( i = x-1; i >= 0; i-- ) {
    136 		num = ( i+1 ) * ( N-K-(n-i-1) );
    137 		denom = ( K-i ) * ( n-i );
    138 		probs[ i ] = ( num/denom ) * probs[ i+1 ];
    139 	}
    140 	ret = sum( probs );
    141 	return min( ret, 1.0 );
    142 }
    143 
    144 
    145 // EXPORTS //
    146 
    147 module.exports = cdf;