Skip to content

Commit 92d23f5

Browse files
feat: add Wald distribution mean
PR-URL: #9502 Ref: #209 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 040d8ec commit 92d23f5

File tree

27 files changed

+2103
-0
lines changed

27 files changed

+2103
-0
lines changed
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Mean
22+
23+
> [Wald][wald-distribution] distribution [expected value][mean].
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
The [expected value][mean] for a [Wald][wald-distribution] random variable with mean `μ` and shape parameter `λ > 0` is
30+
31+
<!-- <equation class="equation" label="eq:wald_expectation" align="center" raw="\mathbb{E}\left[ X \right] = \mu" alt="Expected value for a Wald distribution."> -->
32+
33+
```math
34+
\mathbb{E}\left[ X \right] = \mu
35+
```
36+
37+
<!-- <div class="equation" align="center" data-raw-text="\mathbb{E}\left[ X \right] = \mu" data-equation="eq:wald_expectation">
38+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/wald/mean/docs/img/equation_wald_expectation.svg" alt="Expected value for a Wald distribution.">
39+
<br>
40+
</div> -->
41+
42+
<!-- </equation> -->
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<!-- Package usage documentation. -->
49+
50+
<section class="usage">
51+
52+
## Usage
53+
54+
```javascript
55+
var mean = require( '@stdlib/stats/base/dists/wald/mean' );
56+
```
57+
58+
#### mean( mu, lambda )
59+
60+
Returns the [expected value][mean] for a [Wald][wald-distribution] distribution with parameters `mu` (mean) and `lambda` (shape parameter).
61+
62+
```javascript
63+
var y = mean( 2.0, 1.0 );
64+
// returns 2.0
65+
66+
y = mean( 0.0, 1.0 );
67+
// returns NaN
68+
69+
y = mean( -1.0, 4.0 );
70+
// returns NaN
71+
```
72+
73+
If provided `NaN` as any argument, the function returns `NaN`.
74+
75+
```javascript
76+
var y = mean( NaN, 1.0 );
77+
// returns NaN
78+
79+
y = mean( 0.0, NaN );
80+
// returns NaN
81+
```
82+
83+
If provided `mu <= 0` or `lambda <= 0`, the function returns `NaN`.
84+
85+
```javascript
86+
var y = mean( 0.0, 0.0 );
87+
// returns NaN
88+
89+
y = mean( 0.0, -1.0 );
90+
// returns NaN
91+
92+
y = mean( -1.0, 0.0 );
93+
// returns NaN
94+
```
95+
96+
</section>
97+
98+
<!-- /.usage -->
99+
100+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
101+
102+
<section class="notes">
103+
104+
</section>
105+
106+
<!-- /.notes -->
107+
108+
<!-- Package usage examples. -->
109+
110+
<section class="examples">
111+
112+
## Examples
113+
114+
<!-- eslint no-undef: "error" -->
115+
116+
```javascript
117+
var uniform = require( '@stdlib/random/array/uniform' );
118+
var logEachMap = require( '@stdlib/console/log-each-map' );
119+
var EPS = require( '@stdlib/constants/float64/eps' );
120+
var mean = require( '@stdlib/stats/base/dists/wald/mean' );
121+
122+
var opts = {
123+
'dtype': 'float64'
124+
};
125+
var mu = uniform( 10, EPS, 10.0, opts );
126+
var lambda = uniform( 10, EPS, 20.0, opts );
127+
128+
logEachMap( 'µ: %0.4f, λ: %0.4f, E(X;µ,λ): %0.4f', mu, lambda, mean );
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- C interface documentation. -->
136+
137+
* * *
138+
139+
<section class="c">
140+
141+
## C APIs
142+
143+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
144+
145+
<section class="intro">
146+
147+
</section>
148+
149+
<!-- /.intro -->
150+
151+
<!-- C usage documentation. -->
152+
153+
<section class="usage">
154+
155+
### Usage
156+
157+
```c
158+
#include "stdlib/stats/base/dists/wald/mean.h"
159+
```
160+
161+
#### stdlib_base_dists_wald_mean( mu, lambda )
162+
163+
Returns the [expected value][mean] for a [Wald][wald-distribution] distribution with mean `mu` and shape parameter `lambda`.
164+
165+
```c
166+
double out = stdlib_base_dists_wald_mean( 2.0, 1.0 );
167+
// returns 2.0
168+
```
169+
170+
The function accepts the following arguments:
171+
172+
- **mu**: `[in] double` mean.
173+
- **lambda**: `[in] double` shape parameter.
174+
175+
```c
176+
double stdlib_base_dists_wald_mean( const double mu, const double lambda );
177+
```
178+
179+
</section>
180+
181+
<!-- /.usage -->
182+
183+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
184+
185+
<section class="notes">
186+
187+
</section>
188+
189+
<!-- /.notes -->
190+
191+
<!-- C API usage examples. -->
192+
193+
<section class="examples">
194+
195+
### Examples
196+
197+
```c
198+
#include "stdlib/stats/base/dists/wald/mean.h"
199+
#include <stdlib.h>
200+
#include <stdio.h>
201+
202+
static double random_uniform( const double min, const double max ) {
203+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
204+
return min + ( v*(max-min) );
205+
}
206+
207+
int main( void ) {
208+
double lambda;
209+
double mu;
210+
double y;
211+
int i;
212+
213+
for ( i = 0; i < 10; i++ ) {
214+
mu = random_uniform( 0.1, 5.0 );
215+
lambda = random_uniform( 0.1, 20.0 );
216+
y = stdlib_base_dists_wald_mean( mu, lambda );
217+
printf( "µ: %.4f, λ: %.4f, Mean(X;µ,λ): %.4f\n", mu, lambda, y );
218+
}
219+
}
220+
```
221+
222+
</section>
223+
224+
<!-- /.examples -->
225+
226+
</section>
227+
228+
<!-- /.c -->
229+
230+
<!-- 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. -->
231+
232+
<section class="references">
233+
234+
</section>
235+
236+
<!-- /.references -->
237+
238+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
239+
240+
<section class="related">
241+
242+
</section>
243+
244+
<!-- /.related -->
245+
246+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
247+
248+
<section class="links">
249+
250+
[wald-distribution]: https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution
251+
252+
[mean]: https://en.wikipedia.org/wiki/Mean
253+
254+
</section>
255+
256+
<!-- /.links -->
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var EPS = require( '@stdlib/constants/float64/eps' );
27+
var pkg = require( './../package.json' ).name;
28+
var mean = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var lambda;
35+
var opts;
36+
var mu;
37+
var y;
38+
var i;
39+
40+
opts = {
41+
'dtype': 'float64'
42+
};
43+
mu = uniform( 100, EPS, 100.0, opts );
44+
lambda = uniform( 100, EPS, 20.0, opts );
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
y = mean( mu[ i % 100 ], lambda[ i % 100 ] );
49+
if ( isnan( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
}
53+
b.toc();
54+
if ( isnan( y ) ) {
55+
b.fail( 'should not return NaN' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var format = require( '@stdlib/string/format' );
29+
var EPS = require( '@stdlib/constants/float64/eps' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var mean = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( mean instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
44+
var arrayOpts;
45+
var lambda;
46+
var mu;
47+
var y;
48+
var i;
49+
50+
arrayOpts = {
51+
'dtype': 'float64'
52+
};
53+
mu = uniform( 100, EPS, 100.0, arrayOpts );
54+
lambda = uniform( 100, EPS, 20.0, arrayOpts );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
y = mean( mu[ i % 100 ], lambda[ i % 100 ] );
59+
if ( isnan( y ) ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnan( y ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});

0 commit comments

Comments
 (0)