Skip to content

Commit 1511a4d

Browse files
feat: add C ndarray interface and refactor implementation for stats/base/dnanvariancetk
PR-URL: #4847 Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Aayush Khanna <[email protected]> Reviewed-by: Neeraj Pathak <[email protected]>
1 parent eebd899 commit 1511a4d

23 files changed

+440
-279
lines changed

lib/node_modules/@stdlib/stats/base/dnanvariancetk/README.md

+146-29
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note,
9898
var dnanvariancetk = require( '@stdlib/stats/base/dnanvariancetk' );
9999
```
100100

101-
#### dnanvariancetk( N, correction, x, stride )
101+
#### dnanvariancetk( N, correction, x, strideX )
102102

103103
Computes the [variance][variance] of a double-precision floating-point strided array `x` ignoring `NaN` values and using a one-pass textbook algorithm.
104104

@@ -116,39 +116,36 @@ The function has the following parameters:
116116
- **N**: number of indexed elements.
117117
- **correction**: degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [variance][variance] according to `n-c` where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements. When computing the [variance][variance] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the unbiased sample [variance][variance], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction).
118118
- **x**: input [`Float64Array`][@stdlib/array/float64].
119-
- **stride**: index increment for `x`.
119+
- **strideX**: stride length for `x`.
120120

121-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [variance][variance] of every other element in `x`,
121+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [variance][variance] of every other element in `x`,
122+
123+
<!-- eslint-disable max-len -->
122124

123125
```javascript
124126
var Float64Array = require( '@stdlib/array/float64' );
125-
var floor = require( '@stdlib/math/base/special/floor' );
126127

127-
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN ] );
128-
var N = floor( x.length / 2 );
128+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ] );
129129

130-
var v = dnanvariancetk( N, 1, x, 2 );
130+
var v = dnanvariancetk( 5, 1, x, 2 );
131131
// returns 6.25
132132
```
133133

134134
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
135135

136-
<!-- eslint-disable stdlib/capitalized-comments -->
136+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
137137

138138
```javascript
139139
var Float64Array = require( '@stdlib/array/float64' );
140-
var floor = require( '@stdlib/math/base/special/floor' );
141140

142-
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
141+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
143142
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
144143

145-
var N = floor( x0.length / 2 );
146-
147-
var v = dnanvariancetk( N, 1, x1, 2 );
144+
var v = dnanvariancetk( 5, 1, x1, 2 );
148145
// returns 6.25
149146
```
150147

151-
#### dnanvariancetk.ndarray( N, correction, x, stride, offset )
148+
#### dnanvariancetk.ndarray( N, correction, x, strideX, offsetX )
152149

153150
Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass textbook algorithm and alternative indexing semantics.
154151

@@ -163,18 +160,18 @@ var v = dnanvariancetk.ndarray( x.length, 1, x, 1, 0 );
163160

164161
The function has the following additional parameters:
165162

166-
- **offset**: starting index for `x`.
163+
- **offsetX**: starting index for `x`.
164+
165+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [variance][variance] for every other element in `x` starting from the second element
167166

168-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [variance][variance] for every other value in `x` starting from the second value
167+
<!-- eslint-disable max-len -->
169168

170169
```javascript
171170
var Float64Array = require( '@stdlib/array/float64' );
172-
var floor = require( '@stdlib/math/base/special/floor' );
173171

174-
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
175-
var N = floor( x.length / 2 );
172+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
176173

177-
var v = dnanvariancetk.ndarray( N, 1, x, 2, 1 );
174+
var v = dnanvariancetk.ndarray( 5, 1, x, 2, 1 );
178175
// returns 6.25
179176
```
180177

@@ -201,18 +198,19 @@ var v = dnanvariancetk.ndarray( N, 1, x, 2, 1 );
201198
<!-- eslint no-undef: "error" -->
202199

203200
```javascript
204-
var randu = require( '@stdlib/random/base/randu' );
205-
var round = require( '@stdlib/math/base/special/round' );
206-
var Float64Array = require( '@stdlib/array/float64' );
201+
var uniform = require( '@stdlib/random/base/uniform' );
202+
var filledarrayBy = require( '@stdlib/array/filled-by' );
203+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
207204
var dnanvariancetk = require( '@stdlib/stats/base/dnanvariancetk' );
208205

