Skip to content

Commit eb61ba4

Browse files
feat: add stats/base/dists/bradford/cdf
PR-URL: #5282 Ref: #168 Co-authored-by: Philipp Burckhardt <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]>
1 parent 1ca8479 commit eb61ba4

File tree

18 files changed

+1427
-0
lines changed

18 files changed

+1427
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
# Cumulative Distribution Function
22+
23+
> [Bradford][bradford-distribution] distribution [cumulative distribution function][cdf] (CDF).
24+
25+
<section class="intro">
26+
27+
The [cumulative distribution function][cdf] (CDF) for a [Bradford][bradford-distribution] random variable is
28+
29+
<!-- <equation class="equation" label="eq:bradford_cdf" align="center" raw="F(x;c)=\frac{\ln(1+cx)}{\ln(1+c)}" alt="Cumulative distribution function (CDF) for a Bradford distribution."> -->
30+
31+
```math
32+
F(x;c)=\frac{\ln(1+cx)}{\ln(1+c)}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="F(x;c)=\frac{\ln(1+cx)}{\ln(1+c)}" data-equation="eq:bradford_cdf">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/bradford/cdf/docs/img/equation_bradford_cdf.svg" alt="Cumulative distribution function (CDF) for a Bradford distribution.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
where `c > 0` is the shape parameter of the distribution.
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/bradford/cdf' );
54+
```
55+
56+
#### cdf( x, c )
57+
58+
Evaluates the [cumulative distribution function][cdf] (CDF) for a [Bradford][bradford-distribution] distribution with shape parameter `c` at a value `x`.
59+
60+
```javascript
61+
var y = cdf( 0.1, 0.1 );
62+
// returns ~0.104
63+
64+
y = cdf( 0.5, 5.0 );
65+
// returns ~0.699
66+
67+
y = cdf( 1.0, 10.0 );
68+
// returns 1.0
69+
70+
y = cdf( -0.5, 1.0 );
71+
// returns 0.0
72+
73+
y = cdf( 2.0, 1.0 );
74+
// returns 1.0
75+
```
76+
77+
If provided `NaN` as any argument, the function returns `NaN`.
78+
79+
```javascript
80+
var y = cdf( NaN, 1.0 );
81+
// returns NaN
82+
83+
y = cdf( 0.0, NaN );
84+
// returns NaN
85+
```
86+
87+
If provided a shape parameter `c <= 0`, the function returns `NaN`.
88+
89+
```javascript
90+
var y = cdf( 0.0, 0.0 );
91+
// returns NaN
92+
93+
y = cdf( 0.5, -5.0 );
94+
// returns NaN
95+
```
96+
97+
#### cdf.factory( c )
98+
99+
Returns a function for evaluating the [CDF][cdf] of a [Bradford][bradford-distribution] distribution with shape parameter `c`.
100+
101+
```javascript
102+
var myPDF = cdf.factory( 5.0 );
103+
var y = myPDF( 0.5 );
104+
// returns ~0.699
105+
106+
y = myPDF( 1.0 );
107+
// returns 1.0
108+
```
109+
110+
</section>
111+
112+
<!-- /.usage -->
113+
114+
<section class="examples">
115+
116+
## Examples
117+
118+
<!-- eslint no-undef: "error" -->
119+
120+
```javascript
121+
var uniform = require( '@stdlib/random/array/uniform' );
122+
var cdf = require( '@stdlib/stats/base/dists/bradford/cdf' );
123+
124+
var x = uniform( 10, 0.0, 1.0 );
125+
var c = uniform( 10, 0.1, 10.0 );
126+
127+
var y;
128+
var i;
129+
for ( i = 0; i < x.length; i++ ) {
130+
y = cdf( x[ i ], c[ i ] );
131+
console.log( 'x: %d, c: %d, F(x;c): %d', x[ i ].toFixed( 4 ), c[ i ].toFixed( 4 ), y.toFixed( 4 ) );
132+
}
133+
```
134+
135+
</section>
136+
137+
<!-- /.examples -->
138+
139+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
140+
141+
<section class="related">
142+
143+
</section>
144+
145+
<!-- /.related -->
146+
147+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="links">
150+
151+
[cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function
152+
153+
[bradford-distribution]: https://en.wikipedia.org/wiki/Bradford%27s_law
154+
155+
</section>
156+
157+
<!-- /.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 cdf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var c;
35+
var y;
36+
var i;
37+
38+
x = uniform( 100, 0.0, 1.0 );
39+
c = uniform( 100, 0.1, 10.0 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = cdf( x[ i % x.length ], c[ i % c.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 mycdf;
58+
var x;
59+
var y;
60+
var i;
61+
62+
x = uniform( 100, 0.0, 1.0 );
63+
mycdf = cdf.factory( 5.0 );
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
y = mycdf( x[ i % x.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: 49 additions & 0 deletions
Loading
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
{{alias}}( x, c )
3+
Evaluates the cumulative distribution function
4+
(CDF) for a Bradford distribution
5+
with shape parameter `c` at a value `x`.
6+
7+
If provided `NaN` as any argument, the function returns `NaN`.
8+
9+
If provided `c <= 0`, the function returns `NaN`.
10+
11+
Parameters
12+
----------
13+
x: number
14+
Input value.
15+
16+
c: number
17+
Shape parameter.
18+
19+
Returns
20+
-------
21+
out: number
22+
Evaluated CDF.
23+
24+
Examples
25+
--------
26+
> var y = {{alias}}( 0.1, 0.1 )
27+
~0.104
28+
> y = {{alias}}( 0.5, 5.0 )
29+
~0.699
30+
> y = {{alias}}( 1.0, 10.0 )
31+
1.0
32+
> y = {{alias}}( -1.0, 0.5 )
33+
0.0
34+
> y = {{alias}}( 2.0, 0.5 )
35+
1.0
36+
37+
> y = {{alias}}( 0.5, 0.0 )
38+
NaN
39+
> y = {{alias}}( 0.5, -5.0 )
40+
NaN
41+
42+
> y = {{alias}}( NaN, 1.0 )
43+
NaN
44+
> y = {{alias}}( 1.0, NaN )
45+
NaN
46+
47+
48+
{{alias}}.factory( c )
49+
Returns a function for evaluating the cumulative distribution function
50+
(CDF) of a Bradford distribution with shape parameter `c`.
51+
52+
Parameters
53+
----------
54+
c: number
55+
Shape parameter.
56+
57+
Returns
58+
-------
59+
cdf: Function
60+
Cumulative distribution function (CDF).
61+
62+
Examples
63+
--------
64+
> var myCDF = {{alias}}.factory( 5.0 );
65+
> var y = myCDF( 0.5 )
66+
~0.699
67+
> y = myCDF( 1.0 )
68+
1.0
69+
70+
See Also
71+
--------
72+

0 commit comments

Comments
 (0)