time-to-botec

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

README.md (4029B)


      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 > [Binomial][binomial-distribution] distribution probability mass function (PMF).
     24 
     25 <section class="intro">
     26 
     27 The [probability mass function][pmf] (PMF) for a [binomial][binomial-distribution] random variable is
     28 
     29 <!-- <equation class="equation" label="eq:binomial_pmf" align="center" raw="f(x;n,p)=P(X=x;n,p)=\begin{cases} \textstyle {n \choose x}\, p^x (1-p)^{n-x} & \text{ for } x = 0,1,2,\ldots \\ 0 & \text{ otherwise} \end{cases}" alt="Probability mass function (PMF) for a binomial distribution."> -->
     30 
     31 <div class="equation" align="center" data-raw-text="f(x;n,p)=P(X=x;n,p)=\begin{cases} \textstyle {n \choose x}\, p^x (1-p)^{n-x} &amp; \text{ for } x = 0,1,2,\ldots \\ 0 &amp; \text{ otherwise} \end{cases}" data-equation="eq:binomial_pmf">
     32     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/docs/img/equation_binomial_pmf.svg" alt="Probability mass function (PMF) for a binomial distribution.">
     33     <br>
     34 </div>
     35 
     36 <!-- </equation> -->
     37 
     38 where `n` is the number of trials and `0 <= p <= 1` is the success probability.
     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/binomial/pmf' );
     50 ```
     51 
     52 #### pmf( x, n, p )
     53 
     54 Evaluates the [probability mass function][pmf] (PMF) for a [binomial][binomial-distribution] distribution with number of trials `n` and success probability `p`.
     55 
     56 ```javascript
     57 var y = pmf( 3.0, 20, 0.2 );
     58 // returns ~0.205
     59 
     60 y = pmf( 21.0, 20, 0.2 );
     61 // returns 0.0
     62 
     63 y = pmf( 5.0, 10, 0.4 );
     64 // returns ~0.201
     65 
     66 y = pmf( 0.0, 10, 0.4 );
     67 // returns ~0.006
     68 ```
     69 
     70 If provided `NaN` as any argument, the function returns `NaN`.
     71 
     72 ```javascript
     73 var y = pmf( NaN, 20, 0.5 );
     74 // returns NaN
     75 
     76 y = pmf( 0.0, NaN, 0.5 );
     77 // returns NaN
     78 
     79 y = pmf( 0.0, 20, NaN );
     80 // returns NaN
     81 ```
     82 
     83 If provided a number of trials `n` which is not a nonnegative integer, the function returns `NaN`.
     84 
     85 ```javascript
     86 var y = pmf( 2.0, 1.5, 0.5 );
     87 // returns NaN
     88 
     89 y = pmf( 2.0, -2.0, 0.5 );
     90 // returns NaN
     91 ```
     92 
     93 If provided a success probability `p` outside of `[0,1]`, the function returns `NaN`.
     94 
     95 ```javascript
     96 var y = pmf( 2.0, 20, -1.0 );
     97 // returns NaN
     98 
     99 y = pmf( 2.0, 20, 1.5 );
    100 // returns NaN
    101 ```
    102 
    103 #### pmf.factory( n, p )
    104 
    105 Returns a function for evaluating the [probability mass function][pmf] (PMF) of a [binomial][binomial-distribution] distribution with number of trials `n` and success probability `p`.
    106 
    107 ```javascript
    108 var mypmf = pmf.factory( 10, 0.5 );
    109 
    110 var y = mypmf( 3.0 );
    111 // returns ~0.117
    112 
    113 y = mypmf( 5.0 );
    114 // returns ~0.246
    115 ```
    116 
    117 </section>
    118 
    119 <!-- /.usage -->
    120 
    121 <section class="examples">
    122 
    123 ## Examples
    124 
    125 <!-- eslint no-undef: "error" -->
    126 
    127 ```javascript
    128 var randu = require( '@stdlib/random/base/randu' );
    129 var round = require( '@stdlib/math/base/special/round' );
    130 var pmf = require( '@stdlib/stats/base/dists/binomial/pmf' );
    131 
    132 var i;
    133 var n;
    134 var p;
    135 var x;
    136 var y;
    137 
    138 for ( i = 0; i < 10; i++ ) {
    139     x = round( randu() * 20.0 );
    140     n = round( randu() * 100.0 );
    141     p = randu();
    142     y = pmf( x, n, p );
    143     console.log( 'x: %d, n: %d, p: %d, P(X = x;n,p): %d', x, n, p.toFixed( 4 ), y.toFixed( 4 ) );
    144 }
    145 ```
    146 
    147 </section>
    148 
    149 <!-- /.examples -->
    150 
    151 <section class="links">
    152 
    153 [binomial-distribution]: https://en.wikipedia.org/wiki/Binomial_distribution
    154 
    155 [pmf]: https://en.wikipedia.org/wiki/Probability_mass_function
    156 
    157 </section>
    158 
    159 <!-- /.links -->