Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,91 @@ for ( i = 0; i < 10; i++ ) {

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/stats/base/dists/binomial/cdf.h"
```

#### stdlib_base_dists_binomial_cdf( k, n, p )

Evaluates the cumulative distribution function (CDF) for a binomial distribution.

```c
double out = stdlib_base_dists_binomial_cdf( 5, 10, 0.5 );
// returns ~0.623
```

The function accepts the following arguments:

- **k**: `[in] int` input value
- **n**: `[in] int` number of trials
- **p**: `[in] double` success probability

```c

double stdlib_base_dists_binomial_cdf( const int k, const int n, const double p );

```

</section>
<!-- /.usage -->
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="notes">
</section>
<!-- /.notes -->
<!-- C API usage examples. -->
<section class="examples">
### Examples
```c
#include "stdlib/stats/base/dists/binomial/cdf.h"
#include <stdlib.h>
#include <stdio.h>
int main( void ) {
int k;
int n;
double p;
double y;
int i;

for ( i = 0; i < 25; i++ ) {
k = rand() % 11; // Random integer between 0 and 10
n = k + rand() % (21 - k); // Random integer between k and 20
p = ( (double)rand() / (double)RAND_MAX ); // Random double between 0.0 and 1.0
y = stdlib_base_dists_binomial_cdf( k, n, p );
printf( "k: %d, n: %d, p: %lf, F(k;n,p): %lf\n", k, n, p, y );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,83 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var ceil = require( '@stdlib/math/base/special/ceil' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/array/uniform' );
var randu = require( '@stdlib/random/base/randu' );
var floor = require( '@stdlib/math/base/special/floor' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var cdf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
bench( pkg, function benchmark( b )
{
var len;
var k;
var n;
var p;
var x;
var y;
var i;

len = 100;
k = new Float64Array( len );
n = new Float64Array( len );
p = new Float64Array( len );
for ( i = 0; i < len; i++ )
{
k[ i ] = floor( randu() * 11 );
n[ i ] = floor( randu() * 21 );
p[ i ] = randu();
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = randu() * 100.0;
n = ceil( randu()*100.0 );
p = randu();
y = cdf( x, n, p );
if ( isnan( y ) ) {
for ( i = 0; i < b.iterations; i++ )
{
y = cdf( k[ i % len ], n[ i % len ], p[ i % len ] );
if ( isnan( y ) )
{
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
if ( isnan( y ) )
{
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
} );

bench( pkg+':factory', function benchmark( b ) {
bench( pkg + ':factory', function benchmark( b )
{
var mycdf;
var n;
var p;
var x;
var k;
var y;
var i;

n = 80;
p = 0.4;
n = 10;
p = 0.5;
mycdf = cdf.factory( n, p );
k = uniform( 100, 0, n );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = randu() * 80.0;
y = mycdf( x );
if ( isnan( y ) ) {
for ( i = 0; i < b.iterations; i++ )
{
y = mycdf( k[ i % k.length ] );
if ( isnan( y ) )
{
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
if ( isnan( y ) )
{
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 randint = require( '@stdlib/random/base/randu' );
var randu = require( '@stdlib/random/base/randu' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var cdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( cdf instanceof Error )
};


// MAIN //

bench( pkg, opts, function benchmark( b )
{
var len;
var k;
var n;
var p;
var y;
var i;

len = 100;
k = new Float64Array( len );
n = new Float64Array( len );
p = new Float64Array( len );
for ( i = 0; i < len; i++ )
{
k[ i ] = randint( 0, 10 );
n[ i ] = randint( k[ i ], 20 );
p[ i ] = randu();
}

b.tic();
for ( i = 0; i < b.iterations; i++ )
{
y = cdf( k[ i % len ], n[ i % len ], p[ 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();
} );
Loading
Loading