time-to-botec

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

factory.js (2013B)


      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 asin = require( '@stdlib/math/base/special/asin' );
     26 var sqrt = require( '@stdlib/math/base/special/sqrt' );
     27 var PI = require( '@stdlib/constants/float64/pi' );
     28 
     29 
     30 // VARIABLES //
     31 
     32 var TWO_OVER_PI = 2.0 / PI;
     33 
     34 
     35 // MAIN //
     36 
     37 /**
     38 * Returns a function for evaluating the cumulative distribution function (CDF) for an arcsine distribution with minimum support `a` and maximum support `b`.
     39 *
     40 * @param {number} a - minimum support
     41 * @param {number} b - maximum support
     42 * @returns {Function} CDF
     43 *
     44 * @example
     45 * var cdf = factory( 0.0, 10.0 );
     46 * var y = cdf( 0.5 );
     47 * // returns ~0.144
     48 *
     49 * y = cdf( 8.0 );
     50 * // returns ~0.705
     51 */
     52 function factory( a, b ) {
     53 	if (
     54 		isnan( a ) ||
     55 		isnan( b ) ||
     56 		a >= b
     57 	) {
     58 		return constantFunction( NaN );
     59 	}
     60 	return cdf;
     61 
     62 	/**
     63 	* Evaluates the cumulative distribution function (CDF) for an arcsine distribution.
     64 	*
     65 	* @private
     66 	* @param {number} x - input value
     67 	* @returns {Probability} evaluated CDF
     68 	*
     69 	* @example
     70 	* var y = cdf( 2.0 );
     71 	* // returns <number>
     72 	*/
     73 	function cdf( x ) {
     74 		if ( isnan( x ) ) {
     75 			return NaN;
     76 		}
     77 		if ( x < a ) {
     78 			return 0.0;
     79 		}
     80 		if ( x >= b ) {
     81 			return 1.0;
     82 		}
     83 		return TWO_OVER_PI * asin( sqrt( ( x-a ) / ( b-a ) ) );
     84 	}
     85 }
     86 
     87 
     88 // EXPORTS //
     89 
     90 module.exports = factory;