Skip to content

Commit e276a3e

Browse files
committed
refactor: add protocol support to stats/base/variancewd
1 parent 0622cf1 commit e276a3e

File tree

11 files changed

+258
-325
lines changed

11 files changed

+258
-325
lines changed

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

Lines changed: 10 additions & 11 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 variancewd = require( '@stdlib/stats/base/variancewd' );
9999
```
100100

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

103103
Computes the [variance][variance] of a strided array `x` using Welford's algorithm.
104104

@@ -114,9 +114,9 @@ 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 `strideX` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [variance][variance] of every other element in `x`,
120120

121121
```javascript
122122
var floor = require( '@stdlib/math/base/special/floor' );
@@ -145,7 +145,7 @@ var v = variancewd( N, 1, x1, 2 );
145145
// returns 6.25
146146
```
147147

148-
#### variancewd.ndarray( N, correction, x, stride, offset )
148+
#### variancewd.ndarray( N, correction, x, strideX, offset )
149149

150150
Computes the [variance][variance] of a strided array using Welford's algorithm and alternative indexing semantics.
151151

@@ -195,18 +195,15 @@ var v = variancewd.ndarray( N, 1, x, 2, 1 );
195195
<!-- eslint no-undef: "error" -->
196196

197197
```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' );
198+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
201199
var variancewd = require( '@stdlib/stats/base/variancewd' );
202200

203201
var x;
204202
var i;
205203

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

212209
var v = variancewd( x.length, 1, x, 1 );
@@ -267,6 +264,8 @@ console.log( v );
267264

