Skip to content

Commit 2463b16

Browse files
feat: add implementation of stats/base/dists/halfnormal/kurtosis
PR-URL: #9771 Ref: #9416 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent f79b9dd commit 2463b16

File tree

26 files changed

+1921
-0
lines changed

26 files changed

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

0 commit comments

Comments
 (0)