Skip to content

Commit f93a62a

Browse files
committed
feat: add stats/base/dists/log-logistic/quantile
1 parent d479058 commit f93a62a

31 files changed

Lines changed: 2756 additions & 0 deletions

File tree

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
> [Log-logistic][log-logistic-distribution] distribution [quantile function][quantile-function].
24+
25+
<section class="intro">
26+
27+
The [quantile function][quantile-function] for a [log-logistic][log-logistic-distribution] random variable is
28+
29+
<!-- <equation class="equation" label="eq:log-logistic_quantile_function" align="center" raw="Q(p;\alpha,\beta) = \alpha\left(\dfrac{p}{1-p}\right)^{1/\beta}" alt="Quantile function for a log-logistic distribution."> -->
30+
31+
```math
32+
Q(p;\alpha,\beta) = \alpha\left(\dfrac{p}{1-p}\right)^{1/\beta}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="Q(p;\alpha,\beta) = \alpha\left(\dfrac{p}{1-p}\right)^{1/\beta}" data-equation="eq:log_logistic_quantile_function">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@.../lib/node_modules/@stdlib/stats/base/dists/log-logistic/quantile/docs/img/equation_log_logistic_quantile_function.svg" alt="Quantile function for a log-logistic distribution.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
for `0 <= p <= 1`, where `α > 0` is the scale parameter and `β > 0` is the shape parameter.
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<section class="usage">
49+
50+
## Usage
51+
52+
```javascript
53+
var quantile = require( '@stdlib/stats/base/dists/log-logistic/quantile' );
54+
```
55+
56+
#### quantile( p, alpha, beta )
57+
58+
Evaluates the [quantile function][quantile-function] for a [log-logistic][log-logistic-distribution] distribution with parameters `alpha` (scale parameter) and `beta` (shape parameter).
59+
60+
```javascript
61+
var y = quantile( 0.5, 1.0, 1.0 );
62+
// returns 1.0
63+
64+
y = quantile( 0.5, 4.0, 2.0 );
65+
// returns 4.0
66+
67+
y = quantile( 0.8, 1.0, 1.0 );
68+
// returns 4.0
69+
```
70+
71+
If provided a probability `p` outside the interval `[0,1]`, the function returns `NaN`.
72+
73+
```javascript
74+
var y = quantile( 1.9, 1.0, 1.0 );
75+
// returns NaN
76+
77+
y = quantile( -0.1, 1.0, 1.0 );
78+
// returns NaN
79+
```
80+
81+
If provided `NaN` as any argument, the function returns `NaN`.
82+
83+
```javascript
84+
var y = quantile( NaN, 1.0, 1.0 );
85+
// returns NaN
86+
87+
y = quantile( 0.5, NaN, 1.0 );
88+
// returns NaN
89+
90+
y = quantile( 0.5, 1.0, NaN );
91+
// returns NaN
92+
```
93+
94+
If provided `alpha <= 0`, the function returns `NaN`.
95+
96+
```javascript
97+
var y = quantile( 0.5, -1.0, 1.0 );
98+
// returns NaN
99+
```
100+
101+
If provided `beta <= 0`, the function returns `NaN`.
102+
103+
```javascript
104+
var y = quantile( 0.5, 1.0, -1.0 );
105+
// returns NaN
106+
```
107+
108+
#### quantile.factory( alpha, beta )
109+
110+
Returns a function for evaluating the [quantile function][quantile-function] of a [log-logistic][log-logistic-distribution] distribution with parameters `alpha` (scale parameter) and `beta` (shape parameter).
111+
112+
```javascript
113+
var myQuantile = quantile.factory( 1.0, 1.0 );
114+
115+
var y = myQuantile( 0.5 );
116+
// returns 1.0
117+
118+
y = myQuantile( 0.8 );
119+
// returns 4.0
120+
```
121+
122+
</section>
123+
124+
<!-- /.usage -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var uniform = require( '@stdlib/random/array/uniform' );
134+
var logEachMap = require( '@stdlib/console/log-each-map' );
135+
var quantile = require( '@stdlib/stats/base/dists/log-logistic/quantile' );
136+
137+
var opts = {
138+
'dtype': 'float64'
139+
};
140+
var p = uniform( 10, 0.0, 1.0, opts );
141+
var alpha = uniform( 10, 0.1, 10.0, opts );
142+
var beta = uniform( 10, 0.1, 10.0, opts );
143+
144+
logEachMap( 'p: %0.4f, alpha: %0.4f, beta: %0.4f, Q(p;alpha,beta): %0.4f', p, alpha, beta, quantile );
145+
```
146+
147+
</section>
148+
149+
<!-- /.examples -->
150+
151+
<!-- C interface documentation. -->
152+
153+
* * *
154+
155+
<section class="c">
156+
157+
## C APIs
158+
159+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
160+
161+
<section class="intro">
162+
163+
</section>
164+
165+
<!-- /.intro -->
166+
167+
<!-- C usage documentation. -->
168+
169+
<section class="usage">
170+
171+
### Usage
172+
173+
```c
174+
#include "stdlib/stats/base/dists/log-logistic/quantile.h"
175+
```
176+
177+
#### stdlib_base_dists_log_logistic_quantile( p, alpha, beta )
178+
179+
Evaluates the [quantile function][quantile-function] for a [log-logistic][log-logistic-distribution] distribution with parameters `alpha` (scale parameter) and `beta` (shape parameter).
180+
181+
```c
182+
double out = stdlib_base_dists_log_logistic_quantile( 0.5, 1.0, 1.0 );
183+
// returns 1.0
184+
```
185+
186+
The function accepts the following arguments:
187+
188+
- **p**: `[in] double` input probability.
189+
- **alpha**: `[in] double` scale parameter.
190+
- **beta**: `[in] double` shape parameter.
191+
192+
```c
193+
double stdlib_base_dists_log_logistic_quantile( const double p, const double alpha, const double beta );
194+
```
195+
196+
</section>
197+
198+
<!-- /.usage -->
199+
200+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
201+
202+
<section class="notes">
203+
204+
</section>
205+
206+
<!-- /.notes -->
207+
208+
<!-- C API usage examples. -->
209+
210+
<section class="examples">
211+
212+
### Examples
213+
214+
```c
215+
#include "stdlib/stats/base/dists/log-logistic/quantile.h"
216+
#include <stdlib.h>
217+
#include <stdio.h>
218+
219+
static double random_uniform( const double min, const double max ) {
220+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
221+
return min + ( v * ( max-min ) );
222+
}
223+
224+
int main( void ) {
225+
double alpha;
226+
double beta;
227+
double p;
228+
double y;
229+
int i;
230+
231+
for ( i = 0; i < 25; i++ ) {
232+
p = random_uniform( 0.0, 1.0 );
233+
alpha = random_uniform( 0.1, 10.0 );
234+
beta = random_uniform( 0.1, 10.0 );
235+
y = stdlib_base_dists_log_logistic_quantile( p, alpha, beta );
236+
printf( "p: %lf, alpha: %lf, beta: %lf, Q(p;alpha,beta): %lf\n", p, alpha, beta, y );
237+
}
238+
}
239+
```
240+
241+
</section>
242+
243+
<!-- /.examples -->
244+
245+
</section>
246+
247+
<!-- /.c -->
248+
249+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
250+
251+
<section class="related">
252+
253+
</section>
254+
255+
<!-- /.related -->
256+
257+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
258+
259+
<section class="links">
260+
261+
[log-logistic-distribution]: https://en.wikipedia.org/wiki/Log-logistic_distribution
262+
263+
[quantile-function]: https://en.wikipedia.org/wiki/Quantile_function
264+
265+
</section>
266+
267+
<!-- /.links -->
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 EPS = require( '@stdlib/constants/float64/eps' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var quantile = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var alpha;
36+
var beta;
37+
var opts;
38+
var p;
39+
var y;
40+
var i;
41+
42+
opts = {
43+
'dtype': 'float64'
44+
};
45+
p = uniform( 100, EPS, 1.0 - EPS, opts );
46+
alpha = uniform( 100, EPS, 5.0, opts );
47+
beta = uniform( 100, EPS, 5.0, opts );
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
y = quantile( p[ i % p.length ], alpha[ i % alpha.length ], beta[ i % beta.length ] );
52+
if ( isnan( y ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
}
56+
b.toc();
57+
if ( isnan( y ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
63+
64+
bench( format( '%s::factory', pkg ), function benchmark( b ) {
65+
var myquantile;
66+
var alpha;
67+
var beta;
68+
var opts;
69+
var p;
70+
var y;
71+
var i;
72+
73+
alpha = 2.0;
74+
beta = 3.0;
75+
myquantile = quantile.factory( alpha, beta );
76+
77+
opts = {
78+
'dtype': 'float64'
79+
};
80+
p = uniform( 100, EPS, 1.0 - EPS, opts );
81+
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
y = myquantile( p[ i % p.length ] );
85+
if ( isnan( y ) ) {
86+
b.fail( 'should not return NaN' );
87+
}
88+
}
89+
b.toc();
90+
if ( isnan( y ) ) {
91+
b.fail( 'should not return NaN' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
});

0 commit comments

Comments
 (0)