Skip to content

Commit 4479454

Browse files
feat: add stats/base/dists/wald/mode
PR-URL: #10205 Ref: #209 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Neeraj Pathak <neerajrpathak710@gmail.com>
1 parent 7e5f19e commit 4479454

27 files changed

Lines changed: 2111 additions & 0 deletions

File tree

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