README.md (4883B)
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 > [Truncated normal][truncated-normal-distribution] distribution probability density function (PDF). 24 25 <section class="intro"> 26 27 A normally distributed random variable `X` conditional on `a < X < b` is called a [truncated normal][truncated-normal-distribution] distribution. 28 The [probability density function][pdf] (PDF) for a [truncated normal][truncated-normal-distribution] random variable is 29 30 <!-- <equation class="equation" label="eq:truncated_normal_pdf" align="center" raw="f(x;\mu,\sigma,a,b) = \begin{cases} \frac{\frac{1}{\sigma}\phi(\frac{x - \mu}{\sigma})}{\Phi(\frac{b - \mu}{\sigma}) - \Phi(\frac{a - \mu}{\sigma}) } & \text{ if } a < x < b \\ 0 & \text{ otherwise } \end{cases}" alt="Probability density function (PDF) for a truncated normal distribution."> --> 31 32 <div class="equation" align="center" data-raw-text="f(x;\mu,\sigma,a,b) = \begin{cases} \frac{\frac{1}{\sigma}\phi(\frac{x - \mu}{\sigma})}{\Phi(\frac{b - \mu}{\sigma}) - \Phi(\frac{a - \mu}{\sigma}) } & \text{ if } a < x < b \\ 0 & \text{ otherwise } \end{cases}" data-equation="eq:truncated_normal_pdf"> 33 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/docs/img/equation_truncated_normal_pdf.svg" alt="Probability density function (PDF) for a truncated normal distribution."> 34 <br> 35 </div> 36 37 <!-- </equation> --> 38 39 where `Phi` and `phi` denote the [cumulative distribution function][cdf] and [density function][pdf] of the [normal][normal-distribution] distribution, respectively, `mu` is the location and `sigma > 0` is the scale parameter of the distribution. `a` and `b` are the minimum and maximum support. 40 41 </section> 42 43 <!-- /.intro --> 44 45 <section class="usage"> 46 47 ## Usage 48 49 ```javascript 50 var pdf = require( '@stdlib/stats/base/dists/truncated-normal/pdf' ); 51 ``` 52 53 #### pdf( x, a, b, mu, sigma ) 54 55 Evaluates the probability density function (PDF) for a [truncated normal][truncated-normal-distribution] distribution with lower limit `a`, upper limit `b`, location parameter `mu`, and scale parameter `sigma`. 56 57 ```javascript 58 var y = pdf( 0.9, 0.0, 1.0, 0.0, 1.0 ); 59 // returns ~0.7795 60 61 y = pdf( 0.9, 0.0, 1.0, 0.5, 1.0 ); 62 // returns ~0.9617 63 64 y = pdf( 0.9, -1.0, 1.0, 0.5, 1.0 ); 65 // returns ~0.5896 66 67 y = pdf( 1.4, 0.0, 1.0, 0.0, 1.0 ); 68 // returns 0.0 69 70 y = pdf( -0.9, 0.0, 1.0, 0.0, 1.0 ); 71 // returns 0.0 72 ``` 73 74 If provided `NaN` as any argument, the function returns `NaN`. 75 76 ```javascript 77 var y = pdf( NaN, 0.0, 1.0, 0.5, 2.0 ); 78 // returns NaN 79 80 y = pdf( 0.0, NaN, 1.0, 0.5, 2.0 ); 81 // returns NaN 82 83 y = pdf( 0.0, 0.0, NaN, 0.5, 2.0 ); 84 // returns NaN 85 86 y = pdf( 0.6, 0.0, 1.0, NaN, 2.0 ); 87 // returns NaN 88 89 y = pdf( 0.6, 0.0, 1.0, 0.5, NaN ); 90 // returns NaN 91 ``` 92 93 #### pdf.factory( a, b, mu, sigma ) 94 95 Returns a function for evaluating the [probability density function][pdf] (PDF) for a [truncated normal][truncated-normal-distribution] distribution. 96 97 ```javascript 98 var myPDF = pdf.factory( 0.0, 1.0, 0.0, 1.0 ); 99 var y = myPDF( 0.8 ); 100 // returns ~0.849 101 102 myPDF = pdf.factory( 0.0, 1.0, 0.5, 1.0 ); 103 y = myPDF( 0.8 ); 104 // returns ~0.996 105 ``` 106 107 </section> 108 109 <!-- /.usage --> 110 111 <section class="examples"> 112 113 ## Examples 114 115 <!-- eslint no-undef: "error" --> 116 117 ```javascript 118 var randu = require( '@stdlib/random/base/randu' ); 119 var pdf = require( '@stdlib/stats/base/dists/truncated-normal/pdf' ); 120 121 var sigma; 122 var mu; 123 var a; 124 var b; 125 var x; 126 var y; 127 var i; 128 129 for ( i = 0; i < 25; i++ ) { 130 a = ( randu() * 80.0 ) - 40.0; 131 b = a + ( randu() * 80.0 ); 132 x = ( randu() * 40.0 ) + a; 133 mu = ( randu() * 20.0 ) - 10.0; 134 sigma = ( randu() * 10.0 ) + 2.0; 135 y = pdf( x, a, b, mu, sigma ); 136 console.log( 'x: %d, a: %d, b: %d, mu: %d, sigma: %d, f(x;a,b,mu,sigma): %d', x.toFixed( 4 ), a.toFixed( 4 ), b.toFixed( 4 ), mu.toFixed( 4 ), sigma.toFixed( 4 ), y.toFixed( 4 ) ); 137 } 138 ``` 139 140 </section> 141 142 <!-- /.examples --> 143 144 <section class="links"> 145 146 [cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function 147 148 [pdf]: https://en.wikipedia.org/wiki/Probability_density_function 149 150 [normal-distribution]: https://en.wikipedia.org/wiki/Normal_distribution 151 152 [truncated-normal-distribution]: https://en.wikipedia.org/wiki/Truncated_normal_distribution 153 154 </section> 155 156 <!-- /.links -->