Skip to content

Commit 1e6a9de

Browse files
committed
feat: add support for accessor arrays and refactor stats/base/stdevyc
1 parent a8f5e17 commit 1e6a9de

File tree

11 files changed

+276
-98
lines changed

11 files changed

+276
-98
lines changed

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

Lines changed: 16 additions & 25 deletions
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 stdevyc = require( '@stdlib/stats/base/stdevyc' );
9999
```
100100

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

103103
Computes the [standard deviation][standard-deviation] of a strided array `x` using a one-pass algorithm proposed by Youngs and Cramer.
104104

@@ -114,17 +114,15 @@ The function has the following parameters:
114114
- **N**: number of indexed elements.
115115
- **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).
116116
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
117-
- **stride**: index increment for `x`.
117+
- **strideX**: stride length for `x`.
118118

119-
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`,
119+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`,
120120

121121
```javascript
122-
var floor = require( '@stdlib/math/base/special/floor' );
123122

124123
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
125-
var N = floor( x.length / 2 );
126124

127-
var v = stdevyc( N, 1, x, 2 );
125+
var v = stdevyc( 4, 1, x, 2 );
128126
// returns 2.5
129127
```
130128

@@ -134,18 +132,16 @@ Note that indexing is relative to the first index. To introduce an offset, use [
134132

135133
```javascript
136134
var Float64Array = require( '@stdlib/array/float64' );
137-
var floor = require( '@stdlib/math/base/special/floor' );
138135

139136
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
140137
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
141138

142-
var N = floor( x0.length / 2 );
143139

144-
var v = stdevyc( N, 1, x1, 2 );
140+
var v = stdevyc( 4, 1, x1, 2 );
145141
// returns 2.5
146142
```
147143

148-
#### stdevyc.ndarray( N, correction, x, stride, offset )
144+
#### stdevyc.ndarray( N, correction, x, strideX , offsetX )
149145

150146
Computes the [standard deviation][standard-deviation] of a strided array using a one-pass algorithm proposed by Youngs and Cramer and alternative indexing semantics.
151147

@@ -158,17 +154,15 @@ var v = stdevyc.ndarray( x.length, 1, x, 1, 0 );
158154

159155
The function has the following additional parameters:
160156

161-
- **offset**: starting index for `x`.
157+
- **offsetX**: starting index for `x`.
162158

163-
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 [standard deviation][standard-deviation] for every other value in `x` starting from the second value
159+
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 [standard deviation][standard-deviation] for every other element in the strided array starting from the second element
164160

165161
```javascript
166-
var floor = require( '@stdlib/math/base/special/floor' );
167162

168163
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
169-
var N = floor( x.length / 2 );
170164

171-
var v = stdevyc.ndarray( N, 1, x, 2, 1 );
165+
var v = stdevyc.ndarray( 4, 1, x, 2, 1 );
172166
// returns 2.5
173167
```
174168

@@ -181,6 +175,7 @@ var v = stdevyc.ndarray( N, 1, x, 2, 1 );
181175
## Notes
182176

183177
- If `N <= 0`, both functions return `NaN`.
178+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
184179
- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`.
185180
- Depending on the environment, the typed versions ([`dstdevyc`][@stdlib/stats/base/dstdevyc], [`sstdevyc`][@stdlib/stats/base/sstdevyc], etc.) are likely to be significantly more performant.
186181

@@ -195,18 +190,12 @@ var v = stdevyc.ndarray( N, 1, x, 2, 1 );
195190
<!-- eslint no-undef: "error" -->
196191

