|
| 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 | +# meanwd |
| 22 | + |
| 23 | +> Calculate the [arithmetic mean][arithmetic-mean] of a strided array using Welford's 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@fc6aab656f799b0f4440645b873f037d2c9718e6/lib/node_modules/@stdlib/stats/strided/meanwd/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 meanwd = require( '@stdlib/stats/strided/meanwd' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### meanwd( N, x, strideX ) |
| 55 | + |
| 56 | +Computes the [arithmetic mean][arithmetic-mean] of a strided array `x` using Welford's algorithm. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var x = [ 1.0, -2.0, 2.0 ]; |
| 60 | +var N = x.length; |
| 61 | + |
| 62 | +var v = meanwd( N, x, 1 ); |
| 63 | +// returns ~0.3333 |
| 64 | +``` |
| 65 | + |
| 66 | +The function has the following parameters: |
| 67 | + |
| 68 | +- **N**: number of indexed elements. |
| 69 | +- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. |
| 70 | +- **strideX**: stride length for `x`. |
| 71 | + |
| 72 | +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`, |
| 73 | + |
| 74 | +```javascript |
| 75 | +var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; |
| 76 | + |
| 77 | +var v = meanwd( 4, x, 2 ); |
| 78 | +// returns 1.25 |
| 79 | +``` |
| 80 | + |
| 81 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 82 | + |
| 83 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 84 | + |
| 85 | +```javascript |
| 86 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 87 | + |
| 88 | +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 89 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 90 | + |
| 91 | +var v = meanwd( 4, x1, 2 ); |
| 92 | +// returns 1.25 |
| 93 | +``` |
| 94 | + |
| 95 | +#### meanwd.ndarray( N, x, strideX, offsetX ) |
| 96 | + |
| 97 | +Computes the [arithmetic mean][arithmetic-mean] of a strided array using Welford's algorithm and alternative indexing semantics. |
| 98 | + |
| 99 | +```javascript |
| 100 | +var x = [ 1.0, -2.0, 2.0 ]; |
| 101 | + |
| 102 | +var v = meanwd.ndarray( 3, x, 1, 0 ); |
| 103 | +// returns ~0.33333 |
| 104 | +``` |
| 105 | + |
| 106 | +The function has the following additional parameters: |
| 107 | + |
| 108 | +- **offsetX**: starting index for `x`. |
| 109 | + |
| 110 | +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 |
| 111 | + |
| 112 | +```javascript |
| 113 | +var floor = require( '@stdlib/math/base/special/floor' ); |
| 114 | + |
| 115 | +var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; |
| 116 | + |
| 117 | +var v = meanwd.ndarray( 4, x, 2, 1 ); |
| 118 | +// returns 1.25 |
| 119 | +``` |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.usage --> |
| 124 | + |
| 125 | +<section class="notes"> |
| 126 | + |
| 127 | +## Notes |
| 128 | + |
| 129 | +- If `N <= 0`, both functions return `NaN`. |
| 130 | +- 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]). |
| 131 | +- Depending on the environment, the typed versions ([`dmeanwd`][@stdlib/stats/strided/dmeanwd], [`smeanwd`][@stdlib/stats/strided/smeanwd], etc.) are likely to be significantly more performant. |
| 132 | + |
| 133 | +</section> |
| 134 | + |
| 135 | +<!-- /.notes --> |
| 136 | + |
| 137 | +<section class="examples"> |
| 138 | + |
| 139 | +## Examples |
| 140 | + |
| 141 | +<!-- eslint no-undef: "error" --> |
| 142 | + |
| 143 | +```javascript |
| 144 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 145 | +var meanwd = require( '@stdlib/stats/strided/meanwd' ); |
| 146 | + |
| 147 | +var x = discreteUniform( 10, -50, 50, { |
| 148 | + 'dtype': 'float64' |
| 149 | +}); |
| 150 | +console.log( x ); |
| 151 | + |
| 152 | +var v = meanwd( x.length, x, 1 ); |
| 153 | +console.log( v ); |
| 154 | +``` |
| 155 | + |
| 156 | +</section> |
| 157 | + |
| 158 | +<!-- /.examples --> |
| 159 | + |
| 160 | +* * * |
| 161 | + |
| 162 | +<section class="references"> |
| 163 | + |
| 164 | +## References |
| 165 | + |
| 166 | +- Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022][@welford:1962a]. |
| 167 | +- van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961][@vanreeken:1968a]. |
| 168 | + |
| 169 | +</section> |
| 170 | + |
| 171 | +<!-- /.references --> |
| 172 | + |
| 173 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 174 | + |
| 175 | +<section class="related"> |
| 176 | + |
| 177 | +* * * |
| 178 | + |
| 179 | +## See Also |
| 180 | + |
| 181 | +- <span class="package-name">[`@stdlib/stats/strided/dmeanwd`][@stdlib/stats/strided/dmeanwd]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using Welford's algorithm.</span> |
| 182 | +- <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> |
| 183 | +- <span class="package-name">[`@stdlib/stats/base/nanmeanwd`][@stdlib/stats/base/nanmeanwd]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array, ignoring NaN values and using Welford's algorithm.</span> |
| 184 | +- <span class="package-name">[`@stdlib/stats/strided/smeanwd`][@stdlib/stats/strided/smeanwd]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array using Welford's algorithm.</span> |
| 185 | + |
| 186 | +</section> |
| 187 | + |
| 188 | +<!-- /.related --> |
| 189 | + |
| 190 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 191 | + |
| 192 | +<section class="links"> |
| 193 | + |
| 194 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 195 | + |
| 196 | +[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array |
| 197 | + |
| 198 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 199 | + |
| 200 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 201 | + |
| 202 | +[@welford:1962a]: https://doi.org/10.1080/00401706.1962.10490022 |
| 203 | + |
| 204 | +[@vanreeken:1968a]: https://doi.org/10.1145/362929.362961 |
| 205 | + |
| 206 | +<!-- <related-links> --> |
| 207 | + |
| 208 | +[@stdlib/stats/strided/dmeanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeanwd |
| 209 | + |
| 210 | +[@stdlib/stats/strided/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/mean |
| 211 | + |
| 212 | +[@stdlib/stats/base/nanmeanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmeanwd |
| 213 | + |
| 214 | +[@stdlib/stats/strided/smeanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smeanwd |
| 215 | + |
| 216 | +<!-- </related-links> --> |
| 217 | + |
| 218 | +</section> |
| 219 | + |
| 220 | +<!-- /.links --> |
0 commit comments