time-to-botec

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

factory.js (2212B)


      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 betaFactory = require( './../../../../../base/dists/beta/logcdf' ).factory;
     25 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     26 var NINF = require( '@stdlib/constants/float64/ninf' );
     27 var PINF = require( '@stdlib/constants/float64/pinf' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Returns a function for evaluating the natural logarithm of the cumulative distribution function (CDF) for a beta prime distribution with first shape parameter `alpha` and second shape parameter `beta`.
     34 *
     35 * @param {PositiveNumber} alpha - first shape parameter
     36 * @param {PositiveNumber} beta - second shape parameter
     37 * @returns {Function} logCDF
     38 *
     39 * @example
     40 * var logcdf = factory( 0.5, 0.5 );
     41 *
     42 * var y = logcdf( 0.8 );
     43 * // returns ~-0.767
     44 *
     45 * y = logcdf( 0.3 );
     46 * // returns ~-1.143
     47 */
     48 function factory( alpha, beta ) {
     49 	var betaLogCDF;
     50 	if (
     51 		isnan( alpha ) ||
     52 		isnan( beta ) ||
     53 		alpha <= 0.0 ||
     54 		beta <= 0.0
     55 	) {
     56 		return constantFunction( NaN );
     57 	}
     58 	betaLogCDF = betaFactory( alpha, beta );
     59 	return logcdf;
     60 
     61 	/**
     62 	* Evaluates the natural logarithm of the cumulative distribution function (CDF) for a beta prime distribution.
     63 	*
     64 	* @private
     65 	* @param {number} x - input value
     66 	* @returns {number} evaluated logCDF
     67 	*
     68 	* @example
     69 	* var y = logcdf( 2.0 );
     70 	* // returns <number>
     71 	*/
     72 	function logcdf( x ) {
     73 		if ( isnan( x ) ) {
     74 			return NaN;
     75 		}
     76 		if ( x <= 0.0 ) {
     77 			return NINF;
     78 		}
     79 		if ( x === PINF ) {
     80 			return 0.0;
     81 		}
     82 		return betaLogCDF( x / ( 1.0 + x ) );
     83 	}
     84 }
     85 
     86 
     87 // EXPORTS //
     88 
     89 module.exports = factory;