README.md (4285B)
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 Density Function 22 23 > [Triangular][triangular-distribution] distribution [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/pdf/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 pdf = require( '@stdlib/stats/base/dists/triangular/pdf' ); 50 ``` 51 52 #### pdf( x, a, b, c ) 53 54 Evaluates 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 = pdf( 0.5, -1.0, 1.0, 0.0 ); 58 // returns 0.5 59 60 y = pdf( 0.5, -1.0, 1.0, 0.5 ); 61 // returns 1.0 62 63 y = pdf( -10.0, -20.0, 0.0, -2.0 ); 64 // returns ~0.056 65 66 y = pdf( -2.0, -1.0, 1.0, 0.0 ); 67 // returns 0.0 68 ``` 69 70 If provided `NaN` as any argument, the function returns `NaN`. 71 72 ```javascript 73 var y = pdf( NaN, 0.0, 1.0, 0.5 ); 74 // returns NaN 75 76 y = pdf( 0.0, NaN, 1.0, 0.5 ); 77 // returns NaN 78 79 y = pdf( 0.0, 0.0, NaN, 0.5 ); 80 // returns NaN 81 82 y = pdf( 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 = pdf( 1.0, 1.0, 0.0, 1.5 ); 90 // returns NaN 91 92 y = pdf( 1.0, 1.0, 0.0, -1.0 ); 93 // returns NaN 94 95 y = pdf( 1.0, 0.0, -1.0, 0.5 ); 96 // returns NaN 97 ``` 98 99 #### pdf.factory( a, b, c ) 100 101 Returns a function for evaluating 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 mypdf = pdf.factory( 0.0, 10.0, 5.0 ); 105 var y = mypdf( 2.0 ); 106 // returns 0.08 107 108 y = mypdf( 12.0 ); 109 // returns 0.0 110 ``` 111 112 </section> 113 114 <!-- /.usage --> 115 116 <section class="examples"> 117 118 ## Examples 119 120 <!-- eslint no-undef: "error" --> 121 122 ```javascript 123 var randu = require( '@stdlib/random/base/randu' ); 124 var pdf = require( '@stdlib/stats/base/dists/triangular/pdf' ); 125 126 var a; 127 var b; 128 var c; 129 var x; 130 var y; 131 var i; 132 133 for ( i = 0; i < 25; i++ ) { 134 x = randu() * 30.0; 135 a = randu() * 10.0; 136 b = a + (randu() * 40.0); 137 c = a + ((b-a) * randu()); 138 y = pdf( x, a, b, c ); 139 console.log( 'x: %d, a: %d, b: %d, c: %d, f(x;a,b,c): %d', x.toFixed( 4 ), a.toFixed( 4 ), b.toFixed( 4 ), c.toFixed( 4 ), y.toFixed( 4 ) ); 140 } 141 ``` 142 143 </section> 144 145 <!-- /.examples --> 146 147 <section class="links"> 148 149 [pdf]: https://en.wikipedia.org/wiki/Probability_density_function 150 151 [triangular-distribution]: https://en.wikipedia.org/wiki/Triangular_distribution 152 153 </section> 154 155 <!-- /.links -->