Skip to content

Commit 877c47f

Browse files
feat: refactor and add protocol support to stats/base/variancepn
- Adds accessor arrays support. - Refactors `stats/base/variancepn`. Does this pull request have any related issues? - No Any questions for reviewers of this pull request? - No. Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes. - No. Please ensure the following tasks are completed before submitting this pull request. - [ ] Read, understood, and followed the contributing guidelines. --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent aec0e88 commit 877c47f

File tree

15 files changed

+442
-167
lines changed

15 files changed

+442
-167
lines changed

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

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2020 The Stdlib Authors.
5+
Copyright (c) 2025 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -88,8 +88,10 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note,
8888

8989
</section>
9090

91+
9192
<!-- /.intro -->
9293

94+
9395
<section class="usage">
9496

9597
## Usage
@@ -98,7 +100,7 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note,
98100
var variancepn = require( '@stdlib/stats/base/variancepn' );
99101
```
100102

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

103105
Computes the [variance][variance] of a strided array `x` using a two-pass algorithm.
104106

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

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`,
122+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the [variance][variance] of every other element in `x`,
121123

122124
```javascript
123-
var floor = require( '@stdlib/math/base/special/floor' );
124-
125125
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 );
127126

128-
var v = variancepn( N, 1, x, 2 );
127+
var v = variancepn( 4, 1, x, 2 );
129128
// returns 6.25
130129
```
131130

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

136135
```javascript
137136
var Float64Array = require( '@stdlib/array/float64' );
138-
var floor = require( '@stdlib/math/base/special/floor' );
139137

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

143-
var N = floor( x0.length / 2 );
144-
145-
var v = variancepn( N, 1, x1, 2 );
141+
var v = variancepn( 4, 1, x1, 2 );
146142
// returns 6.25
147143
```
148144

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

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

@@ -160,55 +156,52 @@ var v = variancepn.ndarray( N, 1, x, 1, 0 );
160156

161157
The function has the following additional parameters:
162158

163-
- **offset**: starting index for `x`.
159+
- **offsetX**: starting index for `x`.
164160

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
161+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer , the offset parameters support indexing semantics based on starting indices. For example, to calculate the [variance][variance] for every other value in `x` starting from the second value
166162

167163
```javascript
168-
var floor = require( '@stdlib/math/base/special/floor' );
169-
170164
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 );
172165

173-
var v = variancepn.ndarray( N, 1, x, 2, 1 );
166+
var v = variancepn.ndarray( 4, 1, x, 2, 1 );
174167
// returns 6.25
175168
```
176169

177170
</section>
178171

172+
179173
<!-- /.usage -->
180174

175+
181176
<section class="notes">
182177

183178
## Notes
184179

185180
- If `N <= 0`, both functions return `NaN`.
186181
- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`.
187182
- Depending on the environment, the typed versions ([`dvariancepn`][@stdlib/stats/base/dvariancepn], [`svariancepn`][@stdlib/stats/base/svariancepn], etc.) are likely to be significantly more performant.
183+
- 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]).
188184

189185
</section>
190186

187+
191188
<!-- /.notes -->
192189

190+
193191
<section class="examples">
194192

195193
## Examples
196194

197195
<!-- eslint no-undef: "error" -->
198196

199197
```javascript
200-
var randu = require( '@stdlib/random/base/randu' );
201-
var round = require( '@stdlib/math/base/special/round' );
198+
var uniform = require( '@stdlib/random/array/uniform' );
202199
var Float64Array = require( '@stdlib/array/float64' );
203200
var variancepn = require( '@stdlib/stats/base/variancepn' );
204201

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-
}
202+
var x = uniform( 10, -50.0, 50.0, {
203+
'dtype': 'float64'
204+
});
212205
console.log( x );
213206

214207
var v = variancepn( x.length, 1, x, 1 );
@@ -217,6 +210,7 @@ console.log( v );
217210

218211
</section>
219212

213+
220214
<!-- /.examples -->
221215

222216
* * *
@@ -230,6 +224,7 @@ console.log( v );
230224

231225
</section>
232226

227+
233228
<!-- /.references -->
234229

235230
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
@@ -247,16 +242,21 @@ console.log( v );
247242

248243
</section>
249244

245+
250246
<!-- /.related -->
251247

252248
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
253249

250+
254251
<section class="links">
255252

253+
256254
[variance]: https://en.wikipedia.org/wiki/Variance
257255

258256
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
259257

258+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
259+
260260
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
261261

262262
[@stdlib/stats/base/svariancepn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/svariancepn
@@ -277,6 +277,8 @@ console.log( v );
277277

278278
<!-- </related-links> -->
279279

280+
280281
</section>
281282

283+
282284
<!-- /.links -->

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -21,13 +21,20 @@
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' );
2625
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
2727
var pkg = require( './../package.json' ).name;
2828
var variancepn = require( './../lib/variancepn.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/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.0, 10.0, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -21,13 +21,20 @@
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;
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.0, 10.0, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
{{alias}}( N, correction, x, stride )
3-
Computes the variance of a strided array using a two-pass algorithm.
2+
{{alias}}( N, correction, x, strideX )
3+
Computes the variance of a strided array (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
6+
strided arrays are accessed at runtime.
77

8-
Indexing is relative to the first index. To introduce an offset, use a typed
9-
array view.
8+
Indexing is relative to the first index. To introduce
9+
an offset, use a typed array view.
1010

1111
If `N <= 0`, the function returns `NaN`.
1212

@@ -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
-------
@@ -43,24 +43,23 @@
4343
// Standard Usage:
4444
> var x = [ 1.0, -2.0, 2.0 ];
4545
> {{alias}}( x.length, 1, x, 1 )
46-
~4.3333
46+
4.333333333333334
4747

48-
// Using `N` and `stride` parameters:
48+
// Using `N` and `strideX` 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 )
53-
~4.3333
50+
> var strideX = 2;
51+
> {{alias}}( 4, 1, x, strideX )
52+
NaN
5453

5554
// Using view offsets:
5655
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
5756
> 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 )
61-
~4.3333
57+
> strideX = 2;
58+
> {{alias}}( 4, 1, x1, strideX )
59+
NaN
60+
6261

63-
{{alias}}.ndarray( N, correction, x, stride, offset )
62+
{{alias}}.ndarray( N, correction, x, strideX, offset )
6463
Computes the variance of a strided array using a two-pass algorithm and
6564
alternative indexing semantics.
6665

@@ -88,8 +87,8 @@
8887
x: Array<number>|TypedArray
8988
Input array.
9089

91-
stride: integer
92-
Index increment.
90+
strideX: integer
91+
Stride length.
9392

9493
offset: integer
9594
Starting index.
@@ -108,10 +107,9 @@
108107

109108
// Using offset parameter:
110109
> 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 )
113-
~4.3333
110+
> {{alias}}.ndarray( 4, 1, x, 2, 1 )
111+
NaN
112+
114113

115114
See Also
116115
--------
117-

0 commit comments

Comments
 (0)