Skip to content

Commit fab8c0e

Browse files
pulkitgupta2kgrytestdlib-bot
authored
feat: add accessor protocol support to stats/base/varianceyc
PR-URL: #5891 Closes: #5692 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 7cd25c6 commit fab8c0e

File tree

14 files changed

+782
-428
lines changed

14 files changed

+782
-428
lines changed

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

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

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

103-
Computes the [variance][variance] of a strided array `x` using a one-pass algorithm proposed by Youngs and Cramer.
103+
Computes the [variance][variance] of a strided array using a one-pass algorithm proposed by Youngs and Cramer.
104104

105105
```javascript
106106
var x = [ 1.0, -2.0, 2.0 ];
107107

108-
var v = varianceyc( x.length, 1, x, 1 );
108+
var v = varianceyc( x.length, 1.0, x, 1 );
109109
// returns ~4.3333
110110
```
111111

@@ -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 [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).
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 [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`,
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 = varianceyc( N, 1, x, 2 );
124+
var v = varianceyc( 4, 1.0, x, 2 );
128125
// returns 6.25
129126
```
130127

@@ -134,41 +131,35 @@ 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 = varianceyc( N, 1, x1, 2 );
138+
var v = varianceyc( 4, 1.0, x1, 2 );
145139
// returns 6.25
146140
```
147141

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

150144
Computes the [variance][variance] of a strided array using a one-pass algorithm proposed by Youngs and Cramer and alternative indexing semantics.
151145

152146
```javascript
153147
var x = [ 1.0, -2.0, 2.0 ];
154148

155-
var v = varianceyc.ndarray( x.length, 1, x, 1, 0 );
149+
var v = varianceyc.ndarray( x.length, 1.0, x, 1, 0 );
156150
// returns ~4.33333
157151
```
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 [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
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 = varianceyc.ndarray( N, 1, x, 2, 1 );
162+
var v = varianceyc.ndarray( 4, 1.0, x, 2, 1 );
172163
// returns 6.25
173164
```
174165

@@ -183,6 +174,7 @@ var v = varianceyc.ndarray( N, 1, x, 2, 1 );
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`.
185176
- Depending on the environment, the typed versions ([`dvarianceyc`][@stdlib/stats/strided/dvarianceyc], [`svarianceyc`][@stdlib/stats/strided/svarianceyc], etc.) are likely to be significantly more performant.
177+
- 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]).
186178

187179
</section>
188180

@@ -195,21 +187,15 @@ var v = varianceyc.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 uniform = require( '@stdlib/random/array/uniform' );
201191
var varianceyc = require( '@stdlib/stats/base/varianceyc' );
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 = uniform( 10, -50.0, 50.0, {
194+
'dtype': 'generic'
195+
});
210196
console.log( x );
211197

212-
var v = varianceyc( x.length, 1, x, 1 );
198+
var v = varianceyc( x.length, 1.0, x, 1 );
213199
console.log( v );
214200
```
215201

@@ -256,6 +242,8 @@ console.log( v );
256242

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

245+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
246+
259247
[@stdlib/stats/strided/svarianceyc]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/svarianceyc
260248

261249
[@youngs:1971a]: https://doi.org/10.1080/00401706.1971.10488826

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

Lines changed: 11 additions & 10 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' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var pkg = require( './../package.json' ).name;
28-
var varianceyc = require( './../lib/varianceyc.js' );
28+
var varianceyc = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
2936

3037

3138
// FUNCTIONS //
@@ -38,13 +45,7 @@ var varianceyc = require( './../lib/varianceyc.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.0, 10.0, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {
@@ -53,7 +54,7 @@ function createBenchmark( len ) {
5354

5455
b.tic();
5556
for ( i = 0; i < b.iterations; i++ ) {
56-
v = varianceyc( x.length, 1, x, 1 );
57+
v = varianceyc( x.length, 1.0, x, 1 );
5758
if ( isnan( v ) ) {
5859
b.fail( 'should not return NaN' );
5960
}

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

Lines changed: 10 additions & 9 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 varianceyc = 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 varianceyc = 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.0, 10.0, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {
@@ -53,7 +54,7 @@ function createBenchmark( len ) {
5354

5455
b.tic();
5556
for ( i = 0; i < b.iterations; i++ ) {
56-
v = varianceyc( x.length, 1, x, 1, 0 );
57+
v = varianceyc( x.length, 1.0, x, 1, 0 );
5758
if ( isnan( v ) ) {
5859
b.fail( 'should not return NaN' );
5960
}

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

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

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

99
Indexing is relative to the first index. To introduce an offset, use a typed
1010
array view.
@@ -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 for `x`.
3636

3737
Returns
3838
-------
@@ -43,30 +43,27 @@
4343
--------
4444
// Standard Usage:
4545
> var x = [ 1.0, -2.0, 2.0 ];
46-
> {{alias}}( x.length, 1, x, 1 )
46+
> {{alias}}( x.length, 1.0, x, 1 )
4747
~4.3333
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.0, x, 2 )
5452
~4.3333
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.0, x1, 2 )
6258
~4.3333
6359

64-
{{alias}}.ndarray( N, correction, x, stride, offset )
60+
61+
{{alias}}.ndarray( N, correction, x, strideX, offsetX )
6562
Computes the variance of a strided array using a one-pass algorithm proposed
6663
by Youngs and Cramer and alternative indexing semantics.
6764

6865
While typed array views mandate a view offset based on the underlying
69-
buffer, the `offset` parameter supports indexing semantics based on a
66+
buffer, the offset parameter supports indexing semantics based on a
7067
starting index.
7168

7269
Parameters
@@ -89,11 +86,11 @@
8986
x: Array<number>|TypedArray
9087
Input array.
9188

92-
stride: integer
93-
Index increment.
89+
strideX: integer
90+
Stride length for `x`.
9491

95-
offset: integer
96-
Starting index.
92+
offsetX: integer
93+
Starting index for `x`.
9794

9895
Returns
9996
-------
@@ -104,13 +101,12 @@
104101
--------
105102
// Standard Usage:
106103
> var x = [ 1.0, -2.0, 2.0 ];
107-
> {{alias}}.ndarray( x.length, 1, x, 1, 0 )
104+
> {{alias}}.ndarray( x.length, 1.0, x, 1, 0 )
108105
~4.3333
109106

110107
// Using offset parameter:
111108
> 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 )
109+
> {{alias}}.ndarray( 3, 1.0, x, 2, 1 )
114110
~4.3333
115111

116112
See Also

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

Lines changed: 16 additions & 11 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 `varianceyc`.
@@ -32,34 +37,34 @@ 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 for `x`
3641
* @returns variance
3742
*
3843
* @example
3944
* var x = [ 1.0, -2.0, 2.0 ];
4045
*
41-
* var v = varianceyc( x.length, 1, x, 1 );
46+
* var v = varianceyc( x.length, 1.0, 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 one-pass algorithm proposed by Youngs and Cramer 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 for `x`
58+
* @param offsetX - starting index for `x`
5459
* @returns variance
5560
*
5661
* @example
5762
* var x = [ 1.0, -2.0, 2.0 ];
5863
*
59-
* var v = varianceyc.ndarray( x.length, 1, x, 1, 0 );
64+
* var v = varianceyc.ndarray( x.length, 1.0, 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,19 +73,19 @@ 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 for `x`
7277
* @returns variance
7378
*
7479
* @example
7580
* var x = [ 1.0, -2.0, 2.0 ];
7681
*
77-
* var v = varianceyc( x.length, 1, x, 1 );
82+
* var v = varianceyc( x.length, 1.0, x, 1 );
7883
* // returns ~4.3333
7984
*
8085
* @example
8186
* var x = [ 1.0, -2.0, 2.0 ];
8287
*
83-
* var v = varianceyc.ndarray( x.length, 1, x, 1, 0 );
88+
* var v = varianceyc.ndarray( x.length, 1.0, x, 1, 0 );
8489
* // returns ~4.3333
8590
*/
8691
declare var varianceyc: Routine;

0 commit comments

Comments
 (0)