time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (4811B)


      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 # Probability Mass Function
     22 
     23 > [Negative binomial][negative-binomial-distribution] distribution [probability mass function][pmf] (PMF).
     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/pmf/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 pmf = require( '@stdlib/stats/base/dists/negative-binomial/pmf' );
     50 ```
     51 
     52 #### pmf( x, r, p )
     53 
     54 Evaluates 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 = pmf( 5.0, 20.0, 0.8 );
     58 // returns ~0.157
     59 
     60 y = pmf( 21.0, 20.0, 0.5 );
     61 // returns ~0.06
     62 
     63 y = pmf( 5.0, 10.0, 0.4 );
     64 // returns ~0.016
     65 
     66 y = pmf( 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 = pmf( 21.0, 15.5, 0.5 );
     74 // returns ~0.037
     75 
     76 y = pmf( 5.0, 7.4, 0.4 );
     77 // returns ~0.051
     78 ```
     79 
     80 If provided a `r` which is not a positive number, the function returns `NaN`.
     81 
     82 ```javascript
     83 var y = pmf( 2.0, 0.0, 0.5 );
     84 // returns NaN
     85 
     86 y = pmf( 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 = pmf( NaN, 20.0, 0.5 );
     94 // returns NaN
     95 
     96 y = pmf( 0.0, NaN, 0.5 );
     97 // returns NaN
     98 
     99 y = pmf( 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 = pmf( 2.0, 20, -1.0 );
    107 // returns NaN
    108 
    109 y = pmf( 2.0, 20, 1.5 );
    110 // returns NaN
    111 ```
    112 
    113 #### pmf.factory( r, p )
    114 
    115 Returns a function for evaluating 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 mypmf = pmf.factory( 10, 0.5 );
    119 var y = mypmf( 3.0 );
    120 // returns ~0.03
    121 
    122 y = mypmf( 10.0 );
    123 // returns ~0.088
    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 pmf = require( '@stdlib/stats/base/dists/negative-binomial/pmf' );
    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 );
    149     r = randu() * 50;
    150     p = randu();
    151     y = pmf( x, r, p );
    152     console.log( 'x: %d, r: %d, p: %d, 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 -->