time-to-botec

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

factory.js (2834B)


      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 constantFunction = require( '@stdlib/utils/constant-function' );
     24 var degenerate = require( './../../../../../base/dists/degenerate/quantile' ).factory;
     25 var erfcinv = require( '@stdlib/math/base/special/erfcinv' );
     26 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     27 var round = require( '@stdlib/math/base/special/round' );
     28 var sqrt = require( '@stdlib/math/base/special/sqrt' );
     29 var cdf = require( './../../../../../base/dists/poisson/cdf' );
     30 var SQRT2 = require( '@stdlib/constants/float64/sqrt-two' );
     31 var PINF = require( '@stdlib/constants/float64/pinf' );
     32 var search = require( './search.js' );
     33 
     34 
     35 // MAIN //
     36 
     37 /**
     38 * Returns a function for evaluating the quantile function for a Poisson distribution with mean parameter `lambda`.
     39 *
     40 * @param {NonNegativeNumber} lambda - mean parameter
     41 * @returns {Function} quantile function
     42 *
     43 * @example
     44 * var quantile = factory( 5.0 );
     45 * var y = quantile( 0.4 );
     46 * // returns 4
     47 *
     48 * y = quantile( 0.8 );
     49 * // returns 7
     50 *
     51 * y = quantile( 1.0 );
     52 * // returns Infinity
     53 */
     54 function factory( lambda ) {
     55 	var sigmaInv;
     56 	var sigma;
     57 
     58 	if ( isnan( lambda ) || lambda < 0.0 ) {
     59 		return constantFunction( NaN );
     60 	}
     61 	if ( lambda === 0.0 ) {
     62 		return degenerate( 0.0 );
     63 	}
     64 	sigma = sqrt( lambda );
     65 	sigmaInv = 1.0 / sigma;
     66 	return quantile;
     67 
     68 	/**
     69 	* Evaluates the quantile function for a Poisson distribution.
     70 	*
     71 	* @private
     72 	* @param {Probability} p - input value
     73 	* @returns {NonNegativeInteger} evaluated quantile function
     74 	*
     75 	* @example
     76 	* var y = quantile( 0.3 );
     77 	* // returns <number>
     78 	*/
     79 	function quantile( p ) {
     80 		var guess;
     81 		var corr;
     82 		var x2;
     83 		var x;
     84 
     85 		if ( isnan( p ) || p < 0.0 || p > 1.0 ) {
     86 			return NaN;
     87 		}
     88 		if ( p === 0.0 ) {
     89 			return 0.0;
     90 		}
     91 		if ( p === 1.0 ) {
     92 			return PINF;
     93 		}
     94 		// Cornish-Fisher expansion:
     95 		if ( p < 0.5 ) {
     96 			x = -erfcinv( 2.0 * p ) * SQRT2;
     97 		} else {
     98 			x = erfcinv( 2.0 * ( 1.0 - p ) ) * SQRT2;
     99 		}
    100 		x2 = x * x;
    101 
    102 		// Skewness correction:
    103 		corr = x + (sigmaInv * ( x2 - 1.0 ) / 6.0);
    104 		guess = round( lambda + (sigma * corr) );
    105 		return ( cdf( guess, lambda ) >= p ) ?
    106 			search.left( guess, p, lambda ) :
    107 			search.right( guess, p, lambda );
    108 	}
    109 }
    110 
    111 
    112 // EXPORTS //
    113 
    114 module.exports = factory;