@stdlib/stats-base-dists-binomial-mode 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/NOTICE CHANGED
@@ -1 +1 @@
1
- Copyright (c) 2016-2024 The Stdlib Authors.
1
+ Copyright (c) 2016-2026 The Stdlib Authors.
package/README.md CHANGED
@@ -139,20 +139,112 @@ v = mode( 20, 1.5 );
139
139
  <!-- eslint no-undef: "error" -->
140
140
 
141
141
  ```javascript
142
- var randu = require( '@stdlib/random-base-randu' );
143
- var round = require( '@stdlib/math-base-special-round' );
142
+ var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
143
+ var uniform = require( '@stdlib/random-array-uniform' );
144
+ var logEachMap = require( '@stdlib/console-log-each-map' );
144
145
  var mode = require( '@stdlib/stats-base-dists-binomial-mode' );
145
146
 
146
- var v;
147
- var i;
148
- var n;
149
- var p;
147
+ var opts = {
148
+ 'dtype': 'float64'
149
+ };
150
+ var n = discreteUniform( 10, 0, 100, opts );
151
+ var p = uniform( 10, 0.0, 1.0, opts );
150
152
 
151
- for ( i = 0; i < 10; i++ ) {
152
- n = round( randu() * 100.0 );
153
- p = randu();
154
- v = mode( n, p );
155
- console.log( 'n: %d, p: %d, mode(X;n,p): %d', n, p.toFixed( 4 ), v.toFixed( 4 ) );
153
+ logEachMap( 'n: %0.4f, p: %0.4f, mode(X;n,p): %0.4f', n, p, mode );
154
+ ```
155
+
156
+ </section>
157
+
158
+ <!-- /.examples -->
159
+
160
+ <!-- C interface documentation. -->
161
+
162
+ * * *
163
+
164
+ <section class="c">
165
+
166
+ ## C APIs
167
+
168
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
169
+
170
+ <section class="intro">
171
+
172
+ </section>
173
+
174
+ <!-- /.intro -->
175
+
176
+ <!-- C usage documentation. -->
177
+
178
+ <section class="usage">
179
+
180
+ ### Usage
181
+
182
+ ```c
183
+ #include "stdlib/stats/base/dists/binomial/mode.h"
184
+ ```
185
+
186
+ #### stdlib_base_dists_binomial_mode( n, p )
187
+
188
+ Returns the [mode][mode] of a [binomial][binomial-distribution] distribution with number of trials `n` and success probability `p`.
189
+
190
+ ```c
191
+ double out = stdlib_base_dists_binomial_mode( 100, 0.1 );
192
+ // returns 10
193
+ ```
194
+
195
+ The function accepts the following arguments:
196
+
197
+ - **n**: `[in] int32_t` number of trials.
198
+ - **p**: `[in] double` success probability.
199
+
200
+ ```c
201
+ double stdlib_base_dists_binomial_mode( const int32_t n, const double p );
202
+ ```
203
+
204
+ </section>
205
+
206
+ <!-- /.usage -->
207
+
208
+ <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
209
+
210
+ <section class="notes">
211
+
212
+ </section>
213
+
214
+ <!-- /.notes -->
215
+
216
+ <!-- C API usage examples. -->
217
+
218
+ <section class="examples">
219
+
220
+ ### Examples
221
+
222
+ ```c
223
+ #include "stdlib/stats/base/dists/binomial/mode.h"
224
+ #include "stdlib/math/base/special/ceil.h"
225
+ #include <stdlib.h>
226
+ #include <stdint.h>
227
+ #include <stdio.h>
228
+
229
+ static double random_uniform( const double min, const double max ) {
230
+ double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
231
+ return min + ( v * (max - min) );
232
+ }
233
+
234
+ int main( void ) {
235
+ int32_t n;
236
+ double p;
237
+ double y;
238
+ int i;
239
+
240
+ for ( i = 0; i < 25; i++ ) {
241
+ n = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
242
+ p = random_uniform( 0.0, 1.0 );
243
+ y = stdlib_base_dists_binomial_mode( n, p );
244
+ printf( "n: %d, p: %lf, mode(X;n,p): %lf\n", n, p, y );
245
+ }
246
+
247
+ return 0;
156
248
  }
157
249
  ```