268265
[@stdlib/stats/base/dvariancewd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dvariancewd
269266

267+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
268+
270269
[@stdlib/stats/base/nanvariancewd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanvariancewd
271270

272271
[@stdlib/stats/base/stdevwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/stdevwd

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
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 variancewd = require( './../lib/variancewd.js' );
2929

30+
// VARIABLES //
31+
32+
var options = {
33+
'dtype': 'generic'
34+
}; // FIXED: Added `options` variable
3035

3136
// FUNCTIONS //
3237

@@ -41,10 +46,8 @@ function createBenchmark( len ) {
4146
var x;
4247
var i;
4348

44-
x = [];
45-
for ( i = 0; i < len; i++ ) {
46-
x.push( ( randu()*20.0 ) - 10.0 );
47-
}
49+
x = uniform( len, -10.0, 10.0, options ); // FIXED: Added `options`
50+
4851
return benchmark;
4952

5053
function benchmark( b ) {

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

Lines changed: 8 additions & 5 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 variancewd = require( './../lib/ndarray.js' );
2929

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

3137
// FUNCTIONS //
3238

@@ -41,10 +47,7 @@ function createBenchmark( len ) {
4147
var x;
4248
var i;
4349

44-
x = [];
45-
for ( i = 0; i < len; i++ ) {
46-
x.push( ( randu()*20.0 ) - 10.0 );
47-
}
50+
x = uniform( len, -10.0, 10.0, options ); // FIXED: Added `options`
4851
return benchmark;
4952

5053
function benchmark( b ) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface Routine {
3232
* @param N - number of indexed elements
3333
* @param correction - degrees of freedom adjustment
3434
* @param x - input array
35-
* @param stride - stride length
35+
* @param strideX - stride length
3636
* @returns variance
3737
*
3838
* @example
@@ -41,16 +41,16 @@ interface Routine {
4141
* var v = variancewd( x.length, 1, x, 1 );
4242
* // returns ~4.3333
4343
*/
44-
( N: number, correction: number, x: NumericArray, stride: number ): number;
44+
( N: number, correction: number, x: NumericArray, strideX: number ): number;
4545

4646
/**
4747
* Computes the variance of a strided array using Welford's algorithm and alternative indexing semantics.
4848
*
4949
* @param N - number of indexed elements
5050
* @param correction - degrees of freedom adjustment
5151
* @param x - input array
52-
* @param stride - stride length
53-
* @param offset - starting index
52+
* @param strideX - stride length
53+
* @param offsetX - starting index
5454
* @returns variance
5555
*
5656
* @example
@@ -59,7 +59,7 @@ interface Routine {
5959
* var v = variancewd.ndarray( x.length, 1, x, 1, 0 );
6060
* // returns ~4.3333
6161
*/
62-
ndarray( N: number, correction: number, x: NumericArray, stride: number, offset: number ): number;
62+
ndarray( N: number, correction: number, x: NumericArray, strideX: number, offsetX: number ): number;
6363
}
6464

6565
/**
@@ -68,7 +68,7 @@ interface Routine {
6868
* @param N - number of indexed elements
6969
* @param correction - degrees of freedom adjustment
7070
* @param x - input array
71-
* @param stride - stride length
71+
* @param strideX - stride length
7272
* @returns variance
7373
*
7474
* @example

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@
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 variancewd = require( './../lib' );
2523

26-
var x;
27-
var i;
24+
var x = discreteUniform( 10, -50, 50, {
25+
'dtype': 'float64'
26+
});
2827

29-
x = new Float64Array( 10 );
30-
for ( i = 0; i < x.length; i++ ) {
31-
x[ i ] = round( (randu()*100.0) - 50.0 );
32-
}
3328
console.log( x );
3429

3530
var v = variancewd( x.length, 1, x, 1 );
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* Computes the variance of a strided array using Welford's algorithm and accessors.
21+
*
22+
* @private
23+
* @param {PositiveInteger} N - number of indexed elements
24+
* @param {number} correction - degrees of freedom adjustment
25+
* @param {Object} x - input array object
26+
* @param {Collection} x.data - input array data
27+
* @param {Array<Function>} x.accessors - array element accessors
28+
* @param {integer} strideX - stride length
29+
* @param {NonNegativeInteger} offsetX - starting index
30+
* @returns {number} variance
31+
*/
32+
function variancewd(N, correction, x, strideX, offsetX) {
33+
var xbuf;
34+
var get;
35+
var delta;
36+
var delta2;
37+
var mu;
38+
var M2;
39+
var ix;
40+
var v;
41+
var n;
42+
var i;
43+
44+
n = N - correction;
45+
if (N <= 0 || n <= 0.0) {
46+
return NaN;
47+
}
48+
if (N === 1 || strideX === 0) {
49+
return 0.0;
50+
}
51+
52+
xbuf = x.data;
53+
get = x.accessors[0];
54+
55+
ix = offsetX;
56+
M2 = 0.0;
57+
mu = 0.0;
58+
59+
for (i = 0; i < N; i++) {
60+
v = get(xbuf, ix);
61+
delta = v - mu;
62+
mu += delta / (i + 1);
63+
delta2 = v - mu;
64+
M2 += delta * delta2;
65+
ix += strideX;
66+
}
67+
68+
return M2 / n;
69+
}
70+
71+
module.exports = variancewd;

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@
4545

4646
// MODULES //
4747

48-
var main = require( './main.js' );
48+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
49+
var variancewd = require( './variancewd.js' );
50+
var ndarray = require( './ndarray.js' );
4951

52+
// MAIN //
53+
setReadOnly( variancewd, 'ndarray', ndarray );
5054

5155
// EXPORTS //
52-
53-
module.exports = main;
54-
55-
// exports: { "ndarray": "main.ndarray" }
56+
module.exports = variancewd;

lib/node_modules/@stdlib/stats/base/variancewd/lib/ndarray.js

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
23+
var accessors = require( './accessors.js' );
24+
2125
// MAIN //
2226

2327
/**
@@ -62,49 +66,60 @@
6266
* - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022).
6367
* - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961).
6468
*
69+
/**
70+
* Computes the variance of a strided array using Welford's algorithm.
71+
*
6572
* @param {PositiveInteger} N - number of indexed elements
6673
* @param {number} correction - degrees of freedom adjustment
6774
* @param {NumericArray} x - input array
68-
* @param {integer} stride - stride length
69-
* @param {NonNegativeInteger} offset - starting index
75+
* @param {integer} strideX - stride length
76+
* @param {NonNegativeInteger} offsetX - starting index
7077
* @returns {number} variance
71-
*
72-
* @example
73-
* var floor = require( '@stdlib/math/base/special/floor' );
74-
*
75-
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
76-
* var N = floor( x.length / 2 );
77-
*
78-
* var v = variancewd( N, 1, x, 2, 1 );
79-
* // returns 6.25
8078
*/
81-
function variancewd( N, correction, x, stride, offset ) {
82-
var delta;
83-
var mu;
84-
var M2;
85-
var ix;
86-
var v;
87-
var n;
88-
var i;
79+
function variancewd(N, correction, x, strideX, offsetX) {
80+
var delta;
81+
var delta2;
82+
var mu;
83+
var M2;
84+
var ix;
85+
var v;
86+
var n;
87+
var o;
88+
var i;
89+
90+
if (N <= 0) {
91+
return NaN;
92+
}
93+
if (N === 1 || strideX === 0) {
94+
if (N - correction <= 0) {
95+
return NaN;
96+
}
97+
return 0.0;
98+
}
99+
n = N - correction;
100+
if (n <= 0) {
101+
return NaN;
102+
}
103+
104+
o = arraylike2object(x);
105+
if (o.accessorProtocol) {
106+
return accessors(N, correction, o, strideX, offsetX);
107+
}
108+
109+
ix = offsetX;
110+
M2 = 0.0;
111+
mu = 0.0;
112+
113+
for (i = 0; i < N; i++) {
114+
v = o.data[ix];
115+
delta = v - mu;
116+
mu += delta / (i + 1);
117+
delta2 = v - mu; // Calculate second delta after mean is updated
118+
M2 += delta * delta2;
119+
ix += strideX;
120+
}
89121

90-
n = N - correction;
91-
if ( N <= 0 || n <= 0.0 ) {
92-
return NaN;
93-
}
94-
if ( N === 1 || stride === 0 ) {
95-
return 0.0;
96-
}
97-
ix = offset;
98-
M2 = 0.0;
99-
mu = 0.0;
100-
for ( i = 0; i < N; i++ ) {
101-
v = x[ ix ];
102-
delta = v - mu;
103-
mu += delta / (i+1);
104-
M2 += delta * ( v - mu );
105-
ix += stride;
106-
}
107-
return M2 / n;
122+
return M2 / n;
108123
}
109124

110125

0 commit comments

Comments
 (0)