209-
var x;
210-
var i;
211-
212-
x = new Float64Array( 10 );
213-
for ( i = 0; i < x.length; i++ ) {
214-
x[ i ] = round( (randu()*100.0) - 50.0 );
206+
function rand() {
207+
if ( bernoulli( 0.8 ) < 1 ) {
208+
return NaN;
209+
}
210+
return uniform( -50.0, 50.0 );
215211
}
212+
213+
var x = filledarrayBy( 10, 'float64', rand );
216214
console.log( x );
217215

218216
var v = dnanvariancetk( x.length, 1, x, 1 );
@@ -223,6 +221,125 @@ console.log( v );
223221

224222
<!-- /.examples -->
225223

224+
<!-- C interface documentation. -->
225+
226+
* * *
227+
228+
<section class="c">
229+
230+
## C APIs
231+
232+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
233+
234+
<section class="intro">
235+
236+
</section>
237+
238+
<!-- /.intro -->
239+
240+
<!-- C usage documentation. -->
241+
242+
<section class="usage">
243+
244+
### Usage
245+
246+
```c
247+
#include "stdlib/stats/base/dnanvariancetk.h"
248+
```
249+
250+
#### stdlib_strided_dnanvariancetk( N, correction, \*X, strideX )
251+
252+
Computes the variance of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass textbook algorithm.
253+
254+
```c
255+
const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 };
256+
257+
double v = stdlib_strided_dnanvariancetk( 4, 1.0, x, 1 );
258+
// returns ~4.3333
259+
```
260+
261+
The function accepts the following arguments:
262+
263+
- **N**: `[in] CBLAS_INT` number of indexed elements.
264+
- **correction**: `[in] double` degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [variance][variance] according to `n-c` where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements. When computing the [variance][variance] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the unbiased sample [variance][variance], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction).
265+
- **X**: `[in] double*` input array.
266+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
267+
268+
```c
269+
double stdlib_strided_dnanvariancetk( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX );
270+
```
271+
272+
#### stdlib_strided_dnanvariancetk_ndarray( N, correction, \*X, strideX, offsetX )
273+
274+
Computes the variance of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass textbook algorithm and alternative indexing semantics.
275+
276+
```c
277+
const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 };
278+
279+
double v = stdlib_strided_dnanvariancetk_ndarray( 4, 1.0, x, 1, 0 );
280+
// returns ~4.3333
281+
```
282+
283+
The function accepts the following arguments:
284+
285+
- **N**: `[in] CBLAS_INT` number of indexed elements.
286+
- **correction**: `[in] double` degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [variance][variance] according to `n-c` where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements. When computing the [variance][variance] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the unbiased sample [variance][variance], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction).
287+
- **X**: `[in] double*` input array.
288+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
289+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
290+
291+
```c
292+
double stdlib_strided_dnanvariancetk_ndarray( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
293+
```
294+
295+
</section>
296+
297+
<!-- /.usage -->
298+
299+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
300+
301+
<section class="notes">
302+
303+
</section>
304+
305+
<!-- /.notes -->
306+
307+
<!-- C API usage examples. -->
308+
309+
<section class="examples">
310+
311+
### Examples
312+
313+
```c
314+
#include "stdlib/stats/base/dnanvariancetk.h"
315+
#include <stdio.h>
316+
317+
int main( void ) {
318+
// Create a strided array:
319+
const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 };
320+
321+
// Specify the number of elements:
322+
const int N = 6;
323+
324+
// Specify the stride length:
325+
const int strideX = 2;
326+
327+
// Compute the variance:
328+
double v = stdlib_strided_dnanvariancetk( N, 1, x, strideX );
329+
330+
// Print the result:
331+
printf( "sample variance: %lf\n", v );
332+
}
333+
```
334+
335+
</section>
336+
337+
<!-- /.examples -->
338+
339+
</section>
340+
341+
<!-- /.c -->
342+
226343
* * *
227344
228345
<section class="references">

lib/node_modules/@stdlib/stats/base/dnanvariancetk/benchmark/benchmark.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,30 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2829
var pkg = require( './../package.json' ).name;
2930
var dnanvariancetk = require( './../lib/dnanvariancetk.js' );
3031

3132

3233
// FUNCTIONS //
3334

35+
/**
36+
* Returns a random value or `NaN`.
37+
*
38+
* @private
39+
* @returns {number} random number or `NaN`
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
3448
/**
3549
* Creates a benchmark function.
3650
*
@@ -39,17 +53,7 @@ var dnanvariancetk = require( './../lib/dnanvariancetk.js' );
3953
* @returns {Function} benchmark function
4054
*/
4155
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float64Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
if ( randu() < 0.2 ) {
48-
x[ i ] = NaN;
49-
} else {
50-
x[ i ] = ( randu()*20.0 ) - 10.0;
51-
}
52-
}
56+
var x = filledarrayBy( len, 'float64', rand );
5357
return benchmark;
5458

5559
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dnanvariancetk/benchmark/benchmark.native.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
27+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2628
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2729
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
2930
var tryRequire = require( '@stdlib/utils/try-require' );
3031
var pkg = require( './../package.json' ).name;
3132

@@ -40,6 +41,19 @@ var opts = {
4041

4142
// FUNCTIONS //
4243

44+
/**
45+
* Returns a random value or `NaN`.
46+
*
47+
* @private
48+
* @returns {number} random number or `NaN`
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.8 ) < 1 ) {
52+
return NaN;
53+
}
54+
return uniform( -10.0, 10.0 );
55+
}
56+
4357
/**
4458
* Creates a benchmark function.
4559
*
@@ -48,17 +62,7 @@ var opts = {
4862
* @returns {Function} benchmark function
4963
*/
5064
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float64Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
if ( randu() < 0.2 ) {
57-
x[ i ] = NaN;
58-
} else {
59-
x[ i ] = ( randu()*20.0 ) - 10.0;
60-
}
61-
}
65+
var x = filledarrayBy( len, 'float64', rand );
6266
return benchmark;
6367

6468
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dnanvariancetk/benchmark/benchmark.ndarray.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,30 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2829
var pkg = require( './../package.json' ).name;
2930
var dnanvariancetk = require( './../lib/ndarray.js' );
3031

3132

3233
// FUNCTIONS //
3334

35+
/**
36+
* Returns a random value or `NaN`.
37+
*
38+
* @private
39+
* @returns {number} random number or `NaN`
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
3448
/**
3549
* Creates a benchmark function.
3650
*
@@ -39,17 +53,7 @@ var dnanvariancetk = require( './../lib/ndarray.js' );
3953
* @returns {Function} benchmark function
4054
*/
4155
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float64Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
if ( randu() < 0.2 ) {
48-
x[ i ] = NaN;
49-
} else {
50-
x[ i ] = ( randu()*20.0 ) - 10.0;
51-
}
52-
}
56+
var x = filledarrayBy( len, 'float64', rand );
5357
return benchmark;
5458

5559
function benchmark( b ) {

0 commit comments

Comments
 (0)