time-to-botec

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

ibeta_a_step.js (2322B)


      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 ibetaPowerTerms = require( './ibeta_power_terms.js' );
     37 
     38 
     39 // MAIN //
     40 
     41 /**
     42 * Computes the difference between `ibeta(a,b,x)` and `ibeta(a+k,b,x)`.
     43 *
     44 * @private
     45 * @param {NonNegativeNumber} a - function parameter
     46 * @param {NonNegativeNumber} b - function parameter
     47 * @param {Probability} x - function parameter
     48 * @param {Probability} y - probability equal to `1-x`
     49 * @param {NonNegativeInteger} k - function input
     50 * @param {boolean} normalized - boolean indicating whether to evaluate the power terms of the regularized or non-regularized incomplete beta function
     51 * @param {(Array|TypedArray|Object)} out - output array holding the derivative as the second element
     52 * @returns {number} difference between ibeta(a,b,x) and ibeta(a+k,b,x)
     53 */
     54 function ibetaAStep( a, b, x, y, k, normalized, out ) {
     55 	var prefix;
     56 	var term;
     57 	var sum;
     58 	var i;
     59 
     60 	prefix = ibetaPowerTerms( a, b, x, y, normalized );
     61 	if ( out ) {
     62 		out[ 1 ] = prefix;
     63 	}
     64 	prefix /= a;
     65 	if ( prefix === 0.0 ) {
     66 		return prefix;
     67 	}
     68 	sum = 1.0;
     69 	term = 1.0;
     70 
     71 	// Series summation from 0 to k-1:
     72 	for ( i = 0; i < k-1; ++i ) {
     73 		term *= (a+b+i) * x / (a+i+1.0);
     74 		sum += term;
     75 	}
     76 	prefix *= sum;
     77 	return prefix;
     78 }
     79 
     80 
     81 // EXPORTS //
     82 
     83 module.exports = ibetaAStep;