Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ f(x; r, p) = P(X = x; r,p) = \binom{k+r-1}{x} p^r(1-p)^x \quad\text{for }x = 0,

<!-- </equation> -->

where `r > 0` is the number of successes until experiment is stopped and `0 < p <= 1` is the success probability. The random variable `X` denotes the number of failures until the `r` success is reached.
where `r > 0` is the number of successes until experiment is stopped and `0 < p <= 1` is the success probability. The random variable `X` denotes the number of failures until the `r` success is reached.

</section>

Expand Down Expand Up @@ -161,6 +161,102 @@ 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/negative-binomial/pmf.h"
```

#### stdlib_base_dists_negative_binomial_mode( x, r, p )

Evaluates the probability mass function (PMF) for a negative binomial distribution with number of successes until experiment is stopped `r` and success probability `p`.

```c
double y = stdlib_base_dists_negative_binomial_mode( 5.0, 20.0, 0.8 );
// returns ~0.157
```

The function accepts the following arguments:

- **x**: `[in] double` input value.
- **r**: `[in] double` number of failures until experiment is stopped .
- **p**: `[in] double` success probability .

```c
double stdlib_base_dists_negative_binomial_mode( const double x, const double r, 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/negative-binomial/pmf.h"
#include "stdlib/math/base/special/round.h"
#include "stdlib/constants/float64/eps"
#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 x;
double r;
double p;
double y;
int i;

for ( i = 0; i < 10; i++ ) {
x = stdlib_base_round( random_uniform( 0.0, 30.0 ) );
r = random_uniform( 0.0, 50.0 );
p = random_uniform( 0.0, 1.0 );
y = stdlib_base_dists_negative_binomial_pmf( x, r, p );
printf("x: %f, r: %f, p: %.4f, P(X=x;r,p): %.4f\n", x, r, p, y);
}
}
```

</section>

<!-- /.examples -->

<!-- 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,9 +21,9 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var ceil = require( '@stdlib/math/base/special/ceil' );
var Float64Array = require( '@stdlib/array/float64' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var uniform = require( '@stdlib/random/base/uniform' );
var randu = require( '@stdlib/random/base/randu' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;
Expand All @@ -33,18 +33,26 @@ var pmf = require( './../lib' );
// MAIN //

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

len = 100;
x = new Float64Array( len );
r = new Float64Array( len );
p = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = discreteUniform( 0.0, 100.0 );
r[ i ] = discreteUniform( 0.0, 100.0 );
p[ i ] = uniform( EPS, 1.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ceil( randu()*100.0 );
r = ceil( randu()*100.0 );
p = uniform( EPS, 1.0 );
y = pmf( x, r, p );
y = pmf( x[ i % len ], r[ i % len ], p[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -59,20 +67,26 @@ bench( pkg, function benchmark( b ) {

bench( pkg+':factory', function benchmark( b ) {
var mypmf;
var len;
var r;
var p;
var x;
var y;
var i;

len = 100;
r = 80;
p = 0.4;
mypmf = pmf.factory( r, p );
x = new Float64Array( len );

for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 0.0, 80.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*80 );
y = mypmf( x );
y = mypmf( x[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 tryRequire = require( '@stdlib/utils/try-require' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var uniform = require( '@stdlib/random/base/uniform' );
var EPS = require( '@stdlib/constants/float64/eps' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

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


// MAIN //

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

len = 100;
x = new Float64Array( len );
r = new Float64Array( len );
p = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = discreteUniform( 0.0, 100.0 );
r[ i ] = discreteUniform( 0.0, 100.0 );
p[ i ] = uniform( EPS, 1.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = pmf( x[ i % len ], r[ 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