Hypergeometric distribution variance.
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we’ve built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
[Hypergeometric][hypergeometric-distribution] distribution [variance][variance].
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 [variance][variance] for a [hypergeometric][hypergeometric-distribution] random variable ismath
\mathop{\mathrm{Var}}\left( X \right) = n{K \over N}{(N-K) \over N}{N-n \over N-1}
bash
npm install @stdlib/stats-base-dists-hypergeometric-variance
script
tag without installation and bundlers, use the [ES Module][es-module] available on the [esm
][esm-url] branch (see [README][esm-readme]).deno
][deno-url] branch (see [README][deno-readme] for usage intructions).umd
][umd-url] branch (see [README][umd-readme]).javascript
var variance = require( '@stdlib/stats-base-dists-hypergeometric-variance' );
N
(population size), K
(subpopulation size), and n
(number of draws).javascript
var v = variance( 16, 11, 4 );
// returns ~0.688
v = variance( 2, 1, 1 );
// returns 0.25
NaN
as any argument, the function returns NaN
.javascript
var v = variance( NaN, 10, 4 );
// returns NaN
v = variance( 20, NaN, 4 );
// returns NaN
v = variance( 20, 10, NaN );
// returns NaN
N
, subpopulation size K
, or draws n
which is not a nonnegative integer, the function returns NaN
.javascript
var v = variance( 10.5, 5, 2 );
// returns NaN
v = variance( 10, 1.5, 2 );
// returns NaN
v = variance( 10, 5, -2.0 );
// returns NaN
n
or the subpopulation size K
exceed population size N
, the function returns NaN
.javascript
var v = variance( 10, 5, 12 );
// returns NaN
v = variance( 10, 12, 5 );
// returns NaN
javascript
var randu = require( '@stdlib/random-base-randu' );
var round = require( '@stdlib/math-base-special-round' );
var variance = require( '@stdlib/stats-base-dists-hypergeometric-variance' );
var v;
var i;
var N;
var K;
var n;
for ( i = 0; i < 10; i++ ) {
N = round( randu() * 20 );
K = round( randu() * N );
n = round( randu() * K );
v = variance( N, K, n );
console.log( 'N: %d, K: %d, n: %d, Var(X;N,K,n): %d', N, K, n, v.toFixed( 4 ) );
}
c
#include "stdlib/stats/base/dists/hypergeometric/variance.h"
N
(population size), K
(subpopulation size), and n
(number of draws).c
double out = stdlib_base_dists_hypergeometric_variance( 16, 11, 4 );
// returns ~0.688
[in] int32_t
population size.[in] int32_t
subpopulation size.[in] int32_t
number of draws.c
double stdlib_base_dists_hypergeometric_variance( const int32_t N, const int32_t K, const int32_t n );
c
#include "stdlib/stats/base/dists/hypergeometric/variance.h"
#include "stdlib/math/base/special/ceil.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 v;
int i;
for ( i = 0; i < 10; i++ ) {
N = stdlib_base_ceil( random_uniform( 2.0, 100.0 ) );
K = stdlib_base_ceil( random_uniform( 0.0, N ) );
n = stdlib_base_ceil( random_uniform( 0.0, N ) );
v = stdlib_base_dists_hypergeometric_variance( N, K, n );
printf( "N: %d, K: %d, n: %d, Var(X;N,K,n): %lf\n", N, K, n, v );
}
}