Skip to content

feat: refactor and add protocol support to stats/base/variancepn #5854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions lib/node_modules/@stdlib/stats/base/variancepn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2020 The Stdlib Authors.
Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -88,8 +88,10 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note,

</section>


<!-- /.intro -->


<section class="usage">

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

#### variancepn( N, correction, x, stride )
#### variancepn( N, correction, x, strideX )

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

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

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`,
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`,

```javascript
var floor = require( '@stdlib/math/base/special/floor' );

var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
var N = floor( x.length / 2 );

var v = variancepn( N, 1, x, 2 );
var v = variancepn( 4, 1, x, 2 );
// returns 6.25
```

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

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

var N = floor( x0.length / 2 );

var v = variancepn( N, 1, x1, 2 );
var v = variancepn( 4, 1, x1, 2 );
// returns 6.25
```

#### variancepn.ndarray( N, correction, x, stride, offset )
#### variancepn.ndarray( N, correction, x, strideX, offsetX )

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

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

The function has the following additional parameters:

- **offset**: starting index for `x`.
- **offsetX**: starting index for `x`.

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
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

```javascript
var floor = require( '@stdlib/math/base/special/floor' );

var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
var N = floor( x.length / 2 );

var v = variancepn.ndarray( N, 1, x, 2, 1 );
var v = variancepn.ndarray( 4, 1, x, 2, 1 );
// returns 6.25
```

</section>


<!-- /.usage -->


<section class="notes">

## Notes

- If `N <= 0`, both functions return `NaN`.
- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`.
- 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.
- 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]).

</section>


<!-- /.notes -->


<section class="examples">

## Examples

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

```javascript
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var uniform = require( '@stdlib/random/array/uniform' );
var Float64Array = require( '@stdlib/array/float64' );
var variancepn = require( '@stdlib/stats/base/variancepn' );

var x;
var i;

x = new Float64Array( 10 );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = round( (randu()*100.0) - 50.0 );
}
var x = uniform( 10, -50.0, 50.0, {
'dtype': 'float64'
});
console.log( x );

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

</section>


<!-- /.examples -->

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

</section>


<!-- /.references -->

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

</section>


<!-- /.related -->

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


<section class="links">


[variance]: https://en.wikipedia.org/wiki/Variance

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

[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor

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

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

<!-- </related-links> -->


</section>


<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,13 +21,20 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var uniform = require( '@stdlib/random/array/uniform' );
var pkg = require( './../package.json' ).name;
var variancepn = require( './../lib/variancepn.js' );


// VARIABLES //

var options = {
'dtype': 'generic'
};


// FUNCTIONS //

/**
Expand All @@ -38,13 +45,7 @@ var variancepn = require( './../lib/variancepn.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = [];
for ( i = 0; i < len; i++ ) {
x.push( ( randu()*20.0 ) - 10.0 );
}
var x = uniform( len, -10.0, 10.0, options );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,13 +21,20 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var uniform = require( '@stdlib/random/array/uniform' );
var pow = require( '@stdlib/math/base/special/pow' );
var pkg = require( './../package.json' ).name;
var variancepn = require( './../lib/ndarray.js' );


// VARIABLES //

var options = {
'dtype': 'generic'
};


// FUNCTIONS //

/**
Expand All @@ -38,13 +45,7 @@ var variancepn = require( './../lib/ndarray.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = [];
for ( i = 0; i < len; i++ ) {
x.push( ( randu()*20.0 ) - 10.0 );
}
var x = uniform( len, -10.0, 10.0, options );
return benchmark;

function benchmark( b ) {
Expand Down
48 changes: 23 additions & 25 deletions lib/node_modules/@stdlib/stats/base/variancepn/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

{{alias}}( N, correction, x, stride )
Computes the variance of a strided array using a two-pass algorithm.
{{alias}}( N, correction, x, strideX )
Computes the variance of a strided array (two-pass algorithm).

The `N` and `stride` parameters determine which elements in `x` are accessed
at runtime.
The `N` and stride parameters determine which elements in the
strided arrays are accessed at runtime.

Indexing is relative to the first index. To introduce an offset, use a typed
array view.
Indexing is relative to the first index. To introduce
an offset, use a typed array view.

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

Expand All @@ -30,8 +30,8 @@
x: Array<number>|TypedArray
Input array.

stride: integer
Index increment.
strideX: integer
Stride length.

Returns
-------
Expand All @@ -43,24 +43,23 @@
// Standard Usage:
> var x = [ 1.0, -2.0, 2.0 ];
> {{alias}}( x.length, 1, x, 1 )
~4.3333
4.333333333333334

// Using `N` and `stride` parameters:
// Using `N` and `strideX` parameters:
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> var stride = 2;
> {{alias}}( N, 1, x, stride )
~4.3333
> var strideX = 2;
> {{alias}}( 4, 1, x, strideX )
NaN

// Using view offsets:
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
> stride = 2;
> {{alias}}( N, 1, x1, stride )
~4.3333
> strideX = 2;
> {{alias}}( 4, 1, x1, strideX )
NaN


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

Expand Down Expand Up @@ -88,8 +87,8 @@
x: Array<number>|TypedArray
Input array.

stride: integer
Index increment.
strideX: integer
Stride length.

offset: integer
Starting index.
Expand All @@ -108,10 +107,9 @@

// Using offset parameter:
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}.ndarray( N, 1, x, 2, 1 )
~4.3333
> {{alias}}.ndarray( 4, 1, x, 2, 1 )
NaN


See Also
--------

Loading
Loading