'use strict';

// MODULES //

var betaln = require( '@stdlib/math/base/special/betaln');
var isnan = require( '@stdlib/math/base/utils/is-nan' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var ln = require( '@stdlib/math/base/special/ln' );
var NINF = require( '@stdlib/math/constants/float64-ninf' );
var PINF = require( '@stdlib/math/constants/float64-pinf' );
var nan = require( './nan.js' );


// MAIN //

/**
* Returns a function for evaluating the natural logarithm of the probability density function (PDF) for a beta distribution with first shape parameter `alpha` and second shape parameter `beta`
*
* @param {PositiveNumber} alpha - first shape parameter
* @param {PositiveNumber} beta - second shape parameter
* @returns {Function} logPDF
*
* @example
* var mylogpdf = factory( 0.5, 0.5 );
*
* var y = mylogpdf( 0.8 );
* // returns ~-0.228
*
* y = mylogpdf( 0.3 );
* // returns ~-0.364
*/
function factory( alpha, beta ) {
	var betalnAB;
	if ( isnan( alpha ) || isnan( beta ) ) {
		return nan;
	}
	if ( alpha <= 0.0 || beta <= 0.0 ) {
		return nan;
	}
	betalnAB = betaln( alpha, beta );
	return pdf;

	/**
	* Evaluates the logarithm of the probability density function (PDF) for a beta distribution.
	*
	* @private
	* @param {number} x - input value
	* @returns {number} evaluated logPDF
	*
	* @example
	* var y = logpdf( 0.3 );
	* // returns <number>
	*/
	function pdf( x ) {
		var lnl;
		if ( isnan( x ) ) {
			return NaN;
		}
		if ( x < 0.0 || x > 1.0 ) {
			// Support of the Beta distribution: [0,1]
			return NINF;
		}
		else if ( x === 0.0 ) {
			if ( alpha < 1.0 ) {
				return PINF;
			}
			else if ( alpha > 1.0 ) {
				return NINF;
			}
			return ln( beta );
		}
		else if ( x === 1.0 ) {
			if ( beta < 1.0 ) {
				return PINF;
			}
			else if ( beta > 1.0 ) {
				return NINF;
			}
			return ln( alpha );
		}
		lnl = -betalnAB;
		lnl += (( alpha - 1.0 ) * ln( x )) + (( beta - 1.0 ) * log1p( -x ));
		return lnl;
	} // end FUNCTION pdf()
} // end FUNCTION factory()


// EXPORTS //

module.exports = factory;

'use strict';

// MODULES //

var betaln = require( '@stdlib/math/base/special/betaln');
var isnan = require( '@stdlib/math/base/utils/is-nan' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var ln = require( '@stdlib/math/base/special/ln' );
var NINF = require( '@stdlib/math/constants/float64-ninf' );
var PINF = require( '@stdlib/math/constants/float64-pinf' );
var nan = require( './nan.js' );


// MAIN //

/**
* Returns a function for evaluating the natural logarithm of the probability density function (PDF) for a beta distribution with first shape parameter `alpha` and second shape parameter `beta`
*
* @param {PositiveNumber} alpha - first shape parameter
* @param {PositiveNumber} beta - second shape parameter
* @returns {Function} logPDF
*
* @example
* var mylogpdf = factory( 0.5, 0.5 );
*
* var y = mylogpdf( 0.8 );
* // returns ~-0.228
*
* y = mylogpdf( 0.3 );
* // returns ~-0.364
*/
function factory( alpha, beta ) {
	var betalnAB;
	if (
		isnan( alpha ) ||
		isnan( beta ) ||
		alpha <= 0.0 ||
		beta <= 0.0
	) {
		return nan;
	}
	betalnAB = betaln( alpha, beta );
	return pdf;

	/**
	* Evaluates the natural logarithm of the probability density function (PDF) for a beta distribution.
	*
	* @private
	* @param {number} x - input value
	* @returns {number} evaluated natural logarithm of the PDF
	*
	* @example
	* var y = logpdf( 0.3 );
	* // returns <number>
	*/
	function pdf( x ) {
		var out;
		if ( isnan( x ) ) {
			return NaN;
		}
		if ( x < 0.0 || x > 1.0 ) {
			// Support of the Beta distribution: [0,1]
			return NINF;
		}
		else if ( x === 0.0 ) {
			if ( alpha < 1.0 ) {
				return PINF;
			}
			else if ( alpha > 1.0 ) {
				return NINF;
			}
			return ln( beta );
		}
		else if ( x === 1.0 ) {
			if ( beta < 1.0 ) {
				return PINF;
			}
			else if ( beta > 1.0 ) {
				return NINF;
			}
			return ln( alpha );
		}
		out = -betalnAB;
		out += ( ( alpha-1.0 )*ln(x) ) + ( ( beta-1.0 )*log1p(-x) );
		return out;
	} // end FUNCTION pdf()
} // end FUNCTION factory()


// EXPORTS //

module.exports = factory;

Consolidate conditionals and update descriptions