1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* ## Notice
*
* 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.
*
* ```text
* (C) Copyright John Maddock 2006.
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
* ```
*/
'use strict';
// MODULES //
var gammaDeltaRatio = require( './../../../../base/special/gamma-delta-ratio' );
var factorial = require( './../../../../base/special/factorial' );
var gammainc = require( './../../../../base/special/gammainc' );
var log1p = require( './../../../../base/special/log1p' );
var abs = require( './../../../../base/special/abs' );
var pow = require( './../../../../base/special/pow' );
var ln = require( './../../../../base/special/ln' );
var MIN_VALUE = require( '@stdlib/constants/float64/smallest-normal' );
var EPSILON = require( '@stdlib/constants/float64/eps' );
var fullIGammaPrefix = require( './full_igamma_prefix.js' );
var regularizedGammaPrefix = require( './regularized_gamma_prefix.js' );
// VARIABLES //
var p = new Array( 30 );
// MAIN //
/**
* This is DiDonato and Morris's BGRAT routine, see Eq's 9 through 9.6.
*
* @private
* @param {NonNegativeNumber} a - function parameter
* @param {NonNegativeNumber} b - function parameter
* @param {Probability} x - function parameter
* @param {Probability} y - probability equal to `1-x`
* @param {NonNegativeInteger} s0 - initial value
* @param {number} mult - initial value
* @param {boolean} normalized - boolean indicating whether to evaluate the regularized or non-regularized incomplete beta function
* @returns {number} function value
*/
function betaSmallBLargeASeries( a, b, x, y, s0, mult, normalized ) {
var prefix;
var tmp1;
var tnp1;
var sum;
var b2n;
var bm1;
var lx2;
var lxp;
var mbn;
var lx;
var t4;
var h;
var j;
var m;
var n;
var r;
var t;
var u;
// Some values we'll need later, these are Eq 9.1:
bm1 = b - 1.0;
t = a + ( bm1 / 2.0 );
if ( y < 0.35 ) {
lx = log1p( -y );
} else {
lx = ln( x );
}
u = -t * lx;
// And from from 9.2:
h = regularizedGammaPrefix( b, u );
if ( h <= MIN_VALUE ) {
return s0;
}
if ( normalized ) {
prefix = h / gammaDeltaRatio( a, b );
prefix /= pow( t, b );
} else {
prefix = fullIGammaPrefix( b, u ) / pow( t, b );
}
prefix *= mult;
// We need the quantity Pn. Unfortunately, this is computed recursively and requires a full history of all the previous values. No choice but to declare a big table and hope it's big enough...
p[ 0 ] = 1; // see 9.3.
// Now an initial value for J, see 9.6: gammainc( u, b, regularized, upper )
j = gammainc( u, b, true, true );
j /= h;
// Now we can start to pull things together and evaluate the sum in Eq 9:
sum = s0 + ( prefix * j ); // Value at N = 0
// Some variables we'll need...
tnp1 = 1.0; // 2*N+1
lx2 = lx / 2.0;
lx2 *= lx2;
lxp = 1.0;
t4 = 4.0 * t * t;
b2n = b;
for ( n = 1; n < p.length; ++n ) {
// Begin by evaluating the next Pn from Eq 9.4:
tnp1 += 2.0;
p[ n ] = 0.0;
mbn = b - n;
tmp1 = 3;
for ( m = 1; m < n; ++m ) {
mbn = ( m * b ) - n;
p[ n ] += mbn * p[ n-m ] / factorial( tmp1 );
tmp1 += 2;
}
p[ n ] /= n;
p[ n ] += bm1 / factorial( tnp1 );
// Now we want Jn from Jn-1 using Eq 9.6:
j = ( ( b2n * ( b2n+1.0 ) * j ) + ( ( u+b2n+1.0 ) * lxp ) ) / t4;
lxp *= lx2;
b2n += 2.0;
// Pull it together with Eq 9:
r = prefix * p[ n ] * j;
sum += r;
if ( r > 1.0 ) {
if ( abs( r ) < abs( EPSILON * sum ) ) {
break;
}
} else if ( abs( r / EPSILON ) < abs( sum ) ) {
break;
}
}
return sum;
}
// EXPORTS //
module.exports = betaSmallBLargeASeries;
|