time-to-botec

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

cdf.js (2218B)


      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 isnan = require( '@stdlib/math/base/assert/is-nan' );
     24 var pow = require( '@stdlib/math/base/special/pow' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Evaluates the cumulative distribution function (CDF) for a triangular distribution with lower limit `a` and upper limit `b` and mode `c` at a value `x`.
     31 *
     32 * @param {number} x - input value
     33 * @param {number} a - lower limit
     34 * @param {number} b - upper limit
     35 * @param {number} c - mode
     36 * @returns {Probability} evaluated CDF
     37 *
     38 * @example
     39 * var y = cdf( 0.5, -1.0, 1.0, 0.0 );
     40 * // returns 0.875
     41 *
     42 * @example
     43 * var y = cdf( 0.5, -1.0, 1.0, 0.5 );
     44 * // returns 0.75
     45 *
     46 * @example
     47 * var y = cdf( -10.0, -20.0, 0.0, -2.0 );
     48 * // returns ~0.278
     49 *
     50 * @example
     51 * var y = cdf( -2.0, -1.0, 1.0, 0.0 );
     52 * // returns 0.0
     53 *
     54 * @example
     55 * var y = cdf( NaN, 0.0, 1.0, 0.5 );
     56 * // returns NaN
     57 *
     58 * @example
     59 * var y = cdf( 0.0, NaN, 1.0, 0.5 );
     60 * // returns NaN
     61 *
     62 * @example
     63 * var y = cdf( 0.0, 0.0, NaN, 0.5 );
     64 * // returns NaN
     65 *
     66 * @example
     67 * var y = cdf( 2.0, 1.0, 0.0, NaN );
     68 * // returns NaN
     69 *
     70 * @example
     71 * var y = cdf( 2.0, 1.0, 0.0, 1.5 );
     72 * // returns NaN
     73 */
     74 function cdf( x, a, b, c ) {
     75 	var denom1;
     76 	var denom2;
     77 
     78 	if (
     79 		isnan( x ) ||
     80 		isnan( a ) ||
     81 		isnan( b ) ||
     82 		isnan( c ) ||
     83 		a > c ||
     84 		c > b
     85 	) {
     86 		return NaN;
     87 	}
     88 	if ( x <= a ) {
     89 		return 0.0;
     90 	}
     91 	denom1 = ( b - a ) * ( c - a );
     92 	denom2 = ( b - a ) * ( b - c );
     93 
     94 	// Case: x > a
     95 	if ( x <= c ) {
     96 		return pow( x - a, 2.0 ) / denom1;
     97 	}
     98 	// Case: x > c
     99 	if ( x < b ) {
    100 		return 1.0 - ( pow( b - x, 2.0 ) / denom2 );
    101 	}
    102 	// Case: x >= b
    103 	return 1.0;
    104 }
    105 
    106 
    107 // EXPORTS //
    108 
    109 module.exports = cdf;