time-to-botec

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

factory.js (2198B)


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