time-to-botec

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

factory.js (2356B)


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