-
-
Notifications
You must be signed in to change notification settings - Fork 831
feat: add accessor protocol support and refactor stats/base/nanvariancech
#6167
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
4f7cb74
da0f9d5
418d3ec
4481ce3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -98,7 +98,7 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, | |||||
var nanvariancech = require( '@stdlib/stats/base/nanvariancech' ); | ||||||
``` | ||||||
|
||||||
#### nanvariancech( N, correction, x, stride ) | ||||||
#### nanvariancech( N, correction, x, strideX ) | ||||||
|
||||||
Computes the [variance][variance] of a strided array `x` ignoring `NaN` values and using a one-pass trial mean algorithm. | ||||||
|
||||||
|
@@ -114,17 +114,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 and `n` corresponds to the number of non-`NaN` indexed elements. 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 stided array 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, NaN ]; | ||||||
var N = floor( x.length / 2 ); | ||||||
|
||||||
var v = nanvariancech( N, 1, x, 2 ); | ||||||
var v = nanvariancech( 4, 1, x, 2 ); | ||||||
// returns 6.25 | ||||||
``` | ||||||
|
||||||
|
@@ -134,41 +131,35 @@ 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, NaN ] ); | ||||||
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||||||
|
||||||
var N = floor( x0.length / 2 ); | ||||||
|
||||||
var v = nanvariancech( N, 1, x1, 2 ); | ||||||
var v = nanvariancech( 4, 1, x1, 2 ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// returns 6.25 | ||||||
``` | ||||||
|
||||||
#### nanvariancech.ndarray( N, correction, x, stride, offset ) | ||||||
#### nanvariancech.ndarray( N, correction, x, strideX, offsetX ) | ||||||
|
||||||
Computes the [variance][variance] of a strided array ignoring `NaN` values and using a one-pass trial mean algorithm and alternative indexing semantics. | ||||||
|
||||||
```javascript | ||||||
var x = [ 1.0, -2.0, NaN, 2.0 ]; | ||||||
|
||||||
var v = nanvariancech.ndarray( x.length, 1, x, 1, 0 ); | ||||||
var v = nanvariancech.ndarray( 4, 1, x, 1, 0 ); | ||||||
// returns ~4.33333 | ||||||
``` | ||||||
|
||||||
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` parameter supports indexing semantics based on a starting index. For example, to calculate the [variance][variance] for every other element in the strided array starting from the second element | ||||||
|
||||||
```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, NaN, NaN ]; | ||||||
|
||||||
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 = nanvariancech.ndarray( N, 1, x, 2, 1 ); | ||||||
var v = nanvariancech.ndarray( 5, 1, x, 2, 1 ); | ||||||
// returns 6.25 | ||||||
``` | ||||||
|
||||||
|
@@ -183,6 +174,7 @@ var v = nanvariancech.ndarray( N, 1, x, 2, 1 ); | |||||
- If `N <= 0`, both functions return `NaN`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no note for support of accessor arrays here |
||||||
- If `n - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements), both functions return `NaN`. | ||||||
- The underlying algorithm is a specialized case of Neely's two-pass algorithm. As the variance is invariant with respect to changes in the location parameter, the underlying algorithm uses the first non-`NaN` strided array element as a trial mean to shift subsequent data values and thus mitigate catastrophic cancellation. Accordingly, the algorithm's accuracy is best when data is **unordered** (i.e., the data is **not** sorted in either ascending or descending order such that the first value is an "extreme" value). | ||||||
- 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]). | ||||||
- Depending on the environment, the typed versions ([`dnanvariancech`][@stdlib/stats/base/dnanvariancech], [`snanvariancech`][@stdlib/stats/base/snanvariancech], etc.) are likely to be significantly more performant. | ||||||
|
||||||
</section> | ||||||
|
@@ -196,18 +188,19 @@ var v = nanvariancech.ndarray( N, 1, x, 2, 1 ); | |||||
<!-- eslint no-undef: "error" --> | ||||||
|
||||||
```javascript | ||||||
var randu = require( '@stdlib/random/base/randu' ); | ||||||
var round = require( '@stdlib/math/base/special/round' ); | ||||||
var Float64Array = require( '@stdlib/array/float64' ); | ||||||
var uniform = require( '@stdlib/random/base/uniform' ); | ||||||
var filledarrayBy = require( '@stdlib/array/filled-by' ); | ||||||
var bernoulli = require( '@stdlib/random/base/bernoulli' ); | ||||||
var nanvariancech = require( '@stdlib/stats/base/nanvariancech' ); | ||||||
|
||||||
var x; | ||||||
var i; | ||||||
|
||||||
x = new Float64Array( 10 ); | ||||||
for ( i = 0; i < x.length; i++ ) { | ||||||
x[ i ] = round( (randu()*100.0) - 50.0 ); | ||||||
function rand() { | ||||||
if ( bernoulli( 0.8 ) < 1 ) { | ||||||
return NaN; | ||||||
} | ||||||
return uniform( -50.0, 50.0 ); | ||||||
} | ||||||
|
||||||
var x = filledarrayBy( 10, 'generic', rand ); | ||||||
console.log( x ); | ||||||
|
||||||
var v = nanvariancech( x.length, 1, x, 1 ); | ||||||
|
@@ -281,6 +274,8 @@ console.log( v ); | |||||
|
||||||
[@stdlib/stats/base/variancech]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/variancech | ||||||
|
||||||
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor | ||||||
|
||||||
<!-- </related-links> --> | ||||||
|
||||||
</section> | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,10 @@ | ||||||
|
||||||
{{alias}}( N, correction, x, stride ) | ||||||
{{alias}}( N, correction, x, strideX ) | ||||||
Computes the variance of a strided array ignoring `NaN` values and using a | ||||||
one-pass trial mean 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
there's only 1 array here |
||||||
are accessed at runtime. | ||||||
|
||||||
Indexing is relative to the first index. To introduce an offset, use a typed | ||||||
array view. | ||||||
|
@@ -34,8 +34,8 @@ | |||||
x: Array<number>|TypedArray | ||||||
Input array. | ||||||
|
||||||
stride: integer | ||||||
Index increment. | ||||||
strideX: integer | ||||||
Stride length. | ||||||
|
||||||
Returns | ||||||
------- | ||||||
|
@@ -46,31 +46,28 @@ | |||||
-------- | ||||||
// Standard Usage: | ||||||
> var x = [ 1.0, -2.0, NaN, 2.0 ]; | ||||||
> {{alias}}( x.length, 1, x, 1 ) | ||||||
> {{alias}}( 4, 1, x, 1 ) | ||||||
~4.3333 | ||||||
|
||||||
// Using `N` and `stride` parameters: | ||||||
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a |
||||||
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); | ||||||
> var stride = 2; | ||||||
> {{alias}}( N, 1, x, stride ) | ||||||
> {{alias}}( 3, 1, x, 2 ) | ||||||
~4.3333 | ||||||
|
||||||
// Using view offsets: | ||||||
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar comment regarding |
||||||
> 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 ) | ||||||
> {{alias}}( 3, 1, x1, 2 ) | ||||||
~4.3333 | ||||||
|
||||||
{{alias}}.ndarray( N, correction, x, stride, offset ) | ||||||
|
||||||
{{alias}}.ndarray( N, correction, x, strideX, offsetX ) | ||||||
Computes the variance of a strided array ignoring `NaN` values and using a | ||||||
one-pass trial mean algorithm and alternative indexing semantics. | ||||||
|
||||||
While typed array views mandate a view offset based on the underlying | ||||||
buffer, the `offset` parameter supports indexing semantics based on a | ||||||
starting index. | ||||||
buffer, the offset parameters support indexing semantics based on a | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
starting indices. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Parameters | ||||||
---------- | ||||||
|
@@ -93,10 +90,10 @@ | |||||
x: Array<number>|TypedArray | ||||||
Input array. | ||||||
|
||||||
stride: integer | ||||||
Index increment. | ||||||
strideX: integer | ||||||
Stride length. | ||||||
|
||||||
offset: integer | ||||||
offsetX: integer | ||||||
Starting index. | ||||||
|
||||||
Returns | ||||||
|
@@ -108,13 +105,12 @@ | |||||
-------- | ||||||
// Standard Usage: | ||||||
> var x = [ 1.0, -2.0, NaN, 2.0 ]; | ||||||
> {{alias}}.ndarray( x.length, 1, x, 1, 0 ) | ||||||
> {{alias}}.ndarray( 4, 1, x, 1, 0 ) | ||||||
~4.3333 | ||||||
|
||||||
// Using offset parameter: | ||||||
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar comment regarding |
||||||
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); | ||||||
> {{alias}}.ndarray( N, 1, x, 2, 1 ) | ||||||
> {{alias}}.ndarray( 3, 1, x, 2, 1 ) | ||||||
~4.3333 | ||||||
|
||||||
See Also | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should be traversing over the
NaN
value so that the example can demonstrate how the algorithm deals with it