Skip to content

Commit 0f2ea57

Browse files
authored
feat: add Wald distribution variance package
PR-URL: #9730 Ref: #209 Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Neeraj Pathak <[email protected]>
1 parent 7b1ec1e commit 0f2ea57

File tree

27 files changed

+2087
-0
lines changed

27 files changed

+2087
-0
lines changed
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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+
# Variance
22+
23+
> [Wald][wald-distribution] distribution [variance][variance].
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 [variance][variance] for a [Wald][wald-distribution] random variable with mean `μ` and shape parameter `λ > 0` is
30+
31+
<!-- <equation class="equation" label="eq:wald_variance" align="center" raw="\operatorname{Var}\left[ X \right] = \frac{\mu^{3}}{\lambda}" alt="Variance for a Wald distribution."> -->
32+
33+
```math
34+
\operatorname{Var}\left[ X \right] = \frac{\mu^{3}}{\lambda}
35+
```
36+
37+
<!-- </equation> -->
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<!-- Package usage documentation. -->
44+
45+
<section class="usage">
46+
47+
## Usage
48+
49+
```javascript
50+
var variance = require( '@stdlib/stats/base/dists/wald/variance' );
51+
```
52+
53+
#### variance( mu, lambda )
54+
55+
Returns the [variance][variance] for a [Wald][wald-distribution] distribution with parameters `mu` (mean) and `lambda` (shape parameter).
56+
57+
```javascript
58+
var y = variance( 2.0, 1.0 );
59+
// returns 8.0
60+
61+
y = variance( 0.0, 1.0 );
62+
// returns NaN
63+
64+
y = variance( 4.0, -1.0 );
65+
// returns NaN
66+
```
67+
68+
If provided `NaN` as any argument, the function returns `NaN`.
69+
70+
```javascript
71+
var y = variance( NaN, 1.0 );
72+
// returns NaN
73+
74+
y = variance( 0.0, NaN );
75+
// returns NaN
76+
```
77+
78+
If provided `mu <= 0` or `lambda <= 0`, the function returns `NaN`.
79+
80+
```javascript
81+
var y = variance( 0.0, 0.0 );
82+
// returns NaN
83+
84+
y = variance( 0.0, -1.0 );
85+
// returns NaN
86+
87+
y = variance( -1.0, 0.0 );
88+
// returns NaN
89+
```
90+
91+
</section>
92+
93+
<!-- /.usage -->
94+
95+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
96+
97+
<section class="notes">
98+
99+
</section>
100+
101+
<!-- /.notes -->
102+
103+
<!-- Package usage examples. -->
104+
105+
<section class="examples">
106+
107+
## Examples
108+
109+
<!-- eslint no-undef: "error" -->
110+
111+
```javascript
112+
var uniform = require( '@stdlib/random/array/uniform' );
113+
var logEachMap = require( '@stdlib/console/log-each-map' );
114+
var EPS = require( '@stdlib/constants/float64/eps' );
115+
var variance = require( '@stdlib/stats/base/dists/wald/variance' );
116+
117+
var opts = {
118+
'dtype': 'float64'
119+
};
120+
var mu = uniform( 10, EPS, 10.0, opts );
121+
var lambda = uniform( 10, EPS, 20.0, opts );
122+
123+
logEachMap( 'µ: %0.4f, λ: %0.4f, Var(X;µ,λ): %0.4f', mu, lambda, variance );
124+
```
125+
126+
</section>
127+
128+
<!-- /.examples -->
129+
130+
<!-- C interface documentation. -->
131+
132+
* * *
133+
134+
<section class="c">
135+
136+
## C APIs
137+
138+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
139+
140+
<section class="intro">
141+
142+
</section>
143+
144+
<!-- /.intro -->
145+
146+
<!-- C usage documentation. -->
147+
148+
<section class="usage">
149+
150+
### Usage
151+
152+
```c
153+
#include "stdlib/stats/base/dists/wald/variance.h"
154+
```
155+
156+
#### stdlib_base_dists_wald_variance( mu, lambda )
157+
158+
Returns the [variance][variance] for a [Wald][wald-distribution] distribution with mean `mu` and shape parameter `lambda`.
159+
160+
```c
161+
double out = stdlib_base_dists_wald_variance( 2.0, 1.0 );
162+
// returns 8.0
163+
```
164+
165+
The function accepts the following arguments:
166+
167+
- **mu**: `[in] double` mean.
168+
- **lambda**: `[in] double` shape parameter.
169+
170+
```c
171+
double stdlib_base_dists_wald_variance( const double mu, const double lambda );
172+
```
173+
174+
</section>
175+
176+
<!-- /.usage -->
177+
178+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
179+
180+
<section class="notes">
181+
182+
</section>
183+
184+
<!-- /.notes -->
185+
186+
<!-- C API usage examples. -->
187+
188+
<section class="examples">
189+
190+
### Examples
191+
192+
```c
193+
#include "stdlib/stats/base/dists/wald/variance.h"
194+
#include "stdlib/constants/float64/eps.h"
195+
#include <stdlib.h>
196+
#include <stdio.h>
197+
198+
static double random_uniform( const double min, const double max ) {
199+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
200+
return min + ( v*(max-min) );
201+
}
202+
203+
int main( void ) {
204+
double lambda;
205+
double mu;
206+
double y;
207+
int i;
208+
209+
for ( i = 0; i < 10; i++ ) {
210+
mu = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 5.0 );
211+
lambda = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 20.0 );
212+
y = stdlib_base_dists_wald_variance( mu, lambda );
213+
printf( "µ: %.4f, λ: %.4f, Var(X;µ,λ): %.4f\n", mu, lambda, y );
214+
}
215+
}
216+
```
217+
218+
</section>
219+
220+
<!-- /.examples -->
221+
222+
</section>
223+
224+
<!-- /.c -->
225+
226+
<!-- 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. -->
227+
228+
<section class="references">
229+
230+
</section>
231+
232+
<!-- /.references -->
233+
234+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
235+
236+
<section class="related">
237+
238+
</section>
239+
240+
<!-- /.related -->
241+
242+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
243+
244+
<section class="links">
245+
246+
[wald-distribution]: https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution
247+
248+
[variance]: https://en.wikipedia.org/wiki/Variance
249+
250+
</section>
251+
252+
<!-- /.links -->
253+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 variance = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var lambda;
35+
var len;
36+
var mu;
37+
var y;
38+
var i;
39+
40+
len = 100;
41+
mu = uniform( len, EPS, 100.0 );
42+
lambda = uniform( len, EPS, 20.0 );
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
y = variance( mu[ i%len ], lambda[ i%len ] );
47+
if ( isnan( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
}
51+
b.toc();
52+
if ( isnan( y ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 variance = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( variance instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
44+
var lambda;
45+
var len;
46+
var mu;
47+
var y;
48+
var i;
49+
50+
len = 100;
51+
mu = uniform( len, EPS, 100.0 );
52+
lambda = uniform( len, EPS, 20.0 );
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
y = variance( mu[ i%len ], lambda[ i%len ] );
57+
if ( isnan( y ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( y ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

0 commit comments

Comments
 (0)