158
250
 
@@ -160,6 +252,10 @@ for ( i = 0; i < 10; i++ ) {
160
252
 
161
253
  <!-- /.examples -->
162
254
 
255
+ </section>
256
+
257
+ <!-- /.c -->
258
+
163
259
  <!-- 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. -->
164
260
 
165
261
  <section class="references">
@@ -202,7 +298,7 @@ See [LICENSE][stdlib-license].
202
298
 
203
299
  ## Copyright
204
300
 
205
- Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
301
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
206
302
 
207
303
  </section>
208
304
 
@@ -215,8 +311,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
215
311
  [npm-image]: http://img.shields.io/npm/v/@stdlib/stats-base-dists-binomial-mode.svg
216
312
  [npm-url]: https://npmjs.org/package/@stdlib/stats-base-dists-binomial-mode
217
313
 
218
- [test-image]: https://github.com/stdlib-js/stats-base-dists-binomial-mode/actions/workflows/test.yml/badge.svg?branch=v0.2.2
219
- [test-url]: https://github.com/stdlib-js/stats-base-dists-binomial-mode/actions/workflows/test.yml?query=branch:v0.2.2
314
+ [test-image]: https://github.com/stdlib-js/stats-base-dists-binomial-mode/actions/workflows/test.yml/badge.svg?branch=v0.3.0
315
+ [test-url]: https://github.com/stdlib-js/stats-base-dists-binomial-mode/actions/workflows/test.yml?query=branch:v0.3.0
220
316
 
221
317
  [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/stats-base-dists-binomial-mode/main.svg
222
318
  [coverage-url]: https://codecov.io/github/stdlib-js/stats-base-dists-binomial-mode?branch=main
@@ -228,8 +324,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
228
324
 
229
325
  -->
230
326
 
231
- [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
232
- [chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
327
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
328
+ [chat-url]: https://stdlib.zulipchat.com
233
329
 
234
330
  [stdlib]: https://github.com/stdlib-js/stdlib
235
331
 
@@ -0,0 +1,40 @@
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
+ #ifndef STDLIB_STATS_BASE_DISTS_BINOMIAL_MODE_H
20
+ #define STDLIB_STATS_BASE_DISTS_BINOMIAL_MODE_H
21
+
22
+ #include <stdint.h>
23
+
24
+ /*
25
+ * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
26
+ */
27
+ #ifdef __cplusplus
28
+ extern "C" {
29
+ #endif
30
+
31
+ /**
32
+ * Returns the mode of a binomial distribution with number of trials `n` and success probability `p`.
33
+ */
34
+ double stdlib_base_dists_binomial_mode( const int32_t n, const double p );
35
+
36
+ #ifdef __cplusplus
37
+ }
38
+ #endif
39
+
40
+ #endif // !STDLIB_STATS_BASE_DISTS_BINOMIAL_MODE_H
package/lib/native.js ADDED
@@ -0,0 +1,59 @@
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 addon = require( './../src/addon.node' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Returns the mode of a binomial distribution.
30
+ *
31
+ * @private
32
+ * @param {NonNegativeInteger} n - number of trials
33
+ * @param {Probability} p - success probability
34
+ * @returns {NonNegativeInteger} mode
35
+ *
36
+ * @example
37
+ * var v = mode( 100, 0.1 );
38
+ * // returns 10
39
+ *
40
+ * @example
41
+ * var v = mode( 20, 0.5 );
42
+ * // returns 10
43
+ *
44
+ * @example
45
+ * var v = mode( 20, 1.1 );
46
+ * // returns NaN
47
+ *
48
+ * @example
49
+ * var v = mode( 20, NaN );
50
+ * // returns NaN
51
+ */
52
+ function mode( n, p ) {
53
+ return addon( n, p );
54
+ }
55
+
56
+
57
+ // EXPORTS //
58
+
59
+ module.exports = mode;
package/manifest.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "options": {
3
+ "task": "build",
4
+ "wasm": false
5
+ },
6
+ "fields": [
7
+ {
8
+ "field": "src",
9
+ "resolve": true,
10
+ "relative": true
11
+ },
12
+ {
13
+ "field": "include",
14
+ "resolve": true,
15
+ "relative": true
16
+ },
17
+ {
18
+ "field": "libraries",
19
+ "resolve": false,
20
+ "relative": false
21
+ },
22
+ {
23
+ "field": "libpath",
24
+ "resolve": true,
25
+ "relative": false
26
+ }
27
+ ],
28
+ "confs": [
29
+ {
30
+ "task": "build",
31
+ "wasm": false,
32
+ "src": [
33
+ "./src/main.c"
34
+ ],
35
+ "include": [
36
+ "./include"
37
+ ],
38
+ "libraries": [],
39
+ "libpath": [],
40
+ "dependencies": [
41
+ "@stdlib/math-base-napi-binary",
42
+ "@stdlib/math-base-special-floor"
43
+ ]
44
+ },
45
+ {
46
+ "task": "benchmark",
47
+ "wasm": false,
48
+ "src": [
49
+ "./src/main.c"
50
+ ],
51
+ "include": [
52
+ "./include"
53
+ ],
54
+ "libraries": [],
55
+ "libpath": [],
56
+ "dependencies": [
57
+ "@stdlib/math-base-special-ceil",
58
+ "@stdlib/math-base-special-floor"
59
+ ]
60
+ },
61
+ {
62
+ "task": "examples",
63
+ "wasm": false,
64
+ "src": [
65
+ "./src/main.c"
66
+ ],
67
+ "include": [
68
+ "./include"
69
+ ],
70
+ "libraries": [],
71
+ "libpath": [],
72
+ "dependencies": [
73
+ "@stdlib/math-base-special-ceil",
74
+ "@stdlib/math-base-special-floor"
75
+ ]
76
+ }
77
+ ]
78
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stdlib/stats-base-dists-binomial-mode",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Binomial distribution mode.",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
@@ -14,9 +14,12 @@
14
14
  }
15
15
  ],
16
16
  "main": "./lib",
17
+ "gypfile": false,
17
18
  "directories": {
18
19
  "doc": "./docs",
20
+ "include": "./include",
19
21
  "lib": "./lib",
22
+ "src": "./src",
20
23
  "dist": "./dist"
21
24
  },
22
25
  "types": "./docs/types",
@@ -32,8 +35,10 @@
32
35
  "dependencies": {
33
36
  "@stdlib/constants-float64-pinf": "^0.2.2",
34
37
  "@stdlib/math-base-assert-is-nan": "^0.2.2",
35
- "@stdlib/math-base-assert-is-nonnegative-integer": "^0.2.1",
36
- "@stdlib/math-base-special-floor": "^0.2.3"
38
+ "@stdlib/math-base-assert-is-nonnegative-integer": "^0.2.2",
39
+ "@stdlib/math-base-napi-binary": "^0.3.1",
40
+ "@stdlib/math-base-special-floor": "^0.2.3",
41
+ "@stdlib/utils-library-manifest": "^0.2.3"
37
42
  },
38
43
  "devDependencies": {},
39
44
  "engines": {
package/src/addon.c ADDED
@@ -0,0 +1,22 @@
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
+ #include "stdlib/stats/base/dists/binomial/mode.h"
20
+ #include "stdlib/math/base/napi/binary.h"
21
+
22
+ STDLIB_MATH_BASE_NAPI_MODULE_ID_D( stdlib_base_dists_binomial_mode );
package/src/main.c ADDED
@@ -0,0 +1,42 @@
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
+ #include "stdlib/stats/base/dists/binomial/mode.h"
20
+ #include "stdlib/math/base/special/floor.h"
21
+
22
+ /**
23
+ * Returns the mode of a binomial distribution.
24
+ *
25
+ * @param n number of trials
26
+ * @param p success probability
27
+ * @return mode
28
+ *
29
+ * @example
30
+ * double y = stdlib_base_dists_binomial_mode( 100, 0.1 );
31
+ * // returns 10.0
32
+ */
33
+ double stdlib_base_dists_binomial_mode( const int32_t n, const double p ) {
34
+ if (
35
+ n < 0 ||
36
+ p < 0.0 ||
37
+ p > 1.0
38
+ ) {
39
+ return 0.0 / 0.0;
40
+ }
41
+ return stdlib_base_floor( ( n + 1.0 ) * p );
42
+ }