Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
093b515
feat: implement entropy for hypergeometric distribution
24archit Dec 27, 2025
f3b1220
chore: update copyright years
stdlib-bot Dec 27, 2025
ce6f412
feat: implement native C entropy calculation and fix tests
24archit Dec 27, 2025
e60b64d
Update documentation for hypergeometric entropy
24archit Dec 27, 2025
e71f895
Add dependencies and update keywords in package.json
24archit Dec 27, 2025
8c5fbbf
Replace mean function with entropy in benchmark
24archit Dec 27, 2025
9f01401
feat: implementation and docs (wip)
24archit Dec 27, 2025
7fef0d0
fix(stats-base-dists-hypergeometric-entropy): resolve benchmark lint …
24archit Dec 27, 2025
4eea424
fix: add benchmark and example tasks to manifest.json
24archit Dec 27, 2025
c928498
fix(stats/base/dists/hypergeometric/entropy): update examples, types,…
24archit Dec 27, 2025
ec6929e
docs(hypergeometric/entropy): add equation SVG
24archit Dec 27, 2025
90972d9
Update REPL examples for hypergeometric entropy
24archit Dec 27, 2025
258bd86
Change parameters of hypergeometric_entropy to double
24archit Dec 27, 2025
74479a6
Change hypergeometric_entropy parameters to double
24archit Dec 27, 2025
3583b1e
Fix variable 'N' assignment in runner.jl
24archit Dec 27, 2025
8e55c7f
fix: updated test fixtures with correct runner and expected values
24archit Dec 27, 2025
e3a5a3f
Enhance tests for hypergeometric entropy function
24archit Dec 27, 2025
dbaf11d
Update native tests for hypergeometric entropy function
24archit Dec 27, 2025
6f26850
fix: resolved dependencies in entropy
24archit Dec 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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

> [Hypergeometric][hypergeometric-distribution] distribution [entropy][entropy].

The [entropy][entropy] (in nats) for a [hypergeometric][hypergeometric-distribution] random variable is

<div class="equation" align="center" data-raw-text="H(X) = -\sum_{k=\max(0, n+K-N)}^{\min(n, K)} P(X=k) \ln(P(X=k))" data-equation="eq:entropy">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@515340d9d7170753/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/img/equation_entropy.svg" alt="Entropy for a hypergeometric distribution.">
<br>
</div>

where `N` is the population size, `K` is the subpopulation size, and `n` is the number of draws.

<section class="usage">

## Usage

```javascript
var entropy = require( '@stdlib/stats/base/dists/hypergeometric/entropy' );
```

#### entropy( N, K, n )

Returns the [entropy][entropy] of a [hypergeometric][hypergeometric-distribution] distribution with parameters `N` (population size), `K` (subpopulation size), and `n` (number of draws).

```javascript
var v = entropy( 16, 11, 4 );
// returns ~1.216

v = entropy( 2, 1, 1 );
// returns ~0.693
```

If provided `NaN` for any parameter, the function returns `NaN`.

```javascript
var v = entropy( NaN, 10, 4 );
// returns NaN

v = entropy( 20, NaN, 4 );
// returns NaN

v = entropy( 20, 10, NaN );
// returns NaN
```

If provided a parameter that is not a nonnegative integer, the function returns `NaN`.

```javascript
var v = entropy( 10.5, 5, 2 );
// returns NaN

v = entropy( 10, 1.5, 2 );
// returns NaN

v = entropy( 10, 5, -2.0 );
// returns NaN
```

If the number of draws `n` exceeds the population size `N`, the function returns `NaN`.

```javascript
var v = entropy( 10, 5, 12 );
// returns NaN
```

If the subpopulation size `K` exceeds the population size `N`, the function returns `NaN`.

```javascript
var v = entropy( 10, 12, 5 );
// returns NaN
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

```javascript
var entropy = require( '@stdlib/stats/base/dists/hypergeometric/entropy' );

var v = entropy( 16, 11, 4 );
console.log( v );
// => 1.2156868611027067

v = entropy( 2, 1, 1 );
console.log( v );
// => 0.6931471805599453

v = entropy( 10, 5, 12 );
console.log( v );
// => NaN
```

</section>

<!-- /.examples -->

<section class="references">

</section>

<!-- /.references -->

<section class="related">

</section>

<!-- /.related -->

<section class="links">

[hypergeometric-distribution]: https://en.wikipedia.org/wiki/Hypergeometric_distribution

[entropy]: https://en.wikipedia.org/wiki/Entropy_%28information_theory%29

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 Float64Array = require( '@stdlib/array/float64' );
var discreteUniform = require( '@stdlib/random/base/discrete-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 len;
var N;
var K;
var n;
var y;
var i;

len = 100;
N = new Float64Array( len );
K = new Float64Array( len );
n = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
N[ i ] = discreteUniform( 1, 100 );
K[ i ] = discreteUniform( 1, N[ i ] );
n[ i ] = discreteUniform( 1, N[ i ] );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = entropy( N[ i % len ], K[ i % len ], n[ i % len ] );
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,73 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 Float64Array = require( '@stdlib/array/float64' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
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 len;
var N;
var K;
var n;
var y;
var i;

len = 100;
N = new Float64Array( len );
K = new Float64Array( len );
n = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
N[ i ] = discreteUniform( 1, 100 );
K[ i ] = discreteUniform( 1, N[ i ] );
n[ i ] = discreteUniform( 1, N[ i ] );
}
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = entropy( N[ i % len ], K[ i % len ], n[ i % len ] );
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();
});
Loading
Loading