Skip to content

Commit 20fde5c

Browse files
Jaysukh-409kgrytestdlib-bot
authored andcommitted
feat: add stats/base/dists/planck/entropy
PR-URL: stdlib-js#4325 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 0a10c33 commit 20fde5c

File tree

12 files changed

+692
-0
lines changed

12 files changed

+692
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
> Planck (discrete exponential) 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 Planck random variable is
30+
31+
<!-- <equation class="equation" label="eq:planck_entropy" align="center" raw="H\left( X \right) = \frac{\lambda e^{-\lambda}}{1 - e^{-\lambda}} - \log\left( 1 - e^{-\lambda} \right)" alt="Differential entropy for a Planck distribution."> -->
32+
33+
```math
34+
H\left( X \right) = \frac{\lambda e^{-\lambda}}{1 - e^{-\lambda}} - \log\left( 1 - e^{-\lambda} \right)
35+
```
36+
37+
<!-- </equation> -->
38+
39+
where `λ` is the shape 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/planck/entropy' );
53+
```
54+
55+
#### entropy( lambda )
56+
57+
Returns the [differential entropy][entropy] of a Planck distribution with shape parameter `lambda` (in [nats][nats]).
58+
59+
```javascript
60+
var v = entropy( 0.1 );
61+
// returns ~3.3030
62+
63+
v = entropy( 1.5 );
64+
// returns ~0.6833
65+
```
66+
67+
If provided a shape parameter `lambda` which is `NaN` or nonpositive, the function returns `NaN`.
68+
69+
```javascript
70+
var v = entropy( NaN );
71+
// returns NaN
72+
73+
v = entropy( -1.5 );
74+
// returns NaN
75+
```
76+
77+
</section>
78+
79+
<!-- /.usage -->
80+
81+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
82+
83+
<section class="notes">
84+
85+
</section>
86+
87+
<!-- /.notes -->
88+
89+
<!-- Package usage examples. -->
90+
91+
<section class="examples">
92+
93+
## Examples
94+
95+
<!-- eslint no-undef: "error" -->
96+
97+
```javascript
98+
var uniform = require( '@stdlib/random/array/uniform' );
99+
var entropy = require( '@stdlib/stats/base/dists/planck/entropy' );
100+
101+
var lambda = uniform( 10, 0.1, 5.0 );
102+
103+
var v;
104+
var i;
105+
for ( i = 0; i < lambda.length; i++ ) {
106+
v = entropy( lambda[ i ] );
107+
console.log( 'λ: %d, H(X;λ): %d', lambda[ i ].toFixed( 4 ), v.toFixed( 4 ) );
108+
}
109+
```
110+
111+
</section>
112+
113+
<!-- /.examples -->
114+
115+
<!-- 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. -->
116+
117+
<section class="references">
118+
119+
</section>
120+
121+
<!-- /.references -->
122+
123+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
124+
125+
<section class="related">
126+
127+
</section>
128+
129+
<!-- /.related -->
130+
131+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="links">
134+
135+
[entropy]: https://en.wikipedia.org/wiki/Entropy_%28information_theory%29
136+
137+
[nats]: https://en.wikipedia.org/wiki/Nat_%28unit%29
138+
139+
</section>
140+
141+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 lambda;
34+
var y;
35+
var i;
36+
37+
lambda = uniform( 100, 0.1, 10.0 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = entropy( lambda[ i % lambda.length ] );
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( λ )
3+
Returns the differential entropy of a Planck distribution with shape
4+
parameter `λ` (in nats).
5+
6+
If `λ <= 0`, the function returns `NaN`.
7+
8+
Parameters
9+
----------
10+
λ: number
11+
Shape parameter.
12+
13+
Returns
14+
-------
15+
out: number
16+
Entropy.
17+
18+
Examples
19+
--------
20+
> var v = {{alias}}( 0.1 )
21+
~3.3030
22+
> v = {{alias}}( 1.5 )
23+
~0.6833
24+
25+
See Also
26+
--------
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Returns the differential entropy of a Planck distribution.
23+
*
24+
* ## Notes
25+
*
26+
* - If `lambda <= 0`, the function returns `NaN`.
27+
*
28+
* @param lambda - shape parameter
29+
* @returns differential entropy
30+
*
31+
* @example
32+
* var v = entropy( 0.1 );
33+
* // returns ~3.3030
34+
*
35+
* @example
36+
* var v = entropy( 1.5 );
37+
* // returns ~0.6833
38+
*
39+
* @example
40+
* var v = entropy( 2.9 );
41+
* // returns ~0.2255
42+
*
43+
* @example
44+
* var v = entropy( -1.1 );
45+
* // returns NaN
46+
*
47+
* @example
48+
* var v = entropy( NaN );
49+
* // returns NaN
50+
*/
51+
declare function entropy( lambda: number ): number;
52+
53+
54+
// EXPORTS //
55+
56+
export = entropy;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
import entropy = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
entropy( 0.3 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a number...
30+
{
31+
entropy( true ); // $ExpectError
32+
entropy( false ); // $ExpectError
33+
entropy( null ); // $ExpectError
34+
entropy( undefined ); // $ExpectError
35+
entropy( '5' ); // $ExpectError
36+
entropy( [] ); // $ExpectError
37+
entropy( {} ); // $ExpectError
38+
entropy( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided insufficient arguments...
42+
{
43+
entropy(); // $ExpectError
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
var uniform = require( '@stdlib/random/array/uniform' );
22+
var entropy = require( './../lib' );
23+
24+
var lambda = uniform( 10, 0.1, 5.0 );
25+
26+
var v;
27+
var i;
28+
for ( i = 0; i < lambda.length; i++ ) {
29+
v = entropy( lambda[ i ] );
30+
console.log( 'λ: %d, H(X;λ): %d', lambda[ i ].toFixed( 4 ), v.toFixed( 4 ) );
31+
}

0 commit comments

Comments
 (0)