time-to-botec

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

factory.js (2373B)


      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 NINF = require( '@stdlib/constants/float64/ninf' );
     27 var LN2 = require( '@stdlib/constants/float64/ln-two' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Returns a function for evaluating the natural logarithm of the probability density function (PDF) 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} logPDF
     39 *
     40 * @example
     41 * var logpdf = factory( 0.0, 10.0, 5.0 );
     42 * var y = logpdf( 2.0 );
     43 * // returns ~-2.526
     44 *
     45 * y = logpdf( 12.0 );
     46 * // returns -Infinity
     47 */
     48 function factory( a, b, c ) {
     49 	var denom1;
     50 	var denom2;
     51 	var denom3;
     52 
     53 	if (
     54 		isnan( a ) ||
     55 		isnan( b ) ||
     56 		isnan( c ) ||
     57 		a > c ||
     58 		c > b
     59 	) {
     60 		return constantFunction( NaN );
     61 	}
     62 
     63 	denom1 = ln( b - a ) + ln( c - a );
     64 	denom2 = ln( b - a );
     65 	denom3 = ln( b - a ) + ln( b - c );
     66 	return logpdf;
     67 
     68 	/**
     69 	* Evaluates the natural logarithm of the probability density function (PDF) for a triangular distribution.
     70 	*
     71 	* @private
     72 	* @param {number} x - input value
     73 	* @returns {number} evaluated logPDF
     74 	*
     75 	* @example
     76 	* var y = logpdf( 12.0 );
     77 	* // returns <number>
     78 	*/
     79 	function logpdf( x ) {
     80 		if ( isnan( x ) ) {
     81 			return NaN;
     82 		}
     83 		if ( x < a ) {
     84 			return NINF;
     85 		}
     86 		// Case: x >= a
     87 		if ( x < c ) {
     88 			return LN2 + ln( x - a ) - denom1;
     89 		}
     90 		if ( x === c ) {
     91 			return LN2 - denom2;
     92 		}
     93 		// Case: x > c
     94 		if ( x <= b ) {
     95 			return LN2 + ln( b - x ) - denom3;
     96 		}
     97 		// Case: x > b
     98 		return NINF;
     99 	}
    100 }
    101 
    102 
    103 // EXPORTS //
    104 
    105 module.exports = factory;