From 1a50cdd5ad25656d54fd69896891169e45f1aed8 Mon Sep 17 00:00:00 2001 From: Vishal Gaikwad Date: Fri, 13 Mar 2026 03:30:07 +0530 Subject: [PATCH] feat: add C implementation for stats/base/dists/hypergeometric/quantile --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../quantile/benchmark/benchmark.native.js | 78 ++++++++ .../dists/hypergeometric/quantile/binding.gyp | 170 ++++++++++++++++++ .../hypergeometric/quantile/include.gypi | 53 ++++++ .../base/dists/hypergeometric/quantile.h | 38 ++++ .../hypergeometric/quantile/lib/native.js | 81 +++++++++ .../hypergeometric/quantile/manifest.json | 92 ++++++++++ .../hypergeometric/quantile/package.json | 3 + .../hypergeometric/quantile/src/Makefile | 52 ++++++ .../dists/hypergeometric/quantile/src/addon.c | 22 +++ .../dists/hypergeometric/quantile/src/main.c | 77 ++++++++ .../quantile/test/test.native.js | 167 +++++++++++++++++ 11 files changed, 833 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/binding.gyp create mode 100644 lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/include.gypi create mode 100644 lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/include/stdlib/stats/base/dists/hypergeometric/quantile.h create mode 100644 lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/lib/native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/manifest.json create mode 100644 lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/src/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/src/addon.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/src/main.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/test/test.native.js diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/benchmark/benchmark.native.js new file mode 100644 index 000000000000..115e6e288750 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/benchmark/benchmark.native.js @@ -0,0 +1,78 @@ +/** +* @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 tryRequire = require( '@stdlib/utils/try-require' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var quantile = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( quantile instanceof Error ) +}; + + +// MAIN // + +bench( format( '%s::native', pkg ), opts, function benchmark( b ) { + var len; + var N; + var K; + var n; + var p; + var y; + var i; + + len = 100; + p = new Float64Array( len ); + N = new Float64Array( len ); + K = new Float64Array( len ); + n = new Float64Array( len ); + for ( i = 0; i < len; i++ ) { + p[ i ] = uniform( 0.0, 1.0 ); + 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 = quantile( p[ i % 100 ], N[ i % 100 ], K[ i % 100 ], n[ i % 100 ] ); + 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/quantile/binding.gyp b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/binding.gyp new file mode 100644 index 000000000000..038bb48764e7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/binding.gyp @@ -0,0 +1,170 @@ +# @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. + +# 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/quantile/include.gypi b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/include.gypi new file mode 100644 index 000000000000..de5f9994b817 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/include.gypi @@ -0,0 +1,53 @@ +# @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. + +# 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': [ + ' + +/** +* Evaluates the quantile function for a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n` at a probability `p`. +* +* @param p input value +* @param N population size +* @param K subpopulation size +* @param n number of draws +* @return evaluated quantile function +* +* @example +* double y = stdlib_base_dists_hypergeometric_quantile( 0.4, 40.0, 20.0, 10.0 ); +* // returns 5.0 +*/ +double stdlib_base_dists_hypergeometric_quantile( const double p, const double N, const double K, const double n ) { + double prob; + double x; + + if ( + stdlib_base_is_nan( p ) || + stdlib_base_is_nan( N ) || + stdlib_base_is_nan( K ) || + stdlib_base_is_nan( n ) || + !stdlib_base_is_nonnegative_integer( N ) || + !stdlib_base_is_nonnegative_integer( K ) || + !stdlib_base_is_nonnegative_integer( n ) || + N == STDLIB_CONSTANT_FLOAT64_PINF || + K == STDLIB_CONSTANT_FLOAT64_PINF || + K > N || + n > N || + p < 0.0 || + p > 1.0 + ) { + return 0.0 / 0.0; // NaN + } + if ( p == 0.0 ) { + return stdlib_base_max( 0.0, n + K - N ); + } + if ( p == 1.0 ) { + return stdlib_base_min( n, K ); + } + x = stdlib_base_max( 0.0, n + K - N ); + while ( 1 ) { + prob = stdlib_base_dists_hypergeometric_cdf( x, N, K, n ); + if ( prob > p ) { + break; + } + x += 1.0; + } + return x; +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/test/test.native.js new file mode 100644 index 000000000000..ccc8f3407ba8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/quantile/test/test.native.js @@ -0,0 +1,167 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// FIXTURES // + +var data = require( './fixtures/julia/data.json' ); + + +// VARIABLES // + +var quantile = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( quantile instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof quantile, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) { + var y = quantile( NaN, 20, 20, 10 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + y = quantile( 0.2, NaN, 10, 10 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + y = quantile( 0.2, 20, NaN, 10 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + y = quantile( 0.2, 20, 20, NaN ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided a number outside `[0,1]` for `p` and valid parameters, the function returns `NaN`', opts, function test( t ) { + var y = quantile( 1.1, 20, 10, 10 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + y = quantile( -0.1, 20, 10, 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 = quantile( 0.5, 10.5, 10, 10 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = quantile( 0.5, PINF, 10, 5 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = quantile( 0.5, NINF, 10, 5 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = quantile( 0.5, -2.0, 10, 5 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `K` which is not a nonnegative integer, the function returns `NaN`', opts, function test( t ) { + var y; + + y = quantile( 0.5, 20, 10.5, 10 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = quantile( 0.5, 20, PINF, 5 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = quantile( 0.5, 20, NINF, 5 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = quantile( 0.5, 20, -2.0, 5 ); + 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 = quantile( 0.5, 20, 10, 10.5 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = quantile( 0.5, 20, 10, PINF ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = quantile( 0.5, 20, 10, NINF ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = quantile( 0.5, 20, 5, -2.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'given valid parameters, the function returns `max( 0, n + K - N )` as the 0% quantile', opts, function test( t ) { + var y = quantile( 0.0, 20, 10, 5 ); + t.strictEqual( y, 0, 'returns expected value' ); + + y = quantile( 0.0, 20, 10, 15 ); + t.strictEqual( y, 5, 'returns 5' ); + t.end(); +}); + +tape( 'given valid parameters, the function returns `min( n, K )` as the 100% quantile', opts, function test( t ) { + var y = quantile( 1.0, 30, 20, 15 ); + t.strictEqual( y, 15, 'returns 15' ); + + y = quantile( 1.0, 30, 20, 25 ); + t.strictEqual( y, 20, 'returns 20' ); + + y = quantile( 1.0, 30, 20, 5 ); + t.strictEqual( y, 5, 'returns 5' ); + + t.end(); +}); + +tape( 'the function evaluates the quantile for `p` given valid parameters', opts, function test( t ) { + var expected; + var p; + var N; + var K; + var n; + var y; + var i; + + expected = data.expected; + p = data.p; + N = data.N; + K = data.K; + n = data.n; + for ( i = 0; i < p.length; i++ ) { + y = quantile( p[i], N[i], K[i], n[i] ); + t.strictEqual( y, expected[i], 'p: '+p[i]+', N: '+N[i]+', K: '+K[i]+', n: '+n[i]+', y: '+y+', expected: '+expected[i] ); + } + t.end(); +});