README.md (4819B)
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 # Cumulative Distribution Function 22 23 > [Negative binomial][negative-binomial-distribution] distribution [cumulative distribution function][cdf]. 24 25 <section class="intro"> 26 27 The [cumulative distribution function][cdf] for a [negative binomial][negative-binomial-distribution] random variable `X` is 28 29 <!-- <equation class="equation" label="eq:negative_binomial_cdf" align="center" raw="F(x;r,p)=1-I_p(x+1,r)" alt="Cumulative distribution function for a negative binomial distribution."> --> 30 31 <div class="equation" align="center" data-raw-text="F(x;r,p)=1-I_p(x+1,r)" data-equation="eq:negative_binomial_cdf"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/negative-binomial/cdf/docs/img/equation_negative_binomial_cdf.svg" alt="Cumulative distribution function for a negative binomial distribution."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 where `r` is the number of successes until experiment is stopped, `p` is the success probability in each trial and `I` is the [lower regularized incomplete beta function][incomplete-beta]. 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 cdf = require( '@stdlib/stats/base/dists/negative-binomial/cdf' ); 50 ``` 51 52 #### cdf( x, r, p ) 53 54 Evaluates the [cumulative distribution function][cdf] 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 = cdf( 5.0, 20.0, 0.8 ); 58 // returns ~0.617 59 60 y = cdf( 21.0, 20.0, 0.5 ); 61 // returns ~0.622 62 63 y = cdf( 5.0, 10.0, 0.4 ); 64 // returns ~0.034 65 66 y = cdf( 0.0, 10.0, 0.9 ); 67 // returns ~0.349 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 = cdf( 21.0, 15.5, 0.5 ); 74 // returns ~0.859 75 76 y = cdf( 5.0, 7.4, 0.4 ); 77 // returns ~0.131 78 ``` 79 80 If provided a `r` which is not a positive number, the function returns `NaN`. 81 82 ```javascript 83 var y = cdf( 2.0, 0.0, 0.5 ); 84 // returns NaN 85 86 y = cdf( 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 = cdf( NaN, 20.0, 0.5 ); 94 // returns NaN 95 96 y = cdf( 0.0, NaN, 0.5 ); 97 // returns NaN 98 99 y = cdf( 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 = cdf( 2.0, 20, -1.0 ); 107 // returns NaN 108 109 y = cdf( 2.0, 20, 1.5 ); 110 // returns NaN 111 ``` 112 113 #### cdf.factory( r, p ) 114 115 Returns a function for evaluating the [cumulative distribution function][cdf] 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 mycdf = cdf.factory( 10, 0.5 ); 119 var y = mycdf( 3.0 ); 120 // returns ~0.046 121 122 y = mycdf( 11.0 ); 123 // returns ~0.668 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 cdf = require( '@stdlib/stats/base/dists/negative-binomial/cdf' ); 139 140 var i; 141 var r; 142 var p; 143 var x; 144 var y; 145 146 for ( i = 0; i < 10; i++ ) { 147 x = randu() * 50; 148 r = randu() * 50; 149 p = randu(); 150 y = cdf( x, r, p ); 151 console.log( 'x: %d, r: %d, p: %d, F(x;r,p): %d', x.toFixed( 4 ), r.toFixed( 4 ), p.toFixed( 4 ), y.toFixed( 4 ) ); 152 } 153 ``` 154 155 </section> 156 157 <!-- /.examples --> 158 159 <section class="links"> 160 161 [cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function 162 163 [incomplete-beta]: https://en.wikipedia.org/wiki/Beta_function#Incomplete_beta_function 164 165 [negative-binomial-mixture-representation]: https://en.wikipedia.org/wiki/Negative_binomial_distribution#Gamma.E2.80.93Poisson_mixture 166 167 [negative-binomial-distribution]: https://en.wikipedia.org/wiki/Negative_binomial_distribution 168 169 </section> 170 171 <!-- /.links -->