Skip to content

Commit 094bea4

Browse files
Jaysukh-409kgrytestdlib-bot
authored
feat: add stats/base/dists/planck/logpmf
PR-URL: #4182 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 7d285da commit 094bea4

File tree

16 files changed

+1279
-0
lines changed

16 files changed

+1279
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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 Probability Mass Function
22+
23+
> Evaluate the logarithm of the [probability mass function][pmf] (PMF) for a Planck (discrete exponential) distribution.
24+
25+
<section class="intro">
26+
27+
The [probability mass function][pmf] (PMF) for a Planck random variable is defined as
28+
29+
<!-- <equation class="equation" label="eq:planck_pmf" align="center" raw="\Pr(X = x, \lambda) = \begin{cases}(1 - e^{-\lambda})e^{-\lambda x} & \text{for } x = 0, 1, 2, \ldots \\ 0 & \text{otherwise} \end{cases}" alt="Probability mass function (PMF) for a Planck distribution."> -->
30+
31+
```math
32+
\Pr(X = x, \lambda) = \begin{cases}(1 - e^{-\lambda})e^{-\lambda x} & \text{for } x = 0, 1, 2, \ldots \\ 0 & \text{otherwise} \end{cases}
33+
```
34+
35+
<!-- </equation> -->
36+
37+
where `λ` is the shape parameter and `x` denotes the count of events in a quantized system.
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var logpmf = require( '@stdlib/stats/base/dists/planck/logpmf' );
49+
```
50+
51+
#### logpmf( x, lambda )
52+
53+
Evaluates the logarithm of the [probability mass function][pmf] (PMF) of a Planck (discrete exponential) distribution with shape parameter `lambda`.
54+
55+
```javascript
56+
var y = logpmf( 4.0, 0.3 );
57+
// returns ~-2.5502
58+
59+
y = logpmf( 2.0, 1.7 );
60+
// returns ~-3.6017
61+
62+
y = logpmf( -1.0, 2.5 );
63+
// returns -Infinity
64+
```
65+
66+
If provided `NaN` as any argument, the function returns `NaN`.
67+
68+
```javascript
69+
var y = logpmf( NaN, 0.0 );
70+
// returns NaN
71+
72+
y = logpmf( 0.0, NaN );
73+
// returns NaN
74+
```
75+
76+
If provided a shape parameter `lambda` which is nonpositive number, the function returns `NaN`.
77+
78+
```javascript
79+
var y = logpmf( 2.0, -1.0 );
80+
// returns NaN
81+
```
82+
83+
#### logpmf.factory( lambda )
84+
85+
Returns a function for evaluating the logarithm of the [probability mass function][pmf] (PMF) of a Planck (discrete exponential) distribution with shape parameter `lambda`.
86+
87+
```javascript
88+
var mylogpmf = logpmf.factory( 0.5 );
89+
var y = mylogpmf( 3.0 );
90+
// returns ~-2.4328
91+
92+
y = mylogpmf( 1.0 );
93+
// returns ~-1.4328
94+
```
95+
96+
</section>
97+
98+
<!-- /.usage -->
99+
100+
<section class="notes">
101+
102+
## Notes
103+
104+
- In virtually all cases, using the `logpmf` or `logcdf` functions is preferable to manually computing the logarithm of the `pmf` or `cdf`, respectively, since the latter is prone to overflow and underflow.
105+
106+
</section>
107+
108+
<!-- /.notes -->
109+
110+
<section class="examples">
111+
112+
## Examples
113+
114+
<!-- eslint no-undef: "error" -->
115+
116+
```javascript
117+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
118+
var uniform = require( '@stdlib/random/array/uniform' );
119+
var logpmf = require( '@stdlib/stats/base/dists/planck/logpmf' );
120+
121+
var x = discreteUniform( 10, 0, 5 );
122+
var lambda = uniform( 10, 0.1, 5.0 );
123+
124+
var y;
125+
var i;
126+
for ( i = 0; i < lambda.length; i++ ) {
127+
y = logpmf( x[ i ], lambda[ i ] );
128+
console.log( 'x: %d, λ: %d, ln( P( X = x; λ ) ): %d', x[ i ], lambda[ i ].toFixed( 4 ), y.toFixed( 4 ) );
129+
}
130+
```
131+
132+
</section>
133+
134+
<!-- /.examples -->
135+
136+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
137+
138+
<section class="related">
139+
140+
</section>
141+
142+
<!-- /.related -->
143+
144+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
145+
146+
<section class="links">
147+
148+
[pmf]: https://en.wikipedia.org/wiki/Probability_mass_function
149+
150+
</section>
151+
152+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var logpmf = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var lambda;
35+
var x;
36+
var y;
37+
var i;
38+
39+
x = discreteUniform( 100, 0, 40 );
40+
lambda = uniform( 100, 0.1, 10.0 );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
y = logpmf( x[ i % x.length ], lambda[ i % lambda.length ] );
45+
if ( isnan( y ) ) {
46+
b.fail( 'should not return NaN' );
47+
}
48+
}
49+
b.toc();
50+
if ( isnan( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
56+
57+
bench( pkg+':factory', function benchmark( b ) {
58+
var mylogpmf;
59+
var x;
60+
var y;
61+
var i;
62+
63+
x = discreteUniform( 100, 0, 40 );
64+
mylogpmf = logpmf.factory( 0.3 );
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
y = mylogpmf( x[ i % x.length ] );
69+
if ( isnan( y ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( y ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
{{alias}}( x, λ )
3+
Evaluates the logarithm of the probability mass function (PMF) for a Planck
4+
distribution with shape parameter `λ` at a value `x`.
5+
6+
If provided `NaN` as any argument, the function returns `NaN`.
7+
8+
If `λ <= 0`, the function returns `NaN`.
9+
10+
Parameters
11+
----------
12+
x: number
13+
Input value.
14+
15+
λ: number
16+
Shape parameter.
17+
18+
Returns
19+
-------
20+
out: number
21+
Evaluated logPMF.
22+
23+
Examples
24+
--------
25+
> var y = {{alias}}( 4.0, 0.3 )
26+
~-2.5502
27+
> y = {{alias}}( 2.0, 1.7 )
28+
~-3.6017
29+
> y = {{alias}}( -1.0, 2.5 )
30+
-Infinity
31+
> y = {{alias}}( 0.0, NaN )
32+
NaN
33+
> y = {{alias}}( NaN, 0.5 )
34+
NaN
35+
// Invalid shape parameter:
36+
> y = {{alias}}( 2.0, -1.0 )
37+
NaN
38+
39+
40+
{{alias}}.factory( λ )
41+
Returns a function for evaluating the logarithm of the probability mass
42+
function (PMF) of a Planck distribution with shape parameter `λ`.
43+
44+
Parameters
45+
----------
46+
λ: number
47+
Shape parameter.
48+
49+
Returns
50+
-------
51+
logpmf: Function
52+
Logarithm of the probability mass function (PMF).
53+
54+
Examples
55+
--------
56+
> var mylogpmf = {{alias}}.factory( 0.5 );
57+
> var y = mylogpmf( 3.0 )
58+
~-2.4328
59+
> y = mylogpmf( 1.0 )
60+
~-1.4328
61+
62+
See Also
63+
--------
64+

0 commit comments

Comments
 (0)