README.md (4702B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Probability Mass Function 22 23 > [Hypergeometric][hypergeometric-distribution] distribution [probability mass function][pmf] (PMF). 24 25 <section class="intro"> 26 27 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 28 29 <!-- <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."> --> 30 31 <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}} & \text{ for } x = 0,1,2,\ldots \\ 0 & \text{ otherwise} \end{cases}" data-equation="eq:hypergeometric_pmf"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/pmf/docs/img/equation_hypergeometric_pmf.svg" alt="Probability mass function (PMF) for a hypergeometric distribution."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 </section> 39 40 <!-- /.intro --> 41 42 <section class="usage"> 43 44 ## Usage 45 46 ```javascript 47 var pmf = require( '@stdlib/stats/base/dists/hypergeometric/pmf' ); 48 ``` 49 50 #### pmf( x, N, K, n ) 51 52 Evaluates 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). 53 54 ```javascript 55 var y = pmf( 1.0, 8, 4, 2 ); 56 // returns ~0.571 57 58 y = pmf( 2.0, 8, 4, 2 ); 59 // returns ~0.214 60 61 y = pmf( 0.0, 8, 4, 2 ); 62 // returns ~0.214 63 64 y = pmf( 1.5, 8, 4, 2 ); 65 // returns 0.0 66 ``` 67 68 If provided `NaN` as any argument, the function returns `NaN`. 69 70 ```javascript 71 var y = pmf( NaN, 10, 5, 2 ); 72 // returns NaN 73 74 y = pmf( 0.0, NaN, 5, 2 ); 75 // returns NaN 76 77 y = pmf( 0.0, 10, NaN, 2 ); 78 // returns NaN 79 80 y = pmf( 0.0, 10, 5, NaN ); 81 // returns NaN 82 ``` 83 84 If provided a population size `N`, subpopulation size `K` or draws `n` which is not a nonnegative integer, the function returns `NaN`. 85 86 ```javascript 87 var y = pmf( 2.0, 10.5, 5, 2 ); 88 // returns NaN 89 90 y = pmf( 2.0, 10, 1.5, 2 ); 91 // returns NaN 92 93 y = pmf( 2.0, 10, 5, -2.0 ); 94 // returns NaN 95 ``` 96 97 If the number of draws `n` exceeds population size `N`, the function returns `NaN`. 98 99 ```javascript 100 var y = pmf( 2.0, 10, 5, 12 ); 101 // returns NaN 102 103 y = pmf( 2.0, 8, 3, 9 ); 104 // returns NaN 105 ``` 106 107 #### pmf.factory( N, K, n ) 108 109 Returns a function for evaluating 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). 110 111 ```javascript 112 var mypmf = pmf.factory( 30, 20, 5 ); 113 var y = mypmf( 4.0 ); 114 // returns ~0.34 115 116 y = mypmf( 1.0 ); 117 // returns ~0.029 118 ``` 119 120 </section> 121 122 <!-- /.usage --> 123 124 <section class="examples"> 125 126 ## Examples 127 128 <!-- eslint no-undef: "error" --> 129 130 ```javascript 131 var randu = require( '@stdlib/random/base/randu' ); 132 var round = require( '@stdlib/math/base/special/round' ); 133 var pmf = require( '@stdlib/stats/base/dists/hypergeometric/pmf' ); 134 135 var i; 136 var N; 137 var K; 138 var n; 139 var x; 140 var y; 141 142 for ( i = 0; i < 10; i++ ) { 143 x = round( randu() * 5.0 ); 144 N = round( randu() * 20.0 ); 145 K = round( randu() * N ); 146 n = round( randu() * N ); 147 y = pmf( x, N, K, n ); 148 console.log( 'x: %d, N: %d, K: %d, n: %d, P(X=x;N,K,n): %d', x, N, K, n, y.toFixed( 4 ) ); 149 } 150 ``` 151 152 </section> 153 154 <!-- /.examples --> 155 156 <section class="links"> 157 158 [hypergeometric-distribution]: https://en.wikipedia.org/wiki/Hypergeometric_distribution 159 160 [pmf]: https://en.wikipedia.org/wiki/Probability_mass_function 161 162 </section> 163 164 <!-- /.links -->