time-to-botec

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

factory.js (2156B)


      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 pow = require( '@stdlib/math/base/special/pow' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns a function for evaluating the cumulative distribution function (CDF) for a triangular distribution with lower limit `a` and 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} CDF
     37 *
     38 * @example
     39 * var cdf = factory( 0.0, 10.0, 2.0 );
     40 * var y = cdf( 0.5 );
     41 * // returns 0.0125
     42 *
     43 * y = cdf( 8.0 );
     44 * // returns 0.95
     45 */
     46 function factory( a, b, c ) {
     47 	var denom1;
     48 	var denom2;
     49 
     50 	if (
     51 		isnan( a ) ||
     52 		isnan( b ) ||
     53 		isnan( c )
     54 	) {
     55 		return constantFunction( NaN );
     56 	}
     57 	if ( !( a <= c && c <= b ) ) {
     58 		return constantFunction( NaN );
     59 	}
     60 
     61 	denom1 = ( b - a ) * ( c - a );
     62 	denom2 = ( b - a ) * ( b - c );
     63 	return cdf;
     64 
     65 	/**
     66 	* Evaluates the cumulative distribution function (CDF) for a triangular distribution.
     67 	*
     68 	* @private
     69 	* @param {number} x - input value
     70 	* @returns {Probability} evaluated CDF
     71 	*
     72 	* @example
     73 	* var y = cdf( 2.0 );
     74 	* // returns <number>
     75 	*/
     76 	function cdf( x ) {
     77 		if ( isnan( x ) ) {
     78 			return NaN;
     79 		}
     80 		if ( x <= a ) {
     81 			return 0.0;
     82 		}
     83 		// Case: x > a
     84 		if ( x <= c ) {
     85 			return pow( x - a, 2.0 ) / denom1;
     86 		}
     87 		// Case: x > c
     88 		if ( x < b ) {
     89 			return 1 - ( pow( b - x, 2.0 ) / denom2 );
     90 		}
     91 		// Case: x >= b
     92 		return 1.0;
     93 	}
     94 }
     95 
     96 
     97 // EXPORTS //
     98 
     99 module.exports = factory;