@stdlib/stats-base-dists-binomial-stdev 0.2.1 → 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,110 @@ v = stdev( 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 stdev = require( '@stdlib/stats-base-dists-binomial-stdev' );
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 = stdev( n, p );
155
- console.log( 'n: %d, p: %d, SD(X;n,p): %d', n, p.toFixed( 4 ), v.toFixed( 4 ) );
153
+ logEachMap( 'n: %0.4f, p: %0.4f, SD(X;n,p): %0.4f', n, p, stdev );
154
+ ```
155
+
156
+ </section>
157
+
158
+ <!-- /.examples -->
159
+
160
+ <!-- C interface documentation. -->
161
+
162
+ <section class="c">
163
+
164
+ ## C APIs
165
+
166
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
167
+
168
+ <section class="intro">
169
+
170
+ </section>
171
+
172
+ <!-- /.intro -->
173
+
174
+ <!-- C usage documentation. -->
175
+
176
+ <section class="usage">
177
+
178
+ ### Usage
179
+
180
+ ```c
181
+ #include "stdlib/stats/base/dists/binomial/stdev.h"
182
+ ```
183
+
184
+ #### stdlib_base_dists_binomial_stdev( n, p )
185
+
186
+ Returns the [standard deviation][stdev] of a [binomial][binomial-distribution] distribution with number of trials `n` and success probability `p`.
187
+
188
+ ```c
189
+ double out = stdlib_base_dists_binomial_stdev( 100, 0.1 );
190
+ // returns 3.0
191
+ ```
192
+
193
+ The function accepts the following arguments:
194
+
195
+ - **n**: `[in] int32_t` number of trials.
196
+ - **p**: `[in] double` success probability.
197
+
198
+ ```c
199
+ double stdlib_base_dists_binomial_stdev( const int32_t n, const double p );
200
+ ```
201
+
202
+ </section>
203
+
204
+ <!-- /.usage -->
205
+
206
+ <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
207
+
208
+ <section class="notes">
209
+
210
+ </section>
211
+
212
+ <!-- /.notes -->
213
+
214
+ <!-- C API usage examples. -->
215
+
216
+ <section class="examples">
217
+
218
+ ### Examples
219
+
220
+ ```c
221
+ #include "stdlib/stats/base/dists/binomial/stdev.h"
222
+ #include "stdlib/math/base/special/ceil.h"
223
+ #include <stdlib.h>
224
+ #include <stdint.h>
225
+ #include <stdio.h>
226
+
227
+ static double random_uniform( const double min, const double max ) {
228
+ double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
229
+ return min + ( v * (max - min) );
230
+ }
231
+
232
+ int main( void ) {
233
+ int32_t n;
234
+ double p;
235
+ double y;
236
+ int i;
237
+
238
+ for ( i = 0; i < 25; i++ ) {
239
+ n = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
240
+ p = random_uniform( 0.0, 1.0 );
241
+ y = stdlib_base_dists_binomial_stdev( n, p );
242
+ printf( "n: %d, p: %lf, SD(X;n,p): %lf\n", n, p, y );
243
+ }
244
+
245
+ return 0;
156
246
  }
157
247
  ```
158
248
 
@@ -160,6 +250,10 @@ for ( i = 0; i < 10; i++ ) {
160
250
 
161
251
  <!-- /.examples -->
162
252
 
253
+ </section>
254
+
255
+ <!-- /.c -->
256
+
163
257
  <!-- 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
258
 
165
259
  <section class="references">
@@ -202,7 +296,7 @@ See [LICENSE][stdlib-license].
202
296
 
203
297
  ## Copyright
204
298
 
205
- Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
299
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
206
300
 
207
301
  </section>
208
302
 
@@ -215,8 +309,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
215
309
  [npm-image]: http://img.shields.io/npm/v/@stdlib/stats-base-dists-binomial-stdev.svg
216
310
  [npm-url]: https://npmjs.org/package/@stdlib/stats-base-dists-binomial-stdev
217
311
 
218
- [test-image]: https://github.com/stdlib-js/stats-base-dists-binomial-stdev/actions/workflows/test.yml/badge.svg?branch=v0.2.1
219
- [test-url]: https://github.com/stdlib-js/stats-base-dists-binomial-stdev/actions/workflows/test.yml?query=branch:v0.2.1
312
+ [test-image]: https://github.com/stdlib-js/stats-base-dists-binomial-stdev/actions/workflows/test.yml/badge.svg?branch=v0.3.0
313
+ [test-url]: https://github.com/stdlib-js/stats-base-dists-binomial-stdev/actions/workflows/test.yml?query=branch:v0.3.0
220
314
 
221
315
  [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/stats-base-dists-binomial-stdev/main.svg
222
316
  [coverage-url]: https://codecov.io/github/stdlib-js/stats-base-dists-binomial-stdev?branch=main
@@ -228,8 +322,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
228
322
 
229
323
  -->
230
324
 
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
325
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
326
+ [chat-url]: https://stdlib.zulipchat.com
233
327
 
234
328
  [stdlib]: https://github.com/stdlib-js/stdlib
235
329
 
@@ -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_STDEV_H
20
+ #define STDLIB_STATS_BASE_DISTS_BINOMIAL_STDEV_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 standard deviation of a binomial distribution with number of trials `n` and success probability `p`.
33
+ */
34
+ double stdlib_base_dists_binomial_stdev( const int32_t n, const double p );
35
+
36
+ #ifdef __cplusplus
37
+ }
38
+ #endif
39
+
40
+ #endif // !STDLIB_STATS_BASE_DISTS_BINOMIAL_STDEV_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 standard deviation of a binomial distribution with number of trials `n` and success probability `p`.
30
+ *
31
+ * @private
32
+ * @param {NonNegativeInteger} n - number of trials
33
+ * @param {Probability} p - success probability
34
+ * @returns {PositiveNumber} standard deviation
35
+ *
36
+ * @example
37
+ * var v = stdev( 100, 0.1 );
38
+ * // returns 3.0
39
+ *
40
+ * @example
41
+ * var v = stdev( 20, 0.5 );
42
+ * // returns ~2.236
43
+ *
44
+ * @example
45
+ * var v = stdev( 20, 1.1 );
46
+ * // returns NaN
47
+ *
48
+ * @example
49
+ * var v = stdev( 20, NaN );
50
+ * // returns NaN
51
+ */
52
+ function stdev( n, p ) {
53
+ return addon( n, p );
54
+ }
55
+
56
+
57
+ // EXPORTS //
58
+
59
+ module.exports = stdev;
package/manifest.json ADDED
@@ -0,0 +1,81 @@
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-assert-is-nan",
43
+ "@stdlib/math-base-special-sqrt"
44
+ ]
45
+ },
46
+ {
47
+ "task": "benchmark",
48
+ "wasm": false,
49
+ "src": [
50
+ "./src/main.c"
51
+ ],
52
+ "include": [
53
+ "./include"
54
+ ],
55
+ "libraries": [],
56
+ "libpath": [],
57
+ "dependencies": [
58
+ "@stdlib/math-base-assert-is-nan",
59
+ "@stdlib/math-base-special-sqrt",
60
+ "@stdlib/math-base-special-ceil"
61
+ ]
62
+ },
63
+ {
64
+ "task": "examples",
65
+ "wasm": false,
66
+ "src": [
67
+ "./src/main.c"
68
+ ],
69
+ "include": [
70
+ "./include"
71
+ ],
72
+ "libraries": [],
73
+ "libpath": [],
74
+ "dependencies": [
75
+ "@stdlib/math-base-assert-is-nan",
76
+ "@stdlib/math-base-special-sqrt",
77
+ "@stdlib/math-base-special-ceil"
78
+ ]
79
+ }
80
+ ]
81
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stdlib/stats-base-dists-binomial-stdev",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Binomial distribution standard deviation.",
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",
@@ -30,10 +33,12 @@
30
33
  "url": "https://github.com/stdlib-js/stdlib/issues"
31
34
  },
32
35
  "dependencies": {
33
- "@stdlib/constants-float64-pinf": "^0.2.1",
34
- "@stdlib/math-base-assert-is-nan": "^0.2.1",
35
- "@stdlib/math-base-assert-is-nonnegative-integer": "^0.1.2",
36
- "@stdlib/math-base-special-sqrt": "^0.2.1"
36
+ "@stdlib/constants-float64-pinf": "^0.2.2",
37
+ "@stdlib/math-base-assert-is-nan": "^0.2.2",
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-sqrt": "^0.2.2",
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/stdev.h"
20
+ #include "stdlib/math/base/napi/binary.h"
21
+
22
+ STDLIB_MATH_BASE_NAPI_MODULE_ID_D( stdlib_base_dists_binomial_stdev );
package/src/main.c ADDED
@@ -0,0 +1,45 @@
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/stdev.h"
20
+ #include "stdlib/math/base/assert/is_nan.h"
21
+ #include "stdlib/math/base/special/sqrt.h"
22
+ #include <stdint.h>
23
+
24
+ /**
25
+ * Returns the standard deviation of a binomial distribution.
26
+ *
27
+ * @param n number of trials
28
+ * @param p success probability
29
+ * @return standard deviation
30
+ *
31
+ * @example
32
+ * double y = stdlib_base_dists_binomial_stdev( 100, 0.1 );
33
+ * // returns 3.0
34
+ */
35
+ double stdlib_base_dists_binomial_stdev( const int32_t n, const double p ) {
36
+ if (
37
+ stdlib_base_is_nan( p ) ||
38
+ n < 0 ||
39
+ p < 0.0 ||
40
+ p > 1.0
41
+ ) {
42
+ return 0.0 / 0.0;
43
+ }
44
+ return stdlib_base_sqrt( n * p * (1.0 - p) );
45
+ }