gammaincinv.js (3048B)
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 /* 22 * Translated from the Fortran module by 23 * ---------------------------------------------------------------------- 24 * Authors: 25 * Amparo Gil (U. Cantabria, Santander, Spain) 26 * e-mail: amparo.gil@unican.es 27 * Javier Segura (U. Cantabria, Santander, Spain) 28 * e-mail: javier.segura@unican.es 29 * Nico M. Temme (CWI, Amsterdam, The Netherlands) 30 * e-mail: nico.temme@cwi.nl 31 * --------------------------------------------------------------------- 32 */ 33 34 // MODULES // 35 36 var isnan = require( './../../../../base/assert/is-nan' ); 37 var FLOAT32_SMALLEST = require( '@stdlib/constants/float32/smallest-normal' ); 38 var PINF = require( '@stdlib/constants/float64/pinf' ); 39 var compute = require( './compute.js' ); 40 41 42 // MAIN // 43 44 /** 45 * Inverts the lower gamma function; i.e., computes `xr` such that `P(a,xr) = p`. 46 * 47 * ## Method 48 * 49 * The present code uses different methods of computation depending on the values of the input values: Taylor, asymptotic expansions and high-order Newton methods. 50 * 51 * ## Notes 52 * 53 * - The claimed accuracy obtained using this inversion routine is near `1e-12`. 54 * 55 * ## References 56 * 57 * - A. Gil, J. Segura and N.M. Temme, GammaCHI: a package for the inversion and computation of the gamma and chi-square distribution functions (central and noncentral). Computer Physics Commun 58 * - A. Gil, J. Segura and N.M. Temme. Efficient and accurate algorithms for the computation and inversion of the incomplete gamma function ratios. SIAM J Sci Comput. (2012) 34(6), A2965-A2981 59 * 60 * 61 * @param {Probability} p - probability value 62 * @param {number} a - scale parameter 63 * @param {boolean} [upper=false] - boolean indicating if the function should invert the upper tail of the incomplete gamma function instead; i.e., compute `xr` such that `Q(a,xr) = p`. 64 * @returns {number} function value of the inverse 65 */ 66 function gammaincinv( p, a, upper ) { 67 if ( isnan( p ) || isnan( a ) ) { 68 return NaN; 69 } 70 if ( a < FLOAT32_SMALLEST ) { 71 return NaN; 72 } 73 if ( p > 1.0 || p < 0.0 ) { 74 return NaN; 75 } 76 // Case: invert upper gamma function 77 if ( upper === true ) { 78 if ( p === 0.0 ) { 79 return PINF; 80 } 81 if ( p === 1.0 ) { 82 return 0.0; 83 } 84 return compute( a, 1.0-p, p ); 85 } 86 // Default: invert lower gamma function 87 if ( p === 0.0 ) { 88 return 0.0; 89 } 90 if ( p === 1.0 ) { 91 return PINF; 92 } 93 return compute( a, p, 1.0-p ); 94 } 95 96 97 // EXPORTS // 98 99 module.exports = gammaincinv;