Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 86549x 9x 9x 9x 9x 86549x 43295x 6x 6x 6x 6x 43289x 43295x 2x 2x 2x 2x 2x 43295x 2x 2x 2x 2x 2x 86549x 6x 6x 6x 6x 86549x 6x 5x 6x 1x 1x 6x 2x 2x 2x 4x 4x 4x 86549x 4x 4x 4x 4x 2x 2x 2x 2x 4x 4x 86549x 8x 8x 8x 8x 8x 8x 2x 2x 8x 8x 8x 86549x 2171x 2171x 2171x 2171x 2171x 2171x 2171x 2171x 2171x 2171x 86549x 4021x 4021x 85x 85x 85x 85x 3936x 4021x 3514x 4021x 422x 422x 4021x 1964x 1964x 3936x 3936x 3936x 86549x 3676x 1781x 1781x 1781x 1781x 1781x 1781x 1781x 1781x 1781x 1781x 3676x 920x 920x 469x 234x 234x 234x 469x 235x 235x 920x 451x 451x 451x 451x 451x 451x 451x 451x 451x 451x 177x 88x 88x 88x 177x 89x 89x 451x 274x 274x 138x 274x 136x 136x 274x 274x 137x 137x 137x 137x 137x 137x 274x 451x 3676x 1409x 704x 704x 704x 1409x 705x 705x 2756x 1347x 1347x 1347x 1347x 1347x 1347x 1347x 1347x 1347x 1347x 577x 288x 288x 288x 577x 289x 289x 577x 770x 585x 292x 292x 292x 585x 293x 293x 585x 185x 185x 93x 185x 92x 92x 92x 185x 185x 92x 92x 92x 185x 93x 93x 185x 1347x 86549x 78809x 78809x 39836x 78809x 38973x 38973x 78809x 34857x 34857x 34857x 34857x 34857x 34857x 34857x 34857x 34857x 78809x 23608x 23608x 23608x 3249x 23608x 3249x 3249x 3249x 3249x 3249x 1623x 1623x 3249x 20359x 921x 460x 460x 460x 921x 461x 461x 921x 19438x 15313x 15313x 15313x 15313x 15313x 7657x 15313x 7656x 7656x 15313x 15313x 15313x 15313x 4125x 2063x 2063x 2063x 2063x 2063x 2063x 1031x 1031x 2063x 2063x 1031x 1031x 1031x 2063x 2062x 2062x 2062x 78809x 55201x 55201x 78809x 86549x 22984x 22984x 82485x 86549x 57746x 57746x 57746x 57746x 57746x 86549x 86549x 86549x 1x 1x 1x 1x 1x | /* eslint-disable max-statements, max-lines */
/**
* @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.
* (C) Copyright Paul A. Bristow 2007.
*
* 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
var expm1 = require( '@stdlib/math/base/special/expm1' );
var floor = require( '@stdlib/math/base/special/floor' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var asin = require( '@stdlib/math/base/special/asin' );
var beta = require( '@stdlib/math/base/special/beta' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var exp = require( '@stdlib/math/base/special/exp' );
var pow = require( '@stdlib/math/base/special/pow' );
var powm1 = require( '@stdlib/math/base/special/powm1' );
var max = require( '@stdlib/math/base/special/max' );
var min = require( '@stdlib/math/base/special/min' );
var MAX_FLOAT64 = require( '@stdlib/constants/float64/max' );
var MIN_FLOAT64 = require( '@stdlib/constants/float64/smallest-normal' );
var MAX_INT32 = require( '@stdlib/constants/int32/max' );
var HALF_PI = require( '@stdlib/constants/float64/half-pi' );
var PI = require( '@stdlib/constants/float64/pi' );
var betaSmallBLargeASeries = require( './beta_small_b_large_a_series.js' );
var risingFactorialRatio = require( './rising_factorial_ratio.js' );
var ibetaPowerTerms = require( './ibeta_power_terms.js' );
var ibetaFraction2 = require( './ibeta_fraction2.js' );
var binomialCCDF = require( './binomial_ccdf.js' );
var ibetaAStep = require( './ibeta_a_step.js' );
var ibetaSeries = require( './ibeta_series.js' );
// VARIABLES //
var ONE_OVER_PI = 1.0 / PI;
// MAIN //
/**
* Evaluates the incomplete beta function and its first derivative and assigns results to a provided output array.
*
* ## Notes
*
* - This function divides up the input range and selects the right implementation method for each domain.
*
* @param {Probability} x - function input
* @param {NonNegativeNumber} a - function parameter
* @param {NonNegativeNumber} b - function parameter
* @param {boolean} regularized - boolean indicating if the function should evaluate the regularized boolean beta function
* @param {boolean} upper - boolean indicating if the function should return the upper tail of the incomplete beta function instead
* @param {(Array|TypedArray|Object)} out - output array
* @param {integer} stride - output array stride
* @param {NonNegativeInteger} offset - output array index offset
* @returns {(Array|TypedArray|Object)} function value and first derivative
*
* @example
* var out = ibetaImp( 0.5, 2.0, 2.0, false, false, [ 0.0, 0.0 ], 1, 0 );
* // returns [ ~0.083, ~1.5 ]
*
* @example
* var out = ibetaImp( 0.2, 1.0, 2.0, false, true, [ 0.0, 0.0 ], 1, 0 );
* // returns [ 0.32, 1.6 ]
*
* @example
* var out = ibetaImp( 0.2, 1.0, 2.0, true, true, [ 0.0, 0.0 ], 1, 0 );
* // returns [ 0.64, 1.6 ]
*/
function ibetaImp( x, a, b, regularized, upper, out, stride, offset ) {
var lambda;
var prefix;
var fract;
var bbar;
var div;
var tmp;
var i0;
var i1;
var k;
var n;
var p;
var y;
y = 1.0 - x;
i0 = offset;
i1 = offset + stride;
// Derivative not set...
out[ i1 ] = -1;
if ( isnan( x ) || x < 0.0 || x > 1.0 ) {
out[ i0 ] = NaN;
out[ i1 ] = NaN;
return out;
}
if ( regularized ) {
if ( a < 0.0 || b < 0.0 ) {
out[ i0 ] = NaN;
out[ i1 ] = NaN;
return out;
}
// Extend to a few very special cases...
if ( a === 0.0 ) {
if ( b === 0.0 ) {
out[ i0 ] = NaN;
out[ i1 ] = NaN;
return out;
}
if ( b > 0.0 ) {
out[ i0 ] = ( upper ) ? 0.0 : 1.0;
return out;
}
} else if ( b === 0.0 ) {
if ( a > 0.0 ) {
out[ i0 ] = ( upper ) ? 1.0 : 0.0;
return out;
}
}
} else if ( a <= 0.0 || b <= 0.0 ) {
out[ i0 ] = NaN;
out[ i1 ] = NaN;
return out;
}
if ( x === 0.0 ) {
if ( a === 1.0 ) {
out[ i1 ] = 1.0;
} else {
out[ i1 ] = ( a < 1.0 ) ? MAX_FLOAT64 / 2.0 : MIN_FLOAT64 * 2.0;
}
if ( upper ) {
out[ i0 ] = ( regularized ) ? 1.0 : beta( a, b );
return out;
}
out[ i0 ] = 0.0;
return out;
}
if ( x === 1.0 ) {
if ( b === 1.0 ) {
out[ i1 ] = 1.0;
} else {
out[ i1 ] = ( b < 1.0 ) ? MAX_FLOAT64 / 2.0 : MIN_FLOAT64 * 2.0;
}
if ( upper ) {
out[ i0 ] = 0.0;
} else {
out[ i0 ] = ( regularized ) ? 1.0 : beta( a, b );
}
return out;
}
if ( a === 0.5 && b === 0.5 ) {
out[ i1 ] = ONE_OVER_PI * sqrt( y * x );
// We have an arcsine distribution:
p = ( upper ) ? asin( sqrt(y) ) : asin( sqrt(x) );
p /= HALF_PI;
if ( !regularized ) {
p *= PI;
}
out[ i0 ] = p;
return out;
}
if ( a === 1.0 ) {
tmp = b;
b = a;
a = tmp;
tmp = y;
y = x;
x = tmp;
upper = !upper;
}
if ( b === 1.0 ) {
// Special case see: http://functions.wolfram.com/GammaBetaErf/BetaRegularized/03/01/01/
if ( a === 1.0 ) {
out[ i0 ] = ( upper ) ? y : x;
out[ i1 ] = 1.0;
return out;
}
out[ i1 ] = a * pow( x, a - 1.0 );
if ( y < 0.5 ) {
p = ( upper ) ? -expm1( a * log1p(-y) ) : exp( a * log1p(-y) );
} else {
p = ( upper ) ? -powm1( x, a ) : pow( x, a );
}
if ( !regularized ) {
p /= a;
}
out[ i0 ] = p;
return out;
}
if ( min( a, b ) <= 1.0 ) {
if ( x > 0.5 ) {
tmp = b;
b = a;
a = tmp;
tmp = y;
y = x;
x = tmp;
upper = !upper;
}
if ( max( a, b ) <= 1.0 ) {
// Both a,b < 1:
if ( (a >= min( 0.2, b ) ) || ( pow(x, a) <= 0.9 ) ) {
if ( upper ) {
fract = -( ( regularized ) ? 1.0 : beta( a, b ) );
upper = false;
fract = -ibetaSeries( a, b, x, fract, regularized, out, y );
} else {
fract = ibetaSeries( a, b, x, 0, regularized, out, y );
}
} else {
tmp = b;
b = a;
a = tmp;
tmp = y;
y = x;
x = tmp;
upper = !upper;
if ( y >= 0.3 ) {
if ( upper ) {
fract = -( ( regularized ) ? 1.0 : beta( a, b ) );
upper = false;
fract = -ibetaSeries( a, b, x, fract, regularized, out, y ); // eslint-disable-line max-len
} else {
fract = ibetaSeries( a, b, x, 0, regularized, out, y );
}
} else {
// Sidestep on a, and then use the series representation:
if ( regularized ) {
prefix = 1;
} else {
prefix = risingFactorialRatio( a + b, a, 20 );
}
fract = ibetaAStep( a, b, x, y, 20, regularized, out );
if ( upper ) {
fract -= ( ( regularized ) ? 1 : beta( a, b ) );
upper = false;
fract = -betaSmallBLargeASeries( a + 20.0, b, x, y, fract, prefix, regularized ); // eslint-disable-line max-len
} else {
fract = betaSmallBLargeASeries( a + 20.0, b, x, y, fract, prefix, regularized ); // eslint-disable-line max-len
}
}
}
} else if ( b <= 1.0 || ( x < 0.1 && ( pow( b * x, a ) <= 0.7 ) ) ) {
if ( upper ) {
fract = -( ( regularized ) ? 1 : beta( a, b ) );
upper = false;
fract = -ibetaSeries( a, b, x, fract, regularized, out, y );
} else {
fract = ibetaSeries( a, b, x, 0.0, regularized, out, y );
}
} else {
tmp = b;
b = a;
a = tmp;
tmp = y;
y = x;
x = tmp;
upper = !upper;
if ( y >= 0.3 ) {
if ( upper ) {
fract = -(( regularized ) ? 1.0 : beta( a, b ));
upper = false;
fract = -ibetaSeries( a, b, x, fract, regularized, out, y );
} else {
fract = ibetaSeries( a, b, x, 0.0, regularized, out, y );
}
}
else if ( a >= 15.0 ) {
if ( upper ) {
fract = -(( regularized ) ? 1.0 : beta( a, b ));
upper = false;
fract = -betaSmallBLargeASeries( a, b, x, y, fract, 1.0, regularized ); // eslint-disable-line max-len
} else {
fract = betaSmallBLargeASeries( a, b, x, y, 0.0, 1.0, regularized ); // eslint-disable-line max-len
}
}
else {
if ( regularized ) {
prefix = 1;
} else {
// Sidestep to improve errors:
prefix = risingFactorialRatio( a + b, a, 20.0 );
}
fract = ibetaAStep( a, b, x, y, 20.0, regularized, out );
if ( upper ) {
fract -= ( ( regularized ) ? 1.0 : beta( a, b ) );
upper = false;
fract = -betaSmallBLargeASeries( a + 20.0, b, x, y, fract, prefix, regularized ); // eslint-disable-line max-len
} else {
fract = betaSmallBLargeASeries( a + 20.0, b, x, y, fract, prefix, regularized ); // eslint-disable-line max-len
}
}
}
} else {
// Both a,b >= 1:
if ( a < b ) {
lambda = a - ( (a + b) * x );
} else {
lambda = ( (a + b) * y ) - b;
}
if ( lambda < 0.0 ) {
tmp = b;
b = a;
a = tmp;
tmp = y;
y = x;
x = tmp;
upper = !upper;
}
if ( b < 40.0 ) {
if (
floor(a) === a &&
floor(b) === b &&
a < MAX_INT32 - 100
) {
// Relate to the binomial distribution and use a finite sum:
k = a - 1.0;
n = b + k;
fract = binomialCCDF( n, k, x, y );
if ( !regularized ) {
fract *= beta( a, b );
}
}
else if ( b * x <= 0.7 ) {
if ( upper ) {
fract = -( ( regularized ) ? 1.0 : beta( a, b ) );
upper = false;
fract = -ibetaSeries( a, b, x, fract, regularized, out, y );
} else {
fract = ibetaSeries( a, b, x, 0.0, regularized, out, y );
}
}
else if ( a > 15.0 ) {
// Sidestep so we can use the series representation:
n = floor( b );
if ( n === b ) {
n -= 1;
}
bbar = b - n;
if ( regularized ) {
prefix = 1;
} else {
prefix = risingFactorialRatio( a + bbar, bbar, n );
}
fract = ibetaAStep( bbar, a, y, x, n, regularized );
fract = betaSmallBLargeASeries( a, bbar, x, y, fract, 1.0, regularized ); // eslint-disable-line max-len
fract /= prefix;
}
else if ( regularized ) {
n = floor( b );
bbar = b - n;
if ( bbar <= 0 ) {
n -= 1;
bbar += 1;
}
fract = ibetaAStep( bbar, a, y, x, n, regularized );
fract += ibetaAStep( a, bbar, x, y, 20.0, regularized );
if ( upper ) {
fract -= 1;
}
fract = betaSmallBLargeASeries( a + 20.0, bbar, x, y, fract, 1, regularized ); // eslint-disable-line max-len
if ( upper ) {
fract = -fract;
upper = false;
}
}
else {
fract = ibetaFraction2( a, b, x, y, regularized, out );
}
} else {
fract = ibetaFraction2( a, b, x, y, regularized, out );
}
}
if ( out[ i1 ] < 0.0 ) {
out[ i1 ] = ibetaPowerTerms( a, b, x, y, true );
}
div = y * x;
if ( out[ i1 ] !== 0.0 ) {
if ( ( MAX_FLOAT64 * div < out[ i1 ] ) ) {
// Overflow, return an arbitrarily large value:
out[ i1 ] = MAX_FLOAT64 / 2.0;
} else {
out[ i1 ] /= div;
}
}
out[ i0 ] = ( upper ) ? ( ( regularized ) ? 1.0 : beta( a, b ) ) - fract : fract; // eslint-disable-line max-len
return out;
}
// EXPORTS //
module.exports = ibetaImp;
|