ibeta_fraction2.js (3219B)
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 continuedFraction = require( './../../../../base/tools/continued-fraction' ); 37 var ibetaPowerTerms = require( './ibeta_power_terms.js' ); 38 39 40 // VARIABLES // 41 42 var OPTS = { 43 'keep': true, 44 'maxIter': 1000 45 }; 46 47 48 // FUNCTIONS // 49 50 /** 51 * Continued fraction for the incomplete beta. 52 * 53 * @private 54 * @param {NonNegativeNumber} a - function parameter 55 * @param {NonNegativeNumber} b - function parameter 56 * @param {Probability} x - function parameter 57 * @param {Probability} y - probability equal to `1-x` 58 * @returns {Function} series function 59 */ 60 function ibetaFraction2t( a, b, x, y ) { 61 var m = 0; 62 return next; 63 64 /** 65 * Calculate the numerator and denominator of the next term of the series. 66 * 67 * @private 68 * @returns {Array} series expansion terms 69 */ 70 function next() { 71 var denom; 72 var aN; 73 var bN; 74 75 aN = (a + m - 1) * (a + b + m - 1) * m * (b - m) * x * x; 76 denom = a + ( 2.0*m ) - 1.0; 77 aN /= denom * denom; 78 bN = m; 79 bN += (m * (b - m) * x) / ( a + ( 2.0*m ) - 1.0 ); 80 bN += ( (a+m) * ( (a*y) - (b*x) + 1.0 + ( m*(2.0-x) ) ) ) / ( a + (2.0*m) + 1.0 ); // eslint-disable-line max-len 81 m += 1; 82 return [ aN, bN ]; 83 } 84 } 85 86 87 // MAIN // 88 89 /** 90 * Evaluates the incomplete beta via the continued fraction representation. 91 * 92 * @private 93 * @param {NonNegativeNumber} a - function parameter 94 * @param {NonNegativeNumber} b - function parameter 95 * @param {Probability} x - function parameter 96 * @param {Probability} y - probability equal to `1-x` 97 * @param {boolean} normalized - boolean indicating whether to evaluate the power terms of the regularized or non-regularized incomplete beta function 98 * @param {(Array|TypedArray|Object)} out - output array holding the derivative as the second element 99 * @returns {number} incomplete beta value 100 */ 101 function ibetaFraction2( a, b, x, y, normalized, out ) { 102 var result; 103 var fract; 104 var f; 105 106 result = ibetaPowerTerms( a, b, x, y, normalized ); 107 if ( out ) { 108 out[ 1 ] = result; 109 } 110 if ( result === 0.0 ) { 111 return result; 112 } 113 f = ibetaFraction2t( a, b, x, y ); 114 fract = continuedFraction( f, OPTS ); 115 return result / fract; 116 } 117 118 119 // EXPORTS // 120 121 module.exports = ibetaFraction2;