Skip to content

feat: add support for accessor arrays and refactor stats/base/stdevwd #6527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/node_modules/@stdlib/stats/base/stdevwd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ The function has the following parameters:

- **N**: number of indexed elements.
- **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 [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] 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 corrected sample [standard deviation][standard-deviation], 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).
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
- **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or accessor array.
- **stride**: index increment for `x`.

The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`,
Expand Down Expand Up @@ -172,6 +172,20 @@ var v = stdevwd.ndarray( N, 1, x, 2, 1 );
// returns 2.5
```

```javascript
var accessorArray = {
'get': function get(i) {
return [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ][i];
},
'set': function set(i, v) {
// No-op for this example
},
'length': 8
};
var v = stdevwd.ndarray( 4, 1, accessorArray, 2, 1 );
console.log( v );
```

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -213,6 +227,21 @@ var v = stdevwd( x.length, 1, x, 1 );
console.log( v );
```

```javascript
var accessorArray = {
'get': function get(i) {
return [ 1.0, -2.0, 3.0, -4.0, 5.0 ][i];
},
'set': function set(i, v) {
// No-op for this example
},
'length': 5
};
var v = stdevwd( accessorArray.length, 1, accessorArray, 1 );
console.log( v );
```
- The function supports accessor arrays, which provide an alternative way to define strided arrays using `get` and `set` methods for data access.

</section>

<!-- /.examples -->
Expand Down
61 changes: 54 additions & 7 deletions lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ var stdevwd = require( './../lib/stdevwd.js' );
// FUNCTIONS //

/**
* Creates a benchmark function.
* Creates a benchmark function for a native array.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
function createNativeBenchmark( len ) {
var x;
var i;

x = [];
x = new Array( len );
for ( i = 0; i < len; i++ ) {
x.push( ( randu()*20.0 ) - 10.0 );
x[ i ] = ( randu()*20.0 ) - 10.0;
}
return benchmark;

Expand All @@ -67,6 +67,51 @@ function createBenchmark( len ) {
}
}

/**
* Creates a benchmark function for an accessor array.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createAccessorBenchmark( len ) {
var data;
var x;
var i;

data = new Array( len );
for ( i = 0; i < len; i++ ) {
data[ i ] = ( randu()*20.0 ) - 10.0;
}
x = {
'get': function get( i ) {
return data[ i ];
},
'set': function set() {},
'length': data.length
};
return benchmark;

function benchmark( b ) {
var v;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = stdevwd( x.length, 1, x, 1 );
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

Expand All @@ -87,9 +132,11 @@ function main() {

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':len='+len, f );
f = createNativeBenchmark( len );
bench( pkg+':native:len='+len, f );
f = createAccessorBenchmark( len );
bench( pkg+':accessors:len='+len, f );
}
}

main();
main();
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ var stdevwd = require( './../lib/ndarray.js' );
// FUNCTIONS //

/**
* Creates a benchmark function.
* Creates a benchmark function for a native array.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
function createNativeBenchmark( len ) {
var x;
var i;

x = [];
x = new Array( len );
for ( i = 0; i < len; i++ ) {
x.push( ( randu()*20.0 ) - 10.0 );
x[ i ] = ( randu()*20.0 ) - 10.0;
}
return benchmark;

Expand All @@ -67,6 +67,51 @@ function createBenchmark( len ) {
}
}

/**
* Creates a benchmark function for an accessor array.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createAccessorBenchmark( len ) {
var data;
var x;
var i;

data = new Array( len );
for ( i = 0; i < len; i++ ) {
data[ i ] = ( randu()*20.0 ) - 10.0;
}
x = {
'get': function get( i ) {
return data[ i ];
},
'set': function set() {},
'length': data.length
};
return benchmark;

function benchmark( b ) {
var v;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = stdevwd( x.length, 1, x, 1, 0 );
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

Expand All @@ -87,9 +132,11 @@ function main() {

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':ndarray:len='+len, f );
f = createNativeBenchmark( len );
bench( pkg+':ndarray:native:len='+len, f );
f = createAccessorBenchmark( len );
bench( pkg+':ndarray:accessors:len='+len, f );
}
}

main();
main();
34 changes: 25 additions & 9 deletions lib/node_modules/@stdlib/stats/base/stdevwd/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{{alias}}( N, correction, x, stride )
Computes the standard deviation of a strided array using Welford's
algorithm.
Expand All @@ -7,7 +6,7 @@
at runtime.

Indexing is relative to the first index. To introduce an offset, use a typed
array view.
array view or accessor array.

If `N <= 0`, the function returns `NaN`.

Expand All @@ -20,16 +19,16 @@
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 standard deviation according to `N - c` where `c` corresponds to
the provided degrees of freedom adjustment. When computing the standard
the provided degrees of freedom adjustment.When computing the standard
deviation 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 corrected sample standard deviation,
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).

x: Array<number>|TypedArray
Input array.
x: Array<number>|TypedArray|AccessorArray
Input array (regular, typed, or accessor array).

stride: integer
Index increment.
Expand All @@ -46,6 +45,15 @@
> {{alias}}( x.length, 1, x, 1 )
~2.0817

// Using accessor array:
> var accessorArray = {
> 'get': (i) => [1.0, -2.0, 2.0][i],
> 'set': (i,v) => {},
> 'length': 3
> };
> {{alias}}( accessorArray.length, 1, accessorArray, 1 )
~2.0817

// Using `N` and `stride` parameters:
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
Expand Down Expand Up @@ -86,8 +94,8 @@
array contains data sampled from a larger population; this is commonly
referred to as Bessel's correction).

x: Array<number>|TypedArray
Input array.
x: Array<number>|TypedArray|AccessorArray
Input array (regular, typed, or accessor array).

stride: integer
Index increment.
Expand All @@ -107,12 +115,20 @@
> {{alias}}.ndarray( x.length, 1, x, 1, 0 )
~2.0817

// Using accessor array:
> var accessorArray = {
> 'get': (i) => [1.0, -2.0, 2.0][i],
> 'set': (i,v) => {},
> 'length': 3
> };
> {{alias}}.ndarray( accessorArray.length, 1, accessorArray, 1, 0 )
~2.0817

// Using offset parameter:
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}.ndarray( N, 1, x, 2, 1 )
~2.0817

See Also
--------

--------
21 changes: 21 additions & 0 deletions lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use tabs instead of spaces

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@

import { NumericArray } from '@stdlib/types/array';

/**
* Interface describing an accessor array.
*/
interface AccessorArray {
/**

Check failure on line 29 in lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Expected indentation of 1 tab but found 4 spaces
* Returns an array element.
*/
get( index: number ): number;

Check failure on line 32 in lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Expected indentation of 1 tab but found 4 spaces

Check failure on line 33 in lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Trailing spaces not allowed
/**

Check failure on line 34 in lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Expected indentation of 1 tab but found 4 spaces
* Sets an array element.
*/
set( index: number, value: number ): void;

Check failure on line 37 in lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Expected indentation of 1 tab but found 4 spaces

Check failure on line 38 in lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Trailing spaces not allowed
/**

Check failure on line 39 in lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Expected indentation of 1 tab but found 4 spaces
* Number of elements.
*/
length: number;

Check failure on line 42 in lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Expected indentation of 1 tab but found 4 spaces
}


/**
* Interface describing `stdevwd`.
*/
Expand Down
28 changes: 20 additions & 8 deletions lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ import stdevwd = require( './index' );
stdevwd( x.length, 1, x, 1 ); // $ExpectType number
}

// The function supports accessor arrays...
{
const accessorArray = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use @stdlib/array/base/accessor here and elsewhere

get: ( i: number ) => [1.0, -2.0, 3.0][i],
set: ( i: number, v: number ) => { /* no-op */ },
length: 3
};
stdevwd( accessorArray.length, 1, accessorArray, 1 ); // $ExpectType number
stdevwd.ndarray( accessorArray.length, 1, accessorArray, 1, 0 ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not a number...
{
const x = new Float64Array( 10 );
Expand All @@ -46,14 +57,15 @@ import stdevwd = require( './index' );
{
const x = new Float64Array( 10 );

stdevwd( x.length, '10', x, 1 ); // $ExpectError
stdevwd( x.length, true, x, 1 ); // $ExpectError
stdevwd( x.length, false, x, 1 ); // $ExpectError
stdevwd( x.length, null, x, 1 ); // $ExpectError
stdevwd( x.length, undefined, x, 1 ); // $ExpectError
stdevwd( x.length, [], x, 1 ); // $ExpectError
stdevwd( x.length, {}, x, 1 ); // $ExpectError
stdevwd( x.length, ( x: number ): number => x, x, 1 ); // $ExpectError
stdevwd( x.length, 1, 10, 1 ); // $ExpectError
stdevwd( x.length, 1, '10', 1 ); // $ExpectError
stdevwd( x.length, 1, true, 1 ); // $ExpectError
stdevwd( x.length, 1, false, 1 ); // $ExpectError
stdevwd( x.length, 1, null, 1 ); // $ExpectError
stdevwd( x.length, 1, undefined, 1 ); // $ExpectError
stdevwd( x.length, 1, [ '1' ], 1 ); // $ExpectError
stdevwd( x.length, 1, {}, 1 ); // $ExpectError (plain object lacks accessor methods)
stdevwd( x.length, 1, ( x: number ): number => x, 1 ); // $ExpectError
}

// The compiler throws an error if the function is provided a third argument which is not a numeric array...
Expand Down
Loading
Loading