time-to-botec

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

logcdf.js (2191B)


      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 expm1 = require( '@stdlib/math/base/special/expm1' );
     24 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     25 var log1p = require( '@stdlib/math/base/special/log1p' );
     26 var exp = require( '@stdlib/math/base/special/exp' );
     27 var pow = require( '@stdlib/math/base/special/pow' );
     28 var ln = require( '@stdlib/math/base/special/ln' );
     29 var LNHALF = require( '@stdlib/constants/float64/ln-half' );
     30 var NINF = require( '@stdlib/constants/float64/ninf' );
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Evaluates the logarithm of the cumulative distribution function (CDF) for a Rayleigh distribution with scale parameter `sigma` at a value `x`.
     37 *
     38 * @param {number} x - input value
     39 * @param {NonNegativeNumber} sigma - scale parameter
     40 * @returns {number} evaluated logCDF
     41 *
     42 * @example
     43 * var y = logcdf( 2.0, 3.0 );
     44 * // returns ~-1.613
     45 *
     46 * @example
     47 * var y = logcdf( 1.0, 2.0 );
     48 * // returns ~-2.141
     49 *
     50 * @example
     51 * var y = logcdf( -1.0, 4.0 );
     52 * // returns -Infinity
     53 *
     54 * @example
     55 * var y = logcdf( NaN, 1.0 );
     56 * // returns NaN
     57 *
     58 * @example
     59 * var y = logcdf( 0.0, NaN );
     60 * // returns NaN
     61 *
     62 * @example
     63 * // Negative scale parameter:
     64 * var y = logcdf( 2.0, -1.0 );
     65 * // returns NaN
     66 */
     67 function logcdf( x, sigma ) {
     68 	var s2;
     69 	var p;
     70 	if (
     71 		isnan( x ) ||
     72 		isnan( sigma ) ||
     73 		sigma < 0.0
     74 	) {
     75 		return NaN;
     76 	}
     77 	if ( sigma === 0.0 ) {
     78 		return ( x < 0.0 ) ? NINF : 0.0;
     79 	}
     80 	if ( x < 0.0 ) {
     81 		return NINF;
     82 	}
     83 	s2 = pow( sigma, 2.0 );
     84 	p = -pow( x, 2.0 ) / ( 2.0 * s2 );
     85 	return ( p < LNHALF ) ? log1p( -exp( p ) ) : ln( -expm1( p ) );
     86 }
     87 
     88 
     89 // EXPORTS //
     90 
     91 module.exports = logcdf;