Skip to content

Commit ad72a95

Browse files
committed
feat: add implementation of stats/base/dists/anglit/entropy
Signed-off-by: Bhargav Dabhade <bhargava2005dabhade@gmail.com> --- 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: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: missing_dependencies - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 8336cd9 commit ad72a95

File tree

26 files changed

+2039
-0
lines changed

26 files changed

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

0 commit comments

Comments
 (0)