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 166 167 168 169 170 171
|
/**
* @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_64_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 lanczosSumExpGScaled = require( './../../../../base/special/gamma-lanczos-sum-expg-scaled' );
var sumSeries = require( './../../../../base/tools/sum-series' );
var log1p = require( './../../../../base/special/log1p' );
var sqrt = require( './../../../../base/special/sqrt' );
var exp = require( './../../../../base/special/exp' );
var pow = require( './../../../../base/special/pow' );
var ln = require( './../../../../base/special/ln' );
var MIN_VALUE = require( '@stdlib/constants/float64/smallest-normal' );
var MAX_LN = require( '@stdlib/constants/float64/max-ln' );
var MIN_LN = require( '@stdlib/constants/float64/min-ln' );
var G = require( '@stdlib/constants/float64/gamma-lanczos-g' );
var E = require( '@stdlib/constants/float64/e' );
// VARIABLES //
var opts = {
'maxTerms': 100
};
// FUNCTIONS //
/**
* Series approximation to the incomplete beta.
*
* @private
* @param {NonNegativeNumber} a - function parameter
* @param {NonNegativeNumber} b - function parameter
* @param {Probability} x - function parameter
* @param {number} result - initial result value
* @returns {Function} series function
*/
function ibetaSeriesT( a, b, x, result ) {
var poch = 1.0 - b;
var n = 1;
return next;
/**
* Calculate the next term of the series.
*
* @private
* @returns {number} series expansion term
*/
function next() {
var r = result / a;
a += 1.0;
result *= poch * x / n;
n += 1;
poch += 1.0;
return r;
}
}
// MAIN //
/**
* Incomplete beta series.
*
* @private
* @param {NonNegativeNumber} a - function parameter
* @param {NonNegativeNumber} b - function parameter
* @param {Probability} x - function parameter
* @param {NonNegativeInteger} s0 - initial value
* @param {boolean} normalized - boolean indicating whether to evaluate the power terms of the regularized or non-regularized incomplete beta function
* @param {(Array|TypedArray|Object)} out - output array holding the derivative as the second element
* @param {Probability} y - probability equal to `1-x`
* @returns {number} function value
*/
function ibetaSeries( a, b, x, s0, normalized, out, y ) {
var result;
var agh;
var bgh;
var cgh;
var l1;
var l2;
var c;
var s;
if ( normalized ) {
c = a + b;
// Incomplete beta power term, combined with the Lanczos approximation:
agh = a + G - 0.5;
bgh = b + G - 0.5;
cgh = c + G - 0.5;
result = lanczosSumExpGScaled( c ) / ( lanczosSumExpGScaled( a ) * lanczosSumExpGScaled( b ) ); // eslint-disable-line max-len
l1 = ln( cgh / bgh ) * ( b - 0.5 );
l2 = ln( x * cgh / agh ) * a;
// Check for over/underflow in the power terms:
if (
l1 > MIN_LN &&
l1 < MAX_LN &&
l2 > MIN_LN &&
l2 < MAX_LN
) {
if ( a * b < bgh * 10.0 ) {
result *= exp( ( b-0.5 ) * log1p( a / bgh ) );
} else {
result *= pow( cgh / bgh, b - 0.5 );
}
result *= pow( x * cgh / agh, a );
result *= sqrt( agh / E );
if ( out ) {
out[ 1 ] = result * pow( y, b );
}
}
else {
// We need logs, and this *will* cancel:
result = ln( result ) + l1 + l2 + ( ( ln( agh ) - 1.0 ) / 2.0 );
if ( out ) {
out[ 1 ] = exp( result + ( b * ln( y ) ) );
}
result = exp( result );
}
}
else {
// Non-normalized, just compute the power:
result = pow( x, a );
}
if ( result < MIN_VALUE ) {
return s0; // Safeguard: series can't cope with denorms.
}
s = ibetaSeriesT( a, b, x, result );
opts.initialValue = s0;
return sumSeries( s, opts );
}
// EXPORTS //
module.exports = ibetaSeries;
|