time-to-botec

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

factory.js (2263B)


      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 log1p = require( '@stdlib/math/base/special/log1p' );
     27 var abs = require( '@stdlib/math/base/special/abs' );
     28 var exp = require( '@stdlib/math/base/special/exp' );
     29 var ln = require( '@stdlib/math/base/special/ln' );
     30 var NINF = require( '@stdlib/constants/float64/ninf' );
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Returns a function for evaluating the logarithm of the probability density function (PDF) for a logistic distribution.
     37 *
     38 * @param {number} mu - location parameter
     39 * @param {NonNegativeNumber} s - scale parameter
     40 * @returns {Function} logPDF
     41 *
     42 * @example
     43 * var logpdf = factory( 10.0, 2.0 );
     44 * var y = logpdf( 10.0 );
     45 * // returns ~-2.079
     46 *
     47 * y = logpdf( 5.0 );
     48 * // returns ~-3.351
     49 */
     50 function factory( mu, s ) {
     51 	var ls;
     52 	if ( isnan( mu ) || isnan( s ) || s < 0.0 ) {
     53 		return constantFunction( NaN );
     54 	}
     55 	if ( s === 0.0 ) {
     56 		return degenerate( mu );
     57 	}
     58 	ls = ln( s );
     59 	return logpdf;
     60 
     61 	/**
     62 	* Evaluates the logarithm of the probability density function (PDF) for a logistic distribution.
     63 	*
     64 	* @private
     65 	* @param {number} x - input value
     66 	* @returns {number} evaluated logPDF
     67 	*
     68 	* @example
     69 	* var y = logpdf( -1.2 );
     70 	* // returns <number>
     71 	*/
     72 	function logpdf( x ) {
     73 		var az;
     74 		var z;
     75 		if ( isnan( x ) ) {
     76 			return NaN;
     77 		}
     78 		if ( x === NINF ) {
     79 			return NINF;
     80 		}
     81 		z = ( x - mu ) / s;
     82 		az = -abs( z );
     83 		return az - (2.0 * log1p( exp( az ) )) - ls;
     84 	}
     85 }
     86 
     87 
     88 // EXPORTS //
     89 
     90 module.exports = factory;