Skip to content

Commit 242c076

Browse files
feat: add stats/base/dists/halfnormal/mean
PR-URL: #9613 Ref: #9416 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Shubham <b4mshubham@gmail.com>
1 parent b18cbc4 commit 242c076

27 files changed

Lines changed: 1958 additions & 0 deletions

File tree

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
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+
> [Half-normal][half-normal-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 [half-normal][half-normal-distribution] random variable with scale parameter `σ > 0` is
30+
31+
<!-- <equation class="equation" label="eq:halfnormal_expectation" align="center" raw="\mathbb{E}\left[ X \right] = \sigma \sqrt{\frac{2}{\pi}}" alt="Expected value for a half-normal distribution."> -->
32+
33+
```math
34+
\mathbb{E}\left[ X \right] = \sigma \sqrt{\frac{2}{\pi}}
35+
```
36+
37+
<!-- <div class="equation" align="center" data-raw-text="\mathbb{E}\left[ X \right] = \sigma \sqrt{\frac{2}{\pi}}" data-equation="eq:halfnormal_expectation">
38+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/halfnormal/mean/docs/img/equation_halfnormal_expectation.svg" alt="Expected value 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 mean = require( '@stdlib/stats/base/dists/halfnormal/mean' );
56+
```
57+
58+
#### mean( sigma )
59+
60+
Returns the [expected value][mean] for a [half-normal][half-normal-distribution] distribution with scale parameter `sigma`.
61+
62+
```javascript
63+
var y = mean( 1.0 );
64+
// returns ~0.798
65+
66+
y = mean( 4.0 );
67+
// returns ~3.192
68+
69+
y = mean( 0.5 );
70+
// returns ~0.399
71+
```
72+
73+
If provided `NaN` as any argument, the function returns `NaN`.
74+
75+
```javascript
76+
var y = mean( NaN );
77+
// returns NaN
78+
```
79+
80+
If provided `sigma <= 0`, the function returns `NaN`.
81+
82+
```javascript
83+
var y = mean( 0.0 );
84+
// returns NaN
85+
86+
y = mean( -1.0 );
87+
// returns NaN
88+
```
89+
90+
</section>
91+
92+
<!-- /.usage -->
93+
94+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
95+
96+
<section class="notes">
97+
98+
</section>
99+
100+
<!-- /.notes -->
101+
102+
<!-- Package usage examples. -->
103+
104+
<section class="examples">
105+
106+
## Examples
107+
108+
<!-- eslint no-undef: "error" -->
109+
110+
```javascript
111+
var uniform = require( '@stdlib/random/array/uniform' );
112+
var logEachMap = require( '@stdlib/console/log-each-map' );
113+
var mean = require( '@stdlib/stats/base/dists/halfnormal/mean' );
114+
115+
var opts = {
116+
'dtype': 'float64'
117+
};
118+
var sigma = uniform( 10, 0.1, 20.0, opts );
119+
120+
logEachMap( 'σ: %lf, E(X;σ): %lf', sigma, mean );
121+
```
122+
123+
</section>
124+
125+
<!-- /.examples -->
126+
127+
<!-- 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. -->
128+
129+
<section class="references">
130+
131+
</section>
132+
133+
<!-- /.references -->
134+
135+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
136+
137+
<section class="related">
138+
139+
</section>
140+
141+
<!-- /.related -->
142+
143+
<!-- Section for C API documentation. -->
144+
145+
* * *
146+
147+
<section class="c">
148+
149+
## C APIs
150+
151+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
152+
153+
<section class="intro">
154+
155+
</section>
156+
157+
<!-- /.intro -->
158+
159+
<!-- C API usage documentation. -->
160+
161+
<section class="usage">
162+
163+
### Usage
164+
165+
```c
166+
#include "stdlib/stats/base/dists/halfnormal/mean.h"
167+
```
168+
169+
#### stdlib_base_dists_halfnormal_mean( sigma )
170+
171+
Returns the expected value for a [half-normal][half-normal-distribution] distribution with scale parameter `sigma`.
172+
173+
- **sigma**: `[in] double` scale parameter.
174+
175+
```c
176+
double stdlib_base_dists_halfnormal_mean( const double sigma );
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/halfnormal/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 sigma;
209+
double y;
210+
int i;
211+
212+
for ( i = 0; i < 10; i++ ) {
213+
sigma = random_uniform( 0.1, 20.0 );
214+
y = stdlib_base_dists_halfnormal_mean( sigma );
215+
printf( "σ: %lf, E(X;σ): %lf\n", sigma, y );
216+
}
217+
}
218+
```
219+
220+
</section>
221+
222+
<!-- /.examples -->
223+
224+
</section>
225+
226+
<!-- /.c -->
227+
228+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
229+
230+
<section class="related">
231+
232+
</section>
233+
234+
<!-- /.related -->
235+
236+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
237+
238+
<section class="links">
239+
240+
[half-normal-distribution]: https://en.wikipedia.org/wiki/Half-normal_distribution
241+
242+
[mean]: https://en.wikipedia.org/wiki/Mean
243+
244+
</section>
245+
246+
<!-- /.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 Float64Array = require( '@stdlib/array/float64' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var EPS = require( '@stdlib/constants/float64/eps' );
28+
var pkg = require( './../package.json' ).name;
29+
var mean = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var sigma;
36+
var len;
37+
var y;
38+
var i;
39+
40+
len = 100;
41+
sigma = new Float64Array( len );
42+
for ( i = 0; i < len; i++ ) {
43+
sigma[ i ] = ( randu() * 20.0 ) + EPS;
44+
}
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
y = mean( sigma[ i % len ] );
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: 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 mean = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( mean 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 = mean( 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)