README.md (8978B)
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 # dsemtk 22 23 > Calculate the [standard error of the mean][standard-error] of a double-precision floating-point strided array using a one-pass textbook 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@5801695664293426455789e96b013ef4320d0569/lib/node_modules/@stdlib/stats/base/dsemtk/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@5801695664293426455789e96b013ef4320d0569/lib/node_modules/@stdlib/stats/base/dsemtk/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 dsemtk = require( '@stdlib/stats/base/dsemtk' ); 63 ``` 64 65 #### dsemtk( 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 textbook 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 = dsemtk( 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 = dsemtk( 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 = dsemtk( N, 1, x1, 2 ); 113 // returns 1.25 114 ``` 115 116 #### dsemtk.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 textbook 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 = dsemtk.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 = dsemtk.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 - Some caution should be exercised when using the one-pass textbook algorithm. Literature overwhelmingly discourages the algorithm's use for two reasons: 1) the lack of safeguards against underflow and overflow and 2) the risk of catastrophic cancellation. These concerns have merit; however, the one-pass textbook algorithm should not be dismissed outright. For data distributions with a moderately large standard deviation to mean ratio (i.e., **coefficient of variation**), the one-pass textbook algorithm may be acceptable, especially when performance is paramount and some precision loss is acceptable (including a risk of computing a negative variance due to floating-point rounding errors!). In short, no single "best" algorithm for computing the standard error of the mean exists. The "best" algorithm depends on the underlying data distribution, your performance requirements, and your minimum precision requirements. When evaluating which algorithm to use, consider the relative pros and cons, and choose the algorithm which best serves your needs. 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 dsemtk = require( '@stdlib/stats/base/dsemtk' ); 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 = dsemtk( 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 - 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]. 199 200 </section> 201 202 <!-- /.references --> 203 204 <section class="links"> 205 206 [standard-error]: https://en.wikipedia.org/wiki/Standard_error 207 208 [standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation 209 210 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array-float64 211 212 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 213 214 [@ling:1974a]: https://doi.org/10.2307/2286154 215 216 </section> 217 218 <!-- /.links -->