time-to-botec

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

ibeta_roots.js (2699B)


      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_64_0/boost/math/special_functions/detail/ibeta_inverse.hpp}. The implementation has been modified for JavaScript.
     22 *
     23 * ```text
     24 * Copyright John Maddock 2006.
     25 * Copyright Paul A. Bristow 2007.
     26 *
     27 * Use, modification and distribution are subject to the
     28 * Boost Software License, Version 1.0. (See accompanying file
     29 * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
     30 * ```
     31 */
     32 
     33 'use strict';
     34 
     35 // MODULES //
     36 
     37 var kernelBetainc = require( './../../../../base/special/kernel-betainc' ).assign;
     38 var abs = require( './../../../../base/special/abs' );
     39 var FLOAT64_MAX = require( '@stdlib/constants/float64/max' );
     40 var FLOAT64_MIN_NORM = require( '@stdlib/constants/float64/smallest-normal' );
     41 
     42 
     43 // MAIN //
     44 
     45 /**
     46 * Returns a root finding function.
     47 *
     48 * @private
     49 * @param {PositiveNumber} a - function parameter
     50 * @param {PositiveNumber} b - function parameter
     51 * @param {Probability} target - probability value
     52 * @param {boolean} invert - boolean indicating whether to find the roots of the upper or lower incomplete beta function
     53 * @returns {Function} root finding function
     54 */
     55 function ibetaRoots( a, b, target, invert ) {
     56 	return roots;
     57 
     58 	/**
     59 	* Calculates roots.
     60 	*
     61 	* @private
     62 	* @param {number} x - input value
     63 	* @returns {Array} roots
     64 	*/
     65 	function roots( x ) {
     66 		var buf;
     67 		var f1;
     68 		var f2;
     69 		var f;
     70 		var y;
     71 
     72 		y = 1.0 - x;
     73 
     74 		buf = [ 0.0, 0.0 ];
     75 		kernelBetainc( x, a, b, true, invert, buf, 1, 0 );
     76 		f = buf[ 0 ] - target;
     77 		f1 = buf[ 1 ];
     78 		if ( invert ) {
     79 			f1 = -f1;
     80 		}
     81 		if ( y === 0.0 ) {
     82 			y = FLOAT64_MIN_NORM * 64.0;
     83 		}
     84 		if ( x === 0.0 ) {
     85 			x = FLOAT64_MIN_NORM * 64.0;
     86 		}
     87 		f2 = f1 * ( -(y*a) + ( ( b-2.0 ) * x ) + 1.0 );
     88 		if ( abs( f2 ) < y * x * FLOAT64_MAX ) {
     89 			f2 /= (y * x);
     90 		}
     91 		if ( invert ) {
     92 			f2 = -f2;
     93 		}
     94 		// Make sure we don't have a zero derivative:
     95 		if ( f1 === 0.0 ) {
     96 			f1 = ( ( invert ) ? -1.0 : 1.0 ) * FLOAT64_MIN_NORM * 64.0;
     97 		}
     98 		return [ f, f1, f2 ];
     99 	}
    100 }
    101 
    102 
    103 // EXPORTS //
    104 
    105 module.exports = ibetaRoots;