Skip to content
Closed
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,142 @@
<!--

@license Apache-2.0

Copyright (c) 2018 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) 2018 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 mean = 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 = mean( 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,72 @@
/**
* @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 pkg = require( './../package.json' ).name;


// VARIABLES //

var mean = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( mean instanceof Error )
};


// MAIN //

bench( pkg+'::native', 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 = mean( 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