diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/README.md b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/README.md new file mode 100644 index 000000000000..5d5076091fd1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/README.md @@ -0,0 +1,142 @@ + + +# Entropy + +> [Hypergeometric][hypergeometric-distribution] distribution [entropy][entropy]. + +The [entropy][entropy] (in nats) for a [hypergeometric][hypergeometric-distribution] random variable is + +
+ Entropy for a hypergeometric distribution. +
+
+ +where `N` is the population size, `K` is the subpopulation size, and `n` is the number of draws. + +
+ +## 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 +``` + +
+ + + +
+ +## 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 +``` + +
+ + + +
+ +
+ + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/benchmark.js new file mode 100644 index 000000000000..0f3e426d9996 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/benchmark.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/benchmark.native.js new file mode 100644 index 000000000000..24ce5169b513 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/benchmark.native.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/c/Makefile new file mode 100644 index 000000000000..a4bd7b38fd74 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/c/benchmark.c new file mode 100644 index 000000000000..ae9b73441779 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/c/benchmark.c @@ -0,0 +1,143 @@ +/** +* @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. +*/ + +#include "stdlib/stats/base/dists/hypergeometric/entropy.h" +#include +#include +#include +#include +#include +#include + +#define NAME "hypergeometric-entropy" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random integer on the interval [min,max]. +* +* @param min minimum value (inclusive) +* @param max maximum value (inclusive) +* @return random integer +*/ +static int32_t random_int( const int32_t min, const int32_t max ) { + int32_t v = rand() % ( max - min + 1 ); + return min + v; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + double elapsed; + int32_t N[ 100 ]; + int32_t K[ 100 ]; + int32_t n[ 100 ]; + double y; + double t; + int i; + + for ( i = 0; i < 100; i++ ) { + N[i] = random_int( 1, 100 ); + K[i] = random_int( 0, N[i] ); + n[i] = random_int( 0, N[i] ); + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + y = stdlib_base_dists_hypergeometric_entropy( N[ i % 100 ], K[ i % 100 ], n[ i % 100 ] ); + if ( y != y ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( y != y ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int i; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + printf( "# c::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", i+1 ); + } + print_summary( REPEATS, REPEATS ); +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/binding.gyp b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/binding.gyp new file mode 100644 index 000000000000..5ed67b6a6277 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/binding.gyp @@ -0,0 +1,108 @@ +# @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. + +{ + 'includes': [ + './include.gypi', + ], + 'variables': { + 'addon_target_name%': 'addon', + 'conditions': [ + [ + 'OS=="win"', + { + 'obj': 'obj', + }, + { + 'obj': 'o', + } + ], + ], + }, + 'targets': [ + { + 'target_name': '<(addon_target_name)', + 'dependencies': [], + 'include_dirs': [ + '<@(include_dirs)', + ], + 'sources': [ + '<@(src_files)', + ], + 'link_settings': { + 'libraries': [ + '<@(libraries)', + ], + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + 'cflags': [ + '-Wall', + '-O3', + ], + 'cflags_c': [ + '-std=c99', + ], + 'cflags_cpp': [ + '-std=c++11', + ], + 'ldflags': [], + 'conditions': [ + [ + 'OS=="mac"', + { + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], + [ + 'OS!="win"', + { + 'cflags': [ + '-fPIC', + ], + }, + ], + ], + }, + { + 'target_name': 'copy_addon', + 'type': 'none', + 'dependencies': [ + '<(addon_target_name)', + ], + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + 'inputs': [], + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], + }, + ], +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/img/equation_hypogeometric_entropy.svg b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/img/equation_hypogeometric_entropy.svg new file mode 100644 index 000000000000..6cfdcb349ad1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/img/equation_hypogeometric_entropy.svg @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/repl.txt new file mode 100644 index 000000000000..15449e093730 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/repl.txt @@ -0,0 +1,56 @@ +{{alias}}( N, K, n ) + Evaluates the entropy for a hypergeometric distribution with population + size `N`, subpopulation size `K`, and number of draws `n`. + + 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 subpopulation size `K` exceeds 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.216 + > v = {{alias}}( 2, 1, 1 ) + ~0.693 + + > v = {{alias}}( NaN, 10, 5 ) + NaN + > v = {{alias}}( 20, NaN, 5 ) + NaN + > v = {{alias}}( 20, 10, NaN ) + NaN + + > v = {{alias}}( 10.5, 5, 2 ) + NaN + > v = {{alias}}( 10, 1.5, 2 ) + NaN + > v = {{alias}}( 10, 5, -2.0 ) + NaN + > v = {{alias}}( 10, 5, 12 ) + NaN + > v = {{alias}}( 10, 12, 5 ) + NaN + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/types/index.d.ts new file mode 100644 index 000000000000..412311cbb7e5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/types/index.d.ts @@ -0,0 +1,75 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Returns the 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` exceeds 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.2156868611027067 +* +* @example +* var v = entropy( 2, 1, 1 ); +* // returns 0.6931471805599453 +* +* @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; diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/types/test.ts new file mode 100644 index 000000000000..0e2035e085b3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @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. +*/ + +import entropy = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + entropy( 16, 12, 5 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than three numbers... +{ + entropy( true, 3, 2 ); // $ExpectError + entropy( false, 4, 2 ); // $ExpectError + entropy( '5', 3, 2 ); // $ExpectError + entropy( [], 1, 1 ); // $ExpectError + entropy( {}, 2, 1 ); // $ExpectError + entropy( ( x: number ): number => x, 2, 1 ); // $ExpectError + + entropy( 90, true, 12 ); // $ExpectError + entropy( 90, false, 12 ); // $ExpectError + entropy( 50, '5', 8 ); // $ExpectError + entropy( 80, [], 10 ); // $ExpectError + entropy( 90, {}, 12 ); // $ExpectError + entropy( 80, ( x: number ): number => x, 12 ); // $ExpectError + + entropy( 90, 18, true ); // $ExpectError + entropy( 90, 18, false ); // $ExpectError + entropy( 50, 10, '5' ); // $ExpectError + entropy( 80, 16, [] ); // $ExpectError + entropy( 90, 18, {} ); // $ExpectError + entropy( 80, 16, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + entropy(); // $ExpectError + entropy( 30 ); // $ExpectError + entropy( 30, 6 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/examples/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/examples/c/Makefile new file mode 100644 index 000000000000..25ced822f96a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/examples/c/example.c b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/examples/c/example.c new file mode 100644 index 000000000000..b5e894243424 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/examples/c/example.c @@ -0,0 +1,43 @@ +/** +* @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. +*/ + +#include "stdlib/stats/base/dists/hypergeometric/entropy.h" +#include +#include +#include + +static int32_t random_int( const int32_t min, const int32_t max ) { + int32_t v = rand() % ( max - min + 1 ); + return min + v; +} + +int main( void ) { + int32_t N; + int32_t K; + int32_t n; + double y; + int i; + + for ( i = 0; i < 10; i++ ) { + N = random_int( 1, 100 ); + K = random_int( 0, N ); + n = random_int( 0, N ); + y = stdlib_base_dists_hypergeometric_entropy( N, K, n ); + printf( "N: %d, K: %d, n: %d, H(X;N,K,n): %.4f\n", N, K, n, y ); + } +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/examples/index.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/examples/index.js new file mode 100644 index 000000000000..c5a090c93c96 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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'; + +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var entropy = require( './../lib' ); + +var v; +var i; +var N; +var K; +var n; + +for ( i = 0; i < 10; i++ ) { + N = round( randu() * 100 ); + K = round( randu() * N ); + n = round( randu() * N ); + v = entropy( N, K, n ); + console.log( 'N: %d, K: %d, n: %d, H(X;N,K,n): %d', N, K, n, v.toFixed( 4 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/include.gypi b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/include.gypi new file mode 100644 index 000000000000..15aa04ad4f3b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/include.gypi @@ -0,0 +1,35 @@ +# @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. + +{ + 'variables': { + 'src_dir': './src', + 'include_dirs': [ + ' + +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Returns the entropy of a hypergeometric distribution. +*/ +double stdlib_base_dists_hypergeometric_entropy( const double N, const double K, const double n ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_STATS_BASE_DISTS_HYPERGEOMETRIC_ENTROPY_H diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/index.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/index.js new file mode 100644 index 000000000000..379f61e6bf7a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/index.js @@ -0,0 +1,43 @@ +/** +* @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'; + +/** +* Hypergeometric distribution entropy. +* +* @module @stdlib/stats/base/dists/hypergeometric/entropy +* +* @example +* var entropy = require( '@stdlib/stats/base/dists/hypergeometric/entropy' ); +* +* var v = entropy( 16, 11, 4 ); +* // returns ~1.216 +* +* v = entropy( 2, 1, 1 ); +* // returns ~0.693 +*/ + +// MODULES // + +var entropy = require( './main.js' ); + + +// EXPORTS // + +module.exports = entropy; diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/main.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/main.js new file mode 100644 index 000000000000..9d6c520a6253 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/main.js @@ -0,0 +1,96 @@ +/** +* @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 PINF = require( '@stdlib/constants/float64/pinf' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isNonNegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' ); +var ln = require( '@stdlib/math/base/special/ln' ); +var pmf = require( '@stdlib/stats/base/dists/hypergeometric/pmf' ); +var accumulator = require( '@stdlib/stats/incr/sum' ); // Import accumulator + + +// MAIN // + +/** +* Returns the entropy of a hypergeometric distribution. +* +* @param {NonNegativeInteger} N - population size +* @param {NonNegativeInteger} K - subpopulation size +* @param {NonNegativeInteger} n - number of draws +* @returns {number} entropy +* +* @example +* var h = entropy( 16, 11, 4 ); +* // returns ~1.216 +* +* @example +* var h = entropy( 2, 1, 1 ); +* // returns ~0.693 +* +* @example +* var h = entropy( 10, 5, 12 ); +* // returns NaN +*/ +function entropy( N, K, n ) { + var acc; + var min; + var max; + var k; + var p; + + if ( + isnan( N ) || + isnan( K ) || + isnan( n ) + ) { + return NaN; + } + if ( + !isNonNegativeInteger( N ) || + !isNonNegativeInteger( K ) || + !isNonNegativeInteger( n ) || + N === PINF || + K === PINF || + K > N || + n > N + ) { + return NaN; + } + + min = ( (n + K - N ) > 0 ) ? ( n + K - N ) : 0; + max = ( n < K ) ? n : K; + + acc = accumulator(); + + for ( k = min; k <= max; k++ ) { + p = pmf( k, N, K, n ); + if ( p > 0.0 ) { + acc( -p * ln( p ) ); + } + } + return acc(); +} + + +// EXPORTS // + +module.exports = entropy; diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/native.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/native.js new file mode 100644 index 000000000000..5b9f762f73ff --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/native.js @@ -0,0 +1,52 @@ +/** +* @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 addon = require( './../src/addon.node' ); + + +// MAIN // + +/** +* Returns the entropy of a hypergeometric distribution. +* +* @private +* @param {NonNegativeInteger} N - population size +* @param {NonNegativeInteger} K - subpopulation size +* @param {NonNegativeInteger} n - number of draws +* @returns {number} entropy +* +* @example +* var v = entropy( 16, 11, 4 ); +* // returns ~1.216 +* +* @example +* var v = entropy( 2, 1, 1 ); +* // returns ~0.693 +*/ +function entropy( N, K, n ) { + return addon( N, K, n ); +} + + +// EXPORTS // + +module.exports = entropy; diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/manifest.json new file mode 100644 index 000000000000..382e476a4eed --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/manifest.json @@ -0,0 +1,71 @@ +{ + "options": { + "task": "build", + "wasm": false + }, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "task": "build", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/napi/ternary" + ] + }, + { + "task": "benchmark", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + }, + { + "task": "examples", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + } + ] +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/package.json b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/package.json new file mode 100644 index 000000000000..e3e959ae16d9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/base/dists/hypergeometric/entropy", + "version": "0.0.0", + "description": "Hypergeometric distribution entropy.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "gypfile": true, + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "src": "./src", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "distribution", + "dist", + "parameter", + "discrete", + "hypergeometric", + "univariate", + "entropy" + ] +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/src/Makefile b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/src/Makefile new file mode 100644 index 000000000000..7733b6180cb4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + + +# RULES # + +#/ +# Removes generated files for building an add-on. +# +# @example +# make clean-addon +#/ +clean-addon: + $(QUIET) -rm -f *.o *.node + +.PHONY: clean-addon + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: clean-addon + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/src/addon.c b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/src/addon.c new file mode 100644 index 000000000000..a5337c29f41a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/src/addon.c @@ -0,0 +1,23 @@ +/** +* @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. +*/ + +#include "stdlib/stats/base/dists/hypergeometric/entropy.h" +#include "stdlib/math/base/napi/ternary.h" + +// cppcheck-suppress shadowFunction +STDLIB_MATH_BASE_NAPI_MODULE_III_D( stdlib_base_dists_hypergeometric_entropy ) diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/src/main.c new file mode 100644 index 000000000000..d8330cbcdab0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/src/main.c @@ -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. +*/ + +#include "stdlib/stats/base/dists/hypergeometric/entropy.h" +#include +#include +#include + +static double log_binomial( int32_t n, int32_t k ) { + return lgamma( n + 1.0 ) - lgamma( k + 1.0 ) - lgamma( n - k + 1.0 ); +} + +static double hypergeometric_pmf( int32_t k, int32_t N, int32_t K, int32_t n ) { + return exp( log_binomial( K, k ) + log_binomial( N - K, n - k ) - log_binomial( N, n ) ); +} + +/** +* Returns the entropy of a hypergeometric distribution. +* +* @param N population size +* @param K subpopulation size +* @param n number of draws +* @return entropy +*/ +double stdlib_base_dists_hypergeometric_entropy( const double N, const double K, const double n ) { + int32_t N_int; + int32_t K_int; + int32_t n_int; + + if ( isnan( N ) || isnan( K ) || isnan( n ) ) { + return 0.0 / 0.0; // NaN + } + + if ( floor( N ) != N || floor( K ) != K || floor( n ) != n ) { + return 0.0 / 0.0; // NaN + } + + N_int = (int32_t) N; + K_int = (int32_t) K; + n_int = (int32_t) n; + + if ( N_int < 0 || K_int < 0 || n_int < 0 || K_int > N_int || n_int > N_int ) { + return 0.0 / 0.0; // NaN + } + + int32_t min = ( n + K - N > 0 ) ? ( n + K - N ) : 0; + int32_t max = ( n < K ) ? n : K; + + double H = 0.0; + for ( int32_t k = min; k <= max; k++ ) { + double p = hypergeometric_pmf( k, N, K, n ); + if ( p > 0.0 ) { + H -= p * log( p ); + } + } + return H; +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/fixtures/julia/REQUIRE b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/fixtures/julia/REQUIRE new file mode 100644 index 000000000000..98be20b58ed3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/fixtures/julia/REQUIRE @@ -0,0 +1,3 @@ +Distributions 0.23.8 +julia 1.5 +JSON 0.21 diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/fixtures/julia/data.json b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/fixtures/julia/data.json new file mode 100644 index 000000000000..b676b26dbe49 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/fixtures/julia/data.json @@ -0,0 +1 @@ +{"expected":[0.9605691359496381,2.36022301542335,2.3109206816828194,2.4475318680028515,2.6715398603555514,1.0558304681057127,2.169949020906224,2.1044232178821045,2.343031681075205,2.0774570789871687,2.6210407891017278,1.5937553893178027,1.5734560450295227,2.4385796506160746,1.9940966299257414,1.5532319642804908,1.5902519548351852,2.406928836025579,1.6548923265006898,2.4705696190862434,1.403449215345543,2.608658529914859,2.024375790087892,0.47413931305783763,2.2802727310640862,1.9208073707839328,1.3438421885395349,-0.0,1.5372057905553902,1.3241503191598174,2.2090976552200146,0.686961576597323,2.1593429557620163,1.4354700698325267,1.6410816884282264,2.6005518275965547,1.4719087465540202,1.7357252896272457,2.3981556931342056,2.3335486982916285,1.9237933952987074,1.9440193472399103,1.6410816884282264,0.5163615681440755,2.3568158257847225,2.155448612252608,1.0155879303656914,2.172551987049449,2.121161362239099,1.8937699212001955,2.048104440725605,2.451969341392494,2.0316242521123753,1.4881393032186538,2.2294844886033247,1.6637392276894984,2.3223583749714787,0.9755195263052014,2.574906644243393,2.1731708164537955,1.3692384347131124,2.4892377688029295,1.470960204844828,1.9226398519238475,2.146432996322684,2.697806403143644,2.533711626595903,1.1423430784435036,1.1672296255371846,1.45958958467898,2.241770941138564,1.8961940398404766,1.5991104043055324,1.3666293292913687,1.6910228477156346,0.9242212350762793,1.8087297960360136,-0.0,2.5864303579974557,2.369551238597645,0.6700089798274271,2.2651406935861207,1.1655067789346878,2.283063240817543,1.0210572481905098,1.7212973816077262,0.6931471805599454,2.141678553423554,1.4620612979243952,2.0416491482535806,2.1631937498973013,2.0186997913780296,1.801108552652871,1.6781553412455192,1.6806338005588422,1.5351944584549344,2.5547559587139057,1.7379832235966675,0.47427179388730223,1.1873052356728573],"N":[199.0,200.0,140.0,158.0,212.0,159.0,92.0,118.0,161.0,113.0,211.0,189.0,111.0,137.0,174.0,42.0,59.0,141.0,46.0,133.0,22.0,187.0,69.0,121.0,109.0,76.0,181.0,104.0,55.0,143.0,83.0,27.0,165.0,38.0,25.0,200.0,73.0,203.0,187.0,207.0,204.0,78.0,25.0,203.0,192.0,107.0,100.0,82.0,184.0,74.0,79.0,162.0,66.0,93.0,215.0,27.0,170.0,198.0,179.0,134.0,37.0,153.0,90.0,44.0,144.0,219.0,163.0,24.0,38.0,30.0,160.0,47.0,44.0,68.0,124.0,66.0,203.0,34.0,199.0,109.0,28.0,123.0,66.0,189.0,55.0,203.0,132.0,116.0,66.0,75.0,106.0,102.0,40.0,183.0,108.0,110.0,156.0,35.0,178.0,121.0],"K":[189.0,169.0,40.0,106.0,135.0,18.0,58.0,41.0,41.0,88.0,148.0,8.0,6.0,50.0,160.0,7.0,10.0,40.0,10.0,64.0,9.0,112.0,37.0,1.0,38.0,38.0,166.0,0.0,21.0,4.0,37.0,12.0,65.0,11.0,13.0,95.0,15.0,186.0,131.0,174.0,182.0,17.0,10.0,202.0,128.0,59.0,9.0,29.0,112.0,23.0,32.0,66.0,47.0,17.0,162.0,17.0,42.0,3.0,118.0,111.0,28.0,86.0,55.0,25.0,51.0,100.0,58.0,21.0,7.0,6.0,68.0,22.0,14.0,4.0,8.0,54.0,193.0,10.0,124.0,46.0,11.0,28.0,3.0,26.0,49.0,36.0,1.0,75.0,19.0,55.0,81.0,59.0,12.0,87.0,12.0,6.0,75.0,22.0,172.0,6.0],"n":[11.0,96.0,99.0,53.0,114.0,153.0,65.0,21.0,114.0,84.0,111.0,49.0,45.0,82.0,100.0,28.0,13.0,71.0,33.0,76.0,5.0,113.0,18.0,99.0,37.0,63.0,167.0,68.0,6.0,48.0,52.0,26.0,144.0,32.0,15.0,139.0,65.0,30.0,44.0,137.0,35.0,24.0,13.0,160.0,156.0,22.0,7.0,32.0,165.0,59.0,60.0,45.0,36.0,84.0,32.0,13.0,126.0,158.0,86.0,51.0,31.0,52.0,5.0,20.0,22.0,84.0,90.0,8.0,33.0,10.0,135.0,15.0,36.0,28.0,45.0,3.0,73.0,0.0,134.0,52.0,1.0,65.0,22.0,92.0,49.0,14.0,66.0,93.0,60.0,28.0,68.0,16.0,21.0,176.0,86.0,72.0,86.0,23.0,5.0,18.0]} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/fixtures/julia/runner.jl b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/fixtures/julia/runner.jl new file mode 100644 index 000000000000..af39153ae635 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/fixtures/julia/runner.jl @@ -0,0 +1,77 @@ +#!/usr/bin/env julia +# +# @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. + +import Distributions: entropy, Hypergeometric +import JSON + +""" + gen( N, K, n, name ) + +Generate fixture data and write to file. + +# Arguments + +* `N`: population size +* `K`: subpopulation size +* `n`: number of draws +* `name::AbstractString`: output filename + +# Examples + +``` julia +julia> N = round.( ( rand( 1000 ) .* 200 ) .+ 20 ); +julia> K = round.( rand( 1000 ) .* N ); +julia> n = round.( rand( 1000 ) .* K ); +julia> gen( N, K, n, "data.json" ); +``` +""" +function gen( N, K, n, name ) + z = Array{Float64}( undef, length(N) ); + for i in eachindex(N) + z[ i ] = entropy( Hypergeometric( K[i], N[i] - K[i], n[i] ) ); + end + + # Store data to be written to file as a collection: + data = Dict([ + ("N", N), + ("K", K), + ("n", n), + ("expected", z) + ]); + + # Based on the script directory, create an output filepath: + filepath = joinpath( dir, name ); + + # Write the data to the output filepath as JSON: + outfile = open( filepath, "w" ); + write( outfile, JSON.json(data) ); + write( outfile, "\n" ); + close( outfile ); +end + +# Get the filename: +file = @__FILE__; + +# Extract the directory in which this file resides: +dir = dirname( file ); + +# Generate fixtures: +N = round.( ( rand( 100 ) .* 200 ) .+ 20 ); +K = round.( rand( 100 ) .* N ); +n = round.( rand( 100 ) .* N ); +gen( N, K, n, "data.json" ); diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/test.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/test.js new file mode 100644 index 000000000000..d0dc76563d7d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/test.js @@ -0,0 +1,171 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var entropy = require( './../lib' ); + + +// FIXTURES // + +var data = require( './fixtures/julia/data.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof entropy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if provided `NaN` for any parameter, the function returns `NaN`', function test( t ) { + var v = entropy( NaN, 10, 5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = entropy( 20, NaN, 5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = entropy( 20, 10, NaN ); + t.equal( isnan( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'if provided a population size `N` which is not a nonnegative integer, the function returns `NaN`', function test( t ) { + var v; + + v = entropy( 10.5, 5, 2 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = entropy( -2, 5, 2 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'if provided a subpopulation size `K` which is not a nonnegative integer, the function returns `NaN`', function test( t ) { + var v; + + v = entropy( 10, 5.5, 2 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = entropy( 10, -2, 2 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'if provided a number of draws `n` which is not a nonnegative integer, the function returns `NaN`', function test( t ) { + var v; + + v = entropy( 10, 5, 2.5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = entropy( 10, 5, -2 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'if the number of draws `n` exceeds the population size `N`, the function returns `NaN`', function test( t ) { + var v = entropy( 10, 5, 11 ); + t.equal( isnan( v ), true, 'returns NaN' ); + t.end(); +}); + +tape( 'if the subpopulation size `K` exceeds the population size `N`, the function returns `NaN`', function test( t ) { + var v = entropy( 10, 11, 5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + t.end(); +}); + +tape( 'the function computes the entropy of a hypergeometric distribution', function test( t ) { + var expected; + var delta; + var tol; + var v; + + // Case 1: N=2, K=1, n=1 (Like flipping a coin: 50/50 chance of getting the special item) + v = entropy( 2, 1, 1 ); + expected = 0.69314718056; + delta = abs( v - expected ); + tol = 1.0e-9; + t.ok( delta < tol, 'within tolerance. v: ' + v + '. expected: ' + expected ); + + // Case 2: N=10, K=5, n=1 (Draw 1 from 50/50 mix) -> Same entropy + v = entropy( 10, 5, 1 ); + expected = 0.69314718056; + delta = abs( v - expected ); + t.ok( delta < tol, 'within tolerance. v: ' + v + '. expected: ' + expected ); + + // Case 3: N=16, K=11, n=4 (From your documentation example) + v = entropy( 16, 11, 4 ); + expected = 1.216395; + delta = abs( v - expected ); + tol = 1.0e-5; + t.ok( delta < tol, 'within tolerance. v: ' + v + '. expected: ' + expected ); + + t.end(); +}); + +tape( 'the function matches expected results generated by Julia', function test( t ) { + var expected; + var delta; + var tol; + var v; + var N; + var K; + var n; + var i; + + for ( i = 0; i < data.expected.length; i++ ) { + N = data.N[ i ]; + K = data.K[ i ]; + n = data.n[ i ]; + expected = data.expected[ i ]; + + v = entropy( N, K, n ); + + delta = abs( v - expected ); + tol = 50.0 * EPS; + + t.ok( delta <= tol, 'within tolerance. N: ' + N + '. K: ' + K + '. n: ' + n + '. v: ' + v + '. expected: ' + expected ); + } + t.end(); +}); + +tape( 'if the outcome is certain (only one possible value for k), the entropy is 0', function test( t ) { + var v; + + // If K=N, every item is special. If we draw n, we ALWAYS get n special items. k=n is 100%. + v = entropy( 10, 10, 5 ); + t.strictEqual( v, 0.0, 'returns 0' ); + + // If K=0, no item is special. We ALWAYS get 0 special items. k=0 is 100%. + v = entropy( 10, 0, 5 ); + t.strictEqual( v, 0.0, 'returns 0' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/test.native.js new file mode 100644 index 000000000000..34608042f282 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/test.native.js @@ -0,0 +1,95 @@ +/** +* @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 tape = require( 'tape' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); + + +// FIXTURES // + +var data = require( './fixtures/julia/data.json' ); + + +// VARIABLES // + +var entropy = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( entropy instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof entropy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) { + var v = entropy( NaN, 10, 5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = entropy( 20, NaN, 5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = entropy( 20, 10, NaN ); + t.equal( isnan( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'if provided an `N` which is not a nonnegative integer, the function returns `NaN`', opts, function test( t ) { + var v = entropy( -2, 4, 2 ); + t.strictEqual( isnan( v ), true, 'returns NaN' ); + t.end(); +}); + +tape( 'the function matches expected results generated by Julia', opts, function test( t ) { + var expected; + var delta; + var tol; + var v; + var N; + var K; + var n; + var i; + + for ( i = 0; i < data.expected.length; i++ ) { + N = data.N[ i ]; + K = data.K[ i ]; + n = data.n[ i ]; + expected = data.expected[ i ]; + + v = entropy( N, K, n ); + + delta = abs( v - expected ); + tol = 50.0 * EPS; + + t.ok( delta <= tol, 'within tolerance. N: ' + N + '. K: ' + K + '. n: ' + n + '. v: ' + v + '. expected: ' + expected ); + } + t.end(); +});