README.md (5712B)
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 # Moment-Generating Function 22 23 > [Negative binomial][negative-binomial-distribution] distribution moment-generating function (MGF). 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 The [moment-generating function][mgf] for a [negative binomial][negative-binomial-distribution] random variable is 30 31 <!-- <equation class="equation" label="eq:negative_binomial_mgf_function" align="center" raw="M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = \biggl(\frac{\left( 1- p \right) e^t }{1 - p e^t}\biggr)^{\!r} \text{ for }t<-\log p" alt="Moment-generating function (MGF) for a negative binomial distribution."> --> 32 33 <div class="equation" align="center" data-raw-text="M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = \biggl(\frac{\left( 1- p \right) e^t }{1 - p e^t}\biggr)^{\!r} \text{ for }t<-\log p" data-equation="eq:negative_binomial_mgf_function"> 34 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@591cf9d5c3a0cd3c1ceec961e5c49d73a68374cb/lib/node_modules/@stdlib/stats/base/dists/negative-binomial/mgf/docs/img/equation_negative_binomial_mgf_function.svg" alt="Moment-generating function (MGF) for a negative binomial distribution."> 35 <br> 36 </div> 37 38 <!-- </equation> --> 39 40 where `r > 0` is the number of failures until the experiment is stopped and `0 <= p <= 1` is the success probability. 41 42 </section> 43 44 <!-- /.intro --> 45 46 <!-- Package usage documentation. --> 47 48 <section class="usage"> 49 50 ## Usage 51 52 ```javascript 53 var mgf = require( '@stdlib/stats/base/dists/negative-binomial/mgf' ); 54 ``` 55 56 #### mgf( t, r, p ) 57 58 Evaluates the [moment-generating function][mgf] for a [negative binomial][negative-binomial-distribution] distribution with number of successes until experiment is stopped `r` and success probability `p`. 59 60 ```javascript 61 var y = mgf( 0.05, 20.0, 0.8 ); 62 // returns ~267.839 63 64 y = mgf( 0.1, 20.0, 0.1 ); 65 // returns ~9.347 66 ``` 67 68 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]. 69 70 ```javascript 71 var y = mgf( 0.1, 15.5, 0.5 ); 72 // returns ~26.375 73 74 y = mgf( 0.5, 7.4, 0.4 ); 75 // returns ~2675.677 76 ``` 77 78 If `t >= -ln( p )`, the function returns `NaN`. 79 80 ```javascript 81 var y = mgf( 0.7, 15.5, 0.5 ); // -ln( p ) = ~0.693 82 // returns NaN 83 ``` 84 85 If provided a `r` which is not a positive number, the function returns `NaN`. 86 87 ```javascript 88 var y = mgf( 0.2, 0.0, 0.5 ); 89 // returns NaN 90 91 y = mgf( 0.2, -2.0, 0.5 ); 92 // returns NaN 93 ``` 94 95 If provided `NaN` as any argument, the function returns `NaN`. 96 97 ```javascript 98 var y = mgf( NaN, 20.0, 0.5 ); 99 // returns NaN 100 101 y = mgf( 0.0, NaN, 0.5 ); 102 // returns NaN 103 104 y = mgf( 0.0, 20.0, NaN ); 105 // returns NaN 106 ``` 107 108 If provided a success probability `p` outside of `[0,1]`, the function returns `NaN`. 109 110 ```javascript 111 var y = mgf( 0.2, 20, -1.0 ); 112 // returns NaN 113 114 y = mgf( 0.2, 20, 1.5 ); 115 // returns NaN 116 ``` 117 118 #### mgf.factory( r, p ) 119 120 Returns a function for evaluating the [moment-generating function][mgf] of a [negative binomial][negative-binomial-distribution] distribution with number of successes until experiment is stopped `r` and success probability `p`. 121 122 ```javascript 123 var myMGF = mgf.factory( 4.3, 0.4 ); 124 var y = myMGF( 0.2 ); 125 // returns ~4.696 126 127 y = myMGF( 0.4 ); 128 // returns ~30.83 129 ``` 130 131 </section> 132 133 <!-- /.usage --> 134 135 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 136 137 <section class="notes"> 138 139 </section> 140 141 <!-- /.notes --> 142 143 <!-- Package usage examples. --> 144 145 <section class="examples"> 146 147 ## Examples 148 149 <!-- eslint no-undef: "error" --> 150 151 ```javascript 152 var randu = require( '@stdlib/random/base/randu' ); 153 var round = require( '@stdlib/math/base/special/round' ); 154 var mgf = require( '@stdlib/stats/base/dists/negative-binomial/mgf' ); 155 156 var p; 157 var r; 158 var t; 159 var y; 160 var i; 161 162 for ( i = 0; i < 10; i++ ) { 163 t = (randu() * 1.0) - 0.5; 164 r = randu() * 50; 165 p = randu(); 166 y = mgf( t, r, p ); 167 console.log( 't: %d, r: %d, p: %d, M_X(t;r,p): %d', t, r.toFixed( 4 ), p.toFixed( 4 ), y.toFixed( 4 ) ); 168 } 169 ``` 170 171 </section> 172 173 <!-- /.examples --> 174 175 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 176 177 <section class="references"> 178 179 </section> 180 181 <!-- /.references --> 182 183 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 184 185 <section class="links"> 186 187 [mgf]: https://en.wikipedia.org/wiki/Moment-generating_function 188 189 [negative-binomial-mixture-representation]: https://en.wikipedia.org/wiki/Negative_binomial_distribution#Gamma.E2.80.93Poisson_mixture 190 191 [negative-binomial-distribution]: https://en.wikipedia.org/wiki/Negative_binomial_distribution 192 193 </section> 194 195 <!-- /.links -->