README.md (3978B)
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 > [Discrete uniform][discrete-uniform-distribution] distribution [probability mass function][pmf] (PMF). 24 25 <section class="intro"> 26 27 The [probability mass function][pmf] (PMF) for a [discrete uniform][discrete-uniform-distribution] random variable is 28 29 <!-- <equation class="equation" label="eq:discrete_uniform_pmf" align="center" raw="P(X=x;a,b)=\begin{cases} \frac{1}{b - a + 1} & \text{for } x \in \{ a, \ldots, b \} \\ 0 & \text{otherwise} \end{cases}" alt="Probability mass function (PMF) for a discrete uniform distribution."> --> 30 31 <div class="equation" align="center" data-raw-text="P(X=x;a,b)=\begin{cases} \frac{1}{b - a + 1} & \text{for } x \in \{ a, \ldots, b \} \\ 0 & \text{otherwise} \end{cases}" data-equation="eq:discrete_uniform_pmf"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@591cf9d5c3a0cd3c1ceec961e5c49d73a68374cb/lib/node_modules/@stdlib/stats/base/dists/discrete-uniform/pmf/docs/img/equation_discrete_uniform_pmf.svg" alt="Probability mass function (PMF) for a discrete uniform distribution."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 where `a` is the minimum support and `b` is the maximum support of the distribution. The parameters must satisfy `a <= b`. 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/discrete-uniform/pmf' ); 50 ``` 51 52 #### pmf( x, a, b ) 53 54 Evaluates the [probability mass function][pmf] (PMF) for a [discrete uniform][discrete-uniform-distribution] distribution with parameters `a` (minimum support) and `b` (maximum support). 55 56 ```javascript 57 var y = pmf( 2.0, 0, 4 ); 58 // returns ~0.2 59 60 y = pmf( 5.0, 0, 4 ); 61 // returns 0.0 62 63 y = pmf( 3, -4, 4 ); 64 // returns ~0.111 65 ``` 66 67 If provided `NaN` as any argument, the function returns `NaN`. 68 69 ```javascript 70 var y = pmf( NaN, -2, 2 ); 71 // returns NaN 72 73 y = pmf( 1.0, NaN, 4 ); 74 // returns NaN 75 76 y = pmf( 2.0, 0, NaN ); 77 // returns NaN 78 ``` 79 80 If `a` or `b` is not an integer value, the function returns `NaN`. 81 82 ```javascript 83 var y = pmf( 2.0, 1, 5.5 ); 84 // returns NaN 85 ``` 86 87 If provided `a > b`, the function returns `NaN`. 88 89 ```javascript 90 var y = pmf( 2.0, 3, 2 ); 91 // returns NaN 92 ``` 93 94 #### pmf.factory( a, b ) 95 96 Returns a `function` for evaluating the [PMF][pmf] for a [discrete uniform][discrete-uniform-distribution] distribution with parameters `a` (minimum support) and `b` (maximum support). 97 98 ```javascript 99 var myPDF = pmf.factory( 6, 7 ); 100 var y = myPDF( 7.0 ); 101 // returns 0.5 102 103 y = myPDF( 5.0 ); 104 // returns 0.0 105 ``` 106 107 </section> 108 109 <!-- /.usage --> 110 111 <section class="examples"> 112 113 ## Examples 114 115 <!-- eslint no-undef: "error" --> 116 117 ```javascript 118 var randint = require( '@stdlib/random/base/discrete-uniform' ); 119 var pmf = require( '@stdlib/stats/base/dists/discrete-uniform/pmf' ); 120 121 var randa = randint.factory( 0, 10 ); 122 var randb = randint.factory(); 123 var a; 124 var b; 125 var x; 126 var y; 127 var i; 128 129 for ( i = 0; i < 25; i++ ) { 130 a = randa(); 131 x = randb( a, a+randa() ); 132 b = randb( a, a+randa() ); 133 y = pmf( x, a, b ); 134 console.log( 'x: %d, a: %d, b: %d, P(X=x;a,b): %d', x.toFixed( 4 ), a.toFixed( 4 ), b.toFixed( 4 ), y.toFixed( 4 ) ); 135 } 136 ``` 137 138 </section> 139 140 <!-- /.examples --> 141 142 <section class="links"> 143 144 [pmf]: https://en.wikipedia.org/wiki/Probability_mass_function 145 146 [discrete-uniform-distribution]: https://en.wikipedia.org/wiki/Discrete_uniform_distribution 147 148 </section> 149 150 <!-- /.links -->