factory.js (2340B)
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 30 31 // MAIN // 32 33 /** 34 * Returns a function for evaluating the natural logarithm of the probability density function (logPDF) for a beta prime distribution with first shape parameter `alpha` and second shape parameter `beta`. 35 * 36 * @param {PositiveNumber} alpha - first shape parameter 37 * @param {PositiveNumber} beta - second shape parameter 38 * @returns {Function} logPDF 39 * 40 * @example 41 * var logpdf = factory( 0.5, 0.5 ); 42 * 43 * var y = logpdf( 0.8 ); 44 * // returns ~-1.62 45 * 46 * y = logpdf( 0.3 ); 47 * // returns ~-0.805 48 */ 49 function factory( alpha, beta ) { 50 var betalnAB; 51 if ( 52 isnan( alpha ) || 53 isnan( beta ) || 54 alpha <= 0.0 || 55 beta <= 0.0 56 ) { 57 return constantFunction( NaN ); 58 } 59 betalnAB = betaln( alpha, beta ); 60 return logpdf; 61 62 /** 63 * Evaluates the natural logarithm of the probability density function (PDF) for a beta prime distribution. 64 * 65 * @private 66 * @param {number} x - input value 67 * @returns {number} evaluated natural logarithm of the PDF 68 * 69 * @example 70 * var y = logpdf( 0.3 ); 71 * // returns <number> 72 */ 73 function logpdf( x ) { 74 var out; 75 if ( isnan( x ) ) { 76 return NaN; 77 } 78 if ( x <= 0.0 ) { 79 // Support of the BetaPrime distribution: (0,∞) 80 return NINF; 81 } 82 out = ( alpha-1.0 ) * ln( x ); 83 out -= ( alpha+beta ) * log1p( x ); 84 out -= betalnAB; 85 return out; 86 } 87 } 88 89 90 // EXPORTS // 91 92 module.exports = factory;