Skip to content

Commit 510e34a

Browse files
committed
Add exponential logpdf
1 parent cb6b8af commit 510e34a

18 files changed

Lines changed: 1082 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Logarithm of Probability Density Function
2+
3+
> Evaluate the natural logarithm of the probability density function (PDF) for an [exponential][exponential-distribution] distribution.
4+
5+
<section class="intro">
6+
7+
The [probability density function][pdf] (PDF) for an [exponential][exponential-distribution] random variable is
8+
9+
<!-- <equation class="equation" label="eq:exponential_pdf" align="center" raw="f(x;\lambda) = \begin{cases} \lambda e^{-\lambda x} & x \ge 0 \\ 0 & x < 0 \end{cases}" alt="Probability density function (PDF) for a Exponential distribution."> -->
10+
11+
<div class="equation" align="center" data-raw-text="f(x;\lambda) = \begin{cases} \lambda e^{-\lambda x} &amp; x \ge 0 \\ 0 &amp; x &lt; 0 \end{cases}" data-equation="eq:exponential_pdf">
12+
<img src="https://cdn.rawgit.com/stdlib-js/stdlib/bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/dist/exponential/pdf/docs/img/equation_exponential_pdf.svg" alt="Probability density function (PDF) for a Exponential distribution.">
13+
<br>
14+
</div>
15+
16+
<!-- </equation> -->
17+
18+
where `λ` is the rate parameter.
19+
20+
</section>
21+
22+
<!-- /.intro -->
23+
24+
<section class="usage">
25+
26+
## Usage
27+
28+
```javascript
29+
var logpdf = require( '@stdlib/math/base/dist/exponential/logpdf' );
30+
```
31+
32+
#### logpdf( x, lambda )
33+
34+
Evaluates the natural logarithm of the [probability density function][pdf] (PDF) for an [exponential][exponential-distribution] distribution with rate parameter `lambda`.
35+
36+
```javascript
37+
var y = logpdf( 2.0, 0.3 );
38+
// returns ~-1.804
39+
40+
y = logpdf( 2.0, 1.0 );
41+
// returns ~-2.0
42+
```
43+
44+
If provided `NaN` as any argument, the function returns `NaN`.
45+
46+
```javascript
47+
var y = logpdf( NaN, 0.0 );
48+
// returns NaN
49+
50+
y = logpdf( 0.0, NaN );
51+
// returns NaN
52+
```
53+
54+
If provided `lambda < 0`, the function returns `NaN`.
55+
56+
```javascript
57+
var y = logpdf( 2.0, -1.0 );
58+
// returns NaN
59+
```
60+
61+
#### logpdf.factory( lambda )
62+
63+
Returns a function for evaluating the natural logarithm of the probability density function ([PDF][pdf]) for an exponential distribution with rate parameter `lambda`.
64+
65+
```javascript
66+
var mylogpdf = logpdf.factory( 0.1 );
67+
68+
var y = mylogpdf( 8.0 );
69+
// returns ~-3.103
70+
71+
y = mylogpdf( 5.0 );
72+
// returns ~-2.803
73+
```
74+
75+
</section>
76+
77+
<!-- /.usage -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
```javascript
84+
var randu = require( '@stdlib/math/base/random/randu' );
85+
var logpdf = require( '@stdlib/math/base/dist/exponential/logpdf' );
86+
87+
var lambda;
88+
var x;
89+
var y;
90+
var i;
91+
92+
for ( i = 0; i < 10; i++ ) {
93+
x = randu() * 10.0;
94+
lambda = randu() * 10.0;
95+
y = logpdf( x, lambda );
96+
console.log( 'x: %d, λ: %d, ln(f(x;λ)): %d', x, lambda, y );
97+
}
98+
```
99+
100+
</section>
101+
102+
<!-- /.examples -->
103+
104+
<section class="links">
105+
106+
[pdf]: https://en.wikipedia.org/wiki/Probability_density_function
107+
108+
[exponential-distribution]: https://en.wikipedia.org/wiki/Exponential_distribution
109+
110+
</section>
111+
112+
<!-- /.links -->
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var randu = require( '@stdlib/math/base/random/randu' );
7+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
8+
var EPS = require( '@stdlib/math/constants/float64-eps' );
9+
var pkg = require( './../package.json' ).name;
10+
var logpdf = require( './../lib' );
11+
12+
13+
// MAIN //
14+
15+
bench( pkg, function benchmark( b ) {
16+
var lambda;
17+
var x;
18+
var y;
19+
var i;
20+
21+
b.tic();
22+
for ( i = 0; i < b.iterations; i++ ) {
23+
x = ( randu()*100.0 );
24+
lambda = ( randu()*100.0 ) + EPS;
25+
y = logpdf( x, lambda );
26+
if ( isnan( y ) ) {
27+
b.fail( 'should not return NaN' );
28+
}
29+
}
30+
b.toc();
31+
if ( isnan( y ) ) {
32+
b.fail( 'should not return NaN' );
33+
}
34+
b.pass( 'benchmark finished' );
35+
b.end();
36+
});
37+
38+
bench( pkg+':factory', function benchmark( b ) {
39+
var lambda;
40+
var mylogpdf;
41+
var x;
42+
var y;
43+
var i;
44+
45+
lambda = 10.0;
46+
mylogpdf = logpdf.factory( lambda );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
x = ( randu()*100.0 ) + EPS;
51+
y = mylogpdf( x );
52+
if ( isnan( y ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
}
56+
b.toc();
57+
if ( isnan( y ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
Lines changed: 59 additions & 0 deletions
Loading
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
{{alias}}( x, λ )
3+
Evaluates the natural logarithm of the probability density function (PDF)
4+
for an exponential distribution with rate parameter `λ` at a value `x`.
5+
6+
If provided `NaN` as any argument, the function returns `NaN`.
7+
8+
If provided a negative value for `λ`, the function returns `NaN`.
9+
10+
Parameters
11+
----------
12+
x: number
13+
Input value.
14+
15+
λ: number
16+
Rate parameter.
17+
18+
Returns
19+
-------
20+
out: number
21+
Evaluated logPDF.
22+
23+
Examples
24+
--------
25+
> var y = {{alias}}( 0.3, 4.0 )
26+
~0.186
27+
> y = {{alias}}( 2.0, 0.7 )
28+
~1.757
29+
> y = {{alias}}( -1.0, 0.5 )
30+
-Infinity
31+
> y = {{alias}}( 0, NaN )
32+
NaN
33+
> y = {{alias}}( NaN, 2.0 )
34+
NaN
35+
36+
// Negative rate:
37+
> y = {{alias}}( 2.0, -1.0 )
38+
NaN
39+
40+
{{alias}}.factory( λ )
41+
Returns a function for evaluating the natural logarithm of the probability
42+
density function (PDF) for an exponential distribution with rate parameter
43+
`λ`.
44+
45+
Parameters
46+
----------
47+
λ: number
48+
Rate parameter.
49+
50+
Returns
51+
-------
52+
logpdf: Function
53+
Logarithm of probability density function (PDF).
54+
55+
Examples
56+
--------
57+
> var mylogpdf = {{alias}}.factory( 0.5 );
58+
> var y = mylogpdf( 3.0 )
59+
~-2.193
60+
61+
See Also
62+
--------
63+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
var randu = require( '@stdlib/math/base/random/randu' );
4+
var logpdf = require( './../lib' );
5+
6+
var lambda;
7+
var x;
8+
var y;
9+
var i;
10+
11+
for ( i = 0; i < 10; i++ ) {
12+
x = randu() * 10.0;
13+
lambda = randu() * 10.0;
14+
y = logpdf( x, lambda );
15+
console.log( 'x: %d, λ: %d, ln(f(x;λ)): %d', x, lambda, y );
16+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var constantFunction = require( '@stdlib/utils/constant-function' );
6+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
7+
var ln = require( '@stdlib/math/base/special/ln' );
8+
var NINF = require( '@stdlib/math/constants/float64-ninf' );
9+
var PINF = require( '@stdlib/math/constants/float64-pinf' );
10+
11+
12+
// MAIN //
13+
14+
/**
15+
* Returns a function for evaluating the natural logarithm of the probability density function (PDF) for an exponential distribution with parameter `lambda`.
16+
*
17+
* @param {PositiveNumber} lambda - rate parameter
18+
* @returns {Function} logarithm of probability density function (logPDF)
19+
*
20+
* @example
21+
* var logpdf = factory( 0.5 );
22+
* var y = logpdf( 3.0 );
23+
* // returns ~-2.913
24+
*
25+
* y = logpdf( 1.0 );
26+
* // returns ~-1.193
27+
*/
28+
function factory( lambda ) {
29+
if ( isnan( lambda ) || lambda < 0.0 || lambda === PINF ) {
30+
return constantFunction( NaN );
31+
}
32+
return logpdf;
33+
34+
/**
35+
* Evaluates the natural logarithm of the probability density function (PDF) for an exponential distribution.
36+
*
37+
* @private
38+
* @param {number} x - input value
39+
* @returns {number} evaluated logPDF
40+
*
41+
* @example
42+
* var y = logpdf( 2.3 );
43+
* // returns <number>
44+
*/
45+
function logpdf( x ) {
46+
if ( isnan( x ) ) {
47+
return NaN;
48+
}
49+
if ( x < 0.0 ) {
50+
return NINF;
51+
}
52+
return -( x*lambda ) + ln( lambda );
53+
} // end FUNCTION logpdf()
54+
} // end FUNCTION factory()
55+
56+
57+
// EXPORTS //
58+
59+
module.exports = factory;

0 commit comments

Comments
 (0)