factory.js (2962B)
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 trunc = require( '@stdlib/math/base/special/trunc' ); 27 var max = require( '@stdlib/math/base/special/max' ); 28 var min = require( '@stdlib/math/base/special/min' ); 29 var pmf = require( './../../../../../base/dists/hypergeometric/pmf' ); 30 var PINF = require( '@stdlib/constants/float64/pinf' ); 31 var Float64Array = require( '@stdlib/array/float64' ); 32 var sum = require( './sum.js' ); 33 34 35 // MAIN // 36 37 /** 38 * Returns a function for evaluating the cumulative distribution function (CDF) for a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n`. 39 * 40 * @param {NonNegativeInteger} N - population size 41 * @param {NonNegativeInteger} K - subpopulation size 42 * @param {NonNegativeInteger} n - number of draws 43 * @returns {Function} CDF 44 * 45 * @example 46 * var mycdf = factory( 30, 20, 5 ); 47 * var y = mycdf( 4.0 ); 48 * // returns ~0.891 49 * 50 * y = mycdf( 1.0 ); 51 * // returns ~0.031 52 */ 53 function factory( N, K, n ) { 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 return cdf; 69 70 /** 71 * Evaluates the cumulative distribution function (CDF) for a hypergeometric distribution. 72 * 73 * @private 74 * @param {number} x - input value 75 * @returns {Probability} evaluated CDF 76 * 77 * @example 78 * var y = cdf( 2.0 ); 79 * // returns <number> 80 */ 81 function cdf( x ) { 82 var denom; 83 var probs; 84 var num; 85 var ret; 86 var i; 87 88 if ( isnan( x ) ) { 89 return NaN; 90 } 91 x = trunc( x ); 92 if ( x < max( 0, n + K - N ) ) { 93 return 0.0; 94 } 95 if ( x >= min( n, K ) ) { 96 return 1.0; 97 } 98 99 probs = new Float64Array( x+1 ); 100 probs[ x ] = pmf( x, N, K, n ); 101 102 /* 103 * Use recurrence relation: 104 * 105 * (x+1)( N - K - (n-x-1) )P(X=x+1)=(K-x)(n-x)P(X=x) 106 */ 107 for ( i = x-1; i >= 0; i-- ) { 108 num = ( i+1 ) * ( N-K-(n-i-1) ); 109 denom = ( K-i ) * ( n-i ); 110 probs[ i ] = ( num/denom ) * probs[ i+1 ]; 111 } 112 ret = sum( probs ); 113 return min( ret, 1.0 ); 114 } 115 } 116 117 118 // EXPORTS // 119 120 module.exports = factory;