time-to-botec

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

factory.js (2145B)


      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 sqrt = require( '@stdlib/math/base/special/sqrt' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns a function for evaluating the quantile function for a triangular distribution with lower limit `a`, upper limit `b` and mode `c`.
     32 *
     33 * @param {number} a - lower limit
     34 * @param {number} b - upper limit
     35 * @param {number} c - mode
     36 * @returns {Function} quantile function
     37 *
     38 * @example
     39 * var quantile = factory( 2.0, 4.0, 2.5 );
     40 * var y = quantile( 0.4 );
     41 * // returns ~2.658
     42 *
     43 * y = quantile( 0.8 );
     44 * // returns ~3.225
     45 */
     46 function factory( a, b, c ) {
     47 	var pInflection;
     48 	var fact1;
     49 	var fact2;
     50 
     51 	if (
     52 		isnan( a ) ||
     53 		isnan( b ) ||
     54 		isnan( c ) ||
     55 		a > c ||
     56 		c > b
     57 	) {
     58 		return constantFunction( NaN );
     59 	}
     60 
     61 	pInflection = ( c - a ) / ( b - a );
     62 	fact1 = ( b - a ) * ( c - a);
     63 	fact2 = ( b - a ) * ( b - c );
     64 	return quantile;
     65 
     66 	/**
     67 	* Evaluates the quantile function for a triangular distribution.
     68 	*
     69 	* @private
     70 	* @param {Probability} p - input value
     71 	* @returns {number} evaluated quantile function
     72 	*
     73 	* @example
     74 	* var y = quantile( 0.3 );
     75 	* // returns <number>
     76 	*/
     77 	function quantile( p ) {
     78 		if ( isnan( p ) || p < 0.0 || p > 1.0 ) {
     79 			return NaN;
     80 		}
     81 		if ( p < pInflection ) {
     82 			return a + sqrt( fact1 * p );
     83 		}
     84 		if ( p > pInflection ) {
     85 			return b - sqrt( fact2 * ( 1.0 - p ) );
     86 		}
     87 		// Case: p = pInflection
     88 		return c;
     89 	}
     90 }
     91 
     92 
     93 // EXPORTS //
     94 
     95 module.exports = factory;