Skip to content

feat: add stats/base/dists/planck/pmf #3896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/planck/pmf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<!--

@license Apache-2.0

Copyright (c) 2024 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# Probability Mass Function

> Planck (discrete exponential) distribution [probability mass function][pmf] (PMF).

<section class="intro">

The [probability mass function][pmf] (PMF) for a Planck random variable is defined as

<!-- <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."> -->

```math
\Pr(X = x, \lambda) = \begin{cases}(1 - e^{-\lambda})e^{-\lambda x} & \text{for } x = 0, 1, 2, \ldots \\ 0 & \text{otherwise} \end{cases}
```

<!-- </equation> -->

where `λ` is the shape parameter. The random variable `X` denotes the count of events in a quantized system.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var pmf = require( '@stdlib/stats/base/dists/planck/pmf' );
```

#### pmf( x, lambda )

Evaluates the [probability mass function][pmf] (PMF) of a Planck (discrete exponential) distribution with shape parameter `lambda`.

```javascript
var y = pmf( 4.0, 0.3 );
// returns ~0.0781

y = pmf( 2.0, 1.7 );
// returns ~0.0273

y = pmf( -1.0, 2.5 );
// returns 0.0
```

If provided `NaN` as any argument, the function returns `NaN`.

```javascript
var y = pmf( NaN, 0.0 );
// returns NaN

y = pmf( 0.0, NaN );
// returns NaN
```

If provided a shape parameter `lambda` which is a nonpositive number, the function returns `NaN`.

```javascript
var y = pmf( 2.0, -1.0 );
// returns NaN
```

#### pmf.factory( lambda )

Returns a function for evaluating the [probability mass function][pmf] (PMF) of a Planck (discrete exponential) distribution with shape parameter `lambda`.

```javascript
var mypmf = pmf.factory( 0.5 );
var y = mypmf( 3.0 );
// returns ~0.0878

y = mypmf( 1.0 );
// returns ~0.2387
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var uniform = require( '@stdlib/random/array/uniform' );
var pmf = require( '@stdlib/stats/base/dists/planck/pmf' );

var lambda = uniform( 10, 0.1, 5.0 );
var x = discreteUniform( 10, 0, 5 );

var y;
var i;
for ( i = 0; i < lambda.length; i++ ) {
y = pmf( x[ i ], lambda[ i ] );
console.log( 'x: %d, λ: %d, P(X = x; λ): %d', x[ i ], lambda[ i ].toFixed( 4 ), y.toFixed( 4 ) );
}
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[pmf]: https://en.wikipedia.org/wiki/Probability_mass_function

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var pmf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var lambda;
var x;
var y;
var i;

x = discreteUniform( 100, 0, 40 );
lambda = uniform( 100, 0.1, 10.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = pmf( x[ i % x.length ], lambda[ i % lambda.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':factory', function benchmark( b ) {
var mypmf;
var x;
var y;
var i;

x = discreteUniform( 100, 0, 40 );
mypmf = pmf.factory( 0.3 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mypmf( x[ i % x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
63 changes: 63 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/planck/pmf/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

{{alias}}( x, λ )
Evaluates the probability mass function (PMF) for a Planck distribution with
shape parameter `λ` at a value `x`.

If provided `NaN` as any argument, the function returns `NaN`.

If `λ <= 0`, the function returns `NaN`.

Parameters
----------
x: number
Input value.

λ: number
Shape parameter.

Returns
-------
out: number
Evaluated PMF.

Examples
--------
> var y = {{alias}}( 4.0, 0.3 )
~0.0781
> y = {{alias}}( 2.0, 1.7 )
~0.0273
> y = {{alias}}( -1.0, 0.5 )
0.0
> y = {{alias}}( 0.0, NaN )
NaN
> y = {{alias}}( NaN, 0.5 )
NaN
// Invalid shape parameter:
> y = {{alias}}( 2.0, -1.0 )
NaN


{{alias}}.factory( λ )
Returns a function for evaluating the probability mass function (PMF) of a
Planck distribution with shape parameter `λ`.

Parameters
----------
λ: number
Shape parameter.

Returns
-------
pmf: Function
Probability mass function (PMF).

Examples
--------
> var mypmf = {{alias}}.factory( 0.5 );
> var y = mypmf( 3.0 )
~0.0878
> y = mypmf( 1.0 )
~0.2387

See Also
--------
Loading
Loading