-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add implementation of stats/base/dists/anglit/entropy
#10620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,245 @@ | ||||
| <!-- | ||||
| @license Apache-2.0 | ||||
| Copyright (c) 2026 The Stdlib Authors. | ||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| you may not use this file except in compliance with the License. | ||||
| You may obtain a copy of the License at | ||||
| http://www.apache.org/licenses/LICENSE-2.0 | ||||
| Unless required by applicable law or agreed to in writing, software | ||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| See the License for the specific language governing permissions and | ||||
| limitations under the License. | ||||
| --> | ||||
|
|
||||
| # Entropy | ||||
|
|
||||
| > [Anglit][anglit-distribution] distribution [differential entropy][entropy]. | ||||
| <section class="intro"> | ||||
|
|
||||
| The [differential entropy][entropy] for an [anglit][anglit-distribution] random variable is | ||||
|
|
||||
| <!-- <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."> --> | ||||
|
|
||||
| ```math | ||||
| h\left( X \right) = 1 - \ln(2) + \ln(\sigma) | ||||
| ``` | ||||
|
|
||||
| <!-- </equation> --> | ||||
|
|
||||
| where `σ > 0` is the scale parameter. | ||||
|
|
||||
| </section> | ||||
|
|
||||
| <!-- /.intro --> | ||||
|
|
||||
| <section class="usage"> | ||||
|
|
||||
| ## Usage | ||||
|
|
||||
| ```javascript | ||||
| var entropy = require( '@stdlib/stats/base/dists/anglit/entropy' ); | ||||
| ``` | ||||
|
|
||||
| #### entropy( mu, sigma ) | ||||
|
|
||||
| Returns the [differential entropy][entropy] of an [Anglit][anglit-distribution] distribution with location parameter mu and scale parameter sigma | ||||
|
|
||||
| ```javascript | ||||
| var v = entropy( 0.0, 1.0 ); | ||||
| // returns ~0.307 | ||||
|
|
||||
| v = entropy( 0.0, 2.0 ); | ||||
| // returns ~1.000 | ||||
|
|
||||
| v = entropy( 1.0, 0.5 ); | ||||
| // returns ~-0.386 | ||||
| ``` | ||||
|
|
||||
| If provided `NaN` as any argument, the function returns `NaN`. | ||||
|
|
||||
| ```javascript | ||||
| var v = entropy( NaN, 1.0 ); | ||||
| // returns NaN | ||||
|
|
||||
| v = entropy( 0.0, NaN ); | ||||
| // returns NaN | ||||
| ``` | ||||
|
|
||||
| If provided `sigma <= 0`, the function returns `NaN`. | ||||
|
|
||||
| ```javascript | ||||
| var y = entropy( 0.0, -1.0 ); | ||||
| // returns NaN | ||||
|
|
||||
| y = entropy( 0.0, 0.0 ); | ||||
| // returns NaN | ||||
| ``` | ||||
|
|
||||
| </section> | ||||
|
|
||||
| <!-- /.usage --> | ||||
|
|
||||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||||
|
|
||||
| <section class="notes"> | ||||
|
|
||||
| </section> | ||||
|
|
||||
| <!-- /.notes --> | ||||
|
|
||||
| <!-- Package usage examples. --> | ||||
|
|
||||
| <section class="examples"> | ||||
|
|
||||
| ## Examples | ||||
|
|
||||
| <!-- eslint no-undef: "error" --> | ||||
|
|
||||
| ```javascript | ||||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||||
| var logEachMap = require( '@stdlib/console/log-each-map' ); | ||||
| var entropy = require( '@stdlib/stats/base/dists/anglit/entropy' ); | ||||
|
|
||||
| var opts = { | ||||
| 'dtype': 'float64' | ||||
| }; | ||||
|
|
||||
| var mu = uniform( 25, -5.0, 5.0, opts ); | ||||
| var sigma = uniform( mu.length, 0.5, 5.0, opts ); | ||||
|
|
||||
| logEachMap( 'mu: %0.4f, sigma: %0.4f, h(X;mu,sigma): %0.4f', mu, sigma, entropy ); | ||||
| ``` | ||||
|
|
||||
| </section> | ||||
|
|
||||
| <!-- /.examples --> | ||||
|
|
||||
| <!-- C interface documentation. --> | ||||
|
|
||||
| * * * | ||||
|
|
||||
| <section class="c"> | ||||
|
|
||||
| ## C APIs | ||||
|
|
||||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||||
|
|
||||
| <section class="intro"> | ||||
|
|
||||
| </section> | ||||
|
|
||||
| <!-- /.intro --> | ||||
|
|
||||
| <!-- C usage documentation. --> | ||||
|
|
||||
| <section class="usage"> | ||||
|
|
||||
| ### Usage | ||||
|
|
||||
| ```c | ||||
| #include "stdlib/stats/base/dists/anglit/entropy.h" | ||||
| ``` | ||||
|
|
||||
| #### stdlib_base_dists_anglit_entropy( mu, sigma ) | ||||
|
|
||||
| Returns the differential entropy of an Anglit distribution with location parameter `mu` and scale parameter `sigma`. | ||||
|
|
||||
| ```c | ||||
| double out = stdlib_base_dists_anglit_entropy( 0.0, 1.0 ); | ||||
| // returns ~0.307 | ||||
| ``` | ||||
|
|
||||
| The function accepts the following arguments: | ||||
|
|
||||
| - **mu**: `[in] double` location parameter. | ||||
| - **sigma**: `[in] double` scale parameter. | ||||
|
|
||||
| ```c | ||||
| The function accepts the following arguments: | ||||
|
|
||||
| - **x**: `[in] double` input value. | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Anglit Entropy utilizes only 2 parameters |
||||
| - **mu**: `[in] double` location parameter. | ||||
| - **sigma**: `[in] double` scale parameter. | ||||
|
|
||||
| ```c | ||||
| double stdlib_base_dists_anglit_entropy( const double mu, const double sigma ); | ||||
| ``` | ||||
| </section> | ||||
| <!-- /.usage --> | ||||
| <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||||
| <section class="notes"> | ||||
| </section> | ||||
| <!-- /.notes --> | ||||
| <!-- C API usage examples. --> | ||||
| <section class="examples"> | ||||
| ### Examples | ||||
| ```c | ||||
| #include "stdlib/stats/base/dists/anglit/entropy.h" | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| static double random_uniform( const double min, const double max ) { | ||||
| double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); | ||||
| return min + ( v*(max-min) ); | ||||
| } | ||||
| int main( void ) { | ||||
| double mu; | ||||
| double sigma; | ||||
| double y; | ||||
| int i; | ||||
| for ( i = 0; i < 25; i++ ) { | ||||
| mu = random_uniform( -5.0, 5.0 ); | ||||
| sigma = random_uniform( 0.5, 10.0 ); | ||||
| y = stdlib_base_dists_anglit_entropy( mu, sigma ); | ||||
| printf( "mu: %lf, sigma: %lf, h(X;mu,sigma): %lf\n", mu, sigma, y ); | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same Comment but w.r.t the C examples. |
||||
| } | ||||
| } | ||||
| ``` | ||||
|
|
||||
| </section> | ||||
|
|
||||
| <!-- /.examples --> | ||||
|
|
||||
| </section> | ||||
|
|
||||
| <!-- /.c --> | ||||
|
|
||||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||||
|
|
||||
| <section class="related"> | ||||
|
|
||||
| </section> | ||||
|
|
||||
| <!-- /.related --> | ||||
|
|
||||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||||
|
|
||||
| <section class="links"> | ||||
|
|
||||
| [entropy]: https://en.wikipedia.org/wiki/Entropy_%28information_theory%29 | ||||
|
|
||||
| [anglit-distribution]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.anglit.html | ||||
|
|
||||
| </section> | ||||
|
|
||||
| <!-- /.links --> | ||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var entropy = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var sigma; | ||
| var opts; | ||
| var mu; | ||
| var y; | ||
| var i; | ||
|
|
||
| opts = { | ||
| 'dtype': 'float64' | ||
| }; | ||
| mu = uniform( 100, -5.0, 5.0, opts ); | ||
| sigma = uniform( 100, 0.1, 10.0, opts ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| y = entropy( mu[ i % mu.length ], sigma[ i % sigma.length ] ); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
|
|
||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,69 @@ | ||||||||||||||
| /** | ||||||||||||||
| * @license Apache-2.0 | ||||||||||||||
| * | ||||||||||||||
| * Copyright (c) 2026 The Stdlib Authors. | ||||||||||||||
| * | ||||||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||||||
| * You may obtain a copy of the License at | ||||||||||||||
| * | ||||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||
| * | ||||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||||
| * limitations under the License. | ||||||||||||||
| */ | ||||||||||||||
|
|
||||||||||||||
| 'use strict'; | ||||||||||||||
|
|
||||||||||||||
| // MODULES // | ||||||||||||||
|
|
||||||||||||||
| var resolve = require( 'path' ).resolve; | ||||||||||||||
| var bench = require( '@stdlib/bench' ); | ||||||||||||||
| var tryRequire = require( '@stdlib/utils/try-require' ); | ||||||||||||||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||||||||||||||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||||||||||||||
| var format = require( '@stdlib/string/format' ); | ||||||||||||||
| var pkg = require( './../package.json' ).name; | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| // VARIABLES // | ||||||||||||||
|
|
||||||||||||||
| var entropy = tryRequire( resolve( __dirname, './../lib/native.js' ) ); | ||||||||||||||
| var opts = { | ||||||||||||||
| 'skip': ( entropy instanceof Error ) | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| // MAIN // | ||||||||||||||
|
|
||||||||||||||
| bench( format( '%s::native', pkg ), opts, function benchmark( b ) { | ||||||||||||||
| var sigma; | ||||||||||||||
| var mu; | ||||||||||||||
| var y; | ||||||||||||||
| var i; | ||||||||||||||
|
|
||||||||||||||
| mu = uniform( 100, -5.0, 5.0, { | ||||||||||||||
| 'dtype': 'float64' | ||||||||||||||
| }); | ||||||||||||||
| sigma = uniform( 100, 0.1, 10.0, { | ||||||||||||||
| 'dtype': 'float64' | ||||||||||||||
| }); | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
you can represent this a bit better this way. |
||||||||||||||
|
|
||||||||||||||
| b.tic(); | ||||||||||||||
| for ( i = 0; i < b.iterations; i++ ) { | ||||||||||||||
| y = entropy( mu[ i % mu.length ], sigma[ i % sigma.length ] ); | ||||||||||||||
| if ( isnan( y ) ) { | ||||||||||||||
| b.fail( 'should not return NaN' ); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| b.toc(); | ||||||||||||||
|
|
||||||||||||||
| if ( isnan( y ) ) { | ||||||||||||||
| b.fail( 'should not return NaN' ); | ||||||||||||||
| } | ||||||||||||||
| b.pass( 'benchmark finished' ); | ||||||||||||||
| b.end(); | ||||||||||||||
| }); | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do update the documented version with the changes in the corresponding JS file.