README.md (4430B)
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 # Quantile Function 22 23 > [Triangular][triangular-distribution] distribution [quantile function][quantile-function]. 24 25 <section class="intro"> 26 27 The [quantile function][quantile-function] for a [Triangular][triangular-distribution] random variable is 28 29 <!-- <equation class="equation" label="eq:triangular_quantile_function" align="center" raw="Q(p;a,b,c) = \begin{cases} a + \sqrt{(b-a)(c-a)p} & \text{ for } 0 \le p \le F(c) \\ b - \sqrt{(b-a)(b-c)(1-p)} & \text{ for } F(c) \le p \le 1 \end{cases}" alt="Quantile function for a triangular distribution."> --> 30 31 <div class="equation" align="center" data-raw-text="Q(p;a,b,c) = \begin{cases} a + \sqrt{(b-a)(c-a)p} & \text{ for } 0 \le p \le F(c) \\ b - \sqrt{(b-a)(b-c)(1-p)} & \text{ for } F(c) \le p \le 1 \end{cases}" data-equation="eq:triangular_quantile_function"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/triangular/quantile/docs/img/equation_triangular_quantile_function.svg" alt="Quantile 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 quantile = require( '@stdlib/stats/base/dists/triangular/quantile' ); 50 ``` 51 52 #### quantile( p, a, b, c ) 53 54 Evaluates the [quantile function][quantile-function] for a [triangular][triangular-distribution] distribution with parameters `a` (lower limit), `b` (upper limit) and `c` (mode). 55 56 ```javascript 57 var y = quantile( 0.9, -1.0, 1.0, 0.0 ); 58 // returns ~0.553 59 60 y = quantile( 0.1, -1.0, 1.0, 0.5 ); 61 // returns ~-0.452 62 63 y = quantile( 0.1, -20.0, 0.0, -2.0 ); 64 // returns -14.0 65 66 y = quantile( 0.8, 0.0, 20.0, 0.0 ); 67 // returns ~11.056 68 ``` 69 70 If provided a probability `p` outside the interval `[0,1]`, the function returns `NaN`. 71 72 ```javascript 73 var y = quantile( 1.9, 0.0, 1.0, 0.5 ); 74 // returns NaN 75 76 y = quantile( -0.1, 0.0, 1.0, 0.5 ); 77 // returns NaN 78 ``` 79 80 If provided `NaN` as any argument, the function returns `NaN`. 81 82 ```javascript 83 var y = quantile( NaN, 0.0, 1.0, 0.5 ); 84 // returns NaN 85 86 y = quantile( 0.1, NaN, 1.0, 0.5 ); 87 // returns NaN 88 89 y = quantile( 0.1, 0.0, NaN, 0.5 ); 90 // returns NaN 91 92 y = quantile( 0.1, 0.0, 1.0, NaN ); 93 // returns NaN 94 ``` 95 96 If provided parameters not satisfying `a <= c <= b`, the function returns `NaN`. 97 98 ```javascript 99 var y = quantile( 0.1, 1.0, 0.0, 1.5 ); 100 // returns NaN 101 102 y = quantile( 0.1, 1.0, 0.0, -1.0 ); 103 // returns NaN 104 105 y = quantile( 0.1, 0.0, -1.0, 0.5 ); 106 // returns NaN 107 ``` 108 109 #### quantile.factory( a, b, c ) 110 111 Returns a function for evaluating the [quantile function][quantile-function] of a [triangular][triangular-distribution] distribution with parameters `a` (lower limit), `b` (upper limit) and `c` (mode). 112 113 ```javascript 114 var myquantile = quantile.factory( 2.0, 4.0, 2.5 ); 115 var y = myquantile( 0.4 ); 116 // returns ~2.658 117 118 y = myquantile( 0.8 ); 119 // returns ~3.225 120 ``` 121 122 </section> 123 124 <!-- /.usage --> 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 quantile = require( '@stdlib/stats/base/dists/triangular/quantile' ); 135 136 var a; 137 var b; 138 var c; 139 var p; 140 var y; 141 var i; 142 143 for ( i = 0; i < 25; i++ ) { 144 p = randu(); 145 a = randu() * 10.0; 146 b = a + (randu() * 40.0); 147 c = a + ((b-a) * randu()); 148 y = quantile( p, a, b, c ); 149 console.log( 'p: %d, a: %d, b: %d, c: %d, Q(p;a,b,c): %d', p.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 [triangular-distribution]: https://en.wikipedia.org/wiki/Triangular_distribution 160 161 [quantile-function]: https://en.wikipedia.org/wiki/Quantile_function 162 163 </section> 164 165 <!-- /.links -->