-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add stats/base/dists/bradford/cdf
#5282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8b8478c
feat: add implementation
anandkaranubc bd98a5e
feat: add tests
anandkaranubc b566479
feat: add docs, benchmarks and examples
anandkaranubc 0ac07a7
refactor: random number generation in examples
anandkaranubc 257e1e5
refactor: use uniform array in benchmarks
anandkaranubc 9f7dd5e
bench: add factory benchmarks
anandkaranubc 9b92935
fix: update implementation to include support for x outside of [0,1]
anandkaranubc d48dd90
docs: apply suggestions from code review
Planeshifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
lib/node_modules/@stdlib/stats/base/dists/bradford/cdf/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# Cumulative Distribution Function | ||
|
||
> [Bradford][bradford-distribution] distribution [cumulative distribution function][cdf] (CDF). | ||
|
||
<section class="intro"> | ||
|
||
The [cumulative distribution function][cdf] (CDF) for a [Bradford][bradford-distribution] random variable is | ||
|
||
<!-- <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."> --> | ||
|
||
```math | ||
F(x;c)=\frac{\ln(1+cx)}{\ln(1+c)} | ||
``` | ||
|
||
<!-- <div class="equation" align="center" data-raw-text="F(x;c)=\frac{\ln(1+cx)}{\ln(1+c)}" data-equation="eq:bradford_cdf"> | ||
<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."> | ||
<br> | ||
</div> --> | ||
|
||
<!-- </equation> --> | ||
|
||
where `c > 0` is the shape parameter of the distribution. | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var cdf = require( '@stdlib/stats/base/dists/bradford/cdf' ); | ||
``` | ||
|
||
#### cdf( x, c ) | ||
|
||
Evaluates the [cumulative distribution function][cdf] (CDF) for a [Bradford][bradford-distribution] distribution with shape parameter `c` at a value `x`. | ||
|
||
```javascript | ||
var y = cdf( 0.1, 0.1 ); | ||
// returns ~0.104 | ||
|
||
y = cdf( 0.5, 5.0 ); | ||
// returns ~0.699 | ||
|
||
y = cdf( 1.0, 10.0 ); | ||
// returns 1.0 | ||
|
||
y = cdf( -0.5, 1.0 ); | ||
// returns 0.0 | ||
|
||
y = cdf( 2.0, 1.0 ); | ||
// returns 1.0 | ||
``` | ||
|
||
If provided `NaN` as any argument, the function returns `NaN`. | ||
|
||
```javascript | ||
var y = cdf( NaN, 1.0 ); | ||
// returns NaN | ||
|
||
y = cdf( 0.0, NaN ); | ||
// returns NaN | ||
``` | ||
|
||
If provided a shape parameter `c <= 0`, the function returns `NaN`. | ||
|
||
```javascript | ||
var y = cdf( 0.0, 0.0 ); | ||
// returns NaN | ||
|
||
y = cdf( 0.5, -5.0 ); | ||
// returns NaN | ||
``` | ||
|
||
#### cdf.factory( c ) | ||
|
||
Returns a function for evaluating the [CDF][cdf] of a [Bradford][bradford-distribution] distribution with shape parameter `c`. | ||
|
||
```javascript | ||
var myPDF = cdf.factory( 5.0 ); | ||
var y = myPDF( 0.5 ); | ||
// returns ~0.699 | ||
|
||
y = myPDF( 1.0 ); | ||
// returns 1.0 | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var uniform = require( '@stdlib/random/array/uniform' ); | ||
var cdf = require( '@stdlib/stats/base/dists/bradford/cdf' ); | ||
|
||
var x = uniform( 10, 0.0, 1.0 ); | ||
var c = uniform( 10, 0.1, 10.0 ); | ||
|
||
var y; | ||
var i; | ||
for ( i = 0; i < x.length; i++ ) { | ||
y = cdf( x[ i ], c[ i ] ); | ||
console.log( 'x: %d, c: %d, F(x;c): %d', x[ i ].toFixed( 4 ), c[ i ].toFixed( 4 ), y.toFixed( 4 ) ); | ||
} | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function | ||
|
||
[bradford-distribution]: https://en.wikipedia.org/wiki/Bradford%27s_law | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
78 changes: 78 additions & 0 deletions
78
lib/node_modules/@stdlib/stats/base/dists/bradford/cdf/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var uniform = require( '@stdlib/random/array/uniform' ); | ||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var pkg = require( './../package.json' ).name; | ||
var cdf = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var x; | ||
var c; | ||
var y; | ||
var i; | ||
|
||
x = uniform( 100, 0.0, 1.0 ); | ||
c = uniform( 100, 0.1, 10.0 ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
y = cdf( x[ i % x.length ], c[ i % c.length ] ); | ||
if ( isnan( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnan( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); | ||
|
||
bench( pkg+':factory', function benchmark( b ) { | ||
var mycdf; | ||
var x; | ||
var y; | ||
var i; | ||
|
||
x = uniform( 100, 0.0, 1.0 ); | ||
mycdf = cdf.factory( 5.0 ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
y = mycdf( x[ i % x.length ] ); | ||
if ( isnan( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnan( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
49 changes: 49 additions & 0 deletions
49
...odules/@stdlib/stats/base/dists/bradford/cdf/docs/img/equation_bradford_cdf.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions
72
lib/node_modules/@stdlib/stats/base/dists/bradford/cdf/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
{{alias}}( x, c ) | ||
Evaluates the cumulative distribution function | ||
(CDF) for a Bradford distribution | ||
with shape parameter `c` at a value `x`. | ||
|
||
If provided `NaN` as any argument, the function returns `NaN`. | ||
|
||
If provided `c <= 0`, the function returns `NaN`. | ||
|
||
Parameters | ||
---------- | ||
x: number | ||
Input value. | ||
|
||
c: number | ||
Shape parameter. | ||
|
||
Returns | ||
------- | ||
out: number | ||
Evaluated CDF. | ||
|
||
Examples | ||
-------- | ||
> var y = {{alias}}( 0.1, 0.1 ) | ||
~0.104 | ||
> y = {{alias}}( 0.5, 5.0 ) | ||
~0.699 | ||
> y = {{alias}}( 1.0, 10.0 ) | ||
1.0 | ||
> y = {{alias}}( -1.0, 0.5 ) | ||
0.0 | ||
> y = {{alias}}( 2.0, 0.5 ) | ||
1.0 | ||
|
||
> y = {{alias}}( 0.5, 0.0 ) | ||
NaN | ||
> y = {{alias}}( 0.5, -5.0 ) | ||
NaN | ||
|
||
> y = {{alias}}( NaN, 1.0 ) | ||
NaN | ||
> y = {{alias}}( 1.0, NaN ) | ||
NaN | ||
|
||
|
||
{{alias}}.factory( c ) | ||
Returns a function for evaluating the cumulative distribution function | ||
(CDF) of a Bradford distribution with shape parameter `c`. | ||
|
||
Parameters | ||
---------- | ||
c: number | ||
Shape parameter. | ||
|
||
Returns | ||
------- | ||
cdf: Function | ||
Cumulative distribution function (CDF). | ||
|
||
Examples | ||
-------- | ||
> var myPDF = {{alias}}.factory( 5.0 ); | ||
> var y = myPDF( 0.5 ) | ||
~0.699 | ||
> y = myPDF( 1.0 ) | ||
1.0 | ||
|
||
See Also | ||
-------- | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.