Skip to content

Commit 75d869b

Browse files
committed
feat: add stats/base/dists/log-logistic/cdf
1 parent 62019bc commit 75d869b

File tree

31 files changed

+2660
-0
lines changed

31 files changed

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