Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
283 changes: 283 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/wald/mgf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# Moment-Generating Function

> [Wald][wald-distribution] distribution moment-generating function (MGF).

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

The [moment-generating function][mgf] for a [Wald][wald-distribution] random variable is

<!-- <equation class="equation" label="eq:wald_mgf_function" align="center" raw="M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = \exp\left[\frac{\lambda}{\mu}\left(1-\sqrt{1-\frac{2\mu^2 t}{\lambda}}\right)\right]" alt="Moment-generating function (MGF) for a Wald distribution."> -->

```math
M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = \exp\left[\frac{\lambda}{\mu}\left(1-\sqrt{1-\frac{2\mu^2 t}{\lambda}}\right)\right]
```

<!-- <div class="equation" align="center" data-raw-text="M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = \exp\left[\frac{\lambda}{\mu}\left(1-\sqrt{1-\frac{2\mu^2 t}{\lambda}}\right)\right]" data-equation="eq:wald_mgf_function">
<img src="" alt="Moment-generating function (MGF) for a Wald distribution.">
<br>
</div> -->

<!-- </equation> -->

where `mu > 0` is the mean and `lambda > 0` is the shape parameter. The MGF is defined for `t < lambda / (2 * mu^2)`.

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var mgf = require( '@stdlib/stats/base/dists/wald/mgf' );
```

#### mgf( t, mu, lambda )

Evaluates the [moment-generating function][mgf] (MGF) for a [Wald][wald-distribution] distribution with parameters `mu` (mean) and `lambda` (shape parameter).

```javascript
var y = mgf( 0.1, 2.0, 3.0 );
// returns ~1.2405

y = mgf( -1.0, 0.5, 2.0 );
// returns ~0.6237
```

If provided `NaN` as any argument, the function returns `NaN`.

```javascript
var y = mgf( NaN, 0.5, 2.0 );
// returns NaN

y = mgf( 0.1, NaN, 2.0 );
// returns NaN

y = mgf( 0.1, 0.5, NaN );
// returns NaN
```

If provided `mu <= 0`, the function returns `NaN`.

```javascript
var y = mgf( 0.1, -1.0, 2.0 );
// returns NaN
```

If provided `lambda <= 0`, the function returns `NaN`.

```javascript
var y = mgf( 0.1, 0.5, -2.0 );
// returns NaN
```

If provided `t >= lambda / (2 * mu^2)`, the function returns `NaN`.

```javascript
var y = mgf( 1.0, 2.0, 3.0 );
// returns NaN
```

#### mgf.factory( mu, lambda )

Returns a function for evaluating the [moment-generating function][mgf] (MGF) of a [Wald][wald-distribution] distribution with parameters `mu` and `lambda`.

```javascript
var mymgf = mgf.factory( 2.0, 3.0 );

var y = mymgf( 0.1 );
// returns ~1.2405

y = mymgf( 0.2 );
// returns ~1.6085
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var uniform = require( '@stdlib/random/array/uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var mgf = require( '@stdlib/stats/base/dists/wald/mgf' );

var opts = {
'dtype': 'float64'
};
var lambda = uniform( 10, 50.0, 100.0, opts );
var mu = uniform( 10, 0.1, 2.0, opts );
var t = uniform( 10, -5.0, 5.0, opts );

logEachMap( 't: %0.4f, µ: %0.4f, λ: %0.4f, M_X(t;µ,λ): %0.4f', t, mu, lambda, mgf );
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/stats/base/dists/wald/mgf.h"
```

#### stdlib_base_dists_wald_mgf( t, mu, lambda )

Evaluates the [moment-generating function][mgf] (MGF) for a [Wald][wald-distribution] distribution with parameters `mu` (mean) and `lambda` (shape parameter).

```c
double y = stdlib_base_dists_wald_mgf( 0.1, 2.0, 3.0 );
// returns ~1.2405
```

The function accepts the following arguments:

- **t**: `[in] double` input value.
- **mu**: `[in] double` mean.
- **lambda**: `[in] double` shape parameter.

```c
double stdlib_base_dists_wald_mgf( const double t, const double mu, const double lambda );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/stats/base/dists/wald/mgf.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 lambda;
double mu;
double t;
double y;
int i;

for ( i = 0; i < 10; i++ ) {
t = random_uniform( -5.0, 5.0 );
mu = random_uniform( 0.1, 2.0 );
lambda = random_uniform( 50.0, 100.0 );
y = stdlib_base_dists_wald_mgf( t, mu, lambda );
printf( "t: %lf, µ: %lf, λ: %lf, M_X(t;µ,λ): %lf\n", t, mu, lambda, y );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[wald-distribution]: https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution

[mgf]: https://en.wikipedia.org/wiki/Moment-generating_function

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var mgf = require( './../lib' );


// MAIN //

bench(pkg, function benchmark(b) {
var lambda;
var opts;
var mu;
var t;
var y;
var i;

opts = {
'dtype': 'float64'
};
t = uniform(100, -5.0, 5.0, opts);
mu = uniform(100, 0.1, 2.0, opts);
lambda = uniform(100, 50.0, 100.0, opts);

b.tic();
for (i = 0; i < b.iterations; i++) {
y = mgf( t[ i%t.length ], mu[ i%mu.length], lambda[ i%lambda.length ]);
if (isnan(y)) {
b.fail('should not return NaN');
}
}
b.toc();
if (isnan(y)) {
b.fail('should not return NaN');
}
b.pass('benchmark finished');
b.end();
});

bench(format('%s:factory', pkg), function benchmark(b) {
var mymgf;
var t;
var y;
var i;

mymgf = mgf.factory(2.0, 50.0);
t = uniform(100, -5.0, 5.0, {
'dtype': 'float64'
});

b.tic();
for (i = 0; i < b.iterations; i++) {
y = mymgf( t[ i%t.length ] );
if (isnan(y)) {
b.fail('should not return NaN');
}
}
b.toc();
if (isnan(y)) {
b.fail('should not return NaN');
}
b.pass('benchmark finished');
b.end();
});
Loading
Loading