time-to-botec

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

factory.js (2289B)


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