⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
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
279 changes: 279 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/wald/cdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
<!--

@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.

-->

# Cumulative Distribution Function

> [Wald][wald-distribution] distribution [cumulative distribution function][cdf].

<section class="intro">

The [cumulative distribution function][cdf] for a [Wald][wald-distribution] random variable is

<!-- <equation class="equation" label="eq:wald_cdf" align="center" raw="F(x;\mu,\lambda)=\Phi(\sqrt{\lambda/x}((x/\mu)-1))+\exp(2\lambda/\mu)\Phi(-\sqrt{\lambda/x}((x/\mu)+1))" alt="Cumulative distribution function (CDF) for a Wald distribution."> -->

```math
F(x;\mu,\lambda) = \Phi\!\left(\sqrt{\frac{\lambda}{x}}\left(\frac{x}{\mu}-1\right)\right) + \exp\!\left(\frac{2\lambda}{\mu}\right)\,\Phi\!\left(-\sqrt{\frac{\lambda}{x}}\left(\frac{x}{\mu}+1\right)\right)
```

<!-- </equation> -->

where `µ > 0` is the mean and `λ > 0` is the shape parameter.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

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

#### cdf( x, mu, lambda )

Evaluates the [cumulative distribution function][cdf] (CDF) for a [Wald][wald-distribution] distribution with parameters `mu` (mean) and `lambda` (shape parameter).

```javascript
var y = cdf( 2.0, 2.0, 1.0 );
// returns ~0.714

y = cdf( 1.0, 2.0, 1.0 );
// returns ~0.490

y = cdf( 4.0, 2.0, 1.0 );
// returns ~0.873
```

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

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

y = cdf( 2.0, NaN, 1.0 );
// returns NaN

y = cdf( 2.0, 2.0, NaN );
// returns NaN
```

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

```javascript
var y = cdf( 2.0, 0.0, -3.0 );
// returns NaN

y = cdf( 2.0, -1.0, -2.0 );
// returns NaN

y = cdf( 2.0, -2.0, -1.0 );
// returns NaN
```

If provided `x <= 0`, the function returns `0`.

```javascript
var y = cdf( 0.0, 2.0, 1.0 );
// returns 0.0

y = cdf( -1.0, 2.0, 1.0 );
// returns 0.0
```

If `lambda = 0`, the function evaluates the [CDF][cdf] of a [degenerate distribution][degenerate-distribution] centered at `mu`.

```javascript
var y = cdf( 2.0, 8.0, 0.0 );
// returns 0.0

y = cdf( 8.0, 8.0, 0.0 );
// returns 1.0

y = cdf( 10.0, 8.0, 0.0 );
// returns 1.0
```

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

Returns a function for evaluating the [cumulative distribution function][cdf] of a wald distribution with parameters `mu` and `lambda`.

```javascript
var mycdf = cdf.factory( 1.0, 1.0 );

var y = mycdf( 2.0 );
// returns ~0.885

y = mycdf( 8.0 );
// returns ~0.999
Comment on lines +125 to +126
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throws an error if 0.998 is rounded of to 1.000 so used this instead!!

```

</section>

<!-- /.usage -->

<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 EPS = require( '@stdlib/constants/float64/eps' );
var cdf = require( '@stdlib/stats/base/dists/wald/cdf' );

var opts = {
'dtype': 'float64'
};
var x = uniform( 10, EPS, 10.0, opts );
var mu = uniform( 10, EPS, 10.0, opts );
var lambda = uniform( 10, EPS, 20.0, opts );

logEachMap( 'x: %0.4f, µ: %0.4f, λ: %0.4f, f(x;µ,λ): %0.4f', x, mu, lambda, cdf );
```

</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/cdf.h"
```

#### stdlib_base_dists_wald_cdf( x, mu, lambda )

Evaluates the [cumulative distribution function][cdf] (CDF) for a [Wald][wald-distribution] distribution with parameters `mu` (mean) and `lambda` (shape parameter).

```c
double out = stdlib_base_dists_wald_cdf( 2.0, 1.0, 1.0 );
// returns ~0.885
```

The function accepts the following arguments:

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

```c
double stdlib_base_dists_wald_cdf( const double x, 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/cdf.h"
#include "stdlib/constants/float64/eps.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.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 x;
double y;
int i;

for ( i = 0; i < 10; i++ ) {
x = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 10.0 );
mu = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 10.0 );
lambda = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 10.0 );
y = stdlib_base_dists_wald_cdf( x, mu, lambda );
printf( "x: %lf, µ: %lf, λ: %lf, F(x;µ,λ): %lf\n", x, mu, lambda, y );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- 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">

[cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function

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

[degenerate-distribution]: https://en.wikipedia.org/wiki/Degenerate_distribution

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* @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 EPS = require( '@stdlib/constants/float64/eps' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var cdf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var lambda;
var len;
var mu;
var x;
var y;
var i;

len = 100;
x = uniform( len, EPS, 100.0 );
mu = uniform( len, EPS, 50.0 );
lambda = uniform( len, EPS, 20.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = cdf( x[ i % len ], mu[ i % len ], lambda[ i % len ] );
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 mycdf;
var x;
var y;
var i;

mycdf = cdf.factory( 1.0, 1.5 );
x = uniform( 100, EPS, 10.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mycdf( x[ i % x.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