time-to-botec

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

factory.js (2657B)


      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 betaln = require( '@stdlib/math/base/special/betaln' );
     25 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     26 var log1p = require( '@stdlib/math/base/special/log1p' );
     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 * Returns a function for evaluating the natural logarithm of the probability density function (logPDF) for a beta distribution with first shape parameter `alpha` and second shape parameter `beta`.
     36 *
     37 * @param {PositiveNumber} alpha - first shape parameter
     38 * @param {PositiveNumber} beta - second shape parameter
     39 * @returns {Function} logPDF
     40 *
     41 * @example
     42 * var logpdf = factory( 0.5, 0.5 );
     43 *
     44 * var y = logpdf( 0.8 );
     45 * // returns ~-0.228
     46 *
     47 * y = logpdf( 0.3 );
     48 * // returns ~-0.364
     49 */
     50 function factory( alpha, beta ) {
     51 	var betalnAB;
     52 	if (
     53 		isnan( alpha ) ||
     54 		isnan( beta ) ||
     55 		alpha <= 0.0 ||
     56 		beta <= 0.0
     57 	) {
     58 		return constantFunction( NaN );
     59 	}
     60 	betalnAB = betaln( alpha, beta );
     61 	return logpdf;
     62 
     63 	/**
     64 	* Evaluates the natural logarithm of the probability density function (PDF) for a beta distribution.
     65 	*
     66 	* @private
     67 	* @param {number} x - input value
     68 	* @returns {number} evaluated natural logarithm of the PDF
     69 	*
     70 	* @example
     71 	* var y = logpdf( 0.3 );
     72 	* // returns <number>
     73 	*/
     74 	function logpdf( x ) {
     75 		var out;
     76 		if ( isnan( x ) ) {
     77 			return NaN;
     78 		}
     79 		if ( x < 0.0 || x > 1.0 ) {
     80 			// Support of the Beta distribution: [0,1]
     81 			return NINF;
     82 		}
     83 		if ( x === 0.0 ) {
     84 			if ( alpha < 1.0 ) {
     85 				return PINF;
     86 			}
     87 			if ( alpha > 1.0 ) {
     88 				return NINF;
     89 			}
     90 			return ln( beta );
     91 		}
     92 		if ( x === 1.0 ) {
     93 			if ( beta < 1.0 ) {
     94 				return PINF;
     95 			}
     96 			if ( beta > 1.0 ) {
     97 				return NINF;
     98 			}
     99 			return ln( alpha );
    100 		}
    101 		out = -betalnAB;
    102 		out += ( ( alpha-1.0 )*ln(x) ) + ( ( beta-1.0 )*log1p(-x) );
    103 		return out;
    104 	}
    105 }
    106 
    107 
    108 // EXPORTS //
    109 
    110 module.exports = factory;