About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Triangular distribution moment-generating function (MGF).
The moment-generating function for a triangular random variable is
where a
is the lower limit, b
is the upper limit, and c
is the mode of the distribution. The parameters must satisfy b > a
and a <= b <= c
.
import mgf from 'https://cdn.jsdelivr.net/gh/stdlib-js/stats-base-dists-triangular-mgf@deno/mod.js';
You can also import the following named exports from the package:
import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/stats-base-dists-triangular-mgf@deno/mod.js';
Evaluates the moment-generating function (MGF) for a triangular distribution with parameters a
(lower limit), b
(upper limit), and c
(mode).
var y = mgf( 0.5, -1.0, 1.0, 0.0 );
// returns ~1.021
y = mgf( 0.5, -1.0, 1.0, 0.5 );
// returns ~1.111
y = mgf( -0.3, -20.0, 0.0, -2.0 );
// returns ~24.334
y = mgf( -2.0, -1.0, 1.0, 0.0 );
// returns ~1.381
If provided NaN
as any argument, the function returns NaN
.
var y = mgf( NaN, 0.0, 1.0, 0.5 );
// returns NaN
y = mgf( 0.0, NaN, 1.0, 0.5 );
// returns NaN
y = mgf( 0.0, 0.0, NaN, 0.5 );
// returns NaN
y = mgf( 2.0, 1.0, 0.0, NaN );
// returns NaN
If provided parameters not satisfying a <= c <= b
, the function returns NaN
.
var y = mgf( 2.0, 1.0, 0.0, 1.5 );
// returns NaN
y = mgf( 2.0, 1.0, 0.0, -1.0 );
// returns NaN
y = mgf( 2.0, 0.0, -1.0, 0.5 );
// returns NaN
Returns a function for evaluating the moment-generating function of a triangular distribution with parameters a
(lower limit), b
(upper limit), and c
(mode).
var mymgf = mgf.factory( 0.0, 2.0, 1.0 );
var y = mymgf( -1.0 );
// returns ~0.3996
y = mymgf( 2.0 );
// returns ~10.205
import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@deno/mod.js';
import mgf from 'https://cdn.jsdelivr.net/gh/stdlib-js/stats-base-dists-triangular-mgf@deno/mod.js';
var a;
var b;
var c;
var t;
var v;
var i;
for ( i = 0; i < 10; i++ ) {
t = randu() * 5.0;
a = randu() * 10.0;
b = a + (randu() * 40.0);
c = a + (( b - a ) * randu());
v = mgf( t, a, b, c );
console.log( 't: %d, a: %d, b: %d, c: %d, M_X(t;a,b,c): %d', t.toFixed( 4 ), a.toFixed( 4 ), b.toFixed( 4 ), c.toFixed( 4 ), v.toFixed( 4 ) );
}
#include "stdlib/stats/base/dists/triangular/mgf.h"
Evaluates the moment-generating function (MGF) for a triangular distribution with parameters a
(lower limit), b
(upper limit), and c
(mode).
double y = stdlib_base_dists_triangular_mgf( 0.5, -1.0, 1.0, 0.0 );
// returns ~1.021
The function accepts the following arguments:
- t:
[in] double
input value. - a:
[in] double
lower limit. - b:
[in] double
upper limit. - c:
[in] double
mode.
double stdlib_base_dists_triangular_mgf( const double t, const double a, const double b, const double c );
#include "stdlib/stats/base/dists/triangular/mgf.h"
#include "stdlib/constants/float64/eps.h"
#include <stdlib.h>
#include <stdio.h>
static double random_uniform( const double min, const double max ) {
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v*(max-min) );
}
int main( void ) {
double a;
double b;
double c;
double t;
double y;
int i;
for ( i = 0; i < 25; i++ ) {
t = random_uniform( 0.0, 5.0 );
a = random_uniform( 0.0, 10.0 );
b = random_uniform( a+STDLIB_CONSTANT_FLOAT64_EPS, 40.0 );
c = random_uniform( a, b );
y = stdlib_base_dists_triangular_mgf( t, a, b, c );
printf( "t: %lf, a: %lf, b: %lf, c: %lf, M_X(t;a,b,c): %lf\n", t, a, b, c, y );
}
}
This package is part of stdlib, a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2025. The Stdlib Authors.