Skip to content

Commit 3289245

Browse files
committed
added accessor.js, changed related files and added tests
Signed-off-by: the_deeppp <[email protected]>
1 parent 4748a5a commit 3289245

File tree

5 files changed

+235
-9
lines changed

5 files changed

+235
-9
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
'use strict';
20+
21+
// MAIN //
22+
23+
/**
24+
* Computes the variance of a strided array using a one-pass trial mean algorithm.
25+
*
26+
* ## Method
27+
*
28+
* - This implementation uses a one-pass trial mean approach, as suggested by Chan et al (1983).
29+
*
30+
* ## References
31+
*
32+
* - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958).
33+
* - Ling, Robert F. 1974. "Comparison of Several Algorithms for Computing Sample Means and Variances." _Journal of the American Statistical Association_ 69 (348). American Statistical Association, Taylor & Francis, Ltd.: 859–66. doi:[10.2307/2286154](https://doi.org/10.2307/2286154).
34+
* - Chan, Tony F., Gene H. Golub, and Randall J. LeVeque. 1983. "Algorithms for Computing the Sample Variance: Analysis and Recommendations." _The American Statistician_ 37 (3). American Statistical Association, Taylor & Francis, Ltd.: 242–47. doi:[10.1080/00031305.1983.10483115](https://doi.org/10.1080/00031305.1983.10483115).
35+
* - Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036](https://doi.org/10.1145/3221269.3223036).
36+
*
37+
* @param {PositiveInteger} N - number of indexed elements
38+
* @param {number} correction - degrees of freedom adjustment
39+
* @param {Object} x - input array object
40+
* @param {Function} x.get - accessor function
41+
* @param {integer} stride - stride length
42+
* @param {NonNegativeInteger} offset - starting index
43+
* @returns {number} variance
44+
*
45+
* @example
46+
* var x = {
47+
* 'get': function get( i ) {
48+
* return [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ][ i ];
49+
* }
50+
* };
51+
* var N = 4;
52+
* var stride = 2;
53+
* var offset = 1;
54+
*
55+
* var v = variancech( N, 1, x, stride, offset );
56+
* // returns 6.25
57+
*/
58+
function variancech( N, correction, x, stride, offset ) {
59+
var mu;
60+
var ix;
61+
var M2;
62+
var M;
63+
var d;
64+
var n;
65+
var i;
66+
67+
n = N - correction;
68+
if ( N <= 0 || n <= 0.0 ) {
69+
return NaN;
70+
}
71+
if ( N === 1 || stride === 0 ) {
72+
return 0.0;
73+
}
74+
ix = offset;
75+
76+
// Use an estimate for the mean:
77+
mu = x.get( ix );
78+
ix += stride;
79+
80+
// Compute the variance...
81+
M2 = 0.0;
82+
M = 0.0;
83+
for ( i = 1; i < N; i++ ) {
84+
d = x.get( ix ) - mu;
85+
M2 += d * d;
86+
M += d;
87+
ix += stride;
88+
}
89+
return (M2/n) - ((M/N)*(M/n));
90+
}
91+
92+
// EXPORTS //
93+
94+
module.exports = variancech;

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,45 @@
2424
* @module @stdlib/stats/base/variancech
2525
*
2626
* @example
27+
* var Float64Array = require( '@stdlib/array/float64' );
2728
* var variancech = require( '@stdlib/stats/base/variancech' );
2829
*
29-
* var x = [ 1.0, -2.0, 2.0 ];
30-
* var N = x.length;
30+
* var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
3131
*
32-
* var v = variancech( N, 1, x, 1 );
32+
* var v = variancech( x.length, 1, x, 1 );
3333
* // returns ~4.3333
3434
*
3535
* @example
36+
* var Float64Array = require( '@stdlib/array/float64' );
3637
* var floor = require( '@stdlib/math/base/special/floor' );
3738
* var variancech = require( '@stdlib/stats/base/variancech' );
3839
*
39-
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
40+
* var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
4041
* var N = floor( x.length / 2 );
4142
*
4243
* var v = variancech.ndarray( N, 1, x, 2, 1 );
4344
* // returns 6.25
45+
*
46+
* @example
47+
* var variancech = require( '@stdlib/stats/base/variancech' );
48+
*
49+
* var x = {
50+
* 'get': function get( i ) {
51+
* return [ 1.0, -2.0, 2.0 ][ i ];
52+
* }
53+
* };
54+
*
55+
* var v = variancech( 3, 1, x, 1 );
56+
* // returns ~4.3333
4457
*/
4558

