-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add stats/base/dists/bradford/pdf
#5280
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 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e443b63
feat: add implementation
anandkaranubc cef2d06
feat: add tests
anandkaranubc f8aa307
feat: add docs, benchmarks and examples
anandkaranubc 9996a17
refactor: use log1p instead of ln
anandkaranubc 4af0019
fix: case differences
anandkaranubc eefcb33
refactor: apply suggestions from PR review
anandkaranubc e53463c
refactor: use uniform array in benchmarks
anandkaranubc a4813ba
bench: add factory benchmarks
anandkaranubc 5bd5162
fix: update implementation to include support for x outside of [0,1]
anandkaranubc 85c5fdf
docs: rearrange the description in README
anandkaranubc 87d81be
Merge branch 'stdlib-js:develop' into bradford/pdf
anandkaranubc af381be
docs: remove repeated keywords
anandkaranubc 6f952de
Merge branch 'stdlib-js:develop' into bradford/pdf
anandkaranubc 6efb60b
chore: apply suggestions from review
anandkaranubc 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
163 changes: 163 additions & 0 deletions
163
lib/node_modules/@stdlib/stats/base/dists/bradford/pdf/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,163 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# Probability Density Function | ||
|
||
> [Bradford][bradford-distribution] distribution [probability density function][pdf] (PDF). | ||
|
||
<section class="intro"> | ||
|
||
The [probability density function][pdf] (PDF) for a [bradford][bradford-distribution] random variable is | ||
|
||
<!-- <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."> --> | ||
anandkaranubc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
```math | ||
f(x;c)=\frac{c}{(\ln(1+c)) (1 + cx)} | ||
``` | ||
|
||
<!-- <div class="equation" align="center" data-raw-text="f(x;c)=\frac{c}{(\ln(1+c)) (1 + cx)}" data-equation="eq:bradford_pdf"> | ||
<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."> | ||
anandkaranubc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
<br> | ||
</div> --> | ||
|
||
<!-- </equation> --> | ||
|
||
where `c` is the shape parameter of the distribution. The parameters must satisfy `0 <= x <= 1` and `c > 0`. | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var pdf = require( '@stdlib/stats/base/dists/bradford/pdf' ); | ||
``` | ||
|
||
#### pdf( x, c ) | ||
|
||
Evaluates the [probability density function][pdf] (PDF) for a [bradford][bradford-distribution] distribution with shape parameter `c` at a value `x`. | ||
anandkaranubc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
```javascript | ||
var y = pdf( 0.1, 0.1 ); | ||
// returns ~1.039 | ||
|
||
y = pdf( 0.5, 5.0 ); | ||
// returns ~0.797 | ||
|
||
y = pdf( 1.0, 10.0 ); | ||
// returns ~0.379 | ||
``` | ||
|
||
If provided `NaN` as any argument, the function returns `NaN`. | ||
|
||
```javascript | ||
var y = pdf( NaN, 1.0 ); | ||
// returns NaN | ||
|
||
y = pdf( 0.0, NaN ); | ||
// returns NaN | ||
``` | ||
|
||
If provided an input value `x` outside of the interval `[0,1]`, the function returns `NaN`. | ||
|
||
```javascript | ||
var y = pdf( 2.0, 1.0 ); | ||
// returns NaN | ||
|
||
y = pdf( -0.5, 1.0 ); | ||
// returns NaN | ||
``` | ||
|
||
If provided a shape parameter `c <= 0`, the function returns `NaN`. | ||
|
||
```javascript | ||
var y = pdf( 0.0, 0.0 ); | ||
// returns NaN | ||
|
||
y = pdf( 0.5, -1.0 ); | ||
// returns NaN | ||
``` | ||
|
||
#### pdf.factory( c ) | ||
|
||
Returns a `function` for evaluating the [PDF][pdf] of a [bradford][bradford-distribution] distribution with shape parameter `c`. | ||
anandkaranubc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
```javascript | ||
var myPDF = pdf.factory( 5.0 ); | ||
var y = myPDF( 0.5 ); | ||
// returns ~0.797 | ||
|
||
y = myPDF( 1.0 ); | ||
// returns ~0.465 | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var pdf = require( '@stdlib/stats/base/dists/bradford/pdf' ); | ||
|
||
var x; | ||
var c; | ||
var y; | ||
var i; | ||
|
||
for ( i = 0; i < 25; i++ ) { | ||
x = randu(); | ||
c = ( randu()*10.0 ); | ||
anandkaranubc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
y = pdf( x, c ); | ||
console.log( 'x: %d, c: %d, f(x;c): %d', x.toFixed( 4 ), c.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"> | ||
|
||
[pdf]: https://en.wikipedia.org/wiki/Probability_density_function | ||
|
||
[bradford-distribution]: https://en.wikipedia.org/wiki/Bradford%27s_law | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
62 changes: 62 additions & 0 deletions
62
lib/node_modules/@stdlib/stats/base/dists/bradford/pdf/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,62 @@ | ||
/** | ||
* @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 Float64Array = require( '@stdlib/array/float64' ); | ||
var uniform = require( '@stdlib/random/base/uniform' ); | ||
var EPS = require( '@stdlib/constants/float64/eps' ); | ||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var pkg = require( './../package.json' ).name; | ||
var pdf = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var len; | ||
var x; | ||
var c; | ||
var y; | ||
var i; | ||
|
||
len = 100; | ||
x = new Float64Array( len ); | ||
c = new Float64Array( len ); | ||
for ( i = 0; i < len; i++ ) { | ||
x[ i ] = uniform( 0.0, 1.0 ); | ||
c[ i ] = uniform( EPS, 10.0 ); | ||
} | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
y = pdf( x[ i % len ], c[ i % len ] ); | ||
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(); | ||
}); |
50 changes: 50 additions & 0 deletions
50
...odules/@stdlib/stats/base/dists/bradford/pdf/docs/img/equation_bradford_pdf.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions
69
lib/node_modules/@stdlib/stats/base/dists/bradford/pdf/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,69 @@ | ||
|
||
{{alias}}( x, c ) | ||
Evaluates the probability density function (PDF) for a bradford distribution | ||
anandkaranubc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
with shape parameter `c` at a value `x`. | ||
|
||
If provided `NaN` as any argument, the function returns `NaN`. | ||
|
||
If `x < 0` or `x > 1`, 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 PDF. | ||
|
||
Examples | ||
-------- | ||
> var y = {{alias}}( 0.1, 0.1 ) | ||
~1.039 | ||
> y = {{alias}}( 0.5, 5.0 ) | ||
~0.797 | ||
> y = {{alias}}( 1.0, 10.0 ) | ||
~0.379 | ||
> y = {{alias}}( 0.5, 0.0 ) | ||
NaN | ||
> y = {{alias}}( 2.0, 0.5 ) | ||
NaN | ||
> y = {{alias}}( -1.0, 0.5 ) | ||
NaN | ||
> y = {{alias}}( NaN, 1.0 ) | ||
NaN | ||
> y = {{alias}}( 1.0, NaN ) | ||
NaN | ||
|
||
|
||
{{alias}}.factory( c ) | ||
Returns a function for evaluating the probability density function (PDF) of | ||
a bradford distribution with shape parameter `c`. | ||
anandkaranubc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Parameters | ||
---------- | ||
c: number | ||
Shape parameter. | ||
|
||
Returns | ||
------- | ||
pdf: Function | ||
Probability density function (PDF). | ||
|
||
Examples | ||
-------- | ||
> var myPDF = {{alias}}.factory( 5.0 ); | ||
> var y = myPDF( 0.5 ) | ||
~0.797 | ||
> y = myPDF( 1.0 ) | ||
~0.465 | ||
|
||
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.