quantile.js (3246B)
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 cdf = require( './../../../../../base/dists/negative-binomial/cdf' ); 24 var erfcinv = require( '@stdlib/math/base/special/erfcinv' ); 25 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 26 var round = require( '@stdlib/math/base/special/round' ); 27 var sqrt = require( '@stdlib/math/base/special/sqrt' ); 28 var SQRT2 = require( '@stdlib/constants/float64/sqrt-two' ); 29 var PINF = require( '@stdlib/constants/float64/pinf' ); 30 var search = require( './search.js' ); 31 32 33 // MAIN // 34 35 /** 36 * Evaluates the quantile function for a negative binomial distribution with number of successes until experiment is stopped `r` and success probability `p` at a probability `k`. 37 * 38 * @param {Probability} k - input value 39 * @param {PositiveNumber} r - number of successes until experiment is stopped 40 * @param {Probability} p - success probability 41 * @returns {NonNegativeInteger} evaluated quantile function 42 * 43 * @example 44 * var y = quantile( 0.9, 20.0, 0.2 ); 45 * // returns 106 46 * 47 * @example 48 * var y = quantile( 0.9, 20.0, 0.8 ); 49 * // returns 8 50 * 51 * @example 52 * var y = quantile( 0.5, 10.0, 0.4 ); 53 * // returns 14 54 * 55 * @example 56 * var y = quantile( 0.0, 10.0, 0.9 ); 57 * // returns 0 58 * 59 * @example 60 * var y = quantile( 1.1, 20.0, 0.5 ); 61 * // returns NaN 62 * 63 * @example 64 * var y = quantile( -0.1, 20.0, 0.5 ); 65 * // returns NaN 66 * 67 * @example 68 * var y = quantile( 0.5, 0.0, 0.5 ); 69 * // returns NaN 70 * 71 * @example 72 * var y = quantile( 0.5, -2.0, 0.5 ); 73 * // returns NaN 74 * 75 * @example 76 * var y = quantile( 0.3, 20.0, -1.0 ); 77 * // returns NaN 78 * 79 * @example 80 * var y = quantile( 0.3, 20.0, 1.5 ); 81 * // returns NaN 82 * 83 * @example 84 * var y = quantile( NaN, 20.0, 0.5 ); 85 * // returns NaN 86 * 87 * @example 88 * var y = quantile( 0.3, NaN, 0.5 ); 89 * // returns NaN 90 * 91 * @example 92 * var y = quantile( 0.3, 20.0, NaN ); 93 * // returns NaN 94 */ 95 function quantile( k, r, p ) { 96 var sigmaInv; 97 var guess; 98 var sigma; 99 var corr; 100 var mu; 101 var x2; 102 var x; 103 var q; 104 105 if ( 106 isnan( r ) || 107 isnan( p ) || 108 isnan( k ) || 109 r <= 0.0 || 110 p < 0.0 || 111 p > 1.0 || 112 k < 0.0 || 113 k > 1.0 114 ) { 115 return NaN; 116 } 117 if ( k === 0.0 ) { 118 return 0.0; 119 } 120 if ( k === 1.0 ) { 121 return PINF; 122 } 123 q = 1.0 - p; 124 mu = ( r * q ) / p; 125 sigma = sqrt( r * q ) / p; 126 sigmaInv = 1.0 / sigma; 127 128 // Cornish-Fisher expansion: 129 if ( k < 0.5 ) { 130 x = -erfcinv( 2.0 * k ) * SQRT2; 131 } else { 132 x = erfcinv( 2.0 * (1.0-k) ) * SQRT2; 133 } 134 x2 = x * x; 135 136 // Skewness correction: 137 corr = x + (sigmaInv * ( x2 - 1.0 ) / 6.0); 138 guess = round( mu + (sigma * corr) ); 139 return ( cdf( guess, r, p ) >= k ) ? 140 search.left( guess, k, r, p ) : 141 search.right( guess, k, r, p ); 142 } 143 144 145 // EXPORTS // 146 147 module.exports = quantile;