Skip to content

Commit e69f31a

Browse files
Jaysukh-409kgrytestdlib-bot
authored andcommitted
feat: add stats/base/dists/planck/logcdf
PR-URL: stdlib-js#4227 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent f34520d commit e69f31a

File tree

16 files changed

+1260
-0
lines changed

16 files changed

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

0 commit comments

Comments
 (0)