README.md (4671B)
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 Cumulative Distribution Function 22 23 > [Triangular][triangular-distribution] distribution logarithm of [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/logcdf/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 logcdf = require( '@stdlib/stats/base/dists/triangular/logcdf' ); 50 ``` 51 52 #### logcdf( x, a, b, c ) 53 54 Evaluates the natural logarithm of 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 = logcdf( 0.5, -1.0, 1.0, 0.0 ); 58 // returns ~-0.134 59 60 y = logcdf( 0.5, -1.0, 1.0, 0.5 ); 61 // returns ~-0.288 62 63 y = logcdf( -10.0, -20.0, 0.0, -2.0 ); 64 // returns ~-1.281 65 66 y = logcdf( -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 = logcdf( NaN, 0.0, 1.0, 0.5 ); 74 // returns NaN 75 76 y = logcdf( 0.0, NaN, 1.0, 0.5 ); 77 // returns NaN 78 79 y = logcdf( 0.0, 0.0, NaN, 0.5 ); 80 // returns NaN 81 82 y = logcdf( 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 = logcdf( 2.0, 1.0, 0.0, 1.5 ); 90 // returns NaN 91 92 y = logcdf( 2.0, 1.0, 0.0, -1.0 ); 93 // returns NaN 94 95 y = logcdf( 2.0, 0.0, -1.0, 0.5 ); 96 // returns NaN 97 ``` 98 99 #### logcdf.factory( a, b, c ) 100 101 Returns a function for evaluating the natural logarithm of 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 mylogcdf = logcdf.factory( 0.0, 10.0, 2.0 ); 105 var y = mylogcdf( 0.5 ); 106 // returns ~-4.382 107 108 y = mylogcdf( 8.0 ); 109 // returns ~-0.051 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 logcdf = require( '@stdlib/stats/base/dists/triangular/logcdf' ); 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 = logcdf( 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 [cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function 160 161 [triangular-distribution]: https://en.wikipedia.org/wiki/Triangular_distribution 162 163 </section> 164 165 <!-- /.links -->