time-to-botec

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

logcdf.js (2209B)


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