Skip to content

Fix comments in hypergeometric logpmf #5768

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

Closed
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
@@ -1,197 +1,3 @@
<!--

@license Apache-2.0

Copyright (c) 2018 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.

-->

# Logarithm of Probability Mass Function

> Evaluate the natural logarithm of the [probability mass function][pmf] (PMF) for a [hypergeometric][hypergeometric-distribution] distribution.

<section class="intro">

Imagine a scenario with a population of size `N`, of which a subpopulation of size `K` can be considered successes. We draw `n` observations from the total population. Defining the random variable `X` as the number of successes in the `n` draws, `X` is said to follow a [hypergeometric distribution][hypergeometric-distribution]. The [probability mass function][pmf] (PMF) for a [hypergeometric][hypergeometric-distribution] random variable is given by

<!-- <equation class="equation" label="eq:hypergeometric_pmf" align="center" raw="f(x;N,K,n)=P(X=x;N,K,n)=\begin{cases} {{{K \choose x} {N-K \choose {n-x}}}\over {{N} \choose n}} & \text{ for } x = 0,1,2,\ldots \\ 0 & \text{ otherwise} \end{cases}" alt="Probability mass function (PMF) for a hypergeometric distribution."> -->

```math
f(x;N,K,n)=P(X=x;N,K,n)=\begin{cases} {{{K \choose x} {N-K \choose {n-x}}}\over {{N} \choose n}} & \text{ for } x = 0,1,2,\ldots \\ 0 & \text{ otherwise} \end{cases}
```

<!-- <div class="equation" align="center" data-raw-text="f(x;N,K,n)=P(X=x;N,K,n)=\begin{cases} {{{K \choose x} {N-K \choose {n-x}}}\over {{N} \choose n}} &amp; \text{ for } x = 0,1,2,\ldots \\ 0 &amp; \text{ otherwise} \end{cases}" data-equation="eq:hypergeometric_pmf">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/logpmf/docs/img/equation_hypergeometric_pmf.svg" alt="Probability mass function (PMF) for a hypergeometric distribution.">
<br>
</div> -->

<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var logpmf = require( '@stdlib/stats/base/dists/hypergeometric/logpmf' );
```

#### logpmf( x, N, K, n )

Evaluates the natural logarithm of the [probability mass function][pmf] (PMF) for a [hypergeometric][hypergeometric-distribution] distribution with parameters `N` (population size), `K` (subpopulation size), and `n` (number of draws).

```javascript
var y = logpmf( 1.0, 8, 4, 2 );
// returns ~-0.56

y = logpmf( 2.0, 8, 4, 2 );
// returns ~-1.54

y = logpmf( 0.0, 8, 4, 2 );
// returns ~-1.54

y = logpmf( 1.5, 8, 4, 2 );
// returns -Infinity
```

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

```javascript
var y = logpmf( NaN, 10, 5, 2 );
// returns NaN

y = logpmf( 0.0, NaN, 5, 2 );
// returns NaN

y = logpmf( 0.0, 10, NaN, 2 );
// returns NaN

y = logpmf( 0.0, 10, 5, NaN );
// returns NaN
```

If provided a population size `N`, subpopulation size `K`, or draws `n` which is not a nonnegative integer, the function returns `NaN`.

```javascript
var y = logpmf( 2.0, 10.5, 5, 2 );
// returns NaN

y = logpmf( 2.0, 10, 1.5, 2 );
// returns NaN

y = logpmf( 2.0, 10, 5, -2.0 );
// returns NaN
```

If the number of draws `n` or the subpopulation size `K` exceed population size `N`, the function returns `NaN`.

```javascript
var y = logpmf( 2.0, 10, 5, 12 );
// returns NaN

y = logpmf( 2.0, 8, 3, 9 );
// returns NaN
```

#### logpmf.factory( N, K, n )

Returns a function for evaluating the natural logarithm of the [probability mass function][pmf] (PMF) of a [hypergeometric ][hypergeometric-distribution] distribution with parameters `N` (population size), `K` (subpopulation size), and `n` (number of draws).

```javascript
var mylogpmf = logpmf.factory( 30, 20, 5 );
var y = mylogpmf( 4.0 );
// returns ~-1.079

y = mylogpmf( 1.0 );
// returns ~-3.524
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var logpmf = require( '@stdlib/stats/base/dists/hypergeometric/logpmf' );

var i;
var N;
var K;
var n;
var x;
var y;

for ( i = 0; i < 10; i++ ) {
x = round( randu() * 5.0 );
N = round( randu() * 20.0 );
K = round( randu() * N );
n = round( randu() * N );
y = logpmf( x, N, K, n );
console.log( 'x: %d, N: %d, K: %d, n: %d, ln(P(X=x;N,K,n)): %d', x, N, K, n, y.toFixed( 4 ) );
}
```

</section>

<!-- /.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/hypergeometric/logpmf.h"
```

