time-to-botec

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

betaincinv.js (1936B)


      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 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isnan = require( './../../../../base/assert/is-nan' );
     24 var kernelBetaincinv = require( './../../../../base/special/kernel-betaincinv' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Returns a value `p` such that `p = betainc(a, b, x)`.
     31 *
     32 * @param {Probability} p - function parameter
     33 * @param {PositiveNumber} a - function parameter
     34 * @param {PositiveNumber} b - function parameter
     35 * @param {boolean} [upper=false] - boolean indicating if the function should return the inverse of the upper tail of the incomplete beta function
     36 * @returns {number} function value
     37 *
     38 * @example
     39 * var y = betaincinv( 0.2, 3.0, 3.0 );
     40 * // returns ~0.327
     41 *
     42 * @example
     43 * var y = betaincinv( 0.4, 3.0, 3.0 );
     44 * // returns ~0.446
     45 *
     46 * @example
     47 * var y = betaincinv( 0.4, 3.0, 3.0, true );
     48 * // returns ~0.554
     49 *
     50 * @example
     51 * var y = betaincinv( 0.4, 1.0, 6.0 );
     52 * // returns ~0.082
     53 *
     54 * @example
     55 * var y = betaincinv( 0.8, 1.0, 6.0 );
     56 * // returns ~0.235
     57 */
     58 function betaincinv( p, a, b, upper ) {
     59 	if (
     60 		isnan( p ) ||
     61 		isnan( a ) ||
     62 		isnan( b )
     63 	) {
     64 		return NaN;
     65 	}
     66 	if ( a <= 0.0 || b <= 0.0 ) {
     67 		return NaN;
     68 	}
     69 	if ( p < 0.0 || p > 1.0 ) {
     70 		return NaN;
     71 	}
     72 	if ( upper ) {
     73 		return kernelBetaincinv( a, b, 1.0 - p, p )[ 0 ];
     74 	}
     75 	return kernelBetaincinv( a, b, p, 1.0 - p )[ 0 ];
     76 }
     77 
     78 
     79 // EXPORTS //
     80 
     81 module.exports = betaincinv;