Skip to content

Commit d7cc7a5

Browse files
AryanBhirudkgrytestdlib-botgururaj1512
authored
feat: add support for accessor arrays and refactor stats/base/variancepn
PR-URL: #5829 Closes: #5689 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io> Co-authored-by: Gururaj Gurram <gururajgurram1512@gmail.com>
1 parent d3ea1da commit d7cc7a5

File tree

13 files changed

+478
-150
lines changed

13 files changed

+478
-150
lines changed

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

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

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

103-
Computes the [variance][variance] of a strided array `x` using a two-pass algorithm.
103+
Computes the [variance][variance] of a strided array using a two-pass algorithm.
104104

105105
```javascript
106106
var x = [ 1.0, -2.0, 2.0 ];
107-
var N = x.length;
108107

109-
var v = variancepn( N, 1, x, 1 );
108+
var v = variancepn( x.length, 1, x, 1 );
110109
// returns ~4.3333
111110
```
112111

@@ -115,17 +114,14 @@ The function has the following parameters:
115114
- **N**: number of indexed elements.
116115
- **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. 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).
117116
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
118-
- **stride**: index increment for `x`.
117+
- **strideX**: stride length for `x`.
119118

120-
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`,
119+
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`,
121120

122121
```javascript
123-
var floor = require( '@stdlib/math/base/special/floor' );
124-
125122
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
126-
var N = floor( x.length / 2 );
127123

128-
var v = variancepn( N, 1, x, 2 );
124+
var v = variancepn( 4, 1, x, 2 );
129125
// returns 6.25
130126
```
131127

@@ -135,42 +131,35 @@ Note that indexing is relative to the first index. To introduce an offset, use [
135131

136132
```javascript
137133
var Float64Array = require( '@stdlib/array/float64' );
138-
var floor = require( '@stdlib/math/base/special/floor' );
139134

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

143-
var N = floor( x0.length / 2 );
144-
145-
var v = variancepn( N, 1, x1, 2 );
138+
var v = variancepn( 4, 1, x1, 2 );
146139
// returns 6.25
147140
```
148141

149-
#### variancepn.ndarray( N, correction, x, stride, offset )
142+
#### variancepn.ndarray( N, correction, x, strideX, offsetX )
150143

151144
Computes the [variance][variance] of a strided array using a two-pass algorithm and alternative indexing semantics.
152145

153146
```javascript
154147
var x = [ 1.0, -2.0, 2.0 ];
155-
var N = x.length;
156148

157-
var v = variancepn.ndarray( N, 1, x, 1, 0 );
149+
var v = variancepn.ndarray( x.length, 1, x, 1, 0 );
158150
// returns ~4.33333
159151
```
160152

161153
The function has the following additional parameters:
162154

163-
- **offset**: starting index for `x`.
155+
- **offsetX**: starting index for `x`.
164156

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 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 [variance][variance] for every other element in `x` starting from the second element
166158

167159
```javascript
168-
var floor = require( '@stdlib/math/base/special/floor' );
169-
170160
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
171-
var N = floor( x.length / 2 );
172161

173-
var v = variancepn.ndarray( N, 1, x, 2, 1 );
162+
var v = variancepn.ndarray( 4, 1, x, 2, 1 );
174163
// returns 6.25
175164
```
176165

@@ -184,6 +173,7 @@ var v = variancepn.ndarray( N, 1, x, 2, 1 );
184173

185174
- If `N <= 0`, both functions return `NaN`.
186175
- 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]).
187177
- Depending on the environment, the typed versions ([`dvariancepn`][@stdlib/stats/strided/dvariancepn], [`svariancepn`][@stdlib/stats/strided/svariancepn], etc.) are likely to be significantly more performant.
188178

189179
</section>
@@ -197,18 +187,12 @@ var v = variancepn.ndarray( N, 1, x, 2, 1 );
197187
<!-- eslint no-undef: "error" -->
198188

199189
```javascript
200-
var randu = require( '@stdlib/random/base/randu' );
201-
var round = require( '@stdlib/math/base/special/round' );
202-
var Float64Array = require( '@stdlib/array/float64' );
190+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
203191
var variancepn = require( '@stdlib/stats/base/variancepn' );
204192

205-
var x;
206-
var i;
207-
208-
x = new Float64Array( 10 );
209-
for ( i = 0; i < x.length; i++ ) {
210-
x[ i ] = round( (randu()*100.0) - 50.0 );
211-
}
193+
var x = discreteUniform( 10, -50, 50, {
194+
'dtype': 'float64'
195+
});
212196
console.log( x );
213197

214198
var v = variancepn( x.length, 1, x, 1 );
@@ -265,6 +249,8 @@ console.log( v );
265249

266250
[@schubert:2018a]: https://doi.org/10.1145/3221269.3223036
267251

252+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
253+
268254
<!-- <related-links> -->
269255

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

lib/node_modules/@stdlib/stats/base/variancepn/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 variancepn = require( './../lib/variancepn.js' );
28+
var variancepn = 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 variancepn = require( './../lib/variancepn.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/variancepn/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 variancepn = 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 variancepn = 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/variancepn/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 variance of a strided array using a two-pass algorithm.
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
~4.3333
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
~4.3333
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
~4.3333
6258

63-
{{alias}}.ndarray( N, correction, x, stride, offset )
59+
60+
{{alias}}.ndarray( N, correction, x, strideX, offsetX )
6461
Computes the variance of a strided array using a two-pass algorithm and
6562
alternative 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
~4.3333
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
~4.3333
114110

115111
See Also

lib/node_modules/@stdlib/stats/base/variancepn/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 `variancepn`.
@@ -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 variance
3742
*
3843
* @example
@@ -41,16 +46,16 @@ interface Routine {
4146
* var v = variancepn( x.length, 1, x, 1 );
4247
* // returns ~4.3333
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 variance of a strided array using a two-pass algorithm and 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 variance
5560
*
5661
* @example
@@ -59,7 +64,7 @@ interface Routine {
5964
* var v = variancepn.ndarray( x.length, 1, x, 1, 0 );
6065
* // returns ~4.3333
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 variance
7378
*
7479
* @example

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

2122

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

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

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

0 commit comments

Comments
 (0)