|
| 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 | +# smeanlipw |
| 22 | + |
| 23 | +> Calculate the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using a one-pass trial mean algorithm with 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@39c6ac0ef47c7ee94007922f3602eb7e69a658c0/lib/node_modules/@stdlib/stats/strided/smeanlipw/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 smeanlipw = require( '@stdlib/stats/strided/smeanlipw' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### smeanlipw( N, x, strideX ) |
| 55 | + |
| 56 | +Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 60 | + |
| 61 | +var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); |
| 62 | + |
| 63 | +var v = smeanlipw( x.length, x, 1 ); |
| 64 | +// returns ~0.3333 |
| 65 | +``` |
| 66 | + |
| 67 | +The function has the following parameters: |
| 68 | + |
| 69 | +- **N**: number of indexed elements. |
| 70 | +- **x**: input [`Float32Array`][@stdlib/array/float32]. |
| 71 | +- **strideX**: stride length for `x`. |
| 72 | + |
| 73 | +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`, |
| 74 | + |
| 75 | +```javascript |
| 76 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 77 | + |
| 78 | +var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] ); |
| 79 | + |
| 80 | +var v = smeanlipw( 4, x, 2 ); |
| 81 | +// returns 1.25 |
| 82 | +``` |
| 83 | + |
| 84 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 85 | + |
| 86 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 87 | + |
| 88 | +```javascript |
| 89 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 90 | + |
| 91 | +var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 92 | +var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 93 | + |
| 94 | +var v = smeanlipw( 4, x1, 2 ); |
| 95 | +// returns 1.25 |
| 96 | +``` |
| 97 | + |
| 98 | +#### smeanlipw.ndarray( N, x, strideX, offsetX ) |
| 99 | + |
| 100 | +Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation and alternative indexing semantics. |
| 101 | + |
| 102 | +```javascript |
| 103 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 104 | + |
| 105 | +var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); |
| 106 | + |
| 107 | +var v = smeanlipw.ndarray( x.length, x, 1, 0 ); |
| 108 | +// returns ~0.33333 |
| 109 | +``` |
| 110 | + |
| 111 | +The function has the following additional parameters: |
| 112 | + |
| 113 | +- **offsetX**: starting index for `x`. |
| 114 | + |
| 115 | +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 |
| 116 | + |
| 117 | +```javascript |
| 118 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 119 | + |
| 120 | +var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 121 | + |
| 122 | +var v = smeanlipw.ndarray( 4, x, 2, 1 ); |
| 123 | +// returns 1.25 |
| 124 | +``` |
| 125 | + |
| 126 | +</section> |
| 127 | + |
| 128 | +<!-- /.usage --> |
| 129 | + |
| 130 | +<section class="notes"> |
| 131 | + |
| 132 | +## Notes |
| 133 | + |
| 134 | +- If `N <= 0`, both functions return `NaN`. |
| 135 | +- The underlying algorithm is a specialized case of Welford's algorithm. Similar to the method of assumed mean, the first strided array element is used as a trial mean. The trial mean is subtracted from subsequent data values, and the average deviations used to adjust the initial guess. Accordingly, the algorithm's accuracy is best when data is **unordered** (i.e., the data is **not** sorted in either ascending or descending order such that the first value is an "extreme" value). |
| 136 | + |
| 137 | +</section> |
| 138 | + |
| 139 | +<!-- /.notes --> |
| 140 | + |
| 141 | +<section class="examples"> |
| 142 | + |
| 143 | +## Examples |
| 144 | + |
| 145 | +<!-- eslint no-undef: "error" --> |
| 146 | + |
| 147 | +```javascript |
| 148 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 149 | +var smeanlipw = require( '@stdlib/stats/strided/smeanlipw' ); |
| 150 | + |
| 151 | +var x = discreteUniform( 10, -50, 50, { |
| 152 | + 'dtype': 'float32' |
| 153 | +}); |
| 154 | +console.log( x ); |
| 155 | + |
| 156 | +var v = smeanlipw( x.length, x, 1 ); |
| 157 | +console.log( v ); |
| 158 | +``` |
| 159 | + |
| 160 | +</section> |
| 161 | + |
| 162 | +<!-- /.examples --> |
| 163 | + |
| 164 | +<!-- C interface documentation. --> |
| 165 | + |
| 166 | +* * * |
| 167 | + |
| 168 | +<section class="c"> |
| 169 | + |
| 170 | +## C APIs |
| 171 | + |
| 172 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 173 | + |
| 174 | +<section class="intro"> |
| 175 | + |
| 176 | +</section> |
| 177 | + |
| 178 | +<!-- /.intro --> |
| 179 | + |
| 180 | +<!-- C usage documentation. --> |
| 181 | + |
| 182 | +<section class="usage"> |
| 183 | + |
| 184 | +### Usage |
| 185 | + |
| 186 | +```c |
| 187 | +#include "stdlib/stats/strided/smeanlipw.h" |
| 188 | +``` |
| 189 | + |
| 190 | +#### stdlib_strided_smeanlipw( N, \*X, strideX ) |
| 191 | + |
| 192 | +Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation. |
| 193 | + |
| 194 | +```c |
| 195 | +const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }; |
| 196 | + |
| 197 | +float v = stdlib_strided_smeanlipw( 4, x, 2 ); |
| 198 | +// returns 4.0f |
| 199 | +``` |
| 200 | +
|
| 201 | +The function accepts the following arguments: |
| 202 | +
|
| 203 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 204 | +- **X**: `[in] float*` input array. |
| 205 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 206 | +
|
| 207 | +```c |
| 208 | +float stdlib_strided_smeanlipw( const CBLAS_INT N, const float *X, const CBLAS_INT strideX ); |
| 209 | +``` |
| 210 | + |
| 211 | +#### stdlib_strided_smeanlipw_ndarray( N, \*X, strideX, offsetX ) |
| 212 | + |
| 213 | +Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation and alternative indexing semantics. |
| 214 | + |
| 215 | +```c |
| 216 | +const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }; |
| 217 | + |
| 218 | +float v = stdlib_strided_smeanlipw_ndarray( 4, x, 2, 0 ); |
| 219 | +// returns 4.0f |
| 220 | +``` |
| 221 | +
|
| 222 | +The function accepts the following arguments: |
| 223 | +
|
| 224 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 225 | +- **X**: `[in] float*` input array. |
| 226 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 227 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 228 | +
|
| 229 | +```c |
| 230 | +float stdlib_strided_smeanlipw_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); |
| 231 | +``` |
| 232 | + |
| 233 | +</section> |
| 234 | + |
| 235 | +<!-- /.usage --> |
| 236 | + |
| 237 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 238 | + |
| 239 | +<section class="notes"> |
| 240 | + |
| 241 | +</section> |
| 242 | + |
| 243 | +<!-- /.notes --> |
| 244 | + |
| 245 | +<!-- C API usage examples. --> |
| 246 | + |
| 247 | +<section class="examples"> |
| 248 | + |
| 249 | +### Examples |
| 250 | + |
| 251 | +```c |
| 252 | +#include "stdlib/stats/strided/smeanlipw.h" |
| 253 | +#include <stdio.h> |
| 254 | + |
| 255 | +int main( void ) { |
| 256 | + // Create a strided array: |
| 257 | + const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }; |
| 258 | + |
| 259 | + // Specify the number of elements: |
| 260 | + const int N = 4; |
| 261 | + |
| 262 | + // Specify the stride length: |
| 263 | + const int strideX = 2; |
| 264 | + |
| 265 | + // Compute the arithmetic mean: |
| 266 | + float v = stdlib_strided_smeanlipw( N, x, strideX ); |
| 267 | + |
| 268 | + // Print the result: |
| 269 | + printf( "mean: %f\n", v ); |
| 270 | +} |
| 271 | +``` |
| 272 | +
|
| 273 | +</section> |
| 274 | +
|
| 275 | +<!-- /.examples --> |
| 276 | +
|
| 277 | +</section> |
| 278 | +
|
| 279 | +<!-- /.c --> |
| 280 | +
|
| 281 | +* * * |
| 282 | +
|
| 283 | +<section class="references"> |
| 284 | +
|
| 285 | +## References |
| 286 | +
|
| 287 | +- 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]. |
| 288 | +- 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]. |
| 289 | +- Ling, Robert F. 1974. "Comparison of Several Algorithms for Computing Sample Means and Variances." _Journal of the American Statistical Association_ 69 (348). American Statistical Association, Taylor & Francis, Ltd.: 859–66. doi:[10.2307/2286154][@ling:1974a]. |
| 290 | +- 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]. |
| 291 | +
|
| 292 | +</section> |
| 293 | +
|
| 294 | +<!-- /.references --> |
| 295 | +
|
| 296 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 297 | +
|
| 298 | +<section class="related"> |
| 299 | +
|
| 300 | +* * * |
| 301 | +
|
| 302 | +## See Also |
| 303 | +
|
| 304 | +- <span class="package-name">[`@stdlib/stats/strided/dmeanlipw`][@stdlib/stats/strided/dmeanlipw]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation.</span> |
| 305 | +- <span class="package-name">[`@stdlib/stats/strided/smean`][@stdlib/stats/strided/smean]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array.</span> |
| 306 | +- <span class="package-name">[`@stdlib/stats/strided/smeanli`][@stdlib/stats/strided/smeanli]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array using a one-pass trial mean algorithm.</span> |
| 307 | +
|
| 308 | +</section> |
| 309 | +
|
| 310 | +<!-- /.related --> |
| 311 | +
|
| 312 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 313 | +
|
| 314 | +<section class="links"> |
| 315 | +
|
| 316 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 317 | +
|
| 318 | +[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32 |
| 319 | +
|
| 320 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 321 | +
|
| 322 | +[@welford:1962a]: https://doi.org/10.1080/00401706.1962.10490022 |
| 323 | +
|
| 324 | +[@vanreeken:1968a]: https://doi.org/10.1145/362929.362961 |
| 325 | +
|
| 326 | +[@ling:1974a]: https://doi.org/10.2307/2286154 |
| 327 | +
|
| 328 | +[@higham:1993a]: https://doi.org/10.1137/0914050 |
| 329 | +
|
| 330 | +<!-- <related-links> --> |
| 331 | +
|
| 332 | +[@stdlib/stats/strided/dmeanlipw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeanlipw |
| 333 | +
|
| 334 | +[@stdlib/stats/strided/smean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smean |
| 335 | +
|
| 336 | +[@stdlib/stats/strided/smeanli]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smeanli |
| 337 | +
|
| 338 | +<!-- </related-links> --> |
| 339 | +
|
| 340 | +</section> |
| 341 | +
|
| 342 | +<!-- /.links --> |
0 commit comments