time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (10133B)


      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 <!--lint disable maximum-heading-length-->
     22 
     23 # dmeanstdev
     24 
     25 > Calculate the [mean][arithmetic-mean] and [standard deviation][standard-deviation] of a double-precision floating-point strided array.
     26 
     27 <section class="intro">
     28 
     29 The population [standard deviation][standard-deviation] of a finite size population of size `N` is given by
     30 
     31 <!-- <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."> -->
     32 
     33 <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">
     34     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@fc1d35912f15aaa6bfb58aadc07a842b1239c98c/lib/node_modules/@stdlib/stats/base/dmeanstdev/docs/img/equation_population_standard_deviation.svg" alt="Equation for the population standard deviation.">
     35     <br>
     36 </div>
     37 
     38 <!-- </equation> -->
     39 
     40 where the population mean is given by
     41 
     42 <!-- <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."> -->
     43 
     44 <div class="equation" align="center" data-raw-text="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" data-equation="eq:population_mean">
     45     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@fc1d35912f15aaa6bfb58aadc07a842b1239c98c/lib/node_modules/@stdlib/stats/base/dmeanstdev/docs/img/equation_population_mean.svg" alt="Equation for the population mean.">
     46     <br>
     47 </div>
     48 
     49 <!-- </equation> -->
     50 
     51 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`,
     52 
     53 <!-- <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."> -->
     54 
     55 <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">
     56     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@fc1d35912f15aaa6bfb58aadc07a842b1239c98c/lib/node_modules/@stdlib/stats/base/dmeanstdev/docs/img/equation_corrected_sample_standard_deviation.svg" alt="Equation for computing a corrected sample standard deviation.">
     57     <br>
     58 </div>
     59 
     60 <!-- </equation> -->
     61 
     62 where the sample mean is given by
     63 
     64 <!-- <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."> -->
     65 
     66 <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">
     67     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@fc1d35912f15aaa6bfb58aadc07a842b1239c98c/lib/node_modules/@stdlib/stats/base/dmeanstdev/docs/img/equation_sample_mean.svg" alt="Equation for the sample mean.">
     68     <br>
     69 </div>
     70 
     71 <!-- </equation> -->
     72 
     73 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.
     74 
     75 </section>
     76 
     77 <!-- /.intro -->
     78 
     79 <section class="usage">
     80 
     81 ## Usage
     82 
     83 ```javascript
     84 var dmeanstdev = require( '@stdlib/stats/base/dmeanstdev' );
     85 ```
     86 
     87 #### dmeanstdev( N, correction, x, strideX, out, strideOut )
     88 
     89 Computes the [mean][arithmetic-mean] and [standard deviation][standard-deviation] of a double-precision floating-point strided array `x`.
     90 
     91 ```javascript
     92 var Float64Array = require( '@stdlib/array/float64' );
     93 
     94 var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
     95 var out = new Float64Array( 2 );
     96 
     97 var v = dmeanstdev( x.length, 1, x, 1, out, 1 );
     98 // returns <Float64Array>[ ~0.3333, ~2.0817 ]
     99 
    100 var bool = ( v === out );
    101 // returns true
    102 ```
    103 
    104 The function has the following parameters:
    105 
    106 -   **N**: number of indexed elements.
    107 -   **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).
    108 -   **x**: input [`Float64Array`][@stdlib/array/float64].
    109 -   **strideX**: index increment for `x`.
    110 -   **out**: output [`Float64Array`][@stdlib/array/float64] for storing results.
    111 -   **strideOut**: index increment for `out`.
    112 
    113 The `N` and `stride` parameters determine which elements are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`,
    114 
    115 ```javascript
    116 var Float64Array = require( '@stdlib/array/float64' );
    117 var floor = require( '@stdlib/math/base/special/floor' );
    118 
    119 var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
    120 var out = new Float64Array( 2 );
    121 var N = floor( x.length / 2 );
    122 
    123 var v = dmeanstdev( N, 1, x, 2, out, 1 );
    124 // returns <Float64Array>[ 1.25, 2.5 ]
    125 ```
    126 
    127 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
    128 
    129 <!-- eslint-disable stdlib/capitalized-comments -->
    130 
    131 ```javascript
    132 var Float64Array = require( '@stdlib/array/float64' );
    133 var floor = require( '@stdlib/math/base/special/floor' );
    134 
    135 var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
    136 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
    137 
    138 var out0 = new Float64Array( 4 );
    139 var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
    140 
    141 var N = floor( x0.length / 2 );
    142 
    143 var v = dmeanstdev( N, 1, x1, 2, out1, 1 );
    144 // returns <Float64Array>[ 1.25, 2.5 ]
    145 ```
    146 
    147 #### dmeanstdev.ndarray( N, correction, x, strideX, offsetX, out, strideOut, offsetOut )
    148 
    149 Computes the [mean][arithmetic-mean] and [standard deviation][standard-deviation] of a double-precision floating-point strided array and alternative indexing semantics.
    150 
    151 ```javascript
    152 var Float64Array = require( '@stdlib/array/float64' );
    153 
    154 var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
    155 var out = new Float64Array( 2 );
    156 
    157 var v = dmeanstdev.ndarray( x.length, 1, x, 1, 0, out, 1, 0 );
    158 // returns <Float64Array>[ ~0.3333, ~2.0817 ]
    159 ```
    160 
    161 The function has the following additional parameters:
    162 
    163 -   **offsetX**: starting index for `x`.
    164 -   **offsetOut**: starting index for `out`.
    165 
    166 While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameters support indexing semantics based on a starting index. For example, to calculate the [mean][arithmetic-mean] and [standard deviation][standard-deviation] for every other value in `x` starting from the second value
    167 
    168 ```javascript
    169 var Float64Array = require( '@stdlib/array/float64' );
    170 var floor = require( '@stdlib/math/base/special/floor' );
    171 
    172 var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
    173 var out = new Float64Array( 4 );
    174 var N = floor( x.length / 2 );
    175 
    176 var v = dmeanstdev.ndarray( N, 1, x, 2, 1, out, 2, 1 );
    177 // returns <Float64Array>[ 0.0, 1.25, 0.0, 2.5 ]
    178 ```
    179 
    180 </section>
    181 
    182 <!-- /.usage -->
    183 
    184 <section class="notes">
    185 
    186 ## Notes
    187 
    188 -   If `N <= 0`, both functions return a [mean][arithmetic-mean] and [standard deviation][standard-deviation] equal to `NaN`.
    189 -   If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return a [standard deviation][standard-deviation] equal to `NaN`.
    190 
    191 </section>
    192 
    193 <!-- /.notes -->
    194 
    195 <section class="examples">
    196 
    197 ## Examples
    198 
    199 <!-- eslint no-undef: "error" -->
    200 
    201 ```javascript
    202 var randu = require( '@stdlib/random/base/randu' );
    203 var round = require( '@stdlib/math/base/special/round' );
    204 var Float64Array = require( '@stdlib/array/float64' );
    205 var dmeanstdev = require( '@stdlib/stats/base/dmeanstdev' );
    206 
    207 var out;
    208 var x;
    209 var i;
    210 
    211 x = new Float64Array( 10 );
    212 for ( i = 0; i < x.length; i++ ) {
    213     x[ i ] = round( (randu()*100.0) - 50.0 );
    214 }
    215 console.log( x );
    216 
    217 out = new Float64Array( 2 );
    218 dmeanstdev( x.length, 1, x, 1, out, 1 );
    219 console.log( out );
    220 ```
    221 
    222 </section>
    223 
    224 <!-- /.examples -->
    225 
    226 <section class="references">
    227 
    228 </section>
    229 
    230 <!-- /.references -->
    231 
    232 <section class="links">
    233 
    234 [arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
    235 
    236 [standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation
    237 
    238 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array-float64
    239 
    240 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    241 
    242 </section>
    243 
    244 <!-- /.links -->