Skip to content

Commit ca035dd

Browse files
feat: added new implementation and other minor updates
1 parent a5c1993 commit ca035dd

File tree

16 files changed

+67
-66
lines changed

16 files changed

+67
-66
lines changed

lib/node_modules/@stdlib/stats/base/dists/binomial/variance/README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,20 @@ for ( i = 0; i < 10; i++ ) {
167167

168168
#### stdlib_base_dists_binomial_variance( n, p )
169169

170-
Returns the variance of a binomial distribution.
170+
Returns the [variance][variance] of a [binomial][binomial-distribution] distribution with number of trials `n` and success probability `p`.
171171

172172
```c
173-
double out = stdlib_base_dists_binomial_variance( 10, 0.5 );
174-
// returns 2.5
173+
double out = stdlib_base_dists_binomial_variance( 100, 0.1 );
174+
// returns 0.9
175175
```
176176

177177
The function accepts the following arguments:
178178

179-
- **n**: `[in] int` number of trials.
179+
- **n**: `[in] int32_t` number of trials.
180180
- **p**: `[in] double` success probability.
181181

182182
```c
183-
double stdlib_base_dists_binomial_variance( const int n, const double p );
183+
double stdlib_base_dists_binomial_variance( const int32_t n, const double p );
184184
```
185185
186186
</section>
@@ -203,26 +203,30 @@ double stdlib_base_dists_binomial_variance( const int n, const double p );
203203
204204
```c
205205
#include "stdlib/stats/base/dists/binomial/variance.h"
206+
#include "stdlib/math/base/special/ceil.h"
206207
#include <stdlib.h>
208+
#include <stdint.h>
207209
#include <stdio.h>
208210
209211
static double random_uniform( const double min, const double max ) {
210212
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
211-
return min + ( v * ( max - min ) );
213+
return min + ( v * (max - min) );
212214
}
213215
214216
int main( void ) {
215-
int n;
217+
int32_t n;
216218
double p;
217219
double y;
218220
int i;
219221
220222
for ( i = 0; i < 25; i++ ) {
221-
n = (int)random_uniform( 1, 100 );
222-
p = random_uniform( 0, 1 );
223+
n = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
224+
p = random_uniform( 0.0, 1.0 );
223225
y = stdlib_base_dists_binomial_variance( n, p );
224-
printf( "n: %d, p: %lf, Var(X;n,p): %lf", n, p, y );
226+
printf( "n: %d, p: %lf, Var(X;n,p): %lf\n", n, p, y );
225227
}
228+
229+
return 0;
226230
}
227231
```
228232

lib/node_modules/@stdlib/stats/base/dists/binomial/variance/benchmark/benchmark.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24+
var ceil = require( '@stdlib/math/base/special/ceil' );
25+
var Int32Array = require( '@stdlib/array/int32' );
2426
var Float64Array = require( '@stdlib/array/float64' );
2527
var randu = require( '@stdlib/random/base/randu' );
26-
var floor = require( '@stdlib/math/base/special/floor' );
2728
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2829
var pkg = require( './../package.json' ).name;
2930
var variance = require( './../lib' );
@@ -39,16 +40,16 @@ bench( pkg, function benchmark( b ) {
3940
var i;
4041

4142
len = 100;
42-
n = new Float64Array( len );
43+
n = new Int32Array( len );
4344
p = new Float64Array( len );
4445
for ( i = 0; i < len; i++ ) {
45-
n[ i ] = floor( ( randu() * 100.0 ) ) + 1;
46-
p[ i ] = ( randu() * 0.8 ) + 0.1;
46+
n[ i ] = ceil( randu() * 100.0 );
47+
p[ i ] = randu();
4748
}
4849

4950
b.tic();
5051
for ( i = 0; i < b.iterations; i++ ) {
51-
y = variance( n[ i % len ], p[ i % len ] );
52+
y = variance( n[ i%len ], p[ i%len ] );
5253
if ( isnan( y ) ) {
5354
b.fail( 'should not return NaN' );
5455
}

lib/node_modules/@stdlib/stats/base/dists/binomial/variance/benchmark/benchmark.native.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -22,9 +22,10 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25+
var Int32Array = require( '@stdlib/array/int32' );
2526
var Float64Array = require( '@stdlib/array/float64' );
2627
var tryRequire = require( '@stdlib/utils/try-require' );
27-
var floor = require( '@stdlib/math/base/special/floor' );
28+
var ceil = require( '@stdlib/math/base/special/ceil' );
2829
var randu = require( '@stdlib/random/base/randu' );
2930
var isnan = require( '@stdlib/math/base/assert/is-nan' );
3031
var pkg = require( './../package.json' ).name;
@@ -48,11 +49,11 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4849
var i;
4950

5051
len = 100;
51-
n = new Float64Array( len );
52+
n = new Int32Array( len );
5253
p = new Float64Array( len );
5354
for ( i = 0; i < len; i++ ) {
54-
n[ i ] = floor( ( randu() * 100.0 ) ) + 1;
55-
p[ i ] = ( randu() * 0.8 ) + 0.1;
55+
n[ i ] = ceil( randu() * 100.0 );
56+
p[ i ] = randu();
5657
}
5758

5859
b.tic();

lib/node_modules/@stdlib/stats/base/dists/binomial/variance/benchmark/c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2024 The Stdlib Authors.
4+
# Copyright (c) 2025 The Stdlib Authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/stats/base/dists/binomial/variance/benchmark/c/benchmark.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -17,7 +17,9 @@
1717
*/
1818

1919
#include "stdlib/stats/base/dists/binomial/variance.h"
20+
#include "stdlib/math/base/special/ceil.h"
2021
#include <stdlib.h>
22+
#include <stdint.h>
2123
#include <stdio.h>
2224
#include <math.h>
2325
#include <time.h>
@@ -93,15 +95,15 @@ static double random_uniform( const double min, const double max ) {
9395
*/
9496
static double benchmark( void ) {
9597
double elapsed;
96-
double n[ 100 ];
98+
int32_t n[ 100 ];
9799
double p[ 100 ];
98100
double y;
99101
double t;
100102
int i;
101103

102104
for ( i = 0; i < 100; i++ ) {
103-
n[ i ] = random_uniform( 1, 100 );
104-
p[ i ] = random_uniform( 0.1, 0.9 );
105+
n[ i ] = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
106+
p[ i ] = random_uniform( 0.0, 1.0 );
105107
}
106108

107109
t = tic();

lib/node_modules/@stdlib/stats/base/dists/binomial/variance/binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @license Apache-2.0
22
#
3-
# Copyright (c) 2024 The Stdlib Authors.
3+
# Copyright (c) 2025 The Stdlib Authors.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/stats/base/dists/binomial/variance/examples/c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2024 The Stdlib Authors.
4+
# Copyright (c) 2025 The Stdlib Authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/stats/base/dists/binomial/variance/examples/c/example.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -17,7 +17,9 @@
1717
*/
1818

1919
#include "stdlib/stats/base/dists/binomial/variance.h"
20+
#include "stdlib/math/base/special/ceil.h"
2021
#include <stdlib.h>
22+
#include <stdint.h>
2123
#include <stdio.h>
2224

2325
static double random_uniform( const double min, const double max ) {
@@ -26,16 +28,16 @@ static double random_uniform( const double min, const double max ) {
2628
}
2729

2830
int main( void ) {
29-
double n;
31+
int32_t n;
3032
double p;
3133
double y;
3234
int i;
3335

3436
for ( i = 0; i < 25; i++ ) {
35-
n = random_uniform( 1, 100 );
36-
p = random_uniform( 0.1, 0.9 );
37+
n = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
38+
p = random_uniform( 0.0, 1.0 );
3739
y = stdlib_base_dists_binomial_variance( n, p );
38-
printf( "n: %lf, p: %lf, Var(X;n,p): %lf\n", n, p, y );
40+
printf( "n: %d, p: %lf, Var(X;n,p): %lf\n", n, p, y );
3941
}
4042

4143
return 0;

lib/node_modules/@stdlib/stats/base/dists/binomial/variance/include.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @license Apache-2.0
22
#
3-
# Copyright (c) 2024 The Stdlib Authors.
3+
# Copyright (c) 2025 The Stdlib Authors.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/stats/base/dists/binomial/variance/include/stdlib/stats/base/dists/binomial/variance.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
1919
#ifndef STDLIB_STATS_BASE_DISTS_BINOMIAL_VARIANCE_H
2020
#define STDLIB_STATS_BASE_DISTS_BINOMIAL_VARIANCE_H
2121

22+
#include <stdint.h>
23+
2224
/*
2325
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
2426
*/
@@ -27,9 +29,9 @@ extern "C" {
2729
#endif
2830

2931
/**
30-
* Returns the variance of a binomial distribution.
32+
* Returns the variance of a binomial distribution with number of trials `n` and success probability `p`.
3133
*/
32-
double stdlib_base_dists_binomial_variance( const double n, const double p );
34+
double stdlib_base_dists_binomial_variance( const int32_t n, const double p );
3335

3436
#ifdef __cplusplus
3537
}

0 commit comments

Comments
 (0)