Skip to content

Commit 5524db1

Browse files
Jaysukh-409kgrytestdlib-bot
authored
feat: add stats/base/dists/planck/quantile
PR-URL: #4392 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 34b3c31 commit 5524db1

File tree

16 files changed

+1244
-0
lines changed

16 files changed

+1244
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
# Quantile Function
22+
23+
> Planck (discrete exponential) distribution [quantile function][quantile-function].
24+
25+
<section class="intro">
26+
27+
The [quantile function][quantile-function] for a Planck random variable is
28+
29+
<!-- <equation class="equation" label="eq:planck_quantile_function" align="center" raw="Q(p;\lambda) = \left\lceil -\frac{\ln(1-p)}{\lambda} - 1 \right\rceil" alt="Quantile function for a Planck distribution."> -->
30+
31+
```math
32+
Q(p;\lambda) = \left\lceil -\frac{\ln(1-p)}{\lambda} \right\rceil \!-1
33+
```
34+
35+
<!-- </equation> -->
36+
37+
for `0 < p < 1` and where `λ` is the shape parameter.
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var quantile = require( '@stdlib/stats/base/dists/planck/quantile' );
49+
```
50+
51+
#### quantile( p, lambda )
52+
53+
Evaluates the [quantile function][quantile-function] for a Planck distribution with shape parameter `lambda` at a probability `p`.
54+
55+
```javascript
56+
var y = quantile( 0.8, 0.4 );
57+
// returns 4
58+
59+
y = quantile( 0.5, 1.4 );
60+
// returns 0
61+
62+
y = quantile( 0.9, 2.1 );
63+
// returns 1
64+
```
65+
66+
If provided an input probability `p` outside the interval `[0,1]`, the function returns `NaN`.
67+
68+
```javascript
69+
var y = quantile( 1.9, 0.5 );
70+
// returns NaN
71+
72+
y = quantile( -0.1, 0.5 );
73+
// returns NaN
74+
```
75+
76+
If provided `NaN` as any argument, the function returns `NaN`.
77+
78+
```javascript
79+
var y = quantile( NaN, 1.0 );
80+
// returns NaN
81+
82+
y = quantile( 0.0, NaN );
83+
// returns NaN
84+
```
85+
86+
If provided a shape parameter `lambda` which is nonpositive, the function returns `NaN`.
87+
88+
```javascript
89+
var y = quantile( 0.4, -1.0 );
90+
// returns NaN
91+
```
92+
93+
#### quantile.factory( lambda )
94+
95+
Returns a function for evaluating the [quantile function][quantile-function] for a Planck distribution with shape parameter `lambda`.
96+
97+
```javascript
98+
var myquantile = quantile.factory( 0.4 );
99+
var y = myquantile( 0.4 );
100+
// returns 1
101+
102+
y = myquantile( 0.8 );
103+
// returns 4
104+
105+
y = myquantile( 1.0 );
106+
// returns Infinity
107+
```
108+
109+
</section>
110+
111+
<!-- /.usage -->
112+
113+
<section class="examples">
114+
115+
## Examples
116+
117+
<!-- eslint no-undef: "error" -->
118+
119+
```javascript
120+
var uniform = require( '@stdlib/random/array/uniform' );
121+
var quantile = require( '@stdlib/stats/base/dists/planck/quantile' );
122+
123+
var lambda = uniform( 10, 0.1, 10.0 );
124+
var p = uniform( 10, 0.0, 1.0 );
125+
126+
var y;
127+
var i;
128+
for ( i = 0; i < lambda.length; i++ ) {
129+
y = quantile( p[ i ], lambda[ i ] );
130+
console.log( 'p: %d, λ: %d, Q(p;λ): %d', p[ i ].toFixed( 4 ), lambda[ i ].toFixed( 4 ), y.toFixed( 4 ) );
131+
}
132+
```
133+
134+
</section>
135+
136+
<!-- /.examples -->
137+
138+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139+
140+
<section class="related">
141+
142+
</section>
143+
144+
<!-- /.related -->
145+
146+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
147+
148+
<section class="links">
149+
150+
[quantile-function]: https://en.wikipedia.org/wiki/Quantile_function
151+
152+
</section>
153+
154+
<!-- /.links -->
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var quantile = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var lambda;
34+
var p;
35+
var y;
36+
var i;
37+
38+
p = uniform( 100, 0.0, 1.0 );
39+
lambda = uniform( 100, 0.1, 10.0 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = quantile( p[ i % p.length ], lambda[ i % lambda.length ] );
44+
if ( isnan( y ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnan( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+':factory', function benchmark( b ) {
57+
var myquantile;
58+
var p;
59+
var y;
60+
var i;
61+
62+
myquantile = quantile.factory( 0.3 );
63+
p = uniform( 100, 0.0, 1.0 );
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
y = myquantile( p[ i % p.length ] );
68+
if ( isnan( y ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( y ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
});
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
{{alias}}( p, λ )
3+
Evaluates the quantile function for a Planck distribution with shape
4+
parameter `λ` at a probability `p`.
5+
6+
If `p < 0` or `p > 1`, the function returns `NaN`.
7+
8+
If `λ <= 0`, the function returns `NaN`.
9+
10+
If provided `NaN` as any argument, the function returns `NaN`.
11+
12+
Parameters
13+
----------
14+
p: number
15+
Input probability.
16+
17+
λ: number
18+
Shape parameter.
19+
20+
Returns
21+
-------
22+
out: number
23+
Evaluated quantile function.
24+
25+
Examples
26+
--------
27+
> var y = {{alias}}( 0.8, 0.4 )
28+
4
29+
> y = {{alias}}( 0.5, 1.4 )
30+
0
31+
> y = {{alias}}( 0.9, 2.1 )
32+
1
33+
34+
> y = {{alias}}( 0.2, -0.1 )
35+
NaN
36+
37+
> y = {{alias}}( NaN, 0.8 )
38+
NaN
39+
> y = {{alias}}( 0.4, NaN )
40+
NaN
41+
42+
> y = {{alias}}( -0.5, 1.0 )
43+
NaN
44+
> y = {{alias}}( 1.5, 1.0 )
45+
NaN
46+
47+
48+
{{alias}}.factory( λ )
49+
Returns a function for evaluating the quantile function of a Planck
50+
distribution with shape parameter `λ`.
51+
52+
Parameters
53+
----------
54+
λ: number
55+
Shape parameter.
56+
57+
Returns
58+
-------
59+
quantile: Function
60+
Quantile function.
61+
62+
Examples
63+
--------
64+
> var myquantile = {{alias}}.factory( 0.4 );
65+
> var y = myquantile( 0.4 )
66+
1
67+
> y = myquantile( 0.8 )
68+
4
69+
> y = myquantile( 1.0 )
70+
Infinity
71+
72+
See Also
73+
--------
74+

0 commit comments

Comments
 (0)