time-to-botec

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

factory.js (2017B)


      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 atan2 = require( '@stdlib/math/base/special/atan2' );
     26 var ln = require( '@stdlib/math/base/special/ln' );
     27 
     28 
     29 // VARIABLES //
     30 
     31 var ONE_OVER_PI = 0.3183098861837907;
     32 
     33 
     34 // MAIN //
     35 
     36 /**
     37 * Returns a function for evaluating the natural logarithm of the cumulative distribution function (logCDF) for a Cauchy distribution with location parameter `x0` and scale parameter `gamma`.
     38 *
     39 * @param {number} x0 - location parameter
     40 * @param {PositiveNumber} gamma - scale parameter
     41 * @returns {Function} logCDF
     42 *
     43 * @example
     44 * var logcdf = factory( 10.0, 2.0 );
     45 *
     46 * var y = logcdf( 10.0 );
     47 * // returns ~-0.693
     48 *
     49 * y = logcdf( 12.0 );
     50 * // returns ~-0.288
     51 */
     52 function factory( x0, gamma ) {
     53 	if (
     54 		isnan( gamma ) ||
     55 		isnan( x0 ) ||
     56 		gamma <= 0.0
     57 	) {
     58 		return constantFunction( NaN );
     59 	}
     60 	return logcdf;
     61 
     62 	/**
     63 	* Evaluates the  natural logarithm of the cumulative distribution function (logCDF) for a Cauchy distribution.
     64 	*
     65 	* @private
     66 	* @param {number} x - input value
     67 	* @returns {number} evaluated logCDF
     68 	*
     69 	* @example
     70 	* var y = logcdf( 2.0 );
     71 	* // returns <number>
     72 	*/
     73 	function logcdf( x ) {
     74 		if ( isnan( x ) ) {
     75 			return NaN;
     76 		}
     77 		return ln( ( ONE_OVER_PI * atan2( x-x0, gamma ) ) + 0.5 );
     78 	}
     79 }
     80 
     81 
     82 // EXPORTS //
     83 
     84 module.exports = factory;