time-to-botec

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

factory.js (1949B)


      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 sqrt = require( '@stdlib/math/base/special/sqrt' );
     26 var exp = require( '@stdlib/math/base/special/exp' );
     27 var pow = require( '@stdlib/math/base/special/pow' );
     28 var TWO_PI = require( '@stdlib/constants/float64/two-pi' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Returns a function for evaluating the probability density function (PDF) for a Lévy distribution.
     35 *
     36 * @param {number} mu - location parameter
     37 * @param {PositiveNumber} c - scale parameter
     38 * @returns {Function} PDF
     39 *
     40 * @example
     41 * var pdf = factory( 10.0, 2.0 );
     42 * var y = pdf( 11.0 );
     43 * // returns ~0.208
     44 *
     45 * y = pdf( 10.0 );
     46 * // returns 0.0
     47 */
     48 function factory( mu, c ) {
     49 	if ( isnan( mu ) || isnan( c ) || c <= 0.0 ) {
     50 		return constantFunction( NaN );
     51 	}
     52 	return pdf;
     53 
     54 	/**
     55 	* Evaluates the probability density function (PDF) for a Lévy distribution.
     56 	*
     57 	* @private
     58 	* @param {number} x - input value
     59 	* @returns {number} evaluated PDF
     60 	*
     61 	* @example
     62 	* var y = pdf( -1.2 );
     63 	* // returns <number>
     64 	*/
     65 	function pdf( x ) {
     66 		if ( isnan( x ) ) {
     67 			return NaN;
     68 		}
     69 		if ( x <= mu ) {
     70 			return 0.0;
     71 		}
     72 		return sqrt( c/TWO_PI ) * exp( -c / ( 2.0*(x-mu) ) ) / pow( x-mu, 1.5 );
     73 	}
     74 }
     75 
     76 
     77 // EXPORTS //
     78 
     79 module.exports = factory;