|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# meanpn |
| 22 | + |
| 23 | +> Calculate the [arithmetic mean][arithmetic-mean] of a strided array 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@7158616c9399e313d462e91b1a5c0f82d090c372/lib/node_modules/@stdlib/stats/strided/meanpn/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 meanpn = require( '@stdlib/stats/strided/meanpn' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### meanpn( N, x, strideX ) |
| 55 | + |
| 56 | +Computes the [arithmetic mean][arithmetic-mean] of a strided array using a two-pass error correction algorithm. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var x = [ 1.0, -2.0, 2.0 ]; |
| 60 | + |
| 61 | +var v = meanpn( 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 the input array |
| 72 | + |
| 73 | +```javascript |
| 74 | +var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; |
| 75 | +var v = meanpn( 4, x, 2 ); |
| 76 | +// returns 1.25 |
| 77 | +``` |
| 78 | + |
| 79 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 80 | + |
| 81 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 82 | + |
| 83 | +```javascript |
| 84 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 85 | + |
| 86 | +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 87 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 88 | + |
| 89 | +var v = meanpn( 4, x1, 2 ); |
| 90 | +// returns 1.25 |
| 91 | +``` |
| 92 | + |
| 93 | +#### meanpn.ndarray( N, x, strideX, offsetX ) |
| 94 | + |
| 95 | +Computes the [arithmetic mean][arithmetic-mean] of a strided array using a two-pass error correction algorithm and alternative indexing semantics. |
| 96 | + |
| 97 | +```javascript |
| 98 | +var x = [ 1.0, -2.0, 2.0 ]; |
| 99 | + |
| 100 | +var v = meanpn.ndarray( x.length, x, 1, 0 ); |
| 101 | +// returns ~0.33333 |
| 102 | +``` |
| 103 | + |
| 104 | +The function has the following additional parameters: |
| 105 | + |
| 106 | +- **offsetX**: starting index for `x`. |
| 107 | + |
| 108 | +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 the strided array starting from the second element |
| 109 | + |
| 110 | +```javascript |
| 111 | +var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; |
| 112 | + |
| 113 | +var v = meanpn.ndarray( 4, x, 2, 1 ); |
| 114 | +// returns 1.25 |
| 115 | +``` |
| 116 | + |
| 117 | +</section> |
| 118 | + |
| 119 | +<!-- /.usage --> |
| 120 | + |
| 121 | +<section class="notes"> |
| 122 | + |
| 123 | +## Notes |
| 124 | + |
| 125 | +- If `N <= 0`, both functions return `NaN`. |
| 126 | +- 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]). |
| 127 | +- Depending on the environment, the typed versions ([`dmeanpn`][@stdlib/stats/strided/dmeanpn], [`smeanpn`][@stdlib/stats/strided/smeanpn], etc.) are likely to be significantly more performant. |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.notes --> |
| 132 | + |
| 133 | +<section class="examples"> |
| 134 | + |
| 135 | +## Examples |
| 136 | + |
| 137 | +<!-- eslint no-undef: "error" --> |
| 138 | + |
| 139 | +```javascript |
| 140 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 141 | +var meanpn = require( '@stdlib/stats/strided/meanpn' ); |
| 142 | + |
| 143 | +var x = discreteUniform( 10, -50, 50, { |
| 144 | + 'dtype': 'float64' |
| 145 | +}); |
| 146 | +console.log( x ); |
| 147 | + |
| 148 | +var v = meanpn( x.length, x, 1 ); |
| 149 | +console.log( v ); |
| 150 | +``` |
| 151 | + |
| 152 | +</section> |
| 153 | + |
| 154 | +<!-- /.examples --> |
| 155 | + |
| 156 | +* * * |
| 157 | + |
| 158 | +<section class="references"> |
| 159 | + |
| 160 | +## References |
| 161 | + |
| 162 | +- 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]. |
| 163 | +- 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]. |
| 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/dmeanpn`][@stdlib/stats/strided/dmeanpn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using a two-pass error correction 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/nanmeanpn`][@stdlib/stats/base/nanmeanpn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array, ignoring NaN values and using a two-pass error correction algorithm.</span> |
| 180 | +- <span class="package-name">[`@stdlib/stats/strided/smeanpn`][@stdlib/stats/strided/smeanpn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array using a two-pass error correction algorithm.</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 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 197 | + |
| 198 | +[@neely:1966a]: https://doi.org/10.1145/365719.365958 |
| 199 | + |
| 200 | +[@schubert:2018a]: https://doi.org/10.1145/3221269.3223036 |
| 201 | + |
| 202 | +<!-- <related-links> --> |
| 203 | + |
| 204 | +[@stdlib/stats/strided/dmeanpn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeanpn |
| 205 | + |
| 206 | +[@stdlib/stats/strided/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/mean |
| 207 | + |
| 208 | +[@stdlib/stats/base/nanmeanpn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmeanpn |
| 209 | + |
| 210 | +[@stdlib/stats/strided/smeanpn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smeanpn |
| 211 | + |
| 212 | +<!-- </related-links> --> |
| 213 | + |
| 214 | +</section> |
| 215 | + |
| 216 | +<!-- /.links --> |
0 commit comments