Skip to content

feat: add stats/base/dists/planck/logcdf #4227

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 7 commits into from
Jan 2, 2025
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
149 changes: 149 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/planck/logcdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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.

-->

# Logarithm of Cumulative Distribution Function

> Evaluate the logarithm of [cumulative distribution function][cdf] Planck (discrete exponential) distribution.

<section class="intro">

The [cumulative distribution function][cdf] for a Planck random variable is

<!-- <equation class="equation" label="eq:planck_cdf" align="center" raw="F(x;\lambda) = 1 - e^{-\lambda \cdot (\lfloor x \rfloor + 1)}" alt="CDF for a Planck distribution."> -->

```math
F(x;\lambda) = 1 - e^{-\lambda (\lfloor x \rfloor + 1)}
```

<!-- </equation> -->

where `λ` is the shape parameter and `x` denotes the count of events in a quantized system.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

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

#### logcdf( x, lambda )

Evaluates the logarithm of the [cumulative distribution function][cdf] for a Planck (discrete exponential) distribution with shape parameter `lambda`.

```javascript
var y = logcdf( 2.0, 0.5 );
// returns ~-0.2525

y = logcdf( 2.0, 1.5 );
// returns ~-0.0112
```

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

```javascript
var y = logcdf( NaN, 0.5 );
// returns NaN

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

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

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

#### logcdf.factory( lambda )

Returns a function for evaluating the logarithm of the [cumulative distribution function][cdf] of a Planck (discrete exponential) distribution with shape parameter `lambda`.

```javascript
var mylogcdf = logcdf.factory( 1.5 );
var y = mylogcdf( 3.0 );
// returns ~-0.0025

y = mylogcdf( 1.0 );
// returns ~-0.0511
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

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

</section>

<!-- /.notes -->

<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 logcdf = require( '@stdlib/stats/base/dists/planck/logcdf' );

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

var y;
var i;
for ( i = 0; i < lambda.length; i++ ) {
y = logcdf( x[ i ], lambda[ i ] );
console.log( 'x: %d, λ: %d, ln(F(x;λ)): %d', x[ i ].toFixed( 4 ), 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">

[cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 logcdf = 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 = logcdf( 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 mylogcdf;
var x;
var y;
var i;

x = discreteUniform( 100, 0, 40 );
mylogcdf = logcdf.factory( 0.3 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mylogcdf( 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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

{{alias}}( x, λ )
Evaluates the logarithm of the cumulative distribution function (CDF) 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 logCDF.

Examples
--------
> var y = {{alias}}( 2.0, 0.5 )
~-0.2525
> y = {{alias}}( 2.0, 1.5 )
~-0.0112
> y = {{alias}}( -1.0, 4.0 )
-Infinity
> y = {{alias}}( NaN, 0.5 )
NaN
> y = {{alias}}( 0.0, NaN )
NaN
// Invalid shape parameter
> y = {{alias}}( 2.0, -1.4 )
NaN


{{alias}}.factory( λ )
Returns a function for evaluating the logarithm of the cumulative
distribution function (CDF) of a Planck distribution with shape parameter
`λ`.

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

Returns
-------
logcdf: Function
Logarithm of cumulative distribution function (CDF).

Examples
--------
> var mylogcdf = {{alias}}.factory( 1.5 );
> var y = mylogcdf( 3.0 )
~-0.0025
> y = mylogcdf( 1.0 )
~-0.0511

See Also
--------

Loading
Loading