197192
```javascript
198-
var randu = require( '@stdlib/random/base/randu' );
199-
var round = require( '@stdlib/math/base/special/round' );
200-
var Float64Array = require( '@stdlib/array/float64' );
193+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform');
201194
var stdevyc = require( '@stdlib/stats/base/stdevyc' );
202195

203-
var x;
204-
var i;
205-
206-
x = new Float64Array( 10 );
207-
for ( i = 0; i < x.length; i++ ) {
208-
x[ i ] = round( (randu()*100.0) - 50.0 );
209-
}
196+
var x = discreteUniform( 10, -50, 50, {
197+
'dtype': 'float64'
198+
});
210199
console.log( x );
211200

212201
var v = stdevyc( x.length, 1, x, 1 );
@@ -257,6 +246,8 @@ console.log( v );
257246

258247
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
259248

249+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
250+
260251
[@youngs:1971a]: https://doi.org/10.1080/00401706.1971.10488826
261252

262253
<!-- <related-links> -->

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

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

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var pkg = require( './../package.json' ).name;
28-
var stdevyc = require( './../lib/stdevyc.js' );
29-
28+
var stdevyc = require( './../lib/main.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
3036

3137
// FUNCTIONS //
3238

@@ -38,13 +44,7 @@ var stdevyc = require( './../lib/stdevyc.js' );
3844
* @returns {Function} benchmark function
3945
*/
4046
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = [];
45-
for ( i = 0; i < len; i++ ) {
46-
x.push( ( randu()*20.0 ) - 10.0 );
47-
}
47+
var x = uniform( len, -10, 10, options );
4848
return benchmark;
4949

5050
function benchmark( b ) {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var pkg = require( './../package.json' ).name;
2828
var stdevyc = require( './../lib/ndarray.js' );
2929

30+
// VARIABLES //
31+
32+
var options = {
33+
'dtype': 'generic'
34+
};
35+
3036

3137
// FUNCTIONS //
3238

@@ -38,13 +44,7 @@ var stdevyc = require( './../lib/ndarray.js' );
3844
* @returns {Function} benchmark function
3945
*/
4046
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = [];
45-
for ( i = 0; i < len; i++ ) {
46-
x.push( ( randu()*20.0 ) - 10.0 );
47-
}
47+
var x = uniform( len, -10, 10, options );
4848
return benchmark;
4949

5050
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/stdevyc/docs/repl.txt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
{{alias}}( N, correction, x, stride )
2+
{{alias}}( N, correction, x, strideX )
33
Computes the standard deviation of a strided array using a one-pass
44
algorithm proposed by Youngs and Cramer.
55

6-
The `N` and `stride` parameters determine which elements in `x` are accessed
6+
The `N` and stride parameters determine which elements in the strided array are accessed
77
at runtime.
88

99
Indexing is relative to the first index. To introduce an offset, use a typed
@@ -31,8 +31,8 @@
3131
x: Array<number>|TypedArray
3232
Input array.
3333

34-
stride: integer
35-
Index increment.
34+
strideX: integer
35+
Stride length.
3636

3737
Returns
3838
-------
@@ -46,22 +46,18 @@
4646
> {{alias}}( x.length, 1, x, 1 )
4747
~2.0817
4848

49-
// Using `N` and `stride` parameters:
49+
// Using `N` and stride parameters:
5050
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ];
51-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
52-
> var stride = 2;
53-
> {{alias}}( N, 1, x, stride )
51+
> {{alias}}( 3, 1, x, 2 )
5452
~2.0817
5553

5654
// Using view offsets:
5755
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
5856
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
59-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
60-
> stride = 2;
61-
> {{alias}}( N, 1, x1, stride )
57+
> {{alias}}( 3, 1, x1, 2 )
6258
~2.0817
6359

64-
{{alias}}.ndarray( N, correction, x, stride, offset )
60+
{{alias}}.ndarray( N, correction, x, strideX, offsetX )
6561
Computes the standard deviation of a strided array using a one-pass
6662
algorithm proposed by Youngs and Cramer and alternative indexing semantics.
6763

@@ -89,10 +85,10 @@
8985
x: Array<number>|TypedArray
9086
Input array.
9187

92-
stride: integer
93-
Index increment.
88+
strideX: integer
89+
Stride length.
9490

95-
offset: integer
91+
offsetX: integer
9692
Starting index.
9793

9894
Returns
@@ -109,8 +105,7 @@
109105

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

116111
See Also

lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/index.d.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { NumericArray } from '@stdlib/types/array';
24-
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
2529
/**
2630
* Interface describing `stdevyc`.
2731
*/
@@ -32,7 +36,7 @@ interface Routine {
3236
* @param N - number of indexed elements
3337
* @param correction - degrees of freedom adjustment
3438
* @param x - input array
35-
* @param stride - stride length
39+
* @param strideX - stride length
3640
* @returns standard deviation
3741
*
3842
* @example
@@ -41,16 +45,16 @@ interface Routine {
4145
* var v = stdevyc( x.length, 1, x, 1 );
4246
* // returns ~2.0817
4347
*/
44-
( N: number, correction: number, x: NumericArray, stride: number ): number;
48+
( N: number, correction: number, x: InputArray, strideX: number ): number;
4549

4650
/**
4751
* Computes the standard deviation of a strided array using a one-pass algorithm proposed by Youngs and Cramer and alternative indexing semantics.
4852
*
4953
* @param N - number of indexed elements
5054
* @param correction - degrees of freedom adjustment
5155
* @param x - input array
52-
* @param stride - stride length
53-
* @param offset - starting index
56+
* @param strideX - stride length
57+
* @param offsetX - starting index
5458
* @returns standard deviation
5559
*
5660
* @example
@@ -59,7 +63,7 @@ interface Routine {
5963
* var v = stdevyc.ndarray( x.length, 1, x, 1, 0 );
6064
* // returns ~2.0817
6165
*/
62-
ndarray( N: number, correction: number, x: NumericArray, stride: number, offset: number ): number;
66+
ndarray( N: number, correction: number, x: InputArray, strideX: number, offsetX: number ): number;
6367
}
6468

6569
/**
@@ -68,7 +72,7 @@ interface Routine {
6872
* @param N - number of indexed elements
6973
* @param correction - degrees of freedom adjustment
7074
* @param x - input array
71-
* @param stride - stride length
75+
* @param strideX - stride length
7276
* @returns standard deviation
7377
*
7478
* @example

lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
import AccessorArray = require( '@stdlib/array/base/accessor' );
1920
import stdevyc = require( './index' );
2021

2122

@@ -26,6 +27,7 @@ import stdevyc = require( './index' );
2627
const x = new Float64Array( 10 );
2728

2829
stdevyc( x.length, 1, x, 1 ); // $ExpectType number
30+
stdevyc( x.length, 1, new AccessorArray( x ), 1 ); // $ExpectType number
2931
}
3032

3133
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -101,6 +103,7 @@ import stdevyc = require( './index' );
101103
const x = new Float64Array( 10 );
102104

103105
stdevyc.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number
106+
stdevyc.ndarray( x.length, 1, new AccessorArray( x ), 1, 0 ); // $ExpectType number
104107
}
105108

106109
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...

lib/node_modules/@stdlib/stats/base/stdevyc/examples/index.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@
1818

1919
'use strict';
2020

21-
var randu = require( '@stdlib/random/base/randu' );
22-
var round = require( '@stdlib/math/base/special/round' );
23-
var Float64Array = require( '@stdlib/array/float64' );
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2422
var stdevyc = require( './../lib' );
2523

26-
var x;
27-
var i;
28-
29-
x = new Float64Array( 10 );
30-
for ( i = 0; i < x.length; i++ ) {
31-
x[ i ] = round( (randu()*100.0) - 50.0 );
32-
}
24+
var x = discreteUniform( 10, -50, 50, {
25+
'dtype': 'float64'
26+
});
3327
console.log( x );
3428

3529
var v = stdevyc( x.length, 1, x, 1 );

0 commit comments

Comments
 (0)