README.md (9499B)
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 # dsemch 22 23 > Calculate the [standard error of the mean][standard-error] of a double-precision floating-point strided array using a one-pass trial mean algorithm. 24 25 <section class="intro"> 26 27 The [standard error of the mean][standard-error] of a finite size sample of size `n` is given by 28 29 <!-- <equation class="equation" label="eq:standard_error_of_the_mean" align="center" raw="\sigma_{\bar{x}} = \frac{\sigma}{\sqrt{n}}" alt="Equation for the standard error of the mean."> --> 30 31 <div class="equation" align="center" data-raw-text="\sigma_{\bar{x}} = \frac{\sigma}{\sqrt{n}}" data-equation="eq:standard_error_of_the_mean"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@3ebbbfc49c54971356c0cf8f6282e6720cb07755/lib/node_modules/@stdlib/stats/base/dsemch/docs/img/equation_standard_error_of_the_mean.svg" alt="Equation for the standard error of the mean."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 where `σ` is the population [standard deviation][standard-deviation]. 39 40 Often in the analysis of data, the true population [standard deviation][standard-deviation] is not known _a priori_ and must be estimated from a sample drawn from the population distribution. In this scenario, one must use a sample [standard deviation][standard-deviation] to compute an estimate for the [standard error of the mean][standard-error] 41 42 <!-- <equation class="equation" label="eq:standard_error_of_the_mean_estimate" align="center" raw="\sigma_{\bar{x}} \approx \frac{s}{\sqrt{n}}" alt="Equation for estimating the standard error of the mean."> --> 43 44 <div class="equation" align="center" data-raw-text="\sigma_{\bar{x}} \approx \frac{s}{\sqrt{n}}" data-equation="eq:standard_error_of_the_mean_estimate"> 45 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@3ebbbfc49c54971356c0cf8f6282e6720cb07755/lib/node_modules/@stdlib/stats/base/dsemch/docs/img/equation_standard_error_of_the_mean_estimate.svg" alt="Equation for estimating the standard error of the mean."> 46 <br> 47 </div> 48 49 <!-- </equation> --> 50 51 where `s` is the sample [standard deviation][standard-deviation]. 52 53 </section> 54 55 <!-- /.intro --> 56 57 <section class="usage"> 58 59 ## Usage 60 61 ```javascript 62 var dsemch = require( '@stdlib/stats/base/dsemch' ); 63 ``` 64 65 #### dsemch( N, correction, x, stride ) 66 67 Computes the [standard error of the mean][standard-error] of a double-precision floating-point strided array `x` using a one-pass trial mean algorithm. 68 69 ```javascript 70 var Float64Array = require( '@stdlib/array/float64' ); 71 72 var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); 73 var N = x.length; 74 75 var v = dsemch( N, 1, x, 1 ); 76 // returns ~1.20185 77 ``` 78 79 The function has the following parameters: 80 81 - **N**: number of indexed elements. 82 - **correction**: degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the corrected sample [standard deviation][standard-deviation], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). 83 - **x**: input [`Float64Array`][@stdlib/array/float64]. 84 - **stride**: index increment for `x`. 85 86 The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [standard error of the mean][standard-error] of every other element in `x`, 87 88 ```javascript 89 var Float64Array = require( '@stdlib/array/float64' ); 90 var floor = require( '@stdlib/math/base/special/floor' ); 91 92 var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] ); 93 var N = floor( x.length / 2 ); 94 95 var v = dsemch( N, 1, x, 2 ); 96 // returns 1.25 97 ``` 98 99 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 100 101 <!-- eslint-disable stdlib/capitalized-comments --> 102 103 ```javascript 104 var Float64Array = require( '@stdlib/array/float64' ); 105 var floor = require( '@stdlib/math/base/special/floor' ); 106 107 var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 108 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 109 110 var N = floor( x0.length / 2 ); 111 112 var v = dsemch( N, 1, x1, 2 ); 113 // returns 1.25 114 ``` 115 116 #### dsemch.ndarray( N, correction, x, stride, offset ) 117 118 Computes the [standard error of the mean][standard-error] of a double-precision floating-point strided array using a one-pass trial mean algorithm and alternative indexing semantics. 119 120 ```javascript 121 var Float64Array = require( '@stdlib/array/float64' ); 122 123 var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); 124 var N = x.length; 125 126 var v = dsemch.ndarray( N, 1, x, 1, 0 ); 127 // returns ~1.20185 128 ``` 129 130 The function has the following additional parameters: 131 132 - **offset**: starting index for `x`. 133 134 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 [standard error of the mean][standard-error] for every other value in `x` starting from the second value 135 136 ```javascript 137 var Float64Array = require( '@stdlib/array/float64' ); 138 var floor = require( '@stdlib/math/base/special/floor' ); 139 140 var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 141 var N = floor( x.length / 2 ); 142 143 var v = dsemch.ndarray( N, 1, x, 2, 1 ); 144 // returns 1.25 145 ``` 146 147 </section> 148 149 <!-- /.usage --> 150 151 <section class="notes"> 152 153 ## Notes 154 155 - If `N <= 0`, both functions return `NaN`. 156 - If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`. 157 - The underlying algorithm is a specialized case of Neely's two-pass algorithm. As the standard deviation is invariant with respect to changes in the location parameter, the underlying algorithm uses the first strided array element as a trial mean to shift subsequent data values and thus mitigate catastrophic cancellation. 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). 158 159 </section> 160 161 <!-- /.notes --> 162 163 <section class="examples"> 164 165 ## Examples 166 167 <!-- eslint no-undef: "error" --> 168 169 ```javascript 170 var randu = require( '@stdlib/random/base/randu' ); 171 var round = require( '@stdlib/math/base/special/round' ); 172 var Float64Array = require( '@stdlib/array/float64' ); 173 var dsemch = require( '@stdlib/stats/base/dsemch' ); 174 175 var x; 176 var i; 177 178 x = new Float64Array( 10 ); 179 for ( i = 0; i < x.length; i++ ) { 180 x[ i ] = round( (randu()*100.0) - 50.0 ); 181 } 182 console.log( x ); 183 184 var v = dsemch( x.length, 1, x, 1 ); 185 console.log( v ); 186 ``` 187 188 </section> 189 190 <!-- /.examples --> 191 192 * * * 193 194 <section class="references"> 195 196 ## References 197 198 - 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]. 199 - 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]. 200 - Chan, Tony F., Gene H. Golub, and Randall J. LeVeque. 1983. "Algorithms for Computing the Sample Variance: Analysis and Recommendations." _The American Statistician_ 37 (3). American Statistical Association, Taylor & Francis, Ltd.: 242–47. doi:[10.1080/00031305.1983.10483115][@chan:1983a]. 201 - 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]. 202 203 </section> 204 205 <!-- /.references --> 206 207 <section class="links"> 208 209 [standard-error]: https://en.wikipedia.org/wiki/Standard_error 210 211 [standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation 212 213 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array-float64 214 215 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 216 217 [@neely:1966a]: https://doi.org/10.1145/365719.365958 218 219 [@ling:1974a]: https://doi.org/10.2307/2286154 220 221 [@chan:1983a]: https://doi.org/10.1080/00031305.1983.10483115 222 223 [@schubert:2018a]: https://doi.org/10.1145/3221269.3223036 224 225 </section> 226 227 <!-- /.links -->