Skip to content

Commit 794dd4a

Browse files
feat: add stats/base/dists/halfnormal/mode
PR-URL: #9705 Ref: #9416 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Lokesh Ranjan <lokesh.ranjan.phe23@itbhu.ac.in>
1 parent c9d512a commit 794dd4a

File tree

27 files changed

+1900
-0
lines changed

27 files changed

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

0 commit comments

Comments
 (0)