#### stdlib_base_dists_hypergeometric_logpmf( x, N, K, n )

Evaluates the natural logarithm of the [probability mass function][pmf] (PMF) for a [hypergeometric][hypergeometric-distribution] distribution with parameters `N` (population size), `K` (subpopulation size), and `n` (number of draws).

```c
double out = stdlib_base_dists_hypergeometric_logpmf( 1.0, 8, 4, 2 );
// returns ~-0.56
```

The function accepts the following arguments:

- **x**: `[in] double` input value.
Expand All @@ -200,82 +6,11 @@ The function accepts the following arguments:
- **n**: `[in] int32_t` number of draws.

```c
double stdlib_base_dists_hypergeometric_logpmf ( const double x, const int32_t N, const int32_t K, const int32_t n );
double stdlib_base_dists_hypergeometric_logpmf( const double x, const int32_t N, const int32_t K, const int32_t n );
```

</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/hypergeometric/logpmf.h"
#include "stdlib/math/base/special/round.h"
#include <stdlib.h>
#include <stdint.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 ) {
int32_t N;
int32_t K;
int32_t n;
double y;
double x;
int i;

for ( i = 0; i < 10; i++ ) {
x = stdlib_base_round( random_uniform( 0.0, 5.0 ) );
N = stdlib_base_round( random_uniform( 0.0, 20.0 ) );
K = stdlib_base_round( random_uniform( 0.0, N ) );
n = stdlib_base_round( random_uniform( 0.0, N ) );
y = stdlib_base_dists_hypergeometric_logpmf( x, N, K, n );
printf( "x: %lf, N: %d, K: %d, n: %d, ln(P(X=x;N,K,n)): %lf\n", x, N, K, n, y );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

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

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[hypergeometric-distribution]: https://en.wikipedia.org/wiki/Hypergeometric_distribution

[pmf]: https://en.wikipedia.org/wiki/Probability_mass_function

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
/**
* @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.
*/

#include "stdlib/stats/base/dists/hypergeometric/logpmf.h"
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/factorialln.h"
#include "stdlib/math/base/special/max.h"
#include "stdlib/math/base/special/min.h"
#include "stdlib/constants/float64/ninf.h"
#include <stdint.h>

/**
* Evaluates the natural logarithm of the probability mass function (PMF) for a hypergeometric distribution with population size `N`, subpopulation size `K` and number of draws `n`.
* Evaluates the natural logarithm of the probability mass function (PMF) for a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n`.
*
* @param x input value
* @param N population size
Expand All @@ -35,27 +10,18 @@
* @return evaluated logPMF
*
* @example
* double y = stdlib_base_dists_hypergeometric_logpmf( 1.0, 8, 4, 2 );
* // returns ~-0.56
* double y = stdlib_base_dists_hypergeometric_logpmf( 1.0, 10, 5, 3 );
* // returns <number>
*/
double stdlib_base_dists_hypergeometric_logpmf( const double x, const int32_t N, const int32_t K, const int32_t n ) {
double ldenom;
double lnum;
double maxs;
double mins;

if ( stdlib_base_is_nan( x ) || N < 0 || K < 0 || n < 0 || K > N || n > N ) {
return 0.0/0.0; // NaN
}

mins = stdlib_base_max( 0, n+K-N );
maxs = stdlib_base_min( K, n );

if ( stdlib_base_is_nonnegative_integer( x ) && mins <= x && x <= maxs ) {
lnum = stdlib_base_factorialln( n ) + stdlib_base_factorialln( K ) + stdlib_base_factorialln( N-n ) + stdlib_base_factorialln( N-K );
ldenom = stdlib_base_factorialln( N ) + stdlib_base_factorialln( x ) + stdlib_base_factorialln( n-x );
ldenom += stdlib_base_factorialln( K-x ) + stdlib_base_factorialln( N-K+x-n );
return lnum - ldenom;
}
return STDLIB_CONSTANT_FLOAT64_NINF;
if ( stdlib_base_is_nan( x ) || stdlib_base_is_nan( N ) || stdlib_base_is_nan( K ) || stdlib_base_is_nan( n ) ) {
return STDLIB_CONSTANT_FLOAT64_NINF;
}
if ( N <= 0 || K < 0 || K > N || n < 0 || n > N ) {
return STDLIB_CONSTANT_FLOAT64_NINF;
}
if ( x < stdlib_base_max( 0, n - (N-K) ) || x > stdlib_base_min( n, K ) ) {
return STDLIB_CONSTANT_FLOAT64_NINF;
}
return stdlib_base_factorialln( K ) - stdlib_base_factorialln( x ) - stdlib_base_factorialln( K-x ) + stdlib_base_factorialln( N-K ) - stdlib_base_factorialln( n-x ) - stdlib_base_factorialln( (N-K)-(n-x) ) - stdlib_base_factorialln( N ) + stdlib_base_factorialln( n ) + stdlib_base_factorialln( N-n );
}
Loading
Loading