Skip to content

Commit d16409a

Browse files
Jaysukh-409kgrytestdlib-bot
authored
feat: add stats/base/dists/planck/pmf
PR-URL: #3896 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent d43f119 commit d16409a

File tree

16 files changed

+1263
-0
lines changed

16 files changed

+1263
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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 Mass Function
22+
23+
> Planck (discrete exponential) distribution [probability mass function][pmf] (PMF).
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 (discrete exponential) 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. The random variable `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 pmf = require( '@stdlib/stats/base/dists/planck/pmf' );
49+
```
50+
51+
#### pmf( x, lambda )
52+
53+
Evaluates the [probability mass function][pmf] (PMF) of a Planck (discrete exponential) distribution with shape parameter `lambda`.
54+
55+
```javascript
56+
var y = pmf( 4.0, 0.3 );
57+
// returns ~0.0781
58+
59+
y = pmf( 2.0, 1.7 );
60+
// returns ~0.0273
61+
62+
y = pmf( -1.0, 2.5 );
63+
// returns 0.0
64+
```
65+
66+
If provided `NaN` as any argument, the function returns `NaN`.
67+
68+
```javascript
69+
var y = pmf( NaN, 0.0 );
70+
// returns NaN
71+
72+
y = pmf( 0.0, NaN );
73+
// returns NaN
74+
```
75+
76+
If provided a shape parameter `lambda` which is a nonpositive number, the function returns `NaN`.
77+
78+
```javascript
79+
var y = pmf( 2.0, -1.0 );
80+
// returns NaN
81+
```
82+
83+
#### pmf.factory( lambda )
84+
85+
Returns a function for evaluating the [probability mass function][pmf] (PMF) of a Planck (discrete exponential) distribution with shape parameter `lambda`.
86+
87+
```javascript
88+
var mypmf = pmf.factory( 0.5 );
89+
var y = mypmf( 3.0 );
90+
// returns ~0.0878
91+
92+
y = mypmf( 1.0 );
93+
// returns ~0.2387
94+
```
95+
96+
</section>
97+
98+
<!-- /.usage -->
99+
100+
<section class="examples">
101+
102+
## Examples
103+
104+
<!-- eslint no-undef: "error" -->
105+
106+
```javascript
107+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
108+
var uniform = require( '@stdlib/random/array/uniform' );
109+
var pmf = require( '@stdlib/stats/base/dists/planck/pmf' );
110+
111+
var lambda = uniform( 10, 0.1, 5.0 );
112+
var x = discreteUniform( 10, 0, 5 );
113+
114+
var y;
115+
var i;
116+
for ( i = 0; i < lambda.length; i++ ) {
117+
y = pmf( x[ i ], lambda[ i ] );
118+
console.log( 'x: %d, λ: %d, P(X = x; λ): %d', x[ i ], lambda[ i ].toFixed( 4 ), y.toFixed( 4 ) );
119+
}
120+
```
121+
122+
</section>
123+
124+
<!-- /.examples -->
125+
126+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
127+
128+
<section class="related">
129+
130+
</section>
131+
132+
<!-- /.related -->
133+
134+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
135+
136+
<section class="links">
137+
138+
[pmf]: https://en.wikipedia.org/wiki/Probability_mass_function
139+
140+
</section>
141+
142+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 pmf = 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 = pmf( 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 mypmf;
59+
var x;
60+
var y;
61+
var i;
62+
63+
x = discreteUniform( 100, 0, 40 );
64+
mypmf = pmf.factory( 0.3 );
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
y = mypmf( 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,63 @@
1+
2+
{{alias}}( x, λ )
3+
Evaluates the probability mass function (PMF) for a Planck distribution with
4+
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 PMF.
22+
23+
Examples
24+
--------
25+
> var y = {{alias}}( 4.0, 0.3 )
26+
~0.0781
27+
> y = {{alias}}( 2.0, 1.7 )
28+
~0.0273
29+
> y = {{alias}}( -1.0, 0.5 )
30+
0.0
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 probability mass function (PMF) of a
42+
Planck distribution with shape parameter `λ`.
43+
44+
Parameters
45+
----------
46+
λ: number
47+
Shape parameter.
48+
49+
Returns
50+
-------
51+
pmf: Function
52+
Probability mass function (PMF).
53+
54+
Examples
55+
--------
56+
> var mypmf = {{alias}}.factory( 0.5 );
57+
> var y = mypmf( 3.0 )
58+
~0.0878
59+
> y = mypmf( 1.0 )
60+
~0.2387
61+
62+
See Also
63+
--------

0 commit comments

Comments
 (0)