time-to-botec

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

binomial_ccdf.js (2941B)


      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 * ## Notice
     20 *
     21 * The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_61_0/boost/math/special_functions/beta.hpp}. The implementation has been modified for JavaScript.
     22 *
     23 * ```text
     24 * (C) Copyright John Maddock 2006.
     25 *
     26 * Use, modification and distribution are subject to the
     27 * Boost Software License, Version 1.0. (See accompanying file
     28 * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
     29 * ```
     30 */
     31 
     32 'use strict';
     33 
     34 // MODULES //
     35 
     36 var binomcoef = require( './../../../../base/special/binomcoef' );
     37 var floor = require( './../../../../base/special/floor' );
     38 var pow = require( './../../../../base/special/pow' );
     39 var MIN_VALUE = require( '@stdlib/constants/float64/smallest-normal' );
     40 
     41 
     42 // MAIN //
     43 
     44 /**
     45 * For integer arguments we can relate the incomplete beta to the complement of the binomial distribution cdf and use this finite sum.
     46 *
     47 * @private
     48 * @param {NonNegativeInteger} n - number of trials
     49 * @param {NonNegativeInteger} k - function input
     50 * @param {Probability} x - function input
     51 * @param {Probability} y - probability equal to `1-x`
     52 * @returns {number} sum
     53 */
     54 function binomialCCDF( n, k, x, y ) {
     55 	var startTerm;
     56 	var result;
     57 	var start;
     58 	var term;
     59 	var i;
     60 
     61 	result = pow( x, n );
     62 	if ( result > MIN_VALUE ) {
     63 		term = result;
     64 		for ( i = floor( n - 1 ); i > k; i-- ) {
     65 			term *= ((i + 1) * y) / ((n - i) * x);
     66 			result += term;
     67 		}
     68 	} else {
     69 		// First term underflows so we need to start at the mode of the distribution and work outwards:
     70 		start = floor( n * x );
     71 		if ( start <= k + 1 ) {
     72 			start = floor( k + 2 );
     73 		}
     74 		result = pow( x, start ) * pow( y, n - start );
     75 		result *= binomcoef( floor(n), floor(start) );
     76 		if ( result === 0.0 ) {
     77 			// OK, starting slightly above the mode didn't work, we'll have to sum the terms the old fashioned way:
     78 			for ( i = start - 1; i > k; i-- ) {
     79 				result += pow( x, i ) * pow( y, n - i );
     80 				result *= binomcoef( floor(n), floor(i) );
     81 			}
     82 		} else {
     83 			term = result;
     84 			startTerm = result;
     85 			for ( i = start - 1; i > k; i-- ) {
     86 				term *= ((i + 1) * y) / ((n - i) * x);
     87 				result += term;
     88 			}
     89 			term = startTerm;
     90 			for ( i = start + 1; i <= n; i++ ) {
     91 				term *= (n - i + 1) * x / (i * y);
     92 				result += term;
     93 			}
     94 		}
     95 	}
     96 	return result;
     97 }
     98 
     99 
    100 // EXPORTS //
    101 
    102 module.exports = binomialCCDF;