time-to-botec

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

_mgf.js (1795B)


      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 betaFcn = require( '@stdlib/math/base/special/beta' );
     24 var abs = require( '@stdlib/math/base/special/abs' );
     25 var EPS = require( '@stdlib/constants/float64/eps' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Evaluates the moment-generating function (MGF) for a beta distribution with first shape parameter `alpha` and second shape parameter `beta` at a value `t`.
     32 *
     33 * @private
     34 * @param {number} t - input value
     35 * @param {PositiveNumber} alpha - first shape parameter
     36 * @param {PositiveNumber} beta - second shape parameter
     37 * @returns {number} evaluated MGF
     38 *
     39 * @example
     40 * var y = mgf( 0.5, 1.0, 1.0 );
     41 * // returns ~1.297
     42 *
     43 * @example
     44 * var y = mgf( 0.5, 2.0, 4.0 );
     45 * // returns ~1.186
     46 *
     47 * @example
     48 * var y = mgf( 3.0, 2.0, 2.0 );
     49 * // returns ~5.575
     50 *
     51 * @example
     52 * var y = mgf( -0.8, 4.0, 4.0 );
     53 * // returns ~0.676
     54 */
     55 function mgf( t, alpha, beta ) {
     56 	var summand;
     57 	var denom;
     58 	var sum;
     59 	var c;
     60 	var k;
     61 
     62 	denom = betaFcn( alpha, beta );
     63 	sum = 1.0;
     64 	c = 1.0;
     65 	k = 1;
     66 	do {
     67 		c *= t / k;
     68 		summand = ( betaFcn( alpha+k, beta ) / denom ) * c;
     69 		sum += summand;
     70 		k += 1;
     71 	} while ( abs( summand / sum ) >= EPS );
     72 	return sum;
     73 }
     74 
     75 
     76 // EXPORTS //
     77 
     78 module.exports = mgf;