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