Skip to content

Commit e2f44f1

Browse files
committed
feat: add stats/strided/meanpw
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 9a64928 commit e2f44f1

File tree

16 files changed

+1679
-0
lines changed

16 files changed

+1679
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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+
# meanpw
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a strided array using pairwise summation.
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@e4a1cd44f903f1758776279afedd500ce2dc6ddd/lib/node_modules/@stdlib/stats/strided/meanpw/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 meanpw = require( '@stdlib/stats/strided/meanpw' );
52+
```
53+
54+
#### meanpw( N, x, strideX )
55+
56+
Computes the [arithmetic mean][arithmetic-mean] of a strided array using pairwise summation.
57+
58+
```javascript
59+
var x = [ 1.0, -2.0, 2.0 ];
60+
61+
var v = meanpw( 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 ];
75+
76+
var v = meanpw( 4, 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 -->
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 ] );
88+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
89+
90+
var v = meanpw( 4, x1, 2 );
91+
// returns 1.25
92+
```
93+
94+
#### meanpw.ndarray( N, x, strideX, offsetX )
95+
96+
Computes the [arithmetic mean][arithmetic-mean] of a strided array using pairwise summation and alternative indexing semantics.
97+
98+
```javascript
99+
var x = [ 1.0, -2.0, 2.0 ];
100+
101+
var v = meanpw.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 ];
113+
114+
var v = meanpw.ndarray( 4, 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+
- 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]).
128+
- In general, pairwise summation is more numerically stable than ordinary recursive summation (i.e., "simple" summation), with slightly worse performance. While not the most numerically stable summation technique (e.g., compensated summation techniques such as the Kahan–Babuška-Neumaier algorithm are generally more numerically stable), pairwise summation strikes a reasonable balance between numerical stability and performance. If either numerical stability or performance is more desirable for your use case, consider alternative summation techniques.
129+
- Depending on the environment, the typed versions ([`dmeanpw`][@stdlib/stats/strided/dmeanpw], [`smeanpw`][@stdlib/stats/strided/smeanpw], 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
143+
var meanpw = require( '@stdlib/stats/strided/meanpw' );
144+
145+
var x = discreteUniform( 10, -50, 50, {
146+
'dtype': 'float64'
147+
});
148+
console.log( x );
149+
150+
var v = meanpw( x.length, x, 1 );
151+
console.log( v );
152+
```
153+
154+
</section>
155+
156+
<!-- /.examples -->
157+
158+
* * *
159+
160+
<section class="references">
161+
162+
## References
163+
164+
- Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050][@higham:1993a].
165+
166+
</section>
167+
168+
<!-- /.references -->
169+
170+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
171+
172+
<section class="related">
173+
174+
* * *
175+
176+
## See Also
177+
178+
- <span class="package-name">[`@stdlib/stats/strided/dmeanpw`][@stdlib/stats/strided/dmeanpw]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using pairwise summation.</span>
179+
- <span class="package-name">[`@stdlib/stats/strided/mean`][@stdlib/stats/strided/mean]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array.</span>
180+
- <span class="package-name">[`@stdlib/stats/strided/smeanpw`][@stdlib/stats/strided/smeanpw]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array using pairwise summation.</span>
181+
182+
</section>
183+
184+
<!-- /.related -->
185+
186+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
187+
188+
<section class="links">
189+
190+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
191+
192+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
193+
194+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
195+
196+
[@higham:1993a]: https://doi.org/10.1137/0914050
197+
198+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
199+
200+
<!-- <related-links> -->
201+
202+
[@stdlib/stats/strided/dmeanpw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeanpw
203+
204+
[@stdlib/stats/strided/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/mean
205+
206+
[@stdlib/stats/strided/smeanpw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smeanpw
207+
208+
<!-- </related-links> -->
209+
210+
</section>
211+
212+
<!-- /.links -->
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var meanpw = require( './../lib/meanpw.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
var x = uniform( len, -10, 10, options );
49+
return benchmark;
50+
51+
function benchmark( b ) {
52+
var v;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = meanpw( x.length, x, 1 );
58+
if ( isnan( v ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( v ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
/**
75+
* Main execution sequence.
76+
*
77+
* @private
78+
*/
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
f = createBenchmark( len );
92+
bench( pkg+':len='+len, f );
93+
}
94+
}
95+
96+
main();

0 commit comments

Comments
 (0)