mgf.js (2219B)
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 var pow = require( '@stdlib/math/base/special/pow' ); 26 27 28 // MAIN // 29 30 /** 31 * Evaluates the moment-generating function (MGF) for a triangular distribution with lower limit `a`, upper limit `b`, and mode `c` at a value `t`. 32 * 33 * @param {number} t - input value 34 * @param {number} a - lower limit 35 * @param {number} b - upper limit 36 * @param {number} c - mode 37 * @returns {number} evaluated MGF 38 * 39 * @example 40 * var y = mgf( 0.5, -1.0, 1.0, 0.0 ); 41 * // returns ~1.021 42 * 43 * @example 44 * var y = mgf( 0.5, -1.0, 1.0, 0.5 ); 45 * // returns ~1.111 46 * 47 * @example 48 * var y = mgf( -0.3, -20.0, 0.0, -2.0 ); 49 * // returns ~24.334 50 * 51 * @example 52 * var y = mgf( -2.0, -1.0, 1.0, 0.0 ); 53 * // returns ~1.381 54 * 55 * @example 56 * var y = mgf( NaN, 0.0, 1.0, 0.5 ); 57 * // returns NaN 58 * 59 * @example 60 * var y = mgf( 0.0, NaN, 1.0, 0.5 ); 61 * // returns NaN 62 * 63 * @example 64 * var y = mgf( 0.0, 0.0, NaN, 0.5 ); 65 * // returns NaN 66 * 67 * @example 68 * var y = mgf( 0.5, 1.0, 0.0, NaN ); 69 * // returns NaN 70 * 71 * @example 72 * var y = mgf( 0.5, 1.0, 0.0, 1.5 ); 73 * // returns NaN 74 */ 75 function mgf( t, a, b, c ) { 76 var bmc; 77 var bma; 78 var cma; 79 var ret; 80 81 if ( 82 isnan( t ) || 83 isnan( a ) || 84 isnan( b ) || 85 isnan( c ) || 86 a > c || 87 c > b 88 ) { 89 return NaN; 90 } 91 if ( t === 0.0 ) { 92 return 1.0; 93 } 94 bmc = b - c; 95 bma = b - a; 96 cma = c - a; 97 ret = (bmc * exp( a * t )) - (bma * exp( c * t )); 98 ret += cma * exp( b * t ); 99 ret *= 2.0; 100 ret /= bma * cma * bmc * pow( t, 2.0 ); 101 return ret; 102 } 103 104 105 // EXPORTS // 106 107 module.exports = mgf;