time-to-botec

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

factory.js (2554B)


      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 exp = require( '@stdlib/math/base/special/exp' );
     28 var ln = require( '@stdlib/math/base/special/ln' );
     29 var PINF = require( '@stdlib/constants/float64/pinf' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Returns a function for evaluating the probability density function (PDF) 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} PDF
     40 *
     41 * @example
     42 * var pdf = factory( 0.5, 0.5 );
     43 *
     44 * var y = pdf( 0.8 );
     45 * // returns ~0.796
     46 *
     47 * y = pdf( 0.3 );
     48 * // returns ~0.695
     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 pdf;
     62 
     63 	/**
     64 	* Evaluates the probability density function (PDF) for a beta distribution.
     65 	*
     66 	* @private
     67 	* @param {number} x - input value
     68 	* @returns {number} evaluated PDF
     69 	*
     70 	* @example
     71 	* var y = pdf( 0.3 );
     72 	* // returns <number>
     73 	*/
     74 	function pdf( 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 0.0;
     82 		}
     83 		if ( x === 0.0 ) {
     84 			if ( alpha < 1.0 ) {
     85 				return PINF;
     86 			}
     87 			if ( alpha > 1.0 ) {
     88 				return 0.0;
     89 			}
     90 			return beta;
     91 		}
     92 		if ( x === 1.0 ) {
     93 			if ( beta < 1.0 ) {
     94 				return PINF;
     95 			}
     96 			if ( beta > 1.0 ) {
     97 				return 0.0;
     98 			}
     99 			return alpha;
    100 		}
    101 		out = -betalnAB;
    102 		out += ( alpha-1.0 ) * ln( x );
    103 		out += ( beta-1.0 ) * log1p( -x );
    104 		return exp( out );
    105 	}
    106 }
    107 
    108 
    109 // EXPORTS //
    110 
    111 module.exports = factory;