Skip to content

Commit 1949944

Browse files
feat: add stats/base/dists/bradford/pdf
PR-URL: #5280 Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 9cff95d commit 1949944

File tree

18 files changed

+1384
-0
lines changed

18 files changed

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

0 commit comments

Comments
 (0)