-
-
Notifications
You must be signed in to change notification settings - Fork 908
feat: add stats/base/dists/bradford/median
#5275
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
Planeshifter
merged 12 commits into
stdlib-js:develop
from
anandkaranubc:bradford/median
Feb 21, 2025
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
732af8d
feat: add implementation
anandkaranubc a8da41d
feat: add tests
anandkaranubc 891d515
feat: add docs, benchmarks and examples
anandkaranubc cab5657
refactor: follow code convention
anandkaranubc 1d1750c
refactor: random number generation in examples
anandkaranubc baed14b
refactor: use uniform array in benchmarks
anandkaranubc 6820e92
fix: parameter types
anandkaranubc 703cae2
refactor: update median formula for better readability
anandkaranubc c3ef3b1
docs: fix the description in README
anandkaranubc 8ea4228
docs: improve clarity in README
anandkaranubc fb0ec1b
docs: fix typo in title
anandkaranubc 9ce9331
chore: remove linebreak
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
138 changes: 138 additions & 0 deletions
138
lib/node_modules/@stdlib/stats/base/dists/bradford/median/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,138 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# Median | ||
|
||
> [Bradford][bradford-distribution] distribution [median][median]. | ||
|
||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
The [median][median] for a [Bradford][bradford-distribution] random variable is | ||
|
||
<!-- <equation class="equation" label="eq:bradford_median" align="center" raw="\mathop{\mathrm{Median}}\left( X \right) = \frac{\sqrt{1 + c} - 1}{c}" alt="Median for a Bradford distribution."> --> | ||
|
||
```math | ||
\mathop{\mathrm{Median}}\left( X \right) = \frac{\sqrt{1 + c} - 1}{c} | ||
``` | ||
|
||
<!-- <div class="equation" align="center" data-raw-text="\mathop{\mathrm{Median}}\left( X \right) = \frac{\sqrt{1 + c} - 1}{c}" data-equation="eq:bradford_median"> | ||
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@591cf9d5c3a0cd3c1ceec961e5c49d73a68374cb/lib/node_modules/@stdlib/stats/base/dists/bradford/median/docs/img/equation_bradford_median.svg" alt="Median for a Bradford distribution."> | ||
<br> | ||
</div> --> | ||
|
||
<!-- </equation> --> | ||
|
||
where `c` is the shape parameter. | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- Package usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var median = require( '@stdlib/stats/base/dists/bradford/median' ); | ||
``` | ||
|
||
#### median( c ) | ||
|
||
Returns the [median][median] of a [Bradford][bradford-distribution] distribution with shape parameter `c`. | ||
|
||
```javascript | ||
var v = median( 0.1 ); | ||
// returns ~0.488 | ||
|
||
v = median( 10.0 ); | ||
// returns ~0.232 | ||
``` | ||
|
||
If provided a shape parameter `c <= 0`, the function returns `NaN`. | ||
|
||
```javascript | ||
var v = median( 0.0 ); | ||
// returns NaN | ||
|
||
v = median( -1.5 ); | ||
// returns NaN | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var uniform = require( '@stdlib/random/array/uniform' ); | ||
var median = require( '@stdlib/stats/base/dists/bradford/median' ); | ||
|
||
var c = uniform( 10, 0.1, 10.0 ); | ||
|
||
var v; | ||
var i; | ||
for ( i = 0; i < c.length; i++ ) { | ||
v = median( c[ i ] ); | ||
console.log( 'c: %d, Median(X;c): %d', c[ i ].toFixed( 4 ), v.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"> | ||
|
||
[bradford-distribution]: https://en.wikipedia.org/wiki/Bradford%27s_law | ||
|
||
[median]: https://en.wikipedia.org/wiki/Median | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
52 changes: 52 additions & 0 deletions
52
lib/node_modules/@stdlib/stats/base/dists/bradford/median/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,52 @@ | ||
/** | ||
* @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 median = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var c; | ||
var y; | ||
var i; | ||
|
||
c = uniform( 100, 0.1, 10.0 ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
y = median( 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(); | ||
}); |
51 changes: 51 additions & 0 deletions
51
.../@stdlib/stats/base/dists/bradford/median/docs/img/equation_bradford_median.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions
29
lib/node_modules/@stdlib/stats/base/dists/bradford/median/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,29 @@ | ||
|
||
{{alias}}( c ) | ||
Returns the median of a Bradford distribution with shape | ||
parameter `c`. | ||
|
||
If `c <= 0`, the function returns `NaN`. | ||
|
||
Parameters | ||
---------- | ||
c: number | ||
Shape parameter. | ||
|
||
Returns | ||
------- | ||
out: number | ||
Median. | ||
|
||
Examples | ||
-------- | ||
> var v = {{alias}}( 0.1 ) | ||
~0.488 | ||
> v = {{alias}}( 0.5 ) | ||
~0.449 | ||
> v = {{alias}}( 10.0 ) | ||
~0.232 | ||
|
||
See Also | ||
-------- | ||
|
60 changes: 60 additions & 0 deletions
60
lib/node_modules/@stdlib/stats/base/dists/bradford/median/docs/types/index.d.ts
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,60 @@ | ||
/* | ||
* @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. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
/** | ||
* Returns the median of a Bradford distribution. | ||
* | ||
* ## Notes | ||
* | ||
* - If `c <= 0`, the function returns `NaN`. | ||
* | ||
* @param c - shape parameter | ||
* @returns median | ||
* | ||
* @example | ||
* var v = median( 0.1 ); | ||
* // returns ~0.488 | ||
* | ||
* @example | ||
* var v = median( 0.5 ); | ||
* // returns ~0.449 | ||
* | ||
* @example | ||
* var v = median( 10.0 ); | ||
* // returns ~0.232 | ||
* | ||
* @example | ||
* var v = median( 0.0 ); | ||
* // returns NaN | ||
* | ||
* @example | ||
* var v = median( -1.0 ); | ||
* // returns NaN | ||
* | ||
* @example | ||
* var v = median( NaN ); | ||
* // returns NaN | ||
*/ | ||
declare function median( c: number ): number; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = median; |
44 changes: 44 additions & 0 deletions
44
lib/node_modules/@stdlib/stats/base/dists/bradford/median/docs/types/test.ts
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,44 @@ | ||
/* | ||
* @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. | ||
*/ | ||
|
||
import median = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a number... | ||
{ | ||
median( 0.3 ); // $ExpectType number | ||
} | ||
|
||
// The compiler throws an error if the function is provided a value other than a number... | ||
{ | ||
median( true ); // $ExpectError | ||
median( false ); // $ExpectError | ||
median( null ); // $ExpectError | ||
median( undefined ); // $ExpectError | ||
median( '5' ); // $ExpectError | ||
median( [] ); // $ExpectError | ||
median( {} ); // $ExpectError | ||
median( ( x: number ): number => x ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided insufficient arguments... | ||
{ | ||
median(); // $ExpectError | ||
} |
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.