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..334beaea7df8 --- /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..874622842114 --- /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) 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(); +}); 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..42ca4ee74ddc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/benchmark/benchmark.native.js @@ -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(); +}); 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..c92bf10fcdc2 --- /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/mean.h" +#include +#include +#include +#include +#include +#include + +#define NAME "hypergeometric-mean" +#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_mean( 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..68a1ca11d160 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/binding.gyp @@ -0,0 +1,170 @@ +# @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. + +# A `.gyp` file for building a Node.js native add-on. +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # List of files to include in this file: + 'includes': [ + './include.gypi', + ], + + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Target name should match the add-on export name: + 'addon_target_name%': 'addon', + + # Set variables based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + { + # Define the object file suffix: + 'obj': 'obj', + }, + { + # Define the object file suffix: + 'obj': 'o', + } + ], # end condition (OS=="win") + ], # end conditions + }, # end variables + + # Define compile targets: + 'targets': [ + + # Target to generate an add-on: + { + # The target name should match the add-on export name: + 'target_name': '<(addon_target_name)', + + # Define dependencies: + 'dependencies': [], + + # Define directories which contain relevant include headers: + 'include_dirs': [ + # Local include directory: + '<@(include_dirs)', + ], + + # List of source files: + 'sources': [ + '<@(src_files)', + ], + + # Settings which should be applied when a target's object files are used as linker input: + 'link_settings': { + # Define libraries: + 'libraries': [ + '<@(libraries)', + ], + + # Define library directories: + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + + # C/C++ compiler flags: + 'cflags': [ + # Enable commonly used warning options: + '-Wall', + + # Aggressive optimization: + '-O3', + ], + + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', + ], + + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], + + # Apply conditions based on the host OS: + 'conditions': [ + [ + 'OS=="mac"', + { + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], # end condition (OS=="mac") + [ + 'OS!="win"', + { + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], + }, + ], # end condition (OS!="win") + ], # end conditions + }, # end target <(addon_target_name) + + # Target to copy a generated add-on to a standard location: + { + 'target_name': 'copy_addon', + + # Declare that the output of this target is not linked: + 'type': 'none', + + # Define dependencies: + 'dependencies': [ + # Require that the add-on be generated before building this target: + '<(addon_target_name)', + ], + + # Define a list of actions: + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], + + # Declare the expected outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Define the command-line invocation: + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target copy_addon + ], # end targets +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/img/equation_hypergeometric_mean.svg b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/img/equation_hypergeometric_mean.svg new file mode 100644 index 000000000000..6cfaec4ed66e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/docs/img/equation_hypergeometric_mean.svg @@ -0,0 +1,30 @@ + +double-struck upper E left-bracket upper X right-bracket equals n StartFraction upper K Over upper N EndFraction + + + \ 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..dc69409e8e7f --- /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, 2 ) + NaN + > v = {{alias}}( 20, NaN, 5, 2 ) + NaN + > v = {{alias}}( 20, 10, NaN, 2 ) + 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..77a30496748f --- /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) 2019 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..98827a2d40bb --- /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) 2019 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 mean = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + mean( 16, 12, 5 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than three numbers... +{ + mean( true, 3, 2 ); // $ExpectError + mean( false, 4, 2 ); // $ExpectError + mean( '5', 3, 2 ); // $ExpectError + mean( [], 1, 1 ); // $ExpectError + mean( {}, 2, 1 ); // $ExpectError + mean( ( x: number ): number => x, 2, 1 ); // $ExpectError + + mean( 90, true, 12 ); // $ExpectError + mean( 90, false, 12 ); // $ExpectError + mean( 50, '5', 8 ); // $ExpectError + mean( 80, [], 10 ); // $ExpectError + mean( 90, {}, 12 ); // $ExpectError + mean( 80, ( x: number ): number => x, 12 ); // $ExpectError + + mean( 90, 18, true ); // $ExpectError + mean( 90, 18, false ); // $ExpectError + mean( 50, 10, '5' ); // $ExpectError + mean( 80, 16, [] ); // $ExpectError + mean( 90, 18, {} ); // $ExpectError + mean( 80, 16, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + mean(); // $ExpectError + mean( 30 ); // $ExpectError + mean( 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..c4af680314c4 --- /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/mean.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, 20 ); + K = random_int( 0, N ); + n = random_int( 0, K ); + y = stdlib_base_dists_hypergeometric_mean( N, K, n ); + printf( "N: %d, K: %d, n: %d, E(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..8198166bca4b --- /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) 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'; + +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var mean = require( './../lib' ); + +var v; +var i; +var N; +var K; +var n; + +for ( i = 0; i < 10; i++ ) { + N = round( randu() * 20 ); + K = round( randu() * N ); + n = round( randu() * K ); + v = mean( N, K, n ); + console.log( 'N: %d, K: %d, n: %d, E(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..ecfaf82a3279 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/include.gypi @@ -0,0 +1,53 @@ +# @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. + +# A GYP include file for building a Node.js native add-on. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + ' + +/* +* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Returns the expected value of a hypergeometric distribution. +*/ +double stdlib_base_dists_hypergeometric_mean( const int32_t N, const int32_t K, const int32_t n ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_STATS_BASE_DISTS_HYPERGEOMETRIC_MEAN_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..e7ed3551ffa7 --- /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) 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'; + +/** +* Hypergeometric distribution expected value. +* +* @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..c2549f2dc2cf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/main.js @@ -0,0 +1,95 @@ +/** +* @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 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' ); + + +// MAIN // + +/** +* Returns the Shannon 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 min; + var max; + var H; + 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; + } + + // Support range: + min = ((n + K - N) > 0) ? (n + K - N) : 0; + max = (n < K) ? n : K; + + H = 0.0; + for (k = min; k <= max; k++) { + p = pmf(k, N, K, n); + if (p > 0.0) { + H -= p * ln(p); + } + } + return H; +} + + +// 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..13ee8e82426a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/lib/native.js @@ -0,0 +1,60 @@ +/** +* @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 expected value of a hypergeometric distribution. +* +* @private +* @param {NonNegativeInteger} N - population size +* @param {NonNegativeInteger} K - subpopulation size +* @param {NonNegativeInteger} n - number of draws +* @returns {NonNegativeNumber} expected value +* +* @example +* var v = mean( 16, 11, 4 ); +* // returns 2.75 +* +* @example +* var v = mean( 2, 1, 1 ); +* // returns 0.5 +* +* @example +* var v = mean( 10, 5, 12 ); +* // returns NaN +* +* @example +* var v = mean( 10, -2, 4 ); +* // returns NaN +*/ +function mean( N, K, n ) { + return addon( N, K, n ); +} + + +// EXPORTS // + +module.exports = mean; 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..cc767c51da3b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/stats/base/dists/hypergeometric/mean", + "version": "0.0.0", + "description": "Hypergeometric distribution expected value.", + "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" + ] +} 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..6c087587f6d5 --- /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/mean.h" +#include "stdlib/math/base/napi/ternary.h" + +// cppcheck-suppress shadowFunction +STDLIB_MATH_BASE_NAPI_MODULE_III_D( stdlib_base_dists_hypergeometric_mean ) 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..509428ce6853 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/src/main.c @@ -0,0 +1,39 @@ +/** +* @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/mean.h" +#include + +/** +* Returns the expected value of a hypergeometric distribution. +* +* @param N population size +* @param K subpopulation size +* @param n number of draws +* @return expected value +* +* @example +* double v = stdlib_base_dists_hypergeometric_mean( 16, 11, 4 ); +* // returns 2.75 +*/ +double stdlib_base_dists_hypergeometric_mean( const int32_t N, const int32_t K, const int32_t n ) { + if ( N < 0 || K < 0 || n < 0 || K > N || n > N ) { + return 0.0/0.0; // NaN + } + return ( (double)n * (double)K ) / (double)N; +} 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..b1c6db48482f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/fixtures/julia/data.json @@ -0,0 +1 @@ +{"expected":[1.0756302521008403,26.704545454545453,28.983050847457626,2.4324324324324325,61.25714285714286,26.86315789473684,14.795454545454545,34.5,2.9293478260869565,17.68421052631579,13.538461538461538,7.5,0.011764705882352941,0.0,143.373786407767,19.804878048780488,0.8210526315789474,25.23076923076923,57.6,5.6875,74.16666666666667,18.182926829268293,34.92537313432836,0.18181818181818182,2.234375,6.222222222222222,37.92045454545455,66.45161290322581,13.732394366197184,6.6976744186046515,0.0,19.369127516778523,0.4976958525345622,31.423076923076923,5.714285714285714,15.19047619047619,44.56818181818182,8.32258064516129,3.392857142857143,13.023255813953488,5.47945205479452,106.69626168224299,11.878787878787879,0.16,2.508771929824561,10.458100558659218,0.35294117647058826,0.11428571428571428,53.81725888324873,5.76,16.944444444444443,99.0,4.0,5.078431372549019,31.989637305699482,12.235294117647058,10.54054054054054,1.3928571428571428,55.660377358490564,0.9608938547486033,40.994594594594595,60.07692307692308,147.05825242718447,13.16,3.0,32.28310502283105,0.5357142857142857,5.67816091954023,31.042857142857144,15.422680412371134,0.2898550724637681,7.4,18.76,40.14746543778802,11.088372093023256,17.445454545454545,10.427586206896551,56.77777777777778,126.98019801980197,25.905797101449274,0.0,59.47959183673469,4.583333333333333,0.4444444444444444,0.28,9.526717557251908,109.32057416267942,26.367816091954023,0.13333333333333333,3.5675675675675675,30.23404255319149,25.333333333333332,12.566137566137566,0.18274111675126903,0.1917808219178082,2.857142857142857,8.846153846153847,1.9130434782608696,19.03,4.170731707317073],"N":[119.0,176.0,59.0,37.0,70.0,95.0,88.0,210.0,184.0,209.0,39.0,62.0,85.0,49.0,206.0,205.0,190.0,65.0,150.0,32.0,120.0,164.0,67.0,66.0,64.0,27.0,88.0,124.0,142.0,43.0,25.0,149.0,217.0,130.0,35.0,84.0,176.0,93.0,196.0,129.0,219.0,214.0,165.0,25.0,114.0,179.0,187.0,35.0,197.0,200.0,162.0,121.0,28.0,102.0,193.0,102.0,37.0,112.0,159.0,179.0,185.0,78.0,206.0,200.0,28.0,219.0,28.0,87.0,140.0,97.0,69.0,55.0,150.0,217.0,215.0,110.0,145.0,189.0,202.0,138.0,187.0,196.0,72.0,27.0,50.0,131.0,209.0,87.0,150.0,111.0,188.0,81.0,189.0,197.0,73.0,49.0,78.0,138.0,200.0,164.0],"K":[16.0,100.0,45.0,15.0,67.0,58.0,42.0,161.0,77.0,66.0,24.0,31.0,1.0,1.0,179.0,145.0,52.0,41.0,120.0,26.0,100.0,71.0,60.0,4.0,13.0,14.0,71.0,103.0,65.0,24.0,3.0,74.0,18.0,95.0,20.0,44.0,148.0,86.0,35.0,112.0,48.0,177.0,70.0,2.0,26.0,52.0,11.0,4.0,171.0,128.0,61.0,121.0,16.0,37.0,98.0,39.0,30.0,26.0,150.0,86.0,96.0,71.0,187.0,56.0,12.0,101.0,5.0,26.0,82.0,44.0,10.0,37.0,67.0,132.0,149.0,101.0,63.0,147.0,171.0,65.0,69.0,174.0,66.0,4.0,7.0,39.0,204.0,74.0,5.0,36.0,98.0,76.0,95.0,6.0,7.0,35.0,30.0,24.0,173.0,57.0],"n":[8.0,47.0,38.0,6.0,64.0,44.0,31.0,45.0,7.0,56.0,22.0,15.0,1.0,0.0,165.0,28.0,3.0,40.0,72.0,7.0,89.0,42.0,39.0,3.0,11.0,12.0,47.0,80.0,30.0,12.0,0.0,39.0,6.0,43.0,10.0,29.0,53.0,9.0,19.0,15.0,25.0,129.0,28.0,2.0,11.0,36.0,6.0,1.0,62.0,9.0,45.0,99.0,7.0,14.0,63.0,32.0,13.0,6.0,59.0,2.0,79.0,66.0,162.0,47.0,7.0,70.0,3.0,19.0,53.0,34.0,2.0,11.0,42.0,66.0,16.0,19.0,24.0,73.0,150.0,55.0,0.0,67.0,5.0,3.0,2.0,32.0,112.0,31.0,4.0,11.0,58.0,27.0,25.0,6.0,2.0,4.0,23.0,11.0,22.0,12.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..08ada4ef2bb8 --- /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) 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. + +import Distributions: mean, 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 ] = mean( 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 ) .* K ); +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..3950e7a91a5b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/test.js @@ -0,0 +1,139 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var entropy = require( './../lib' ).default; + + +// 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( '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..7c5c23db5e65 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/entropy/test/test.native.js @@ -0,0 +1,113 @@ +/** +* @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 mean = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( mean instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if provided an `N` which is not a nonnegative integer, the function returns `NaN`', opts, function test( t ) { + var v; + + v = mean( -2, 4, 2 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = mean( -1, 4, 2 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `K` which is not a nonnegative integer, the function returns `NaN`', opts, function test( t ) { + var y; + + y = mean( 20, -2, 10 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = mean( 20, -1, 10 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `n` which is not a nonnegative integer, the function returns `NaN`', opts, function test( t ) { + var y; + + y = mean( 40, 20, -2 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = mean( 40, 20, -1 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the mean of a hypergeometric distribution', opts, function test( t ) { + var expected; + var delta; + var tol; + var N; + var K; + var n; + var y; + var i; + + expected = data.expected; + N = data.N; + K = data.K; + n = data.n; + for ( i = 0; i < n.length; i++ ) { + y = mean( N[i], K[i], n[i] ); + if ( y === expected[i] ) { + t.strictEqual( y, expected[i], 'N: '+N[i]+', K: '+K[i]+', n: '+n[i]+', y: '+y+', expected: '+expected[i] ); + } else { + delta = abs( y - expected[ i ] ); + tol = 1.0 * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. N: '+N[i]+'. K: '+K[i]+'. n: '+n[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' ); + } + } + t.end(); +});