time-to-botec

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

factory.js (2321B)


      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 degenerate = require( './../../../../../base/dists/degenerate/logpdf' ).factory;
     25 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     26 var gammaln = require( '@stdlib/math/base/special/gammaln' );
     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 var LN2 = require( '@stdlib/constants/float64/ln-two' );
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Returns a function for evaluating the natural logarithm of the probability density function (PDF) for a chi distribution with degrees of freedom `k`.
     37 *
     38 * @param {NonNegativeNumber} k - degrees of freedom
     39 * @returns {Function} logPDF
     40 *
     41 * @example
     42 * var logpdf = factory( 0.5 );
     43 *
     44 * var y = logpdf( 2.0 );
     45 * // returns ~-3.115
     46 *
     47 * y = logpdf( 1.0 );
     48 * // returns ~-1.268
     49 */
     50 function factory( k ) {
     51 	var km1;
     52 	var kh;
     53 
     54 	if ( isnan( k ) || k < 0.0 ) {
     55 		return constantFunction( NaN );
     56 	}
     57 	if ( k === 0.0 ) {
     58 		return degenerate( 0.0 );
     59 	}
     60 
     61 	kh = k / 2.0;
     62 	km1 = k - 1.0;
     63 	return logpdf;
     64 
     65 	/**
     66 	* Evaluates the natural logarithm of the probability density function (PDF) for a chi distribution with degrees of freedom `k`.
     67 	*
     68 	* @private
     69 	* @param {number} x - input value
     70 	* @returns {number} evaluated logPDF
     71 	*
     72 	* @example
     73 	* var y = logpdf( 1.0 );
     74 	* // returns <number>
     75 	*/
     76 	function logpdf( x ) {
     77 		var out;
     78 		if ( isnan( x ) ) {
     79 			return NaN;
     80 		}
     81 		if ( x < 0.0 || x === PINF ) {
     82 			return NINF;
     83 		}
     84 		out = ( ( 1.0-kh ) * LN2 ) + ( km1 * ln( x ) ) - ( (x*x) / 2.0 );
     85 		out -= gammaln( kh );
     86 		return out;
     87 	}
     88 }
     89 
     90 
     91 // EXPORTS //
     92 
     93 module.exports = factory;