Skip to content

Commit 3bf90ae

Browse files
committed
fix: correct hypergeometric standard deviation implementation
--- 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: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: passed - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: passed - task: run_c_benchmarks status: passed - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: passed - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed ---
1 parent 68b83d1 commit 3bf90ae

File tree

11 files changed

+73
-114
lines changed

11 files changed

+73
-114
lines changed

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/stdev/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ double out = stdlib_base_dists_hypergeometric_stdev( 16, 11, 4 );
184184

185185
The function accepts the following arguments:
186186

187-
- **N**: `[in] double` population size.
188-
- **K**: `[in] double` subpopulation size.
189-
- **n**: `[in] double` number of draws.
187+
- **N**: `[in] int32_t` population size.
188+
- **K**: `[in] int32_t` subpopulation size.
189+
- **n**: `[in] int32_t` number of draws.
190190

191191
```c
192-
double stdlib_base_dists_hypergeometric_stdev( const double N, const double K, const double n );
192+
double stdlib_base_dists_hypergeometric_stdev( const int32_t N, const int32_t K, const int32_t n );
193193
```
194194
195195
</section>
@@ -211,10 +211,11 @@ double stdlib_base_dists_hypergeometric_stdev( const double N, const double K, c
211211
### Examples
212212
213213
```c
214-
#include "stdlib/math/base/special/round.h"
215214
#include "stdlib/stats/base/dists/hypergeometric/stdev.h"
216-
#include <stdio.h>
215+
#include "stdlib/math/base/special/ceil.h"
217216
#include <stdlib.h>
217+
#include <stdint.h>
218+
#include <stdio.h>
218219
219220
static double random_uniform( const double min, const double max ) {
220221
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
@@ -223,17 +224,17 @@ static double random_uniform( const double min, const double max ) {
223224
224225
int main( void ) {
225226
double sd;
226-
double N;
227-
double K;
228-
double n;
227+
int32_t N;
228+
int32_t K;
229+
int32_t n;
229230
int i;
230231
231232
for ( i = 0; i < 10; i++ ) {
232-
N = stdlib_base_round( random_uniform( 0.0, 20.0 ) );
233-
K = stdlib_base_round( random_uniform( 0.0, N ) );
234-
n = stdlib_base_round( random_uniform( 0.0, K ) );
233+
N = stdlib_base_ceil( random_uniform( 2.0, 100.0 ) );
234+
K = stdlib_base_ceil( random_uniform( 0.0, N ) );
235+
n = stdlib_base_ceil( random_uniform( 0.0, N ) );
235236
sd = stdlib_base_dists_hypergeometric_stdev( N, K, n );
236-
printf( "N: %lf, K: %lf, n: %lf, SD(X;N,K,n): %lf\n", N, K, n, sd );
237+
printf( "N: %d, K: %d, n: %d, SD(X;N,K,n): %lf\n", N, K, n, sd );
237238
}
238239
}
239240
```

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/stdev/benchmark/benchmark.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var floor = require( '@stdlib/math/base/special/floor' );
25-
var randu = require( '@stdlib/random/base/randu' );
26-
var uniform = require( '@stdlib/random/base/uniform' );
27-
var ceil = require( '@stdlib/math/base/special/ceil' );
24+
var Float64Array = require( '@stdlib/array/float64' );
25+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
2826
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2927
var pkg = require( './../package.json' ).name;
3028
var stdev = require( './../lib' );
@@ -33,18 +31,26 @@ var stdev = require( './../lib' );
3331
// MAIN //
3432

3533
bench( pkg, function benchmark( b ) {
34+
var len;
3635
var N;
3736
var K;
3837
var n;
3938
var y;
4039
var i;
4140

41+
len = 100;
42+
N = new Float64Array( len );
43+
K = new Float64Array( len );
44+
n = new Float64Array( len );
45+
for ( i = 0; i < len; i++ ) {
46+
N[ i ] = discreteUniform( 2, 100 );
47+
n[ i ] = discreteUniform( 0, N[ i ] );
48+
K[ i ] = discreteUniform( 0, N[ i ] );
49+
}
50+
4251
b.tic();
4352
for ( i = 0; i < b.iterations; i++ ) {
44-
N = ceil( uniform( 1.0, 100.0 ) );
45-
K = floor( randu()*N );
46-
n = floor( randu()*N );
47-
y = stdev( N, K, n );
53+
y = stdev( N[ i % len ], K[ i % len ], n[ i % len ] );
4854
if ( isnan( y ) ) {
4955
b.fail( 'should not return NaN' );
5056
}

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/stdev/benchmark/benchmark.native.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var Float64Array = require( '@stdlib/array/float64' );
26-
var randu = require( '@stdlib/random/base/randu' );
27-
var uniform = require( '@stdlib/random/base/uniform' );
26+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
2827
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
30-
var floor = require( '@stdlib/math/base/special/floor' );
3129
var pkg = require( './../package.json' ).name;
3230

3331

@@ -54,9 +52,9 @@ bench( pkg+'::native', opts, function benchmark( b ) {
5452
K = new Float64Array( len );
5553
n = new Float64Array( len );
5654
for ( i = 0; i < len; i++ ) {
57-
N[ i ] = floor( uniform( 1.0, 100.0 ) );
58-
K[ i ] = floor( randu() * N[ i ] );
59-
n[ i ] = floor( randu() * N[ i ] );
55+
N[ i ] = discreteUniform( 2, 100 );
56+
n[ i ] = discreteUniform( 0, N[ i ] );
57+
K[ i ] = discreteUniform( 0, N[ i ] );
6058
}
6159

6260
b.tic();

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/stdev/benchmark/c/benchmark.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/special/round.h"
2019
#include "stdlib/stats/base/dists/hypergeometric/stdev.h"
21-
#include <math.h>
22-
#include <stdio.h>
20+
#include "stdlib/math/base/special/ceil.h"
2321
#include <stdlib.h>
22+
#include <stdint.h>
23+
#include <stdio.h>
2424
#include <time.h>
2525
#include <sys/time.h>
2626

@@ -94,17 +94,17 @@ static double random_uniform( const double min, const double max ) {
9494
*/
9595
static double benchmark( void ) {
9696
double elapsed;
97-
double N[ 100 ];
98-
double K[ 100 ];
99-
double n[ 100 ];
97+
int32_t N[ 100 ];
98+
int32_t K[ 100 ];
99+
int32_t n[ 100 ];
100100
double sd;
101101
double t;
102102
int i;
103103

104104
for ( i = 0; i < 100; i++ ) {
105-
N[ i ] = stdlib_base_round( random_uniform( 1.0, 100.0 ) );
106-
K[ i ] = stdlib_base_round( random_uniform( 0.0, N[ i ] ) );
107-
n[ i ] = stdlib_base_round( random_uniform( 0.0, N[ i ] ) );
105+
N[ i ] = stdlib_base_ceil( random_uniform( 2.0, 100.0 ) );
106+
K[ i ] = stdlib_base_ceil( random_uniform( 0.0, N[ i ] ) );
107+
n[ i ] = stdlib_base_ceil( random_uniform( 0.0, N[ i ] ) );
108108
}
109109

110110
t = tic();

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/stdev/examples/c/example.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/special/round.h"
2019
#include "stdlib/stats/base/dists/hypergeometric/stdev.h"
21-
#include <stdio.h>
20+
#include "stdlib/math/base/special/ceil.h"
2221
#include <stdlib.h>
22+
#include <stdint.h>
23+
#include <stdio.h>
2324

2425
static double random_uniform( const double min, const double max ) {
2526
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
@@ -28,16 +29,16 @@ static double random_uniform( const double min, const double max ) {
2829

2930
int main( void ) {
3031
double sd;
31-
double N;
32-
double K;
33-
double n;
32+
int32_t N;
33+
int32_t K;
34+
int32_t n;
3435
int i;
3536

3637
for ( i = 0; i < 10; i++ ) {
37-
N = stdlib_base_round( random_uniform( 0.0, 20.0 ) );
38-
K = stdlib_base_round( random_uniform( 0.0, N ) );
39-
n = stdlib_base_round( random_uniform( 0.0, K ) );
38+
N = stdlib_base_ceil( random_uniform( 2.0, 100.0 ) );
39+
K = stdlib_base_ceil( random_uniform( 0.0, N ) );
40+
n = stdlib_base_ceil( random_uniform( 0.0, N ) );
4041
sd = stdlib_base_dists_hypergeometric_stdev( N, K, n );
41-
printf( "N: %lf, K: %lf, n: %lf, SD(X;N,K,n): %lf\n", N, K, n, sd );
42+
printf( "N: %d, K: %d, n: %d, SD(X;N,K,n): %lf\n", N, K, n, sd );
4243
}
4344
}

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/stdev/include/stdlib/stats/base/dists/hypergeometric/stdev.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef STDLIB_STATS_BASE_DISTS_HYPERGEOMETRIC_STDEV_H
2020
#define STDLIB_STATS_BASE_DISTS_HYPERGEOMETRIC_STDEV_H
2121

22+
#include <stdint.h>
23+
2224
/*
2325
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
2426
*/
@@ -27,14 +29,9 @@ extern "C" {
2729
#endif
2830

2931
/**
30-
* Returns the standard deviation of a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n`.
31-
*
32-
* @param N population size
33-
* @param K subpopulation size
34-
* @param n number of draws
35-
* @return standard deviation of the distribution
32+
* Returns the standard deviation of a hypergeometric distribution.
3633
*/
37-
double stdlib_base_dists_hypergeometric_stdev( const double N, const double K, const double n );
34+
double stdlib_base_dists_hypergeometric_stdev( const int32_t N, const int32_t K, const int32_t n );
3835

3936
#ifdef __cplusplus
4037
}

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/stdev/lib/native.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ var addon = require( './../src/addon.node' );
2626
// MAIN //
2727

2828
/**
29-
* Returns the standard deviation of a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n`.
29+
* Returns the standard deviation of a hypergeometric distribution.
3030
*
3131
* @private
32-
* @param {number} N - population size
33-
* @param {number} K - subpopulation size
34-
* @param {number} n - number of draws
35-
* @returns {number} standard deviation
32+
* @param {NonNegativeInteger} N - population size
33+
* @param {NonNegativeInteger} K - subpopulation size
34+
* @param {NonNegativeInteger} n - number of draws
35+
* @returns {NonNegativeNumber} standard deviation
3636
*
3737
* @example
3838
* var v = stdev( 16, 11, 4 );

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/stdev/manifest.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"libpath": [],
4040
"dependencies": [
4141
"@stdlib/math/base/napi/ternary",
42-
"@stdlib/math/base/assert/is-nonnegative-integer",
4342
"@stdlib/constants/float64/pinf",
4443
"@stdlib/math/base/special/sqrt"
4544
]
@@ -56,10 +55,9 @@
5655
"libraries": [],
5756
"libpath": [],
5857
"dependencies": [
59-
"@stdlib/math/base/assert/is-nonnegative-integer",
6058
"@stdlib/constants/float64/pinf",
6159
"@stdlib/math/base/special/sqrt",
62-
"@stdlib/math/base/special/round"
60+
"@stdlib/math/base/special/ceil"
6361
]
6462
},
6563
{
@@ -74,10 +72,9 @@
7472
"libraries": [],
7573
"libpath": [],
7674
"dependencies": [
77-
"@stdlib/math/base/assert/is-nonnegative-integer",
78-
"@stdlib/math/base/special/round",
7975
"@stdlib/constants/float64/pinf",
80-
"@stdlib/math/base/special/sqrt"
76+
"@stdlib/math/base/special/sqrt",
77+
"@stdlib/math/base/special/ceil"
8178
]
8279
}
8380
]

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/stdev/src/addon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/napi/ternary.h"
2019
#include "stdlib/stats/base/dists/hypergeometric/stdev.h"
20+
#include "stdlib/math/base/napi/ternary.h"
2121

2222
// cppcheck-suppress shadowFunction
23-
STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_dists_hypergeometric_stdev )
23+
STDLIB_MATH_BASE_NAPI_MODULE_III_D( stdlib_base_dists_hypergeometric_stdev )

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/stdev/src/main.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/constants/float64/pinf.h"
20-
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
21-
#include "stdlib/math/base/special/sqrt.h"
2219
#include "stdlib/stats/base/dists/hypergeometric/stdev.h"
20+
#include "stdlib/math/base/special/sqrt.h"
21+
#include "stdlib/constants/float64/pinf.h"
22+
#include <stdint.h>
2323

2424
/**
2525
* Returns the standard deviation of a hypergeometric distribution.
@@ -33,11 +33,11 @@
3333
* double sd = stdlib_base_dists_hypergeometric_stdev( 16, 11, 4 );
3434
* // returns ~0.829
3535
*/
36-
double stdlib_base_dists_hypergeometric_stdev( const double N, const double K, const double n ) {
37-
if ( !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 ) {
38-
return NAN;
36+
double stdlib_base_dists_hypergeometric_stdev( const int32_t N, const int32_t K, const int32_t n ) {
37+
if ( N < 0 || K < 0 || n < 0 || N == STDLIB_CONSTANT_FLOAT64_PINF || K == STDLIB_CONSTANT_FLOAT64_PINF || K > N || n > N ) {
38+
return 0.0/0.0; // NaN
3939
}
40-
41-
double variance = n * ( K / N ) * ( ( N - K ) / N ) * ( ( N - n ) / ( N - 1 ) );
40+
double p = (double)K / (double)N;
41+
double variance = n * p * ( 1.0 - p ) * ( (double)(N-n) / (double)(N-1) );
4242
return stdlib_base_sqrt( variance );
4343
}

0 commit comments

Comments
 (0)