Skip to content

Commit fa93d97

Browse files
feat: added new implementation and other minor updates
1 parent 5aedaee commit fa93d97

File tree

16 files changed

+102
-106
lines changed

16 files changed

+102
-106
lines changed

lib/node_modules/@stdlib/stats/base/dists/binomial/median/README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,20 @@ for ( i = 0; i < 10; i++ ) {
169169

170170
#### stdlib_base_dists_binomial_median( n, p )
171171

172-
Returns the [median][median] of a [Binomial][binomial-distribution] distribution with number of trials \( n \) and success probability \( p \).
172+
Returns the [median][median] of a [binomial][binomial-distribution] distribution with number of trials `n` and success probability `p`.
173173

174174
```c
175-
double out = stdlib_base_dists_binomial_median( 10, 0.5 );
176-
// returns 5
175+
double out = stdlib_base_dists_binomial_median( 100, 0.1 );
176+
// returns 10
177177
```
178178

179179
The function accepts the following arguments:
180180

181-
- **n**: `[in] int` number of trials.
181+
- **n**: `[in] int32_t` number of trials.
182182
- **p**: `[in] double` success probability.
183183

184184
```c
185-
double stdlib_base_dists_binomial_median( const int n, const double p );
185+
double stdlib_base_dists_binomial_median( const int32_t n, const double p );
186186
```
187187
188188
</section>
@@ -205,21 +205,30 @@ double stdlib_base_dists_binomial_median( const int n, const double p );
205205
206206
```c
207207
#include "stdlib/stats/base/dists/binomial/median.h"
208+
#include "stdlib/math/base/special/ceil.h"
208209
#include <stdlib.h>
210+
#include <stdint.h>
209211
#include <stdio.h>
210212
213+
static double random_uniform( const double min, const double max ) {
214+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
215+
return min + ( v * (max - min) );
216+
}
217+
211218
int main( void ) {
212-
int n;
219+
int32_t n;
213220
double p;
214221
double y;
215222
int i;
216223
217224
for ( i = 0; i < 25; i++ ) {
218-
n = rand() % 20 + 1; // random number of trials between 1 and 20
219-
p = (double)rand() / ( (double)RAND_MAX + 1.0 );
225+
n = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
226+
p = random_uniform( 0.0, 1.0 );
220227
y = stdlib_base_dists_binomial_median( n, p );
221228
printf( "n: %d, p: %lf, Median(X;n,p): %lf\n", n, p, y );
222229
}
230+
231+
return 0;
223232
}
224233
```
225234

lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var ceil = require( '@stdlib/math/base/special/ceil' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var Int32Array = require( '@stdlib/array/int32' );
2626
var Float64Array = require( '@stdlib/array/float64' );
27+
var randu = require( '@stdlib/random/base/randu' );
2728
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2829
var pkg = require( './../package.json' ).name;
2930
var median = require( './../lib' );
@@ -39,17 +40,16 @@ bench( pkg, function benchmark( b ) {
3940
var i;
4041

4142
len = 100;
42-
n = new Float64Array(len);
43-
p = new Float64Array(len);
44-
45-
for (i = 0; i < len; i++) {
46-
n[i] = ceil( randu()*100.0 );
47-
p[i] = randu();
43+
n = new Int32Array( len );
44+
p = new Float64Array( len );
45+
for ( i = 0; i < len; i++ ) {
46+
n[ i ] = ceil( randu() * 100.0 );
47+
p[ i ] = randu();
4848
}
4949

5050
b.tic();
5151
for ( i = 0; i < b.iterations; i++ ) {
52-
y = median(n[i % len], p[i % len]);
52+
y = median( n[ i%len ], p[ i%len ] );
5353
if ( isnan( y ) ) {
5454
b.fail( 'should not return NaN' );
5555
}

lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.native.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -22,11 +22,12 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25+
var Int32Array = require( '@stdlib/array/int32' );
2526
var Float64Array = require( '@stdlib/array/float64' );
26-
var randint = require( '@stdlib/random/base/discrete-uniform' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var ceil = require( '@stdlib/math/base/special/ceil' );
2729
var randu = require( '@stdlib/random/base/randu' );
2830
var isnan = require( '@stdlib/math/base/assert/is-nan' );
29-
var tryRequire = require( '@stdlib/utils/try-require' );
3031
var pkg = require( './../package.json' ).name;
3132

3233

@@ -48,10 +49,10 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4849
var i;
4950

5051
len = 100;
51-
n = new Float64Array( len );
52+
n = new Int32Array( len );
5253
p = new Float64Array( len );
5354
for ( i = 0; i < len; i++ ) {
54-
n[ i ] = randint( 1, 100 );
55+
n[ i ] = ceil( randu() * 100.0 );
5556
p[ i ] = randu();
5657
}
5758

lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2024 The Stdlib Authors.
4+
# Copyright (c) 2025 The Stdlib Authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -17,7 +17,9 @@
1717
*/
1818

1919
#include "stdlib/stats/base/dists/binomial/median.h"
20+
#include "stdlib/math/base/special/ceil.h"
2021
#include <stdlib.h>
22+
#include <stdint.h>
2123
#include <stdio.h>
2224
#include <math.h>
2325
#include <time.h>
@@ -37,8 +39,8 @@ static void print_version( void ) {
3739
/**
3840
* Prints the TAP summary.
3941
*
40-
* @param total total number of tests
41-
* @param passing total number of passing tests
42+
* @param total total number of tests
43+
* @param passing total number of passing tests
4244
*/
4345
static void print_summary( int total, int passing ) {
4446
printf( "#\n" );
@@ -52,7 +54,7 @@ static void print_summary( int total, int passing ) {
5254
/**
5355
* Prints benchmarks results.
5456
*
55-
* @param elapsed elapsed time in seconds
57+
* @param elapsed elapsed time in seconds
5658
*/
5759
static void print_results( double elapsed ) {
5860
double rate = (double)ITERATIONS / elapsed;
@@ -75,22 +77,15 @@ static double tic( void ) {
7577
}
7678

7779
/**
78-
* Generates a random number on the interval [0,1).
80+
* Generates a random number on the interval [min,max).
7981
*
80-
* @return random number
82+
* @param min minimum value (inclusive)
83+
* @param max maximum value (exclusive)
84+
* @return random number
8185
*/
82-
static double rand_double( void ) {
83-
int r = rand();
84-
return (double)r / ( (double)RAND_MAX + 1.0 );
85-
}
86-
87-
/**
88-
* Generates a random integer in the range [1, 100].
89-
*
90-
* @return random integer
91-
*/
92-
static int rand_int( void ) {
93-
return (rand() % 100) + 1;
86+
static double random_uniform( const double min, const double max ) {
87+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
88+
return min + ( v*(max-min) );
9489
}
9590

9691
/**
@@ -100,20 +95,20 @@ static int rand_int( void ) {
10095
*/
10196
static double benchmark( void ) {
10297
double elapsed;
98+
int32_t n[ 100 ];
10399
double p[ 100 ];
104-
int n[ 100 ];
105100
double y;
106101
double t;
107102
int i;
108103

109104
for ( i = 0; i < 100; i++ ) {
110-
p[ i ] = rand_double();
111-
n[ i ] = rand_int();
105+
n[ i ] = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
106+
p[ i ] = random_uniform( 0.0, 1.0 );
112107
}
113108

114109
t = tic();
115110
for ( i = 0; i < ITERATIONS; i++ ) {
116-
y = stdlib_base_dists_binomial_median( n[ i%100 ], p[ i%100 ] );
111+
y = stdlib_base_dists_binomial_median( n[ i % 100 ], p[ i % 100 ] );
117112
if ( y != y ) {
118113
printf( "should not return NaN\n" );
119114
break;

lib/node_modules/@stdlib/stats/base/dists/binomial/median/binding.gyp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @license Apache-2.0
22
#
3-
# Copyright (c) 2024 The Stdlib Authors.
3+
# Copyright (c) 2025 The Stdlib Authors.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -167,4 +167,4 @@
167167
], # end actions
168168
}, # end target copy_addon
169169
], # end targets
170-
}
170+
}

lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2024 The Stdlib Authors.
4+
# Copyright (c) 2025 The Stdlib Authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -17,19 +17,28 @@
1717
*/
1818

1919
#include "stdlib/stats/base/dists/binomial/median.h"
20+
#include "stdlib/math/base/special/ceil.h"
2021
#include <stdlib.h>
22+
#include <stdint.h>
2123
#include <stdio.h>
2224

25+
static double random_uniform( const double min, const double max ) {
26+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
27+
return min + ( v * (max - min) );
28+
}
29+
2330
int main( void ) {
31+
int32_t n;
2432
double p;
25-
double n;
2633
double y;
2734
int i;
2835

2936
for ( i = 0; i < 25; i++ ) {
30-
p = (double)rand() / ( (double)RAND_MAX + 1.0 );
31-
n = ( rand() % 100 ) + 1;
37+
n = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
38+
p = random_uniform( 0.0, 1.0 );
3239
y = stdlib_base_dists_binomial_median( n, p );
33-
printf( "n: %lf , p: %lf , H(X;n,p): %lf\n", n, p, y );
40+
printf( "n: %d, p: %lf, Median(X;n,p): %lf\n", n, p, y );
3441
}
42+
43+
return 0;
3544
}

lib/node_modules/@stdlib/stats/base/dists/binomial/median/include.gypi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @license Apache-2.0
22
#
3-
# Copyright (c) 2024 The Stdlib Authors.
3+
# Copyright (c) 2025 The Stdlib Authors.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -50,4 +50,4 @@
5050
'<!@(node -e "var arr = require(\'@stdlib/utils/library-manifest\')(\'./manifest.json\',{},{\'basedir\':process.cwd(),\'paths\':\'posix\'}).libpath; for ( var i = 0; i < arr.length; i++ ) { console.log( arr[ i ] ); }")',
5151
],
5252
}, # end variables
53-
}
53+
}

lib/node_modules/@stdlib/stats/base/dists/binomial/median/include/stdlib/stats/base/dists/binomial/median.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
1919
#ifndef STDLIB_STATS_BASE_DISTS_BINOMIAL_MEDIAN_H
2020
#define STDLIB_STATS_BASE_DISTS_BINOMIAL_MEDIAN_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,9 +29,9 @@ extern "C" {
2729
#endif
2830

2931
/**
30-
* Returns the median of a binomial distribution with `n` trials and success probability `p`.
32+
* Returns the median of a binomial distribution with number of trials `n` and success probability `p`.
3133
*/
32-
double stdlib_base_dists_binomial_median( const double n, const double p );
34+
double stdlib_base_dists_binomial_median( const int32_t n, const double p );
3335

3436
#ifdef __cplusplus
3537
}

0 commit comments

Comments
 (0)