Skip to content

Commit 8380111

Browse files
author
Aadish Jain
committed
feat(binomial-cdf): add binomial cumulative distribution function implementation
1 parent 1d283b5 commit 8380111

File tree

17 files changed

+1439
-21
lines changed

17 files changed

+1439
-21
lines changed

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,91 @@ for ( i = 0; i < 10; i++ ) {
167167

168168
<!-- /.examples -->
169169

170+
<!-- C interface documentation. -->
171+
172+
* * *
173+
174+
<section class="c">
175+
176+
## C APIs
177+
178+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
179+
180+
<section class="intro">
181+
182+
</section>
183+
184+
<!-- /.intro -->
185+
186+
<!-- C usage documentation. -->
187+
188+
<section class="usage">
189+
190+
### Usage
191+
192+
```c
193+
#include "stdlib/stats/base/dists/arcsine/cdf.h"
194+
```
195+
196+
#### stdlib_base_dists_binomial_cdf( k, n, p )
197+
198+
Evaluates the cumulative distribution function (CDF) for a binomial distribution.
199+
200+
```c
201+
double out = stdlib_base_dists_binomial_cdf( 5, 10, 0.5 );
202+
// returns ~0.623
203+
```
204+
205+
The function accepts the following arguments:
206+
207+
- **k**: `[in] int` input value
208+
- **n**: `[in] int` number of trials
209+
- **p**: `[in] double` success probability
210+
211+
```c
212+
213+
double stdlib_base_dists_binomial_cdf( const int k, const int n, const double p );
214+
215+
```
216+
217+
</section>
218+
<!-- /.usage -->
219+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
220+
<section class="notes">
221+
</section>
222+
<!-- /.notes -->
223+
<!-- C API usage examples. -->
224+
<section class="examples">
225+
### Examples
226+
```c
227+
#include "stdlib/stats/base/dists/binomial/cdf.h"
228+
#include <stdlib.h>
229+
#include <stdio.h>
230+
int main( void ) {
231+
int k;
232+
int n;
233+
double p;
234+
double y;
235+
int i;
236+
237+
for ( i = 0; i < 25; i++ ) {
238+
k = rand() % 11; // Random integer between 0 and 10
239+
n = k + rand() % (21 - k); // Random integer between k and 20
240+
p = ( (double)rand() / (double)RAND_MAX ); // Random double between 0.0 and 1.0
241+
y = stdlib_base_dists_binomial_cdf( k, n, p );
242+
printf( "k: %d, n: %d, p: %lf, F(k;n,p): %lf\n", k, n, p, y );
243+
}
244+
}
245+
```
246+
247+
</section>
248+
249+
<!-- /.examples -->
250+
251+
</section>
252+
253+
<!-- /.c -->
254+
170255
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
171256

172257
<section class="related">

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

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var ceil = require( '@stdlib/math/base/special/ceil' );
24+
var Float64Array = require( '@stdlib/array/float64' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var randint = require( '@stdlib/random/array/randu' );
2527
var randu = require( '@stdlib/random/base/randu' );
2628
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2729
var pkg = require( './../package.json' ).name;
@@ -30,55 +32,72 @@ var cdf = require( './../lib' );
3032

3133
// MAIN //
3234

33-
bench( pkg, function benchmark( b ) {
35+
bench( pkg, function benchmark( b )
36+
{
37+
var len;
38+
var k;
3439
var n;
3540
var p;
36-
var x;
3741
var y;
3842
var i;
3943

44+
len = 100;
45+
k = new Float64Array( len );
46+
n = new Float64Array( len );
47+
p = new Float64Array( len );
48+
for ( i = 0; i < len; i++ )
49+
{
50+
k[ i ] = randint( 0, 10 );
51+
n[ i ] = randint( k[ i ], 20 );
52+
p[ i ] = randu();
53+
}
54+
4055
b.tic();
41-
for ( i = 0; i < b.iterations; i++ ) {
42-
x = randu() * 100.0;
43-
n = ceil( randu()*100.0 );
44-
p = randu();
45-
y = cdf( x, n, p );
46-
if ( isnan( y ) ) {
56+
for ( i = 0; i < b.iterations; i++ )
57+
{
58+
y = cdf( k[ i % len ], n[ i % len ], p[ i % len ] );
59+
if ( isnan( y ) )
60+
{
4761
b.fail( 'should not return NaN' );
4862
}
4963
}
5064
b.toc();
51-
if ( isnan( y ) ) {
65+
if ( isnan( y ) )
66+
{
5267
b.fail( 'should not return NaN' );
5368
}
5469
b.pass( 'benchmark finished' );
5570
b.end();
56-
});
71+
} );
5772

58-
bench( pkg+':factory', function benchmark( b ) {
73+
bench( pkg + ':factory', function benchmark( b )
74+
{
5975
var mycdf;
6076
var n;
6177
var p;
62-
var x;
78+
var k;
6379
var y;
6480
var i;
6581

66-
n = 80;
67-
p = 0.4;
82+
n = 10;
83+
p = 0.5;
6884
mycdf = cdf.factory( n, p );
85+
k = uniform( 100, 0, n );
6986

7087
b.tic();
71-
for ( i = 0; i < b.iterations; i++ ) {
72-
x = randu() * 80.0;
73-
y = mycdf( x );
74-
if ( isnan( y ) ) {
88+
for ( i = 0; i < b.iterations; i++ )
89+
{
90+
y = mycdf( k[ i % k.length ] );
91+
if ( isnan( y ) )
92+
{
7593
b.fail( 'should not return NaN' );
7694
}
7795
}
7896
b.toc();
79-
if ( isnan( y ) ) {
97+
if ( isnan( y ) )
98+
{
8099
b.fail( 'should not return NaN' );
81100
}
82101
b.pass( 'benchmark finished' );
83102
b.end();
84-
});
103+
} );
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var randint = require( '@stdlib/random/base/randu' );
27+
var randu = require( '@stdlib/random/base/randu' );
28+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var cdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( cdf instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( pkg, opts, function benchmark( b )
44+
{
45+
var len;
46+
var k;
47+
var n;
48+
var p;
49+
var y;
50+
var i;
51+
52+
len = 100;
53+
k = new Float64Array( len );
54+
n = new Float64Array( len );
55+
p = new Float64Array( len );
56+
for ( i = 0; i < len; i++ )
57+
{
58+
k[ i ] = randint( 0, 10 );
59+
n[ i ] = randint( k[ i ], 20 );
60+
p[ i ] = randu();
61+
}
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ )
65+
{
66+
y = cdf( k[ i % len ], n[ i % len ], p[ i % len ] );
67+
if ( isnan( y ) )
68+
{
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( y ) )
74+
{
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
} );

0 commit comments

Comments
 (0)