Skip to content

Commit b070e0d

Browse files
committed
feat: add stats/base/dists/halfnormal/mode
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 1216f34 commit b070e0d

27 files changed

Lines changed: 1899 additions & 0 deletions

File tree

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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 normal distribution."> -->
32+
33+
```math
34+
\mathop{\mathrm{mode}}\left( X \right)
35+
```
36+
37+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{mode}\left( X \right) = \mu" data-equation="eq:normal_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 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 paramter `sigma`.
61+
62+
```javascript
63+
var y = mode( 1.0 );
64+
// returns 1.0
65+
66+
y = mode( 2.0 );
67+
// returns 2.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 paramter.
162+
- **return**: `[out] double` mode.
163+
164+
```c
165+
double stdlib_base_dists_halfnormal_mode( const double sigma );
166+
```
167+
168+
</section>
169+
170+
<!-- /.usage -->
171+
172+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
173+
174+
<section class="notes">
175+
176+
</section>
177+
178+
<!-- /.notes -->
179+
180+
<!-- C API usage examples. -->
181+
182+
<section class="examples">
183+
184+
### Examples
185+
186+
```c
187+
#include "stdlib/stats/base/dists/halfnormal/mode.h"
188+
#include <stdlib.h>
189+
#include <stdio.h>
190+
191+
static double random_uniform( const double min, const double max ) {
192+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
193+
return min + ( v*(max-min) );
194+
}
195+
196+
int main( void ) {
197+
double sigma;
198+
double y;
199+
int i;
200+
201+
for ( i = 0; i < 10; i++ ) {
202+
sigma = random_uniform( 0.1, 20.0 );
203+
y = stdlib_base_dists_halfnormal_mode( sigma );
204+
printf("σ: %.4f, mode(X;σ): %.4f\n", sigma, y);
205+
}
206+
}
207+
```
208+
209+
</section>
210+
211+
<!-- /.examples -->
212+
213+
</section>
214+
215+
<!-- /.c -->
216+
217+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
218+
219+
<section class="related">
220+
221+
</section>
222+
223+
<!-- /.related -->
224+
225+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
226+
227+
<section class="links">
228+
229+
[half-normal-distribution]: https://en.wikipedia.org/wiki/Half-normal_distribution
230+
231+
[mode]: https://en.wikipedia.org/wiki/Mode_%28statistics%29
232+
233+
</section>
234+
235+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 randu = require( '@stdlib/random/base/randu' );
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 y;
36+
var i;
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
sigma = ( randu()*20.0 ) + EPS;
41+
y = mode( sigma );
42+
if ( isnan( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnan( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
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 Float64Array = require( '@stdlib/array/float64' );
26+
var uniform = require( '@stdlib/random/base/uniform' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
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( pkg+'::native', opts, function benchmark( b ) {
44+
var sigma;
45+
var len;
46+
var y;
47+
var i;
48+
49+
len = 100;
50+
sigma = new Float64Array( len );
51+
for ( i = 0; i < len; i++ ) {
52+
sigma[ i ] = uniform( EPS, 20.0 );
53+
}
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
y = mode( sigma[ 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)