factory.js (1819B)
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 isnan = require( '@stdlib/math/base/assert/is-nan' ); 25 var ln = require( '@stdlib/math/base/special/ln' ); 26 var PINF = require( '@stdlib/constants/float64/pinf' ); 27 28 29 // MAIN // 30 31 /** 32 * Returns a function for evaluating the quantile function of an exponential distribution with rate parameter `lambda`. 33 * 34 * @param {PositiveNumber} lambda - rate parameter 35 * @returns {Function} quantile function 36 * 37 * @example 38 * var quantile = factory( 0.4 ); 39 * var y = quantile( 0.4 ); 40 * // returns ~1.277 41 * 42 * y = quantile( 1.0 ); 43 * // returns Infinity 44 */ 45 function factory( lambda ) { 46 if ( lambda < 0.0 || lambda === PINF || isnan( lambda ) ) { 47 return constantFunction( NaN ); 48 } 49 return quantile; 50 51 /** 52 * Evaluates the quantile function for an exponential distribution. 53 * 54 * @private 55 * @param {Probability} p - input value 56 * @returns {number} evaluated quantile function 57 * 58 * @example 59 * var y = quantile( 0.3 ); 60 * // returns <number> 61 */ 62 function quantile( p ) { 63 if ( isnan( p ) || p < 0.0 || p > 1.0 ) { 64 return NaN; 65 } 66 return -ln( 1.0 - p ) / lambda; 67 } 68 } 69 70 71 // EXPORTS // 72 73 module.exports = factory;