Skip to content

Commit 4cf8ad7

Browse files
Jaysukh-409kgryte
andauthored
feat: add stats/base/dists/planck/cdf
PR-URL: #4130 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent b87d116 commit 4cf8ad7

File tree

16 files changed

+1240
-0
lines changed

16 files changed

+1240
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
# Cumulative Distribution Function
22+
23+
> Planck (discrete exponential) distribution [cumulative distribution function][cdf].
24+
25+
<section class="intro">
26+
27+
The [cumulative distribution function][cdf] for a Planck random variable is
28+
29+
<!-- <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."> -->
30+
31+
```math
32+
F(x;\lambda) = 1 - e^{-\lambda (\lfloor x \rfloor + 1)}
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 cdf = require( '@stdlib/stats/base/dists/planck/cdf' );
49+
```
50+
51+
#### cdf( x, lambda )
52+
53+
Evaluates the [cumulative distribution function][cdf] for a Planck (discrete exponential) distribution with shape parameter `lambda`.
54+
55+
```javascript
56+
var y = cdf( 2.0, 0.5 );
57+
// returns ~0.7769
58+
59+
y = cdf( 2.0, 1.5 );
60+
// returns ~0.9889
61+
```
62+
63+
If provided `NaN` as any argument, the function returns `NaN`.
64+
65+
```javascript
66+
var y = cdf( NaN, 0.5 );
67+
// returns NaN
68+
69+
y = cdf( 0.0, NaN );
70+
// returns NaN
71+
```
72+
73+
If provided a shape parameter `lambda` which is nonpositive, the function returns `NaN`.
74+
75+
```javascript
76+
var y = cdf( 2.0, -1.0 );
77+
// returns NaN
78+
```
79+
80+
#### cdf.factory( lambda )
81+
82+
Returns a function for evaluating the [cumulative distribution function][cdf] of a Planck (discrete exponential) distribution with shape parameter `lambda`.
83+
84+
```javascript
85+
var mycdf = cdf.factory( 1.5 );
86+
var y = mycdf( 3.0 );
87+
// returns ~0.9975
88+
89+
y = mycdf( 1.0 );
90+
// returns ~0.9502
91+
```
92+
93+
</section>
94+
95+
<!-- /.usage -->
96+
97+
<section class="examples">
98+
99+
## Examples
100+
101+
<!-- eslint no-undef: "error" -->
102+
103+
```javascript
104+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
105+
var uniform = require( '@stdlib/random/array/uniform' );
106+
var cdf = require( '@stdlib/stats/base/dists/planck/cdf' );
107+
108+
var x = discreteUniform( 10, 0, 5 );
109+
var lambda = uniform( 10, 0.1, 5.0 );
110+
111+
var y;
112+
var i;
113+
for ( i = 0; i < lambda.length; i++ ) {
114+
y = cdf( x[ i ], lambda[ i ] );
115+
console.log( 'x: %d, λ: %d, F(x;λ): %d', x[ i ].toFixed( 4 ), lambda[ i ].toFixed( 4 ), y.toFixed( 4 ) );
116+
}
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
124+
125+
<section class="related">
126+
127+
</section>
128+
129+
<!-- /.related -->
130+
131+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="links">
134+
135+
[cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function
136+
137+
</section>
138+
139+
<!-- /.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 cdf = 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 = cdf( 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 mycdf;
59+
var x;
60+
var y;
61+
var i;
62+
63+
x = discreteUniform( 100, 0, 40 );
64+
mycdf = cdf.factory( 0.3 );
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
y = mycdf( 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 cumulative distribution function (CDF) 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 CDF.
22+
23+
Examples
24+
--------
25+
> var y = {{alias}}( 2.0, 0.5 )
26+
~0.7769
27+
> y = {{alias}}( 2.0, 1.5 )
28+
~0.9889
29+
> y = {{alias}}( -1.0, 4.0 )
30+
0.0
31+
> y = {{alias}}( NaN, 0.5 )
32+
NaN
33+
> y = {{alias}}( 0.0, NaN )
34+
NaN
35+
// Invalid shape parameter
36+
> y = {{alias}}( 2.0, -1.4 )
37+
NaN
38+
39+
40+
{{alias}}.factory( λ )
41+
Returns a function for evaluating the cumulative distribution function (CDF)
42+
of a Planck distribution with shape parameter `λ`.
43+
44+
Parameters
45+
----------
46+
λ: number
47+
Shape parameter.
48+
49+
Returns
50+
-------
51+
cdf: Function
52+
Cumulative distribution function (CDF).
53+
54+
Examples
55+
--------
56+
> var mycdf = {{alias}}.factory( 1.5 );
57+
> var y = mycdf( 3.0 )
58+
~0.9975
59+
> y = mycdf( 1.0 )
60+
~0.9502
61+
62+
See Also
63+
--------
64+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Evaluates the cumulative distribution function (CDF) for a Planck distribution.
23+
*
24+
* @param x - input value
25+
* @returns evaluated CDF
26+
*/
27+
type Unary = ( x: number ) => number;
28+
29+
/**
30+
* Interface for the cumulative distribution function (CDF) of a Planck distribution.
31+
*/
32+
interface CDF {
33+
/**
34+
* Evaluates the cumulative distribution function (CDF) for a Planck distribution with shape parameter `lambda` at a value `x`.
35+
*
36+
* ## Notes
37+
*
38+
* - If `lambda <= 0`, the function returns `NaN`.
39+
*
40+
* @param x - input value
41+
* @param lambda - shape parameter
42+
* @returns evaluated CDF
43+
*
44+
* @example
45+
* var y = cdf( 2.0, 0.5 );
46+
* // returns ~0.7769
47+
*
48+
* @example
49+
* var y = cdf( 2.0, 1.5 );
50+
* // returns ~0.9889
51+
*
52+
* @example
53+
* var y = cdf( -1.0, 0.5 );
54+
* // returns 0.0
55+
*
56+
* @example
57+
* var y = cdf( NaN, 0.5 );
58+
* // returns NaN
59+
*
60+
* @example
61+
* var y = cdf( 0.0, NaN );
62+
* // returns NaN
63+
*
64+
* @example
65+
* // Invalid probability
66+
* var y = cdf( 2.0, 1.4 );
67+
* // returns NaN
68+
*/
69+
( x: number, lambda: number ): number;
70+
71+
/**
72+
* Returns a function for evaluating the cumulative distribution function (CDF) for a Planck distribution with shape parameter `lambda`.
73+
*
74+
* @param lambda - shape parameter
75+
* @returns CDF
76+
*
77+
* @example
78+
* var mycdf = cdf.factory( 1.5 );
79+
* var y = mycdf( 3.0 );
80+
* // returns ~0.9975
81+
*
82+
* y = mycdf( 1.0 );
83+
* // returns ~0.9502
84+
*/
85+
factory( lambda: number ): Unary;
86+
}
87+
88+
/**
89+
* Planck distribution cumulative distribution function (CDF).
90+
*
91+
* @param x - input value
92+
* @param lambda - shape parameter
93+
* @returns evaluated CDF
94+
*
95+
* @example
96+
* var y = cdf( 2.0, 0.5 );
97+
* // returns ~0.7769
98+
*
99+
* y = cdf( 2.0, 1.5 );
100+
* // returns ~0.9889
101+
*
102+
* var mycdf = cdf.factory( 1.5 );
103+
* y = mycdf( 3.0 );
104+
* // returns ~0.9975
105+
*
106+
* y = mycdf( 1.0 );
107+
* // returns ~0.9502
108+
*/
109+
declare var cdf: CDF;
110+
111+
112+
// EXPORTS //
113+
114+
export = cdf;

0 commit comments

Comments
 (0)