README.md (5010B)
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 [negative binomial][negative-binomial-distribution] distribution. 24 25 <section class="intro"> 26 27 The [probability mass function][pmf] (PMF) for a [negative binomial][negative-binomial-distribution] random variable `X` is 28 29 <!-- <equation class="equation" label="eq:negative_binomial_pmf" align="center" raw="f(x; r, p) = P(X = x; r,p) = \binom{k+r-1}{x} p^r(1-p)^x \quad\text{for }x = 0, 1, 2, \dotsc" alt="Probability mass function (PMF) for a negative binomial distribution."> --> 30 31 <div class="equation" align="center" data-raw-text="f(x; r, p) = P(X = x; r,p) = \binom{k+r-1}{x} p^r(1-p)^x \quad\text{for }x = 0, 1, 2, \dotsc" data-equation="eq:negative_binomial_pmf"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/docs/img/equation_negative_binomial_pmf.svg" alt="Probability mass function (PMF) for a negative binomial distribution."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 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. 39 40 </section> 41 42 <!-- /.intro --> 43 44 <section class="usage"> 45 46 ## Usage 47 48 ```javascript 49 var logpmf = require( '@stdlib/stats/base/dists/negative-binomial/logpmf' ); 50 ``` 51 52 #### logpmf( x, r, p ) 53 54 Evaluates the natural logarithm of the [probability mass function][pmf] for a [negative binomial][negative-binomial-distribution] distribution with number of successes until experiment is stopped `r` and success probability `p`. 55 56 ```javascript 57 var y = logpmf( 5.0, 20.0, 0.8 ); 58 // returns ~-1.853 59 60 y = logpmf( 21.0, 20.0, 0.5 ); 61 // returns ~-2.818 62 63 y = logpmf( 5.0, 10.0, 0.4 ); 64 // returns ~-4.115 65 66 y = logpmf( 0.0, 10.0, 0.9 ); 67 // returns ~-1.054 68 ``` 69 70 While `r` can be interpreted as the number of successes until the experiment is stopped, the [negative binomial][negative-binomial-distribution] distribution is also defined for non-integers `r`. In this case, `r` denotes shape parameter of the [gamma mixing distribution][negative-binomial-mixture-representation]. 71 72 ```javascript 73 var y = logpmf( 21.0, 15.5, 0.5 ); 74 // returns ~-3.292 75 76 y = logpmf( 5.0, 7.4, 0.4 ); 77 // returns ~-2.976 78 ``` 79 80 If provided a `r` which is not a positive number, the function returns `NaN`. 81 82 ```javascript 83 var y = logpmf( 2.0, 0.0, 0.5 ); 84 // returns NaN 85 86 y = logpmf( 2.0, -2.0, 0.5 ); 87 // returns NaN 88 ``` 89 90 If provided `NaN` as any argument, the function returns `NaN`. 91 92 ```javascript 93 var y = logpmf( NaN, 20.0, 0.5 ); 94 // returns NaN 95 96 y = logpmf( 0.0, NaN, 0.5 ); 97 // returns NaN 98 99 y = logpmf( 0.0, 20.0, NaN ); 100 // returns NaN 101 ``` 102 103 If provided a success probability `p` outside of `[0,1]`, the function returns `NaN`. 104 105 ```javascript 106 var y = logpmf( 2.0, 20, -1.0 ); 107 // returns NaN 108 109 y = logpmf( 2.0, 20, 1.5 ); 110 // returns NaN 111 ``` 112 113 #### logpmf.factory( r, p ) 114 115 Returns a function for evaluating the natural logarithm of the [probability mass function][pmf] (PMF) of a [negative binomial][negative-binomial-distribution] distribution with number of successes until experiment is stopped `r` and success probability `p`. 116 117 ```javascript 118 var mylogpmf = logpmf.factory( 10, 0.5 ); 119 var y = mylogpmf( 3.0 ); 120 // returns ~-3.617 121 122 y = mylogpmf( 10.0 ); 123 // returns ~-2.43 124 ``` 125 126 </section> 127 128 <!-- /.usage --> 129 130 <section class="examples"> 131 132 ## Examples 133 134 <!-- eslint no-undef: "error" --> 135 136 ```javascript 137 var randu = require( '@stdlib/random/base/randu' ); 138 var round = require( '@stdlib/math/base/special/round' ); 139 var logpmf = require( '@stdlib/stats/base/dists/negative-binomial/logpmf' ); 140 141 var i; 142 var r; 143 var p; 144 var x; 145 var y; 146 147 for ( i = 0; i < 10; i++ ) { 148 x = round( randu() * 30.0 ); 149 r = randu() * 50.0; 150 p = randu(); 151 y = logpmf( x, r, p ); 152 console.log( 'x: %d, r: %d, p: %d, ln(P(X=x;r,p)): %d', x, r, p.toFixed( 4 ), y.toFixed( 4 ) ); 153 } 154 ``` 155 156 </section> 157 158 <!-- /.examples --> 159 160 <section class="links"> 161 162 [negative-binomial-mixture-representation]: https://en.wikipedia.org/wiki/Negative_binomial_distribution#Gamma.E2.80.93Poisson_mixture 163 164 [negative-binomial-distribution]: https://en.wikipedia.org/wiki/Negative_binomial_distribution 165 166 [pmf]: https://en.wikipedia.org/wiki/Probability_mass_function 167 168 </section> 169 170 <!-- /.links -->