README.md (4927B)
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 # Logarithm of Probability Mass Function 22 23 > Evaluate the natural logarithm of the [probability mass function][pmf] (PMF) for a [hypergeometric][hypergeometric-distribution] distribution. 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/logpmf/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 logpmf = require( '@stdlib/stats/base/dists/hypergeometric/logpmf' ); 48 ``` 49 50 #### logpmf( x, N, K, n ) 51 52 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). 53 54 ```javascript 55 var y = logpmf( 1.0, 8, 4, 2 ); 56 // returns ~-0.56 57 58 y = logpmf( 2.0, 8, 4, 2 ); 59 // returns ~-1.54 60 61 y = logpmf( 0.0, 8, 4, 2 ); 62 // returns ~-1.54 63 64 y = logpmf( 1.5, 8, 4, 2 ); 65 // returns -Infinity 66 ``` 67 68 If provided `NaN` as any argument, the function returns `NaN`. 69 70 ```javascript 71 var y = logpmf( NaN, 10, 5, 2 ); 72 // returns NaN 73 74 y = logpmf( 0.0, NaN, 5, 2 ); 75 // returns NaN 76 77 y = logpmf( 0.0, 10, NaN, 2 ); 78 // returns NaN 79 80 y = logpmf( 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 = logpmf( 2.0, 10.5, 5, 2 ); 88 // returns NaN 89 90 y = logpmf( 2.0, 10, 1.5, 2 ); 91 // returns NaN 92 93 y = logpmf( 2.0, 10, 5, -2.0 ); 94 // returns NaN 95 ``` 96 97 If the number of draws `n` or the subpopulation size `K` exceed population size `N`, the function returns `NaN`. 98 99 ```javascript 100 var y = logpmf( 2.0, 10, 5, 12 ); 101 // returns NaN 102 103 y = logpmf( 2.0, 8, 3, 9 ); 104 // returns NaN 105 ``` 106 107 #### logpmf.factory( N, K, n ) 108 109 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). 110 111 ```javascript 112 var mylogpmf = logpmf.factory( 30, 20, 5 ); 113 var y = mylogpmf( 4.0 ); 114 // returns ~-1.079 115 116 y = mylogpmf( 1.0 ); 117 // returns ~-3.524 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 logpmf = require( '@stdlib/stats/base/dists/hypergeometric/logpmf' ); 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 = logpmf( x, N, K, n ); 148 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 ) ); 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 -->