README.md (4231B)
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 # Cumulative Distribution Function 22 23 > [Triangular][triangular-distribution] distribution [cumulative distribution function][cdf]. 24 25 <section class="intro"> 26 27 The [cumulative distribution function][cdf] for a [triangular][triangular-distribution] random variable is 28 29 <!-- <equation class="equation" label="eq:triangular_cdf" align="center" raw="F(x;a,b,c) = \begin{cases} 0 & \text{for } x \leq a \\ \frac{(x-a)^2}{(b-a)(c-a)} & \text{for } a < x \leq c \\ 1-\frac{(b-x)^2}{(b-a)(b-c)} & \text{for } c < x < b \\ 1 & \text{for } b \leq x \end{cases}" alt="Cumulative distribution function 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 \leq a \\ \frac{(x-a)^2}{(b-a)(c-a)} & \text{for } a < x \leq c \\ 1-\frac{(b-x)^2}{(b-a)(b-c)} & \text{for } c < x < b \\ 1 & \text{for } b \leq x \end{cases}" data-equation="eq:triangular_cdf"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/triangular/cdf/docs/img/equation_triangular_cdf.svg" alt="Cumulative distribution function for a Triangular distribution."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 where `a` is the lower limit, `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 cdf = require( '@stdlib/stats/base/dists/triangular/cdf' ); 50 ``` 51 52 #### cdf( x, a, b, c ) 53 54 Evaluates the [cumulative distribution function][cdf] (CDF) for a [triangular][triangular-distribution] distribution with parameters `a` (lower limit), `b` (upper limit) and `c` (mode). 55 56 ```javascript 57 var y = cdf( 0.5, -1.0, 1.0, 0.0 ); 58 // returns 0.875 59 60 y = cdf( 0.5, -1.0, 1.0, 0.5 ); 61 // returns 0.75 62 63 y = cdf( -10.0, -20.0, 0.0, -2.0 ); 64 // returns ~0.278 65 66 y = cdf( -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 = cdf( NaN, 0.0, 1.0, 0.5 ); 74 // returns NaN 75 76 y = cdf( 0.0, NaN, 1.0, 0.5 ); 77 // returns NaN 78 79 y = cdf( 0.0, 0.0, NaN, 0.5 ); 80 // returns NaN 81 82 y = cdf( 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 = cdf( 2.0, 1.0, 0.0, 1.5 ); 90 // returns NaN 91 92 y = cdf( 2.0, 1.0, 0.0, -1.0 ); 93 // returns NaN 94 95 y = cdf( 2.0, 0.0, -1.0, 0.5 ); 96 // returns NaN 97 ``` 98 99 #### cdf.factory( a, b, c ) 100 101 Returns a function for evaluating the [cumulative distribution function][cdf] of a [triangular][triangular-distribution] distribution with parameters `a` (lower limit), `b` (upper limit) and `c` (mode). 102 103 ```javascript 104 var mycdf = cdf.factory( 0.0, 10.0, 2.0 ); 105 var y = mycdf( 0.5 ); 106 // returns 0.0125 107 108 y = mycdf( 8.0 ); 109 // returns 0.95 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 cdf = require( '@stdlib/stats/base/dists/triangular/cdf' ); 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 = cdf( 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 [cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function 150 151 [triangular-distribution]: https://en.wikipedia.org/wiki/Triangular_distribution 152 153 </section> 154 155 <!-- /.links -->