Skip to content

feat: add C implementation for stats/base/dists/bernoulli/cdf #3962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: minor clean up
  • Loading branch information
Neerajpathak07 committed Jan 16, 2025
commit 21149c52f7c4960f7e36bac6af466b15d61a7e12
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,20 @@ double stdlib_base_dists_bernoulli_cdf( const double x, const double p );
#include <stdlib.h>
#include <stdio.h>

static double random_uniform( const double min, const double max ) {
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v*(max-min) );
}

int main( void ) {
double p;
double x;
double y;
int i;

for ( i = 0; i < 25; i++ ) {
x = ( (double)rand() / (double)RAND_MAX ) * 5.0;
p = ( (double)rand() / (double)RAND_MAX ) ;
for ( i = 0; i < 10; i++ ) {
x = random_uniform( 0.0, 5.0 );
p = random_uniform( 0.0, 1.0 );
y = stdlib_base_dists_bernoulli_cdf( x, p );
printf( "x: %lf , p: %1f, F(X;p): %lf\n", x, p , y );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var Float64Array = require( '@stdlib/array/float64' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var cdf = require( './../lib' );
Expand All @@ -30,16 +32,25 @@ var cdf = require( './../lib' );
// MAIN //

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

len = 100;
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 2.0, 3.0 );
}
p = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
p[ i ] = uniform( 0.0, 1.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*3.0 ) - 1.0;
p = randu();
y = cdf( x, p );
y = cdf( x[ i%100 ], p[ i%100 ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var Float64Array = require( '@stdlib/array/float64' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;
Expand All @@ -39,16 +40,25 @@ var opts = {
// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var len;
var p;
var x;
var y;
var i;

len = 100;
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 2.0, 3.0 );
}
p = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
p[ i ] = uniform( 0.0, 1.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*3.0 ) - 1.0;
p = randu();
y = cdf( x, p );
y = cdf( x[ i%100 ], p[ i%100 ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ static double tic( void ) {
}

/**
* Generates a random number on the interval [0,1).
* Generates a random number on the interval [min,max).
*
* @return random number
* @param min minimum value (inclusive)
* @param max maximum value (exclusive)
* @return random number
*/
static double rand_double( void ) {
int r = rand();
return (double)r / ( (double)RAND_MAX + 1.0 );
static double random_uniform( const double min, const double max ) {
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v * ( max - min ) );
}

/**
Expand All @@ -91,17 +93,22 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
double p;
double x;
double p[ 100 ];
double x[ 100 ];
double y;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
p[ i ] = random_uniform( 0.1, 10.0 );
}
for ( i = 0; i < 100; i++ ) {
x[ i ] = random_uniform( 2.0, 3.0 );
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( (double)rand() / (double)RAND_MAX )*3.0 - 1.0;
p = ( (double)rand() / (double)RAND_MAX ) ;
y = stdlib_base_dists_bernoulli_cdf( x, p );
y = stdlib_base_dists_bernoulli_cdf( x[ i%100 ], p[ i%100 ] );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{{alias}}( x, p )
Evaluates the cumulative distribution function (CDF) for a Bernoulli
distribution with success probability `p` at a value `x`.
distribution with success probability `p`.

If provided `NaN` as any argument, the function returns `NaN`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@
#include <stdlib.h>
#include <stdio.h>

static double random_uniform( const double min, const double max ) {
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v*(max-min) );
}

int main( void ) {
double p;
double x;
double y;
int i;

for ( i = 0; i < 25; i++ ) {
x = ( (double)rand() / (double)RAND_MAX ) * 5.0;
p = ( (double)rand() / (double)RAND_MAX ) ;
for ( i = 0; i < 10; i++ ) {
x = random_uniform( 0.0, 5.0 );
p = random_uniform( 0.0, 1.0 );
y = stdlib_base_dists_bernoulli_cdf( x, p );
printf( "x: %lf , p: %1f, F(X;p): %lf\n", x, p , y );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' );
// MAIN //

/**
* Evaluates the cumulative distribution function (CDF) for a Bernoulli distribution with success probability `p` at a value `x`.
* Evaluates the cumulative distribution function (CDF) for a Bernoulli distribution with success probability `p`.
*
* @param {number} x - input value
* @param {Probability} p - success probability
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var addon = require( './../src/addon.node' );
// MAIN //

/**
* Evaluates the cumulative distribution function (CDF) for a Bernoulli distribution with success probability `p` at a value `x`.
* Evaluates the cumulative distribution function (CDF) for a Bernoulli distribution with success probability `p`.
*
* @param {number} x - input value
* @param {Probability} p - success probability
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "stdlib/math/base/assert/is_nan.h"

/**
* Evaluates the cumulative distribution function (CDF) for a Bernoulli distribution with success probability `p` at a value `x`.
* Evaluates the cumulative distribution function (CDF) for a Bernoulli distribution with success probability `p`.
*
* @param x input value
* @param p success probability
Expand Down
Loading