time-to-botec

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

README.md (4144B)


      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 # Logarithm of Probability Mass Function
     22 
     23 > Evaluate the natural logarithm of the [probability mass function][pmf] (PMF) for a [discrete uniform][discrete-uniform-distribution] distribution.
     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} &amp; \text{for } x \in \{ a, \ldots, b \} \\ 0 &amp; \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/logpmf/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 logpmf = require( '@stdlib/stats/base/dists/discrete-uniform/logpmf' );
     50 ```
     51 
     52 #### logpmf( x, a, b )
     53 
     54 Evaluates the natural logarithm of 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 = logpmf( 2.0, 0, 4 );
     58 // returns ~-1.609
     59 
     60 y = logpmf( 5.0, 0, 4 );
     61 // returns -Infinity
     62 
     63 y = logpmf( 3, -4, 4 );
     64 // returns ~-2.197
     65 ```
     66 
     67 If provided `NaN` as any argument, the function returns `NaN`.
     68 
     69 ```javascript
     70 var y = logpmf( NaN, -2, 2 );
     71 // returns NaN
     72 
     73 y = logpmf( 1.0, NaN, 4 );
     74 // returns NaN
     75 
     76 y = logpmf( 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 = logpmf( 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 = logpmf( 2.0, 3, 2 );
     91 // returns NaN
     92 ```
     93 
     94 #### logpmf.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 myLogPMF = logpmf.factory( 6, 7 );
    100 var y = myLogPMF( 7.0 );
    101 // returns ~-0.693
    102 
    103 y = myLogPMF( 5.0 );
    104 // returns -Infinity
    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 logpmf = require( '@stdlib/stats/base/dists/discrete-uniform/logpmf' );
    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 = logpmf( x, a, b );
    134     console.log( 'x: %d, a: %d, b: %d, ln(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 -->