README.md (3561B)
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 > [Bernoulli][bernoulli-distribution] distribution [probability mass function][pmf] (PMF). 24 25 <section class="intro"> 26 27 The [probability mass function][pmf] (PMF) for a [Bernoulli][bernoulli-distribution] random variable is defined as 28 29 <!-- <equation class="equation" label="eq:bernoulli_pmf" align="center" raw="\Pr(X = x) = \begin{cases} 1-p & \text{ for } x = 0 \\ p & \text{ for } x = 1 \\ 0 & \text{ otherwise } \end{cases}" alt="Probability mass function (PMF) for a Bernoulli distribution."> --> 30 31 <div class="equation" align="center" data-raw-text="\Pr(X = x) = \begin{cases} 1-p & \text{ for } x = 0 \\ p & \text{ for } x = 1 \\ 0 & \text{ otherwise } \end{cases}" data-equation="eq:bernoulli_pmf"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@591cf9d5c3a0cd3c1ceec961e5c49d73a68374cb/lib/node_modules/@stdlib/stats/base/dists/bernoulli/pmf/docs/img/equation_bernoulli_pmf.svg" alt="Probability mass function (PMF) for a Bernoulli distribution."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 where `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/bernoulli/pmf' ); 50 ``` 51 52 #### pmf( x, p ) 53 54 Evaluates the [probability mass function][pmf] (PMF) of a [Bernoulli][bernoulli-distribution] distribution with success probability `0 <= p <= 1`. 55 56 ```javascript 57 var y = pmf( 1.0, 0.3 ); 58 // returns 0.3 59 60 y = pmf( 0.0, 0.3 ); 61 // returns 0.7 62 63 y = pmf( -1.0, 0.5 ); 64 // returns 0.0 65 ``` 66 67 If provided `NaN` as any argument, the function returns `NaN`. 68 69 ```javascript 70 var y = pmf( NaN, 0.0 ); 71 // returns NaN 72 73 y = pmf( 0.0, NaN ); 74 // returns NaN 75 ``` 76 77 If provided a success probability `p` outside of the interval `[0,1]`, the function returns `NaN`. 78 79 ```javascript 80 var y = pmf( 0.0, -1.0 ); 81 // returns NaN 82 83 y = pmf( 0.0, 1.5 ); 84 // returns NaN 85 ``` 86 87 #### pmf.factory( p ) 88 89 Returns a function for evaluating the [probability mass function][pmf] (PMF) of a [Bernoulli][bernoulli-distribution] distribution with success probability `0 <= p <= 1`. 90 91 ```javascript 92 var mypmf = pmf.factory( 0.8 ); 93 var y = mypmf( 0.0 ); 94 // returns 0.2 95 96 y = mypmf( 0.5 ); 97 // returns 0.0 98 ``` 99 100 </section> 101 102 <!-- /.usage --> 103 104 <section class="examples"> 105 106 ## Examples 107 108 <!-- eslint no-undef: "error" --> 109 110 ```javascript 111 var randu = require( '@stdlib/random/base/randu' ); 112 var round = require( '@stdlib/math/base/special/round' ); 113 var pmf = require( '@stdlib/stats/base/dists/bernoulli/pmf' ); 114 115 var p; 116 var x; 117 var y; 118 var i; 119 120 for ( i = 0; i < 10; i++ ) { 121 x = round( randu() * 2.0 ); 122 p = randu(); 123 y = pmf( x, p ); 124 console.log( 'x: %d, p: %d, P( X = x; p ): %d', x, p.toFixed( 4 ), y.toFixed( 4 ) ); 125 } 126 ``` 127 128 </section> 129 130 <!-- /.examples --> 131 132 <section class="links"> 133 134 [bernoulli-distribution]: https://en.wikipedia.org/wiki/Bernoulli_distribution 135 136 [pmf]: https://en.wikipedia.org/wiki/Probability_mass_function 137 138 </section> 139 140 <!-- /.links -->