time-to-botec

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

factory.js (2187B)


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