Skip to content

Commit 9bf5e56

Browse files
feat: add stats/base/dists/halfnormal/entropy
PR-URL: #9753 Ref: #9416 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 2463b16 commit 9bf5e56

File tree

29 files changed

+2130
-0
lines changed

29 files changed

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

0 commit comments

Comments
 (0)