README.md (4728B)
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 Density Function 22 23 > [Triangular][triangular-distribution] distribution logarithm of [probability density function][pdf] (PDF). 24 25 <section class="intro"> 26 27 The [probability density function][pdf] (PDF) for a [triangular][triangular-distribution] random variable is 28 29 <!-- <equation class="equation" label="eq:triangular_pdf" align="center" raw="f(x;a,b,c)=\begin{cases} 0 & \text{for } x < a \\ \frac{2(x-a)}{(b-a)(c-a)} & \text{for } a \le x < c \\ \frac{2}{b-a} & \text{for } x = c \\ \frac{2(b-x)}{(b-a)(b-c)} & \text{for } c < x \le b \\ 0 & \text{for } b < x \end{cases}" alt="Probability density function (PDF) for a triangular distribution."> --> 30 31 <div class="equation" align="center" data-raw-text="f(x;a,b,c)=\begin{cases} 0 & \text{for } x < a \\ \frac{2(x-a)}{(b-a)(c-a)} & \text{for } a \le x < c \\ \frac{2}{b-a} & \text{for } x = c \\ \frac{2(b-x)}{(b-a)(b-c)} & \text{for } c < x \le b \\ 0 & \text{for } b < x \end{cases}" data-equation="eq:triangular_pdf"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/triangular/logpdf/docs/img/equation_triangular_pdf.svg" alt="Probability density function (PDF) for a triangular distribution."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 where `a` is the lower limit and `b` is the upper limit and `c` is the mode. 39 40 </section> 41 42 <!-- /.intro --> 43 44 <section class="usage"> 45 46 ## Usage 47 48 ```javascript 49 var logpdf = require( '@stdlib/stats/base/dists/triangular/logpdf' ); 50 ``` 51 52 #### logpdf( x, a, b, c ) 53 54 Evaluates the natural logarithm of the [probability density function][pdf] (PDF) for a [triangular][triangular-distribution] distribution with parameters `a` (lower limit), `b` (upper limit) and `c` (mode). 55 56 ```javascript 57 var y = logpdf( 0.5, -1.0, 1.0, 0.0 ); 58 // returns ~-0.693 59 60 y = logpdf( 0.5, -1.0, 1.0, 0.5 ); 61 // returns 0.0 62 63 y = logpdf( -10.0, -20.0, 0.0, -2.0 ); 64 // returns ~-2.89 65 66 y = logpdf( -2.0, -1.0, 1.0, 0.0 ); 67 // returns -Infinity 68 ``` 69 70 If provided `NaN` as any argument, the function returns `NaN`. 71 72 ```javascript 73 var y = logpdf( NaN, 0.0, 1.0, 0.5 ); 74 // returns NaN 75 76 y = logpdf( 0.0, NaN, 1.0, 0.5 ); 77 // returns NaN 78 79 y = logpdf( 0.0, 0.0, NaN, 0.5 ); 80 // returns NaN 81 82 y = logpdf( 2.0, 1.0, 0.0, NaN ); 83 // returns NaN 84 ``` 85 86 If provided parameters not satisfying `a <= c <= b`, the function returns `NaN`. 87 88 ```javascript 89 var y = logpdf( 1.0, 1.0, 0.0, 1.5 ); 90 // returns NaN 91 92 y = logpdf( 1.0, 1.0, 0.0, -1.0 ); 93 // returns NaN 94 95 y = logpdf( 1.0, 0.0, -1.0, 0.5 ); 96 // returns NaN 97 ``` 98 99 #### logpdf.factory( a, b, c ) 100 101 Returns a function for evaluating the natural logarithm of the [probability density function][pdf] (PDF) of a [triangular][triangular-distribution] distribution with parameters `a` (lower limit), `b` (upper limit) and `c` (mode). 102 103 ```javascript 104 var mylogpdf = logpdf.factory( 0.0, 10.0, 5.0 ); 105 var y = mylogpdf( 2.0 ); 106 // returns ~-2.526 107 108 y = mylogpdf( 12.0 ); 109 // returns -Infinity 110 ``` 111 112 </section> 113 114 <!-- /.usage --> 115 116 <section class="notes"> 117 118 ## Notes 119 120 - In virtually all cases, using the `logpdf` or `logcdf` functions is preferable to manually computing the logarithm of the `pdf` or `cdf`, respectively, since the latter is prone to overflow and underflow. 121 122 </section> 123 124 <!-- /.notes --> 125 126 <section class="examples"> 127 128 ## Examples 129 130 <!-- eslint no-undef: "error" --> 131 132 ```javascript 133 var randu = require( '@stdlib/random/base/randu' ); 134 var logpdf = require( '@stdlib/stats/base/dists/triangular/logpdf' ); 135 136 var a; 137 var b; 138 var c; 139 var x; 140 var y; 141 var i; 142 143 for ( i = 0; i < 25; i++ ) { 144 x = randu() * 30.0; 145 a = randu() * 10.0; 146 b = a + (randu() * 40.0); 147 c = a + ((b-a) * randu()); 148 y = logpdf( x, a, b, c ); 149 console.log( 'x: %d, a: %d, b: %d, c: %d, ln(f(x;a,b,c)): %d', x.toFixed( 4 ), a.toFixed( 4 ), b.toFixed( 4 ), c.toFixed( 4 ), y.toFixed( 4 ) ); 150 } 151 ``` 152 153 </section> 154 155 <!-- /.examples --> 156 157 <section class="links"> 158 159 [pdf]: https://en.wikipedia.org/wiki/Probability_density_function 160 161 [triangular-distribution]: https://en.wikipedia.org/wiki/Triangular_distribution 162 163 </section> 164 165 <!-- /.links -->