Skip to content

Commit cb781ba

Browse files
committed
feat: add stats/strided/meankbn
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 4c06e6e commit cb781ba

File tree

16 files changed

+1692
-0
lines changed

16 files changed

+1692
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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+
# meankbn
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a strided array using an improved Kahan–Babuška 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@a4861d8b5345a07f446bcfb2bb4bef1bc8346595/lib/node_modules/@stdlib/stats/strided/meankbn/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 meankbn = require( '@stdlib/stats/strided/meankbn' );
52+
```
53+
54+
#### meankbn( N, x, strideX )
55+
56+
Computes the [arithmetic mean][arithmetic-mean] of a strided array using an improved Kahan–Babuška algorithm.
57+
58+
```javascript
59+
var x = [ 1.0, -2.0, 2.0 ];
60+
61+
var v = meankbn( 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 = meankbn( 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 = meankbn( 4, x1, 2 );
91+
// returns 1.25
92+
```
93+
94+
#### meankbn.ndarray( N, x, strideX, offsetX )
95+
96+
Computes the [arithmetic mean][arithmetic-mean] of a strided array using an improved Kahan–Babuška algorithm and alternative indexing semantics.
97+
98+
```javascript
99+
var x = [ 1.0, -2.0, 2.0 ];
100+
101+
var v = meankbn.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 = meankbn.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+
- Depending on the environment, the typed versions ([`dmeankbn`][@stdlib/stats/strided/dmeankbn], [`smeankbn`][@stdlib/stats/base/smeankbn], etc.) are likely to be significantly more performant.
129+
130+
</section>
131+
132+
<!-- /.notes -->
133+
134+
<section class="examples">
135+
136+
## Examples
137+
138+
<!-- eslint no-undef: "error" -->
139+
140+
```javascript
141+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
142+
var meankbn = require( '@stdlib/stats/strided/meankbn' );
143+
144+
var x = discreteUniform( 10, -50, 50, {
145+
'dtype': 'float64'
146+
});
147+
console.log( x );
148+
149+
var v = meankbn( x.length, x, 1 );
150+
console.log( v );
151+
```
152+
153+
</section>
154+
155+
<!-- /.examples -->
156+
157+
* * *
158+
159+
<section class="references">
160+
161+
## References
162+
163+
- Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106][@neumaier:1974a].
164+
165+
</section>
166+
167+
<!-- /.references -->
168+
169+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
170+
171+
<section class="related">
172+
173+
* * *
174+
175+
## See Also
176+
177+
- <span class="package-name">[`@stdlib/stats/strided/dmeankbn`][@stdlib/stats/strided/dmeankbn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using an improved Kahan–Babuška algorithm.</span>
178+
- <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>
179+
- <span class="package-name">[`@stdlib/stats/base/smeankbn`][@stdlib/stats/base/smeankbn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array using an improved Kahan–Babuška algorithm.</span>
180+
181+
</section>
182+
183+
<!-- /.related -->
184+
185+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
186+
187+
<section class="links">
188+
189+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
190+
191+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
192+
193+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
194+
195+
[@neumaier:1974a]: https://doi.org/10.1002/zamm.19740540106
196+
197+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
198+
199+
<!-- <related-links> -->
200+
201+
[@stdlib/stats/strided/dmeankbn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeankbn
202+
203+
[@stdlib/stats/strided/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/mean
204+
205+
[@stdlib/stats/base/smeankbn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/smeankbn
206+
207+
<!-- </related-links> -->
208+
209+
</section>
210+
211+
<!-- /.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 meankbn = require( './../lib/main.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 = meankbn( 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)