time-to-botec

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

logpdf.js (2324B)


      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 isnan = require( '@stdlib/math/base/assert/is-nan' );
     24 var log1p = require( '@stdlib/math/base/special/log1p' );
     25 var abs = require( '@stdlib/math/base/special/abs' );
     26 var exp = require( '@stdlib/math/base/special/exp' );
     27 var ln = require( '@stdlib/math/base/special/ln' );
     28 var NINF = require( '@stdlib/constants/float64/ninf' );
     29 var PINF = require( '@stdlib/constants/float64/pinf' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Evaluates the logarithm of the probability density function (PDF) for a logistic distribution with location parameter `mu` and scale parameter `s` at a value `x`.
     36 *
     37 * @param {number} x - input value
     38 * @param {number} mu - location parameter
     39 * @param {NonNegativeNumber} s - scale parameter
     40 * @returns {number} evaluated logPDF
     41 *
     42 * @example
     43 * var y = logpdf( 2.0, 0.0, 1.0 );
     44 * // returns ~-2.254
     45 *
     46 * @example
     47 * var y = logpdf( -1.0, 4.0, 2.0 );
     48 * // returns ~-3.351
     49 *
     50 * @example
     51 * var y = logpdf( NaN, 0.0, 1.0 );
     52 * // returns NaN
     53 *
     54 * @example
     55 * var y = logpdf( 0.0, NaN, 1.0 );
     56 * // returns NaN
     57 *
     58 * @example
     59 * var y = logpdf( 0.0, 0.0, NaN );
     60 * // returns NaN
     61 *
     62 * @example
     63 * // Negative scale parameter:
     64 * var y = logpdf( 2.0, 0.0, -1.0 );
     65 * // returns NaN
     66 *
     67 * @example
     68 * var y = logpdf( 2.0, 8.0, 0.0 );
     69 * // returns -Infinity
     70 *
     71 * @example
     72 * var y = logpdf( 8.0, 8.0, 0.0 );
     73 * // returns Infinity
     74 */
     75 function logpdf( x, mu, s ) {
     76 	var az;
     77 	var z;
     78 	if (
     79 		isnan( x ) ||
     80 		isnan( mu ) ||
     81 		isnan( s ) ||
     82 		s < 0.0
     83 	) {
     84 		return NaN;
     85 	}
     86 	if ( x === NINF ) {
     87 		return NINF;
     88 	}
     89 	if ( s === 0.0 ) {
     90 		return ( x === mu ) ? PINF : NINF;
     91 	}
     92 	z = ( x - mu ) / s;
     93 	az = -abs( z );
     94 	return az - (2.0 * log1p( exp( az ) )) - ln( s );
     95 }
     96 
     97 
     98 // EXPORTS //
     99 
    100 module.exports = logpdf;