|
| 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 | +# dmeanli |
| 22 | + |
| 23 | +> Compute the [arithmetic mean][arithmetic-mean] of a one-dimensional double-precision floating-point ndarray using a one-pass trial mean 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@42d8f64d805113ab899c79c7c39d6c6bac7fe25c/lib/node_modules/@stdlib/stats/base/ndarray/mean/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 dmeanli = require( '@stdlib/stats/base/ndarray/dmeanli' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### dmeanli( arrays ) |
| 55 | + |
| 56 | +Computes the [arithmetic mean][arithmetic-mean] of a one-dimensional double-precision floating-point ndarray using a one-pass trial mean algorithm. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 60 | +var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| 61 | + |
| 62 | +var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] ); |
| 63 | +var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); |
| 64 | + |
| 65 | +var v = dmeanli( [ x ] ); |
| 66 | +// returns 2.5 |
| 67 | +``` |
| 68 | + |
| 69 | +The function has the following parameters: |
| 70 | + |
| 71 | +- **arrays**: array-like object containing a one-dimensional input ndarray. |
| 72 | + |
| 73 | +</section> |
| 74 | + |
| 75 | +<!-- /.usage --> |
| 76 | + |
| 77 | +<section class="notes"> |
| 78 | + |
| 79 | +## Notes |
| 80 | + |
| 81 | +- If provided an empty one-dimensional ndarray, the function returns `NaN`. |
| 82 | +- The underlying algorithm is a specialized case of Welford's algorithm. Similar to the method of assumed mean, the first ndarray 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). |
| 83 | + |
| 84 | +</section> |
| 85 | + |
| 86 | +<!-- /.notes --> |
| 87 | + |
| 88 | +<section class="examples"> |
| 89 | + |
| 90 | +## Examples |
| 91 | + |
| 92 | +<!-- eslint no-undef: "error" --> |
| 93 | + |
| 94 | +```javascript |
| 95 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 96 | +var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| 97 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 98 | +var dmeanli = require( '@stdlib/stats/base/ndarray/dmeanli' ); |
| 99 | + |
| 100 | +var xbuf = discreteUniform( 10, -50, 50, { |
| 101 | + 'dtype': 'float64' |
| 102 | +}); |
| 103 | +var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 104 | +console.log( ndarray2array( x ) ); |
| 105 | + |
| 106 | +var v = dmeanli( [ x ] ); |
| 107 | +console.log( v ); |
| 108 | +``` |
| 109 | + |
| 110 | +</section> |
| 111 | + |
| 112 | +<!-- /.examples --> |
| 113 | + |
| 114 | +* * * |
| 115 | + |
| 116 | +<section class="references"> |
| 117 | + |
| 118 | +## References |
| 119 | + |
| 120 | +- 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]. |
| 121 | +- 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]. |
| 122 | +- 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]. |
| 123 | + |
| 124 | +</section> |
| 125 | + |
| 126 | +<!-- /.references --> |
| 127 | + |
| 128 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 129 | + |
| 130 | +<section class="related"> |
| 131 | + |
| 132 | +</section> |
| 133 | + |
| 134 | +<!-- /.related --> |
| 135 | + |
| 136 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 137 | + |
| 138 | +<section class="links"> |
| 139 | + |
| 140 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 141 | + |
| 142 | +[@welford:1962a]: https://doi.org/10.1080/00401706.1962.10490022 |
| 143 | + |
| 144 | +[@vanreeken:1968a]: https://doi.org/10.1145/362929.362961 |
| 145 | + |
| 146 | +[@ling:1974a]: https://doi.org/10.2307/2286154 |
| 147 | + |
| 148 | +</section> |
| 149 | + |
| 150 | +<!-- /.links --> |
0 commit comments