time-to-botec

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

logpdf.js (2413B)


      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 betaln = require( '@stdlib/math/base/special/betaln' );
     24 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     25 var log1p = require( '@stdlib/math/base/special/log1p' );
     26 var ln = require( '@stdlib/math/base/special/ln' );
     27 var NINF = require( '@stdlib/constants/float64/ninf' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Evaluates the natural logarithm of the probability density function (logPDF) for a beta prime distribution with first shape parameter `alpha` and second shape parameter `beta` at a value `x`.
     34 *
     35 * @param {number} x - input value
     36 * @param {PositiveNumber} alpha - first shape parameter
     37 * @param {PositiveNumber} beta - second shape parameter
     38 * @returns {number} evaluated logPDF
     39 *
     40 * @example
     41 * var y = logpdf( 0.5, 1.0, 1.0 );
     42 * // returns ~-0.811
     43 *
     44 * @example
     45 * var y = logpdf( 0.5, 2.0, 4.0 );
     46 * // returns ~-0.13
     47 *
     48 * @example
     49 * var y = logpdf( 0.2, 2.0, 2.0 );
     50 * // returns ~-0.547
     51 *
     52 * @example
     53 * var y = logpdf( 0.8, 4.0, 4.0 );
     54 * // returns ~-0.43
     55 *
     56 * @example
     57 * var y = logpdf( -0.5, 4.0, 2.0 );
     58 * // returns -Infinity
     59 *
     60 * @example
     61 * var y = logpdf( 0.5, -1.0, 0.5 );
     62 * // returns NaN
     63 *
     64 * @example
     65 * var y = logpdf( 0.5, 0.5, -1.0 );
     66 * // returns NaN
     67 *
     68 * @example
     69 * var y = logpdf( NaN, 1.0, 1.0 );
     70 * // returns NaN
     71 *
     72 * @example
     73 * var y = logpdf( 0.5, NaN, 1.0 );
     74 * // returns NaN
     75 *
     76 * @example
     77 * var y = logpdf( 0.5, 1.0, NaN );
     78 * // returns NaN
     79 */
     80 function logpdf( x, alpha, beta ) {
     81 	var out;
     82 
     83 	if (
     84 		isnan( x ) ||
     85 		isnan( alpha ) ||
     86 		isnan( beta ) ||
     87 		alpha <= 0.0 ||
     88 		beta <= 0.0
     89 	) {
     90 		return NaN;
     91 	}
     92 	if ( x <= 0.0 ) {
     93 		// Support of the BetaPrime distribution: (0,∞)
     94 		return NINF;
     95 	}
     96 	out = ( alpha-1.0 ) * ln( x );
     97 	out -= ( alpha+beta ) * log1p( x );
     98 	out -= betaln( alpha, beta );
     99 	return out;
    100 }
    101 
    102 
    103 // EXPORTS //
    104 
    105 module.exports = logpdf;