time-to-botec

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

mgf.js (1876B)


      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 isnan = require( '@stdlib/math/base/assert/is-nan' );
     24 var exp = require( '@stdlib/math/base/special/exp' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Evaluates the moment-generating function (MGF) of a uniform distribution with minimum support `a` and maximum support `b` at a value `t`.
     31 *
     32 * @param {number} t - input value
     33 * @param {number} a - minimum support
     34 * @param {number} b - maximum support
     35 * @returns {number} evaluated MGF
     36 *
     37 * @example
     38 * var y = mgf( 2.0, 0.0, 4.0 );
     39 * // returns ~372.495
     40 *
     41 * @example
     42 * var y = mgf( -0.2, 0.0, 4.0 );
     43 * // returns ~0.688
     44 *
     45 * @example
     46 * var y = mgf( 2.0, 0.0, 1.0 );
     47 * // returns ~3.195
     48 *
     49 * @example
     50 * var y = mgf( 0.5, 3.0, 2.0 );
     51 * // returns NaN
     52 *
     53 * @example
     54 * var y = mgf( 0.5, 3.0, 3.0 );
     55 * // returns NaN
     56 *
     57 * @example
     58 * var y = mgf( NaN, 0.0, 1.0 );
     59 * // returns NaN
     60 *
     61 * @example
     62 * var y = mgf( 0.0, NaN, 1.0 );
     63 * // returns NaN
     64 *
     65 * @example
     66 * var y = mgf( 0.0, 0.0, NaN );
     67 * // returns NaN
     68 */
     69 function mgf( t, a, b ) {
     70 	var ret;
     71 	if (
     72 		isnan( t ) ||
     73 		isnan( a ) ||
     74 		isnan( b ) ||
     75 		a >= b
     76 	) {
     77 		return NaN;
     78 	}
     79 	if ( t === 0.0 ) {
     80 		return 1.0;
     81 	}
     82 	// Case: t not equal to zero
     83 	ret = exp( t * b ) - exp( t * a );
     84 	ret /= t * ( b - a );
     85 	return ret;
     86 }
     87 
     88 
     89 // EXPORTS //
     90 
     91 module.exports = mgf;