README.md (4658B)
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 > [Binomial][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 [binomial][binomial-distribution] random variable is 30 31 <!-- <equation class="equation" label="eq:binomial_mgf" align="center" raw="M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = (1-p + pe^t)^n" alt="Moment-generating function (MGF) for a binomial distribution."> --> 32 33 <div class="equation" align="center" data-raw-text="M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = (1-p + pe^t)^n" data-equation="eq:binomial_mgf"> 34 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/binomial/mgf/docs/img/equation_binomial_mgf.svg" alt="Moment-generating function (MGF) for a binomial distribution."> 35 <br> 36 </div> 37 38 <!-- </equation> --> 39 40 where the nonnegative integer `n` is the number of trials 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/binomial/mgf' ); 54 ``` 55 56 #### mgf( t, n, p ) 57 58 Evaluates the [moment-generating function][mgf] for a [binomial][binomial-distribution] distribution with number of trials `n` and success probability `p`. 59 60 ```javascript 61 var y = mgf( 0.5, 20, 0.2 ); 62 // returns ~11.471 63 64 y = mgf( 5.0, 20, 0.2 ); 65 // returns ~4.798e29 66 67 y = mgf( 0.9, 10, 0.4 ); 68 // returns ~99.338 69 70 y = mgf( 0.0, 10, 0.4 ); 71 // returns 1.0 72 ``` 73 74 If provided `NaN` as any argument, the function returns `NaN`. 75 76 ```javascript 77 var y = mgf( NaN, 20, 0.5 ); 78 // returns NaN 79 80 y = mgf( 0.0, NaN, 0.5 ); 81 // returns NaN 82 83 y = mgf( 0.0, 20, NaN ); 84 // returns NaN 85 ``` 86 87 If provided a number of trials `n` which is not a nonnegative integer, the function returns `NaN`. 88 89 ```javascript 90 var y = mgf( 0.2, 1.5, 0.5 ); 91 // returns NaN 92 93 y = mgf( 0.2, -2.0, 0.5 ); 94 // returns NaN 95 ``` 96 97 If provided a success probability `p` outside of `[0,1]`, the function returns `NaN`. 98 99 ```javascript 100 var y = mgf( 0.2, 20, -1.0 ); 101 // returns NaN 102 103 y = mgf( 0.2, 20, 1.5 ); 104 // returns NaN 105 ``` 106 107 #### mgf.factory( n, p ) 108 109 Returns a function for evaluating the [moment-generating function][mgf] of a [binomial][binomial-distribution] distribution with number of trials `n` and success probability `p`. 110 111 ```javascript 112 var myMGF = mgf.factory( 10, 0.5 ); 113 114 var y = myMGF( 0.3 ); 115 // returns ~5.013 116 ``` 117 118 </section> 119 120 <!-- /.usage --> 121 122 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 123 124 <section class="notes"> 125 126 </section> 127 128 <!-- /.notes --> 129 130 <!-- Package usage examples. --> 131 132 <section class="examples"> 133 134 ## Examples 135 136 <!-- eslint no-undef: "error" --> 137 138 ```javascript 139 var randu = require( '@stdlib/random/base/randu' ); 140 var round = require( '@stdlib/math/base/special/round' ); 141 var mgf = require( '@stdlib/stats/base/dists/binomial/mgf' ); 142 143 var n; 144 var p; 145 var t; 146 var y; 147 var i; 148 149 for ( i = 0; i < 10; i++ ) { 150 t = round( randu() * 5.0 ); 151 n = round( randu() * 10.0 ); 152 p = randu(); 153 y = mgf( t, n, p ); 154 console.log( 't: %d, n: %d, p: %d, M_X(t;n,p): %d', t, n, p.toFixed( 4 ), y.toFixed( 4 ) ); 155 } 156 ``` 157 158 </section> 159 160 <!-- /.examples --> 161 162 <!-- 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. --> 163 164 <section class="references"> 165 166 </section> 167 168 <!-- /.references --> 169 170 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 171 172 <section class="links"> 173 174 [binomial-distribution]: https://en.wikipedia.org/wiki/Binomial_distribution 175 176 [mgf]: https://en.wikipedia.org/wiki/Moment-generating_function 177 178 </section> 179 180 <!-- /.links -->