|
| 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 --> |
0 commit comments