Skip to content

Commit f37e80c

Browse files
committed
feat: add stats/strided/nanmeanpn
Ref: #4797 --- 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: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 54b11ec commit f37e80c

File tree

16 files changed

+1867
-0
lines changed

16 files changed

+1867
-0
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# nanmeanpn
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a strided array, ignoring `NaN` values and using a two-pass error correction algorithm.
24+
25+
<section class="intro">
26+
27+
The [arithmetic mean][arithmetic-mean] is defined as
28+
29+
<!-- <equation class="equation" label="eq:arithmetic_mean" align="center" raw="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the arithmetic mean."> -->
30+
31+
```math
32+
\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:arithmetic_mean">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@2ac9b5fc22af7280d57e3c9cb1fe08210c697f4f/lib/node_modules/@stdlib/stats/strided/nanmeanpn/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var nanmeanpn = require( '@stdlib/stats/strided/nanmeanpn' );
52+
```
53+
54+
#### nanmeanpn( N, x, strideX )
55+
56+
Computes the [arithmetic mean][arithmetic-mean] of a strided array, ignoring `NaN` values and using a two-pass error correction algorithm.
57+
58+
```javascript
59+
var x = [ 1.0, -2.0, NaN, 2.0 ];
60+
61+
var v = nanmeanpn( x.length, x, 1 );
62+
// returns ~0.3333
63+
```
64+
65+
The function has the following parameters:
66+
67+
- **N**: number of indexed elements.
68+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
69+
- **strideX**: stride length for `x`.
70+
71+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`,
72+
73+
```javascript
74+
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ];
75+
76+
var v = nanmeanpn( 5, x, 2 );
77+
// returns 1.25
78+
```
79+
80+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
81+
82+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
83+
84+
```javascript
85+
var Float64Array = require( '@stdlib/array/float64' );
86+
87+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
88+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
89+
90+
var v = nanmeanpn( 5, x1, 2 );
91+
// returns 1.25
92+
```
93+
94+
#### nanmeanpn.ndarray( N, x, strideX, offsetX )
95+
96+
Computes the [arithmetic mean][arithmetic-mean] of a strided array, ignoring `NaN` values and using a two-pass error correction algorithm and alternative indexing semantics.
97+
98+
```javascript
99+
var x = [ 1.0, -2.0, NaN, 2.0 ];
100+
101+
var v = nanmeanpn.ndarray( x.length, x, 1, 0 );
102+
// returns ~0.33333
103+
```
104+
105+
The function has the following additional parameters:
106+
107+
- **offsetX**: starting index for `x`.
108+
109+
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 [arithmetic mean][arithmetic-mean] for every other element in `x` starting from the second element
110+
111+
```javascript
112+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ];
113+
114+
var v = nanmeanpn.ndarray( 5, x, 2, 1 );
115+
// returns 1.25
116+
```
117+
118+
</section>
119+
120+
<!-- /.usage -->
121+
122+
<section class="notes">
123+
124+
## Notes
125+
126+
- If `N <= 0`, both functions return `NaN`.
127+
- If every indexed element is `NaN`, both functions return `NaN`.
128+
- 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]).
129+
- Depending on the environment, the typed versions ([`dnanmeanpn`][@stdlib/stats/strided/dnanmeanpn], [`snanmeanpn`][@stdlib/stats/strided/snanmeanpn], etc.) are likely to be significantly more performant.
130+
131+
</section>
132+
133+
<!-- /.notes -->
134+
135+
<section class="examples">
136+
137+
## Examples
138+
139+
<!-- eslint no-undef: "error" -->
140+
141+
```javascript
142+
var uniform = require( '@stdlib/random/base/uniform' );
143+
var filledarrayBy = require( '@stdlib/array/filled-by' );
144+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
145+
var nanmeanpn = require( '@stdlib/stats/strided/nanmeanpn' );
146+
147+
function rand() {
148+
if ( bernoulli( 0.8 ) < 1 ) {
149+
return NaN;
150+
}
151+
return uniform( -50.0, 50.0 );
152+
}
153+
154+
var x = filledarrayBy( 10, 'float64', rand );
155+
console.log( x );
156+
157+
var v = nanmeanpn( x.length, x, 1 );
158+
console.log( v );
159+
```
160+
161+
</section>
162+
163+
<!-- /.examples -->
164+
165+
* * *
166+
167+
<section class="references">
168+
169+
## References
170+
171+
- 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][@neely:1966a].
172+
- 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][@schubert:2018a].
173+
174+
</section>
175+
176+
<!-- /.references -->
177+
178+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
179+
180+
<section class="related">
181+
182+
* * *
183+
184+
## See Also
185+
186+
- <span class="package-name">[`@stdlib/stats/strided/dnanmeanpn`][@stdlib/stats/strided/dnanmeanpn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array, ignoring NaN values and using a two-pass error correction algorithm.</span>
187+
- <span class="package-name">[`@stdlib/stats/strided/meanpn`][@stdlib/stats/strided/meanpn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array using a two-pass error correction algorithm.</span>
188+
- <span class="package-name">[`@stdlib/stats/base/nanmean`][@stdlib/stats/base/nanmean]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array, ignoring NaN values.</span>
189+
- <span class="package-name">[`@stdlib/stats/strided/snanmeanpn`][@stdlib/stats/strided/snanmeanpn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array, ignoring NaN values and using a two-pass error correction algorithm.</span>
190+
191+
</section>
192+
193+
<!-- /.related -->
194+
195+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
196+
197+
<section class="links">
198+
199+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
200+
201+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
202+
203+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
204+
205+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
206+
207+
[@neely:1966a]: https://doi.org/10.1145/365719.365958
208+
209+
[@schubert:2018a]: https://doi.org/10.1145/3221269.3223036
210+
211+
<!-- <related-links> -->
212+
213+
[@stdlib/stats/strided/dnanmeanpn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmeanpn
214+
215+
[@stdlib/stats/strided/meanpn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/meanpn
216+
217+
[@stdlib/stats/base/nanmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmean
218+
219+
[@stdlib/stats/strided/snanmeanpn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanmeanpn
220+
221+
<!-- </related-links> -->
222+
223+
</section>
224+
225+
<!-- /.links -->
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var pkg = require( './../package.json' ).name;
30+
var nanmeanpn = require( './../lib/main.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
48+
/**
49+
* Creates a benchmark function.
50+
*
51+
* @private
52+
* @param {PositiveInteger} len - array length
53+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len ) {
56+
var x = filledarrayBy( len, 'generic', rand );
57+
return benchmark;
58+
59+
function benchmark( b ) {
60+
var v;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
v = nanmeanpn( x.length, x, 1 );
66+
if ( isnan( v ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( v ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( pkg+':len='+len, f );
101+
}
102+
}
103+
104+
main();

0 commit comments

Comments
 (0)