4659
// MODULES //
4760

61+
var ndarray = require( './ndarray.js' );
4862
var main = require( './main.js' );
4963

5064

5165
// EXPORTS //
5266

5367
module.exports = main;
54-
55-
// exports: { "ndarray": "main.ndarray" }
68+
module.exports.ndarray = ndarray;

lib/node_modules/@stdlib/stats/base/variancech/lib/main.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,32 @@
2020

2121
// MODULES //
2222

23-
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
24-
var variancech = require( './variancech.js' );
2523
var ndarray = require( './ndarray.js' );
2624

2725

2826
// MAIN //
2927

30-
setReadOnly( variancech, 'ndarray', ndarray );
28+
/**
29+
* Computes the variance of a strided array using a one-pass trial mean algorithm.
30+
*
31+
* @param {PositiveInteger} N - number of indexed elements
32+
* @param {number} correction - degrees of freedom adjustment
33+
* @param {NumericArray} x - input array
34+
* @param {integer} stride - stride length
35+
* @returns {number} variance
36+
*
37+
* @example
38+
* var x = [ 1.0, -2.0, 2.0 ];
39+
*
40+
* var v = variancech( x.length, 1, x, 1 );
41+
* // returns ~4.3333
42+
*/
43+
function variancech( N, correction, x, stride ) {
44+
return ndarray( N, correction, x, stride, 0 );
45+
}
3146

3247

3348
// EXPORTS //
3449

3550
module.exports = variancech;
51+
module.exports.ndarray = ndarray;

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

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

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var accessors = require( './accessors.js' );
24+
25+
2126
// MAIN //
2227

2328
/**
@@ -59,6 +64,11 @@ function variancech( N, correction, x, stride, offset ) {
5964
var n;
6065
var i;
6166

67+
// Check for accessor array:
68+
if ( x.get ) {
69+
return accessors( N, correction, x, stride, offset );
70+
}
71+
6272
n = N - correction;
6373
if ( N <= 0 || n <= 0.0 ) {
6474
return NaN;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
'use strict';
20+
21+
// MODULES //
22+
23+
var tape = require( 'tape' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var variancech = require( './../lib' );
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.strictEqual( typeof variancech, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'the function supports accessor arrays', function test( t ) {
38+
var x;
39+
var v;
40+
41+
x = {
42+
'get': function get( i ) {
43+
return [ 1.0, -2.0, 2.0 ][ i ];
44+
}
45+
};
46+
v = variancech( 3, 1, x, 1 );
47+
t.equal( v, 4.333333333333333, 'returns expected value' );
48+
t.end();
49+
});
50+
51+
tape( 'the function supports accessor arrays using ndarray interface', function test( t ) {
52+
var x;
53+
var v;
54+
55+
x = {
56+
'get': function get( i ) {
57+
return [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ][ i ];
58+
}
59+
};
60+
v = variancech.ndarray( 4, 1, x, 2, 1 );
61+
t.equal( v, 6.25, 'returns expected value' );
62+
t.end();
63+
});
64+
65+
tape( 'if provided an array of length <= 0, the function returns NaN', function test( t ) {
66+
var x;
67+
var v;
68+
69+
x = {
70+
'get': function get( i ) {
71+
return [ 1.0, -2.0, 2.0 ][ i ];
72+
}
73+
};
74+
75+
v = variancech( 0, 1, x, 1 );
76+
t.equal( isnan( v ), true, 'returns expected value' );
77+
t.end();
78+
});
79+
80+
tape( 'if provided a stride of 0, the function returns 0', function test( t ) {
81+
var x;
82+
var v;
83+
84+
x = {
85+
'get': function get( i ) {
86+
return [ 1.0, -2.0, 2.0 ][ i ];
87+
}
88+
};
89+
90+
v = variancech( 3, 1, x, 0 );
91+
t.equal( v, 0.0, 'returns expected value' );
92+
t.end();
93+
});

0 commit comments

Comments
 (0)