Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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,172 @@
<!--

@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

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

<!-- 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">

The [differential entropy][entropy] for a [hypergeometric][hypergeometric-distribution] random variable is given by

<!-- <equation class="equation" label="eq:hypergeometric_entropy" align="center" raw="H(X) = - \sum P(k) \ln(P(k))" alt="Differential entropy for a hypergeometric distribution."> -->

```math
H(X) = - \sum_{k=\max(0, n+K-N)}^{\min(n, K)} P(X=k) \ln(P(X=k))
```

<!-- <div class="equation" align="center" data-raw-text="H(X) = - \sum P(k) \ln(P(k))" data-equation="eq:hypergeometric_entropy">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/img/equation_hypergeometric_entropy.svg" alt="Differential entropy for a hypergeometric distribution.">
<br>
</div> -->

<!-- </equation> -->

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

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

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

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

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

```javascript
var y = entropy( 16, 11, 4 );
// returns ~1.22

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

If provided `NaN` as any argument, the function returns `NaN`.

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

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

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

If provided a population size `N`, subpopulation size `K` or draws `n` which is not a nonnegative integer, the function returns `NaN`.

```javascript
var y = entropy( 10.5, 4, 2 );
// returns NaN

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

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

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

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

y = entropy( 10, 5, 12 );
// 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 randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var entropy = require( '@stdlib/stats/base/dists/hypergeometric/entropy' );

var i;
var N;
var K;
var n;
var y;

for ( i = 0; i < 10; i++ ) {
N = round( randu() * 20.0 );
K = round( randu() * N );
n = round( randu() * N );
y = entropy( N, K, n );
console.log( 'N: %d, K: %d, n: %d, H(X;N,K,n): %d', N, K, n, y.toFixed( 4 ) );
}
```

</section>

<!-- /.examples -->

<!-- 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">

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

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

[nats]: https://en.wikipedia.org/wiki/Nat_%28unit%29

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @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 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,54 @@

{{alias}}( N, K, n )
Returns the differential entropy of a hypergeometric distribution.

If provided `NaN` as any argument, the function returns `NaN`.

If provided a population size `N`, subpopulation size `K` or draws `n` which
is not a nonnegative integer, the function returns `NaN`.

If the number of draws `n` or the subpopulation size `K` exceed population
size `N`, the function returns `NaN`.

Parameters
----------
N: integer
Population size.

K: integer
Subpopulation size.

n: integer
Number of draws.

Returns
-------
out: number
Entropy.

Examples
--------
> var v = {{alias}}( 16, 11, 4 )
~1.22
> v = {{alias}}( 2, 1, 1 )
~0.693

> v = {{alias}}( 10, 5, 12 )
NaN
> v = {{alias}}( 10.3, 10, 4 )
NaN
> v = {{alias}}( 10, 5.5, 4 )
NaN
> v = {{alias}}( 10, 5, 4.5 )
NaN

> v = {{alias}}( NaN, 10, 4 )
NaN
> v = {{alias}}( 20, NaN, 4 )
NaN
> v = {{alias}}( 20, 10, NaN )
NaN

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* @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.
*/

// TypeScript Version: 4.1

/**
* Returns the differential entropy of a hypergeometric distribution.
*
* ## Notes
*
* - If provided a population size `N`, subpopulation size `K` or draws `n` which is not a nonnegative integer, the function returns `NaN`.
* - If the number of draws `n` or subpopulation size `K` exceed population size `N`, the function returns `NaN`.
*
* @param N - population size
* @param K - subpopulation size
* @param n - number of draws
* @returns entropy
*
* @example
* var v = entropy( 16, 11, 4 );
* // returns ~1.22
*
* @example
* var v = entropy( 2, 1, 1 );
* // returns ~0.693
*
* @example
* var v = entropy( 10, 5, 12 );
* // returns NaN
*
* @example
* var v = entropy( 10.3, 10, 4 );
* // returns NaN
*
* @example
* var v = entropy( 10, 5.5, 4 );
* // returns NaN
*
* @example
* var v = entropy( 10, 5, 4.5 );
* // returns NaN
*
* @example
* var v = entropy( NaN, 10, 4 );
* // returns NaN
*
* @example
* var v = entropy( 20, NaN, 4 );
* // returns NaN
*
* @example
* var v = entropy( 20, 10, NaN );
* // returns NaN
*/
declare function entropy( N: number, K: number, n: number ): number;


// EXPORTS //

export = entropy;
Loading