Skip to content

Commit 21f8e14

Browse files
feat: add stats/base/dists/bradford/mode
PR-URL: #5315 Ref: #168 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 996c248 commit 21f8e14

File tree

11 files changed

+633
-0
lines changed

11 files changed

+633
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
# Mode
22+
23+
> [Bradford][bradford-distribution] distribution [mode][mode].
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
The [mode][mode] for a [Bradford][bradford-distribution] random variable is
30+
31+
<!-- <equation class="equation" label="eq:bradford_mode" align="center" raw="\mathop{\mathrm{mode}}\left( X \right) = 0" alt="Mode for a Bradford distribution."> -->
32+
33+
```math
34+
\mathop{\mathrm{mode}}\left( X \right) = 0
35+
```
36+
37+
<!-- <div class="equation" align="center" data-raw-text="\mathop{\mathrm{mode}}\left( X \right) = 0" data-equation="eq:bradford_mode">
38+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@591cf9d5c3a0cd3c1ceec961e5c49d73a68374cb/lib/node_modules/@stdlib/stats/base/dists/bradford/mode/docs/img/equation_bradford_mode.svg" alt="Mode for a Bradford distribution.">
39+
<br>
40+
</div> -->
41+
42+
<!-- </equation> -->
43+
44+
where `c` is the shape parameter.
45+
46+
</section>
47+
48+
<!-- /.intro -->
49+
50+
<!-- Package usage documentation. -->
51+
52+
<section class="usage">
53+
54+
## Usage
55+
56+
```javascript
57+
var mode = require( '@stdlib/stats/base/dists/bradford/mode' );
58+
```
59+
60+
#### mode( c )
61+
62+
Returns the [mode][mode] of a [Bradford][bradford-distribution] distribution with shape parameter `c`.
63+
64+
```javascript
65+
var v = mode( 0.1 );
66+
// returns 0.0
67+
68+
v = mode( 10.0 );
69+
// returns 0.0
70+
```
71+
72+
If provided a shape parameter `c <= 0`, the function returns `NaN`.
73+
74+
```javascript
75+
var v = mode( 0.0 );
76+
// returns NaN
77+
78+
v = mode( -1.5 );
79+
// returns NaN
80+
```
81+
82+
</section>
83+
84+
<!-- /.usage -->
85+
86+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
87+
88+
<section class="notes">
89+
90+
</section>
91+
92+
<!-- /.notes -->
93+
94+
<!-- Package usage examples. -->
95+
96+
<section class="examples">
97+
98+
## Examples
99+
100+
<!-- eslint no-undef: "error" -->
101+
102+
```javascript
103+
var uniform = require( '@stdlib/random/array/uniform' );
104+
var mode = require( '@stdlib/stats/base/dists/bradford/mode' );
105+
106+
var c = uniform( 10, 0.1, 10.0 );
107+
108+
var v;
109+
var i;
110+
for ( i = 0; i < c.length; i++ ) {
111+
v = mode( c[ i ] );
112+
console.log( 'c: %d, mode(X;c): %d', c[ i ].toFixed( 4 ), v.toFixed( 4 ) );
113+
}
114+
```
115+
116+
</section>
117+
118+
<!-- /.examples -->
119+
120+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
121+
122+
<section class="related">
123+
124+
</section>
125+
126+
<!-- /.related -->
127+
128+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="links">
131+
132+
[bradford-distribution]: https://en.wikipedia.org/wiki/Bradford%27s_law
133+
134+
[mode]: https://en.wikipedia.org/wiki/Mode_%28statistics%29
135+
136+
</section>
137+
138+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 mode = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var c;
34+
var y;
35+
var i;
36+
37+
c = uniform( 100, 0.1, 10.0 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = mode( c[ i % c.length ] );
42+
if ( isnan( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnan( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( c )
3+
Returns the mode of a Bradford distribution with shape parameter `c`.
4+
5+
If `c <= 0`, the function returns `NaN`.
6+
7+
Parameters
8+
----------
9+
c: number
10+
Shape parameter.
11+
12+
Returns
13+
-------
14+
out: number
15+
Mode.
16+
17+
Examples
18+
--------
19+
> var v = {{alias}}( 0.1 )
20+
0.0
21+
> v = {{alias}}( 0.5 )
22+
0.0
23+
> v = {{alias}}( 10.0 )
24+
0.0
25+
26+
See Also
27+
--------
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Returns the mode of a Bradford distribution.
23+
*
24+
* ## Notes
25+
*
26+
* - If `c <= 0`, the function returns `NaN`.
27+
*
28+
* @param c - shape parameter
29+
* @returns mode
30+
*
31+
* @example
32+
* var v = mode( 0.1 );
33+
* // returns 0.0
34+
*
35+
* @example
36+
* var v = mode( 0.5 );
37+
* // returns 0.0
38+
*
39+
* @example
40+
* var v = mode( 10.0 );
41+
* // returns 0.0
42+
*
43+
* @example
44+
* var v = mode( 0.0 );
45+
* // returns NaN
46+
*
47+
* @example
48+
* var v = mode( -1.0 );
49+
* // returns NaN
50+
*
51+
* @example
52+
* var v = mode( NaN );
53+
* // returns NaN
54+
*/
55+
declare function mode( c: number ): number;
56+
57+
58+
// EXPORTS //
59+
60+
export = mode;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
import mode = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
mode( 0.3 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a number...
30+
{
31+
mode( true ); // $ExpectError
32+
mode( false ); // $ExpectError
33+
mode( null ); // $ExpectError
34+
mode( undefined ); // $ExpectError
35+
mode( '5' ); // $ExpectError
36+
mode( [] ); // $ExpectError
37+
mode( {} ); // $ExpectError
38+
mode( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided insufficient arguments...
42+
{
43+
mode(); // $ExpectError
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
var uniform = require( '@stdlib/random/array/uniform' );
22+
var mode = require( './../lib' );
23+
24+
var c = uniform( 10, 0.1, 10.0 );
25+
26+
var v;
27+
var i;
28+
for ( i = 0; i < c.length; i++ ) {
29+
v = mode( c[ i ] );
30+
console.log( 'c: %d, mode(X;c): %d', c[ i ].toFixed( 4 ), v.toFixed( 4 ) );
31+
}

0 commit comments

Comments
 (0)