time-to-botec

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

factory.js (2382B)


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