Skip to content

Commit 042a577

Browse files
Jaysukh-409kgrytestdlib-bot
authored andcommitted
feat: add stats/base/dists/planck/mode
PR-URL: stdlib-js#4350 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 00c76f6 commit 042a577

File tree

10 files changed

+580
-0
lines changed

10 files changed

+580
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
> Planck (discrete exponential) 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 Planck random variable with shape parameter `λ` is
30+
31+
<!-- <equation class="equation" label="eq:planck_mode" align="center" raw="\operatorname{mode}\left( X \right) = 0" alt="Mode for a Planck distribution."> -->
32+
33+
```math
34+
\mathop{\mathrm{mode}}\left( X \right) = 0
35+
```
36+
37+
<!-- </equation> -->
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<!-- Package usage documentation. -->
44+
45+
<section class="usage">
46+
47+
## Usage
48+
49+
```javascript
50+
var mode = require( '@stdlib/stats/base/dists/planck/mode' );
51+
```
52+
53+
#### mode( lambda )
54+
55+
Returns the [mode][mode] of a Planck distribution with shape parameter `lambda`.
56+
57+
```javascript
58+
var v = mode( 0.1 );
59+
// returns 0
60+
61+
v = mode( 1.5 );
62+
// returns 0
63+
```
64+
65+
If provided a shape parameter `lambda` is nonpositive or `NaN`, the function returns `NaN`.
66+
67+
```javascript
68+
var v = mode( NaN );
69+
// returns NaN
70+
71+
v = mode( -1.5 );
72+
// returns NaN
73+
```
74+
75+
</section>
76+
77+
<!-- /.usage -->
78+
79+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
80+
81+
<section class="notes">
82+
83+
</section>
84+
85+
<!-- /.notes -->
86+
87+
<!-- Package usage examples. -->
88+
89+
<section class="examples">
90+
91+
## Examples
92+
93+
<!-- eslint no-undef: "error" -->
94+
95+
```javascript
96+
var uniform = require( '@stdlib/random/array/uniform' );
97+
var mode = require( '@stdlib/stats/base/dists/planck/mode' );
98+
99+
var lambda = uniform( 10, 0.1, 5.0 );
100+
101+
var v;
102+
var i;
103+
for ( i = 0; i < lambda.length; i++ ) {
104+
v = mode( lambda[ i ] );
105+
console.log( 'λ: %d, mode(X;λ): %d', lambda[ i ].toFixed( 4 ), v.toFixed( 4 ) );
106+
}
107+
```
108+
109+
</section>
110+
111+
<!-- /.examples -->
112+
113+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
114+
115+
<section class="references">
116+
117+
</section>
118+
119+
<!-- /.references -->
120+
121+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
122+
123+
<section class="related">
124+
125+
</section>
126+
127+
<!-- /.related -->
128+
129+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
130+
131+
<section class="links">
132+
133+
[mode]: https://en.wikipedia.org/wiki/Mode_%28statistics%29
134+
135+
</section>
136+
137+
<!-- /.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 lambda;
34+
var y;
35+
var i;
36+
37+
lambda = uniform( 100, 0.1, 10.0 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = mode( lambda[ i % lambda.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,26 @@
1+
2+
{{alias}}( λ )
3+
Returns the mode of a Planck distribution with shape parameter `λ`.
4+
5+
If `lambda <= 0`, the function returns `NaN`.
6+
7+
Parameters
8+
----------
9+
λ: number
10+
Shape parameter.
11+
12+
Returns
13+
-------
14+
out: integer
15+
Mode.
16+
17+
Examples
18+
--------
19+
> var v = {{alias}}( 0.1 )
20+
0
21+
> v = {{alias}}( 1.5 )
22+
0
23+
24+
See Also
25+
--------
26+
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Returns the mode of a Planck distribution.
23+
*
24+
* ## Notes
25+
*
26+
* - If `lambda <= 0`, the function returns `NaN`.
27+
*
28+
* @param lambda - shape parameter
29+
* @returns mode
30+
*
31+
* @example
32+
* var v = mode( 0.1 );
33+
* // returns 0
34+
*
35+
* @example
36+
* var v = mode( 1.5 );
37+
* // returns 0
38+
*
39+
* @example
40+
* var v = mode( -1.1 );
41+
* // returns NaN
42+
*
43+
* @example
44+
* var v = mode( NaN );
45+
* // returns NaN
46+
*/
47+
declare function mode( lambda: number ): number;
48+
49+
50+
// EXPORTS //
51+
52+
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 lambda = uniform( 10, 0.1, 5.0 );
25+
26+
var v;
27+
var i;
28+
for ( i = 0; i < lambda.length; i++ ) {
29+
v = mode( lambda[ i ] );
30+
console.log( 'λ: %d, mode(X;λ): %d', lambda[ i ].toFixed( 4 ), v.toFixed( 4 ) );
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
/**
22+
* Planck distribution mode.
23+
*
24+
* @module @stdlib/stats/base/dists/planck/mode
25+
*
26+
* @example
27+
* var mode = require( '@stdlib/stats/base/dists/planck/mode' );
28+
*
29+
* var v = mode( 0.1 );
30+
* // returns 0
31+
*
32+
* v = mode( 1.5 );
33+
* // returns 0
34+
*/
35+
36+
// MODULES //
37+
38+
var mode = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = mode;

0 commit comments

Comments
 (0)