Skip to content

Commit 516be28

Browse files
AadishJPlaneshifterstdlib-bot
authored
feat: add C implementation for stats/base/dists/negative-binomial/variance
PR-URL: #3906 Closes: #3770 Co-authored-by: Philipp Burckhardt <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]>
1 parent b8b6f36 commit 516be28

File tree

17 files changed

+1273
-2
lines changed

17 files changed

+1273
-2
lines changed

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

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,110 @@ for ( i = 0; i < 10; i++ ) {
129129
r = randu() * 100;
130130
p = randu();
131131
v = variance( r, p );
132-
console.log( 'r: %d, p: %d, Var(X;r,p): %d', r, p.toFixed( 4 ), v.toFixed( 4 ) );
132+
console.log( 'r: %d, p: %d, Var(X;r,p): %d', r.toFixed( 4 ), p.toFixed( 4 ), v.toFixed( 4 ) );
133133
}
134134
```
135135

136136
</section>
137137

138138
<!-- /.examples -->
139139

140+
<!-- C interface documentation. -->
141+
142+
* * *
143+
144+
<section class="c">
145+
146+
## C APIs
147+
148+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
149+
150+
<section class="intro">
151+
152+
</section>
153+
154+
<!-- /.intro -->
155+
156+
<!-- C usage documentation. -->
157+
158+
<section class="usage">
159+
160+
### Usage
161+
162+
```c
163+
#include "stdlib/stats/base/dists/negative-binomial/variance.h"
164+
```
165+
166+
#### stdlib_base_dists_negative_binomial_variance( r, p )
167+
168+
Returns the variance of a negative binomial distribution with parameters `r` (number of successes until experiment is stopped) and `p` (success probability).
169+
170+
```c
171+
double v = stdlib_base_dists_negative_binomial_variance( 100.0, 0.2 );
172+
// returns ~2000.0
173+
```
174+
175+
The function accepts the following arguments:
176+
177+
- **r**: `[in] double` number of successes until experiment is stopped.
178+
- **p**: `[in] double` success probability.
179+
180+
```c
181+
double stdlib_base_dists_negative_binomial_variance( const double r, const double p );
182+
```
183+
184+
</section>
185+
186+
<!-- /.usage -->
187+
188+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
189+
190+
<section class="notes">
191+
192+
</section>
193+
194+
<!-- /.notes -->
195+
196+
<!-- C API usage examples. -->
197+
198+
<section class="examples">
199+
200+
### Examples
201+
202+
```c
203+
#include "stdlib/stats/base/dists/negative-binomial/variance.h"
204+
#include <stdlib.h>
205+
#include <stdio.h>
206+
207+
static double random_uniform( const double min, const double max ) {
208+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
209+
return min + ( v*(max-min) );
210+
}
211+
212+
int main( void ) {
213+
double r;
214+
double p;
215+
double v;
216+
int i;
217+
218+
219+
for ( i = 0; i < 10; i++ ) {
220+
r = random_uniform( -1.0, 100.0 );
221+
p = random_uniform( 0.0, 1.0 );
222+
v = stdlib_base_dists_negative_binomial_variance( r, p );
223+
printf( "r: %.4f, p: %.4f, Var(X;r,p): %.4f\n", r, p, v );
224+
}
225+
}
226+
```
227+
228+
</section>
229+
230+
<!-- /.examples -->
231+
232+
</section>
233+
234+
<!-- /.c -->
235+
140236
<!-- 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. -->
141237

142238
<section class="references">
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var variance = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( variance instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg + '::native', opts, function benchmark( b ) {
42+
var len;
43+
var r;
44+
var p;
45+
var y;
46+
var i;
47+
48+
len = 100;
49+
r = uniform( len, 1.0, 11.0 ); // Treat r as double
50+
p = uniform( len, 0.1, 0.9 );
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
y = variance( r[ i % len ], p[ i % len ] );
55+
if ( isnan( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
}
59+
b.toc();
60+
if ( isnan( y ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
# VARIABLES #
20+
21+
ifndef VERBOSE
22+
QUIET := @
23+
else
24+
QUIET :=
25+
endif
26+
27+
# Determine the OS ([1][1], [2][2]).
28+
#
29+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
30+
# [2]: http://stackoverflow.com/a/27776822/2225624
31+
OS ?= $(shell uname)
32+
ifneq (, $(findstring MINGW,$(OS)))
33+
OS := WINNT
34+
else
35+
ifneq (, $(findstring MSYS,$(OS)))
36+
OS := WINNT
37+
else
38+
ifneq (, $(findstring CYGWIN,$(OS)))
39+
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
44+
endif
45+
endif
46+
endif
47+
48+
# Define the program used for compiling C source files:
49+
ifdef C_COMPILER
50+
CC := $(C_COMPILER)
51+
else
52+
CC := gcc
53+
endif
54+
55+
# Define the command-line options when compiling C files:
56+
CFLAGS ?= \
57+
-std=c99 \
58+
-O3 \
59+
-Wall \
60+
-pedantic
61+
62+
# Determine whether to generate position independent code ([1][1], [2][2]).
63+
#
64+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
65+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
66+
ifeq ($(OS), WINNT)
67+
fPIC ?=
68+
else
69+
fPIC ?= -fPIC
70+
endif
71+
72+
# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`):
73+
INCLUDE ?=
74+
75+
# List of source files:
76+
SOURCE_FILES ?=
77+
78+
# List of libraries (e.g., `-lopenblas -lpthread`):
79+
LIBRARIES ?=
80+
81+
# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
82+
LIBPATH ?=
83+
84+
# List of C targets:
85+
c_targets := benchmark.out
86+
87+
88+
# RULES #
89+
90+
#/
91+
# Compiles source files.
92+
#
93+
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
94+
# @param {string} [CFLAGS] - C compiler options
95+
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
96+
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
97+
# @param {string} [SOURCE_FILES] - list of source files
98+
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
99+
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
100+
#
101+
# @example
102+
# make
103+
#
104+
# @example
105+
# make all
106+
#/
107+
all: $(c_targets)
108+
109+
.PHONY: all
110+
111+
#/
112+
# Compiles C source files.
113+
#
114+
# @private
115+
# @param {string} CC - C compiler (e.g., `gcc`)
116+
# @param {string} CFLAGS - C compiler options
117+
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
118+
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`)
119+
# @param {string} SOURCE_FILES - list of source files
120+
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
121+
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
122+
#/
123+
$(c_targets): %.out: %.c
124+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES)
125+
126+
#/
127+
# Runs compiled benchmarks.
128+
#
129+
# @example
130+
# make run
131+
#/
132+
run: $(c_targets)
133+
$(QUIET) ./$<
134+
135+
.PHONY: run
136+
137+
#/
138+
# Removes generated files.
139+
#
140+
# @example
141+
# make clean
142+
#/
143+
clean:
144+
$(QUIET) -rm -f *.o *.out
145+
146+
.PHONY: clean

0 commit comments

Comments
 (0)