Skip to content

Commit 785ec4d

Browse files
updated readme
1 parent c395ff2 commit 785ec4d

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

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

+16-28
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 variancetk = require( '@stdlib/stats/base/variancetk' );
9999
```
100100

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

103103
Computes the [variance][variance] of a strided array `x` using a one-pass textbook algorithm.
104104

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

121121
```javascript
122-
var floor = require( '@stdlib/math/base/special/floor' );
123-
124122
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
125-
var N = floor( x.length / 2 );
126123

127-
var v = variancetk( N, 1, x, 2 );
124+
var v = variancetk( 4, 1, x, 2 );
128125
// returns 6.25
129126
```
130127

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

135132
```javascript
136133
var Float64Array = require( '@stdlib/array/float64' );
137-
var floor = require( '@stdlib/math/base/special/floor' );
138134

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

142-
var N = floor( x0.length / 2 );
143-
144-
var v = variancetk( N, 1, x1, 2 );
138+
var v = variancetk( 4, 1, x1, 2 );
145139
// returns 6.25
146140
```
147141

148-
#### variancetk.ndarray( N, correction, x, stride, offset )
142+
#### variancetk.ndarray( N, correction, x, strideX, offsetX )
149143

150144
Computes the [variance][variance] of a strided array using a one-pass textbook algorithm and alternative indexing semantics.
151145

@@ -158,17 +152,14 @@ var v = variancetk.ndarray( x.length, 1, x, 1, 0 );
158152

159153
The function has the following additional parameters:
160154

161-
- **offset**: starting index for `x`.
155+
- **offsetX**: starting index for `x`.
162156

163-
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
157+
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 element value in the strided array starting from the second element
164158

165159
```javascript
166-
var floor = require( '@stdlib/math/base/special/floor' );
167-
168160
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
169-
var N = floor( x.length / 2 );
170161

171-
var v = variancetk.ndarray( N, 1, x, 2, 1 );
162+
var v = variancetk.ndarray( 4, 1, x, 2, 1 );
172163
// returns 6.25
173164
```
174165

@@ -181,6 +172,7 @@ var v = variancetk.ndarray( N, 1, x, 2, 1 );
181172
## Notes
182173

183174
- If `N <= 0`, both functions return `NaN`.
175+
- 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]).
184176
- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`.
185177
- Some caution should be exercised when using the one-pass textbook algorithm. Literature overwhelmingly discourages the algorithm's use for two reasons: 1) the lack of safeguards against underflow and overflow and 2) the risk of catastrophic cancellation when subtracting the two sums if the sums are large and the variance small. These concerns have merit; however, the one-pass textbook algorithm should not be dismissed outright. For data distributions with a moderately large standard deviation to mean ratio (i.e., **coefficient of variation**), the one-pass textbook algorithm may be acceptable, especially when performance is paramount and some precision loss is acceptable (including a risk of returning a negative variance due to floating-point rounding errors!). In short, no single "best" algorithm for computing the variance exists. The "best" algorithm depends on the underlying data distribution, your performance requirements, and your minimum precision requirements. When evaluating which algorithm to use, consider the relative pros and cons, and choose the algorithm which best serves your needs.
186178
- Depending on the environment, the typed versions ([`dvariancetk`][@stdlib/stats/base/dvariancetk], [`svariancetk`][@stdlib/stats/base/svariancetk], etc.) are likely to be significantly more performant.
@@ -196,18 +188,12 @@ var v = variancetk.ndarray( N, 1, x, 2, 1 );
196188
<!-- eslint no-undef: "error" -->
197189

198190
```javascript
199-
var randu = require( '@stdlib/random/base/randu' );
200-
var round = require( '@stdlib/math/base/special/round' );
201-
var Float64Array = require( '@stdlib/array/float64' );
191+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
202192
var variancetk = require( '@stdlib/stats/base/variancetk' );
203193

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

213199
var v = variancetk( x.length, 1, x, 1 );
@@ -257,6 +243,8 @@ console.log( v );
257243

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

246+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
247+
260248
[@stdlib/stats/base/svariancetk]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/svariancetk
261249

262250
[@ling:1974a]: https://doi.org/10.2307/2286154

lib/node_modules/@stdlib/stats/base/variancetk/lib/accessors.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ function variancetk( N, correction, x, strideX, offsetX ) {
5353
var i;
5454

5555
n = N - correction;
56+
if ( N <= 0 || n <= 0.0 ) {
57+
return NaN;
58+
}
5659

5760
// Cache reference to array data:
5861
xbuf = x.data;
@@ -64,7 +67,7 @@ function variancetk( N, correction, x, strideX, offsetX ) {
6467
return get( xbuf, offsetX );
6568
}
6669
ix = offsetX;
67-
S2 = get( xbuf, ix );
70+
S2 = 0.0;
6871
S = 0.0;
6972
for ( i = 0; i < N; i++ ) {
7073
v = get( xbuf, ix );

0 commit comments

Comments
 (0)