Skip to content

Commit c5dc723

Browse files
gururaj1512kgrytestdlib-bot
authored
feat: add support for accessor arrays and refactor stats/base/stdev
PR-URL: #7333 Closes: #5681 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 33266af commit c5dc723

File tree

12 files changed

+404
-121
lines changed

12 files changed

+404
-121
lines changed

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

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note,
9898
var stdev = require( '@stdlib/stats/base/stdev' );
9999
```
100100

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

103-
Computes the [standard deviation][standard-deviation] of a strided array `x`.
103+
Computes the [standard deviation][standard-deviation] of a strided array.
104104

105105
```javascript
106106
var x = [ 1.0, -2.0, 2.0 ];
@@ -114,17 +114,14 @@ 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' );
123-
124122
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 );
126123

127-
var v = stdev( N, 1, x, 2 );
124+
var v = stdev( 4, 1, x, 2 );
128125
// returns 2.5
129126
```
130127

@@ -134,18 +131,15 @@ Note that indexing is relative to the first index. To introduce an offset, use [
134131

135132
```javascript
136133
var Float64Array = require( '@stdlib/array/float64' );
137-
var floor = require( '@stdlib/math/base/special/floor' );
138134

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

142-
var N = floor( x0.length / 2 );
143-
144-
var v = stdev( N, 1, x1, 2 );
138+
var v = stdev( 4, 1, x1, 2 );
145139
// returns 2.5
146140
```
147141

148-
#### stdev.ndarray( N, correction, x, stride, offset )
142+
#### stdev.ndarray( N, correction, x, strideX, offsetX )
149143

150144
Computes the [standard deviation][standard-deviation] of a strided array using alternative indexing semantics.
151145

@@ -158,17 +152,14 @@ var v = stdev.ndarray( x.length, 1, x, 1, 0 );
158152

159153
The function has the following additional parameters:
160154

161-
- **offset**: starting index for `x`.
155+
- **offsetX**: starting index for `x`.
162156

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
157+
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 `x` starting from the second element
164158

165159
```javascript
166-
var floor = require( '@stdlib/math/base/special/floor' );
167-
168160
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 );
170161

171-
var v = stdev.ndarray( N, 1, x, 2, 1 );
162+
var v = stdev.ndarray( 4, 1, x, 2, 1 );
172163
// returns 2.5
173164
```
174165

@@ -182,6 +173,7 @@ var v = stdev.ndarray( N, 1, x, 2, 1 );
182173

183174
- If `N <= 0`, both functions return `NaN`.
184175
- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`.
176+
- 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]).
185177
- Depending on the environment, the typed versions ([`dstdev`][@stdlib/stats/strided/dstdev], [`sstdev`][@stdlib/stats/strided/sstdev], etc.) are likely to be significantly more performant.
186178

187179
</section>
@@ -195,18 +187,12 @@ var v = stdev.ndarray( N, 1, x, 2, 1 );
195187
<!-- eslint no-undef: "error" -->
196188

197189
```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' );
190+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
201191
var stdev = require( '@stdlib/stats/base/stdev' );
202192

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-
}
193+
var x = discreteUniform( 10, -50, 50, {
194+
'dtype': 'float64'
195+
});
210196
console.log( x );
211197

212198
var v = stdev( x.length, 1, x, 1 );
@@ -250,6 +236,8 @@ console.log( v );
250236

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

239+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
240+
253241
<!-- <related-links> -->
254242

255243
[@stdlib/stats/strided/dstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dstdev

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +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 stdev = require( './../lib/stdev.js' );
28+
var stdev = require( './../lib/main.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
2936

3037

3138
// FUNCTIONS //
@@ -38,13 +45,7 @@ var stdev = require( './../lib/stdev.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
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-
}
48+
var x = uniform( len, -10, 10, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@
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 stdev = require( './../lib/ndarray.js' );
2929

3030

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
36+
37+
3138
// FUNCTIONS //
3239

3340
/**
@@ -38,13 +45,7 @@ var stdev = require( './../lib/ndarray.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
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-
}
48+
var x = uniform( len, -10, 10, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 15 additions & 19 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.
44

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

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

33-
stride: integer
34-
Index increment.
33+
strideX: integer
34+
Stride length.
3535

3636
Returns
3737
-------
@@ -45,22 +45,19 @@
4545
> {{alias}}( x.length, 1, x, 1 )
4646
~2.0817
4747

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

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

63-
{{alias}}.ndarray( N, correction, x, stride, offset )
59+
60+
{{alias}}.ndarray( N, correction, x, strideX, offsetX )
6461
Computes the standard deviation of a strided array using alternative
6562
indexing semantics.
6663

@@ -88,10 +85,10 @@
8885
x: Array<number>|TypedArray
8986
Input array.
9087

91-
stride: integer
92-
Index increment.
88+
strideX: integer
89+
Stride length.
9390

94-
offset: integer
91+
offsetX: integer
9592
Starting index.
9693

9794
Returns
@@ -107,9 +104,8 @@
107104
~2.0817
108105

109106
// Using offset parameter:
110-
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ];
111-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
112-
> {{alias}}.ndarray( N, 1, x, 2, 1 )
107+
> x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ];
108+
> {{alias}}.ndarray( 3, 1, x, 2, 1 )
113109
~2.0817
114110

115111
See Also

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

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

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

23-
import { NumericArray } from '@stdlib/types/array';
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
2429

2530
/**
2631
* Interface describing `stdev`.
@@ -32,7 +37,7 @@ interface Routine {
3237
* @param N - number of indexed elements
3338
* @param correction - degrees of freedom adjustment
3439
* @param x - input array
35-
* @param stride - stride length
40+
* @param strideX - stride length
3641
* @returns standard deviation
3742
*
3843
* @example
@@ -41,16 +46,16 @@ interface Routine {
4146
* var v = stdev( x.length, 1, x, 1 );
4247
* // returns ~2.0817
4348
*/
44-
( N: number, correction: number, x: NumericArray, stride: number ): number;
49+
( N: number, correction: number, x: InputArray, strideX: number ): number;
4550

4651
/**
4752
* Computes the standard deviation of a strided array using alternative indexing semantics.
4853
*
4954
* @param N - number of indexed elements
5055
* @param correction - degrees of freedom adjustment
5156
* @param x - input array
52-
* @param stride - stride length
53-
* @param offset - starting index
57+
* @param strideX - stride length
58+
* @param offsetX - starting index
5459
* @returns standard deviation
5560
*
5661
* @example
@@ -59,7 +64,7 @@ interface Routine {
5964
* var v = stdev.ndarray( x.length, 1, x, 1, 0 );
6065
* // returns ~2.0817
6166
*/
62-
ndarray( N: number, correction: number, x: NumericArray, stride: number, offset: number ): number;
67+
ndarray( N: number, correction: number, x: InputArray, strideX: number, offsetX: number ): number;
6368
}
6469

6570
/**
@@ -68,7 +73,7 @@ interface Routine {
6873
* @param N - number of indexed elements
6974
* @param correction - degrees of freedom adjustment
7075
* @param x - input array
71-
* @param stride - stride length
76+
* @param strideX - stride length
7277
* @returns standard deviation
7378
*
7479
* @example

lib/node_modules/@stdlib/stats/base/stdev/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 stdev = require( './index' );
2021

2122

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

2829
stdev( x.length, 1, x, 1 ); // $ExpectType number
30+
stdev( 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 stdev = require( './index' );
101103
const x = new Float64Array( 10 );
102104

103105
stdev.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number
106+
stdev.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/stdev/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 stdev = 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 = stdev( x.length, 1, x, 1 );

lib/node_modules/@stdlib/stats/base/stdev/lib/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,16 @@
2727
* var stdev = require( '@stdlib/stats/base/stdev' );
2828
*
2929
* var x = [ 1.0, -2.0, 2.0 ];
30-
* var N = x.length;
3130
*
32-
* var v = stdev( N, 1, x, 1 );
31+
* var v = stdev( x.length, 1, x, 1 );
3332
* // returns ~2.0817
3433
*
3534
* @example
36-
* var floor = require( '@stdlib/math/base/special/floor' );
3735
* var stdev = require( '@stdlib/stats/base/stdev' );
3836
*
3937
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
40-
* var N = floor( x.length / 2 );
4138
*
42-
* var v = stdev.ndarray( N, 1, x, 2, 1 );
39+
* var v = stdev.ndarray( 4, 1, x, 2, 1 );
4340
* // returns 2.5
4441
*/
4542

0 commit comments

Comments
 (0)