Skip to content

feat: add stats/base/dists/planck/cdf #4130

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 4 commits into from
Dec 27, 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
139 changes: 139 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/planck/cdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<!--

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

-->

# Cumulative Distribution Function

> Planck (discrete exponential) distribution [cumulative distribution function][cdf].

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

#### cdf( x, lambda )

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

```javascript
var y = cdf( 2.0, 0.5 );
// returns ~0.7769

y = cdf( 2.0, 1.5 );
// returns ~0.9889
```

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

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

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

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

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

#### cdf.factory( lambda )

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

```javascript
var mycdf = cdf.factory( 1.5 );
var y = mycdf( 3.0 );
// returns ~0.9975

y = mycdf( 1.0 );
// returns ~0.9502
```

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

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 = cdf( x[ i ], lambda[ i ] );
console.log( 'x: %d, λ: %d, 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) 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 cdf = 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 = cdf( 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 mycdf;
var x;
var y;
var i;

x = discreteUniform( 100, 0, 40 );
mycdf = cdf.factory( 0.3 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mycdf( 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();
});
64 changes: 64 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/planck/cdf/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

{{alias}}( x, λ )
Evaluates 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 CDF.

Examples
--------
> var y = {{alias}}( 2.0, 0.5 )
~0.7769
> y = {{alias}}( 2.0, 1.5 )
~0.9889
> y = {{alias}}( -1.0, 4.0 )
0.0
> 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 cumulative distribution function (CDF)
of a Planck distribution with shape parameter `λ`.

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

Returns
-------
cdf: Function
Cumulative distribution function (CDF).

Examples
--------
> var mycdf = {{alias}}.factory( 1.5 );
> var y = mycdf( 3.0 )
~0.9975
> y = mycdf( 1.0 )
~0.9502

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* @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.
*/

// TypeScript Version: 4.1

/**
* Evaluates the cumulative distribution function (CDF) for a Planck distribution.
*
* @param x - input value
* @returns evaluated CDF
*/
type Unary = ( x: number ) => number;

/**
* Interface for the cumulative distribution function (CDF) of a Planck distribution.
*/
interface CDF {
/**
* Evaluates the cumulative distribution function (CDF) for a Planck distribution with shape parameter `lambda` at a value `x`.
*
* ## Notes
*
* - If `lambda <= 0`, the function returns `NaN`.
*
* @param x - input value
* @param lambda - shape parameter
* @returns evaluated CDF
*
* @example
* var y = cdf( 2.0, 0.5 );
* // returns ~0.7769
*
* @example
* var y = cdf( 2.0, 1.5 );
* // returns ~0.9889
*
* @example
* var y = cdf( -1.0, 0.5 );
* // returns 0.0
*
* @example
* var y = cdf( NaN, 0.5 );
* // returns NaN
*
* @example
* var y = cdf( 0.0, NaN );
* // returns NaN
*
* @example
* // Invalid probability
* var y = cdf( 2.0, 1.4 );
* // returns NaN
*/
( x: number, lambda: number ): number;

/**
* Returns a function for evaluating the cumulative distribution function (CDF) for a Planck distribution with shape parameter `lambda`.
*
* @param lambda - shape parameter
* @returns CDF
*
* @example
* var mycdf = cdf.factory( 1.5 );
* var y = mycdf( 3.0 );
* // returns ~0.9975
*
* y = mycdf( 1.0 );
* // returns ~0.9502
*/
factory( lambda: number ): Unary;
}

/**
* Planck distribution cumulative distribution function (CDF).
*
* @param x - input value
* @param lambda - shape parameter
* @returns evaluated CDF
*
* @example
* var y = cdf( 2.0, 0.5 );
* // returns ~0.7769
*
* y = cdf( 2.0, 1.5 );
* // returns ~0.9889
*
* var mycdf = cdf.factory( 1.5 );
* y = mycdf( 3.0 );
* // returns ~0.9975
*
* y = mycdf( 1.0 );
* // returns ~0.9502
*/
declare var cdf: CDF;


// EXPORTS //

export = cdf;
Loading
Loading