|
| 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 | +# dstdev |
| 22 | + |
| 23 | +> Calculate the [standard deviation][standard-deviation] of a double-precision floating-point strided array. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The population [standard deviation][standard-deviation] of a finite size population of size `N` is given by |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:population_standard_deviation" align="center" raw="\sigma = \sqrt{\frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2}" alt="Equation for the population standard deviation."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\sigma = \sqrt{\frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\sigma = \sqrt{\frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2}" data-equation="eq:population_standard_deviation"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@4ed7c2df6f2b8325241396b7c0cd96ceb07d4a1f/lib/node_modules/@stdlib/stats/strided/dstdev/docs/img/equation_population_standard_deviation.svg" alt="Equation for the population standard deviation."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +where the population mean is given by |
| 43 | + |
| 44 | +<!-- <equation class="equation" label="eq:population_mean" align="center" raw="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" alt="Equation for the population mean."> --> |
| 45 | + |
| 46 | +```math |
| 47 | +\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i |
| 48 | +``` |
| 49 | + |
| 50 | +<!-- <div class="equation" align="center" data-raw-text="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" data-equation="eq:population_mean"> |
| 51 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@4ed7c2df6f2b8325241396b7c0cd96ceb07d4a1f/lib/node_modules/@stdlib/stats/strided/dstdev/docs/img/equation_population_mean.svg" alt="Equation for the population mean."> |
| 52 | + <br> |
| 53 | +</div> --> |
| 54 | + |
| 55 | +<!-- </equation> --> |
| 56 | + |
| 57 | +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. If one attempts to use the formula for the population [standard deviation][standard-deviation], the result is biased and yields an **uncorrected sample standard deviation**. To compute a **corrected sample standard deviation** for a sample of size `n`, |
| 58 | + |
| 59 | +<!-- <equation class="equation" label="eq:corrected_sample_standard_deviation" align="center" raw="s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2}" alt="Equation for computing a corrected sample standard deviation."> --> |
| 60 | + |
| 61 | +```math |
| 62 | +s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2} |
| 63 | +``` |
| 64 | + |
| 65 | +<!-- <div class="equation" align="center" data-raw-text="s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2}" data-equation="eq:corrected_sample_standard_deviation"> |
| 66 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@4ed7c2df6f2b8325241396b7c0cd96ceb07d4a1f/lib/node_modules/@stdlib/stats/strided/dstdev/docs/img/equation_corrected_sample_standard_deviation.svg" alt="Equation for computing a corrected sample standard deviation."> |
| 67 | + <br> |
| 68 | +</div> --> |
| 69 | + |
| 70 | +<!-- </equation> --> |
| 71 | + |
| 72 | +where the sample mean is given by |
| 73 | + |
| 74 | +<!-- <equation class="equation" label="eq:sample_mean" align="center" raw="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the sample mean."> --> |
| 75 | + |
| 76 | +```math |
| 77 | +\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i |
| 78 | +``` |
| 79 | + |
| 80 | +<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:sample_mean"> |
| 81 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@4ed7c2df6f2b8325241396b7c0cd96ceb07d4a1f/lib/node_modules/@stdlib/stats/strided/dstdev/docs/img/equation_sample_mean.svg" alt="Equation for the sample mean."> |
| 82 | + <br> |
| 83 | +</div> --> |
| 84 | + |
| 85 | +<!-- </equation> --> |
| 86 | + |
| 87 | +The use of the term `n-1` is commonly referred to as Bessel's correction. Note, however, that applying Bessel's correction can increase the mean squared error between the sample standard deviation and population standard deviation. Depending on the characteristics of the population distribution, other correction factors (e.g., `n-1.5`, `n+1`, etc) can yield better estimators. |
| 88 | + |
| 89 | +</section> |
| 90 | + |
| 91 | +<!-- /.intro --> |
| 92 | + |
| 93 | +<section class="usage"> |
| 94 | + |
| 95 | +## Usage |
| 96 | + |
| 97 | +```javascript |
| 98 | +var dstdev = require( '@stdlib/stats/strided/dstdev' ); |
| 99 | +``` |
| 100 | + |
| 101 | +#### dstdev( N, correction, x, strideX ) |
| 102 | + |
| 103 | +Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array. |
| 104 | + |
| 105 | +```javascript |
| 106 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 107 | + |
| 108 | +var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); |
| 109 | + |
| 110 | +var v = dstdev( x.length, 1, x, 1 ); |
| 111 | +// returns ~2.0817 |
| 112 | +``` |
| 113 | + |
| 114 | +The function has the following parameters: |
| 115 | + |
| 116 | +- **N**: number of indexed elements. |
| 117 | +- **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). |
| 118 | +- **x**: input [`Float64Array`][@stdlib/array/float64]. |
| 119 | +- **strideX**: stride length for `x`. |
| 120 | + |
| 121 | +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`, |
| 122 | + |
| 123 | +```javascript |
| 124 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 125 | + |
| 126 | +var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] ); |
| 127 | + |
| 128 | +var v = dstdev( 4, 1, x, 2 ); |
| 129 | +// returns 2.5 |
| 130 | +``` |
| 131 | + |
| 132 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 133 | + |
| 134 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 135 | + |
| 136 | +```javascript |
| 137 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 138 | + |
| 139 | +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 140 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 141 | + |
| 142 | +var v = dstdev( 4, 1, x1, 2 ); |
| 143 | +// returns 2.5 |
| 144 | +``` |
| 145 | + |
| 146 | +#### dstdev.ndarray( N, correction, x, strideX, offsetX ) |
| 147 | + |
| 148 | +Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array using alternative indexing semantics. |
| 149 | + |
| 150 | +```javascript |
| 151 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 152 | + |
| 153 | +var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); |
| 154 | + |
| 155 | +var v = dstdev.ndarray( x.length, 1, x, 1, 0 ); |
| 156 | +// returns ~2.0817 |
| 157 | +``` |
| 158 | + |
| 159 | +The function has the following additional parameters: |
| 160 | + |
| 161 | +- **offsetX**: starting index for `x`. |
| 162 | + |
| 163 | +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 deviation][standard-deviation] for every other element in `x` starting from the second element |
| 164 | + |
| 165 | +```javascript |
| 166 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 167 | + |
| 168 | +var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 169 | + |
| 170 | +var v = dstdev.ndarray( 4, 1, x, 2, 1 ); |
| 171 | +// returns 2.5 |
| 172 | +``` |
| 173 | + |
| 174 | +</section> |
| 175 | + |
| 176 | +<!-- /.usage --> |
| 177 | + |
| 178 | +<section class="notes"> |
| 179 | + |
| 180 | +## Notes |
| 181 | + |
| 182 | +- If `N <= 0`, both functions return `NaN`. |
| 183 | +- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`. |
| 184 | + |
| 185 | +</section> |
| 186 | + |
| 187 | +<!-- /.notes --> |
| 188 | + |
| 189 | +<section class="examples"> |
| 190 | + |
| 191 | +## Examples |
| 192 | + |
| 193 | +<!-- eslint no-undef: "error" --> |
| 194 | + |
| 195 | +```javascript |
| 196 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 197 | +var dstdev = require( '@stdlib/stats/strided/dstdev' ); |
| 198 | + |
| 199 | +var x = discreteUniform( 10, -50, 50, { |
| 200 | + 'dtype': 'float64' |
| 201 | +}); |
| 202 | +console.log( x ); |
| 203 | + |
| 204 | +var v = dstdev( x.length, 1, x, 1 ); |
| 205 | +console.log( v ); |
| 206 | +``` |
| 207 | + |
| 208 | +</section> |
| 209 | + |
| 210 | +<!-- /.examples --> |
| 211 | + |
| 212 | +<!-- C interface documentation. --> |
| 213 | + |
| 214 | +* * * |
| 215 | + |
| 216 | +<section class="c"> |
| 217 | + |
| 218 | +## C APIs |
| 219 | + |
| 220 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 221 | + |
| 222 | +<section class="intro"> |
| 223 | + |
| 224 | +</section> |
| 225 | + |
| 226 | +<!-- /.intro --> |
| 227 | + |
| 228 | +<!-- C usage documentation. --> |
| 229 | + |
| 230 | +<section class="usage"> |
| 231 | + |
| 232 | +### Usage |
| 233 | + |
| 234 | +```c |
| 235 | +#include "stdlib/stats/strided/dstdev.h" |
| 236 | +``` |
| 237 | + |
| 238 | +#### stdlib_strided_dstdev( N, correction, \*X, strideX ) |
| 239 | + |
| 240 | +Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array. |
| 241 | + |
| 242 | +```c |
| 243 | +const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }; |
| 244 | + |
| 245 | +double v = stdlib_strided_dstdev( 4, 1.0, x, 2 ); |
| 246 | +// returns 2.581989 |
| 247 | +``` |
| 248 | +
|
| 249 | +The function accepts the following arguments: |
| 250 | +
|
| 251 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 252 | +- **correction**: `[in] double` 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). |
| 253 | +- **X**: `[in] double*` input array. |
| 254 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 255 | +
|
| 256 | +```c |
| 257 | +double stdlib_strided_dstdev( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX ); |
| 258 | +``` |
| 259 | + |
| 260 | +#### stdlib_strided_dstdev_ndarray( N, correction, \*X, strideX, offsetX ) |
| 261 | + |
| 262 | +Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array using alternative indexing semantics. |
| 263 | + |
| 264 | +```c |
| 265 | +const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }; |
| 266 | + |
| 267 | +double v = stdlib_strided_dstdev_ndarray( 4, 1.0, x, 2, 0 ); |
| 268 | +// returns 2.581989 |
| 269 | +``` |
| 270 | +
|
| 271 | +The function accepts the following arguments: |
| 272 | +
|
| 273 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 274 | +- **correction**: `[in] double` 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). |
| 275 | +- **X**: `[in] double*` input array. |
| 276 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 277 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 278 | +
|
| 279 | +```c |
| 280 | +double stdlib_strided_dstdev_ndarray( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); |
| 281 | +``` |
| 282 | + |
| 283 | +</section> |
| 284 | + |
| 285 | +<!-- /.usage --> |
| 286 | + |
| 287 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 288 | + |
| 289 | +<section class="notes"> |
| 290 | + |
| 291 | +</section> |
| 292 | + |
| 293 | +<!-- /.notes --> |
| 294 | + |
| 295 | +<!-- C API usage examples. --> |
| 296 | + |
| 297 | +<section class="examples"> |
| 298 | + |
| 299 | +### Examples |
| 300 | + |
| 301 | +```c |
| 302 | +#include "stdlib/stats/strided/dstdev.h" |
| 303 | +#include <stdio.h> |
| 304 | + |
| 305 | +int main( void ) { |
| 306 | + // Create a strided array: |
| 307 | + const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }; |
| 308 | + |
| 309 | + // Specify the number of elements: |
| 310 | + const int N = 4; |
| 311 | + |
| 312 | + // Specify the stride length: |
| 313 | + const int strideX = 2; |
| 314 | + |
| 315 | + // Compute the standard deviation: |
| 316 | + double v = stdlib_strided_dstdev( N, 1.0, x, strideX ); |
| 317 | + |
| 318 | + // Print the result: |
| 319 | + printf( "sample standard deviation: %lf\n", v ); |
| 320 | +} |
| 321 | +``` |
| 322 | +
|
| 323 | +</section> |
| 324 | +
|
| 325 | +<!-- /.examples --> |
| 326 | +
|
| 327 | +</section> |
| 328 | +
|
| 329 | +<!-- /.c --> |
| 330 | +
|
| 331 | +<section class="references"> |
| 332 | +
|
| 333 | +</section> |
| 334 | +
|
| 335 | +<!-- /.references --> |
| 336 | +
|
| 337 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 338 | +
|
| 339 | +<section class="related"> |
| 340 | +
|
| 341 | +* * * |
| 342 | +
|
| 343 | +## See Also |
| 344 | +
|
| 345 | +- <span class="package-name">[`@stdlib/stats/strided/dnanstdev`][@stdlib/stats/strided/dnanstdev]</span><span class="delimiter">: </span><span class="description">calculate the standard deviation of a double-precision floating-point strided array ignoring NaN values.</span> |
| 346 | +- <span class="package-name">[`@stdlib/stats/strided/dvariance`][@stdlib/stats/strided/dvariance]</span><span class="delimiter">: </span><span class="description">calculate the variance of a double-precision floating-point strided array.</span> |
| 347 | +- <span class="package-name">[`@stdlib/stats/base/sstdev`][@stdlib/stats/base/sstdev]</span><span class="delimiter">: </span><span class="description">calculate the standard deviation of a single-precision floating-point strided array.</span> |
| 348 | +- <span class="package-name">[`@stdlib/stats/base/stdev`][@stdlib/stats/base/stdev]</span><span class="delimiter">: </span><span class="description">calculate the standard deviation of a strided array.</span> |
| 349 | +
|
| 350 | +</section> |
| 351 | +
|
| 352 | +<!-- /.related --> |
| 353 | +
|
| 354 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 355 | +
|
| 356 | +<section class="links"> |
| 357 | +
|
| 358 | +[standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation |
| 359 | +
|
| 360 | +[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64 |
| 361 | +
|
| 362 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 363 | +
|
| 364 | +<!-- <related-links> --> |
| 365 | +
|
| 366 | +[@stdlib/stats/strided/dnanstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanstdev |
| 367 | +
|
| 368 | +[@stdlib/stats/strided/dvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dvariance |
| 369 | +
|
| 370 | +[@stdlib/stats/base/sstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/sstdev |
| 371 | +
|
| 372 | +[@stdlib/stats/base/stdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/stdev |
| 373 | +
|
| 374 | +<!-- </related-links> --> |
| 375 | +
|
| 376 | +</section> |
| 377 | +
|
| 378 | +<!-- /.links --> |
0 commit comments