time-to-botec

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

README.md (5268B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2018 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 # incrmeanvar
     22 
     23 > Compute an [arithmetic mean][arithmetic-mean] and an [unbiased sample variance][sample-variance] incrementally.
     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="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the arithmetic mean."> -->
     30 
     31 <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:arithmetic_mean">
     32     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@b2df03cb2a582cf1df289c3ddca6922c0db854b4/lib/node_modules/@stdlib/stats/incr/meanvar/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean.">
     33     <br>
     34 </div>
     35 
     36 <!-- </equation> -->
     37 
     38 and the [unbiased sample variance][sample-variance] is defined as
     39 
     40 <!-- <equation class="equation" label="eq:unbiased_sample_variance" align="center" raw="s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2" alt="Equation for the unbiased sample variance."> -->
     41 
     42 <div class="equation" align="center" data-raw-text="s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2" data-equation="eq:unbiased_sample_variance">
     43     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@eafa6e61d15b7c712c9288d59d8e0e3f0aa6c011/lib/node_modules/@stdlib/stats/incr/meanvar/docs/img/equation_unbiased_sample_variance.svg" alt="Equation for the unbiased sample variance.">
     44     <br>
     45 </div>
     46 
     47 <!-- </equation> -->
     48 
     49 <section class="usage">
     50 
     51 ## Usage
     52 
     53 ```javascript
     54 var incrmeanvar = require( '@stdlib/stats/incr/meanvar' );
     55 ```
     56 
     57 #### incrmeanvar( \[out] )
     58 
     59 Returns an accumulator `function` which incrementally computes an [arithmetic mean][arithmetic-mean] and [unbiased sample variance][sample-variance].
     60 
     61 ```javascript
     62 var accumulator = incrmeanvar();
     63 ```
     64 
     65 By default, the returned accumulator `function` returns the accumulated values as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object.
     66 
     67 ```javascript
     68 var Float64Array = require( '@stdlib/array/float64' );
     69 
     70 var accumulator = incrmeanvar( new Float64Array( 2 ) );
     71 ```
     72 
     73 #### accumulator( \[x] )
     74 
     75 If provided an input value `x`, the accumulator function returns updated accumulated values. If not provided an input value `x`, the accumulator function returns the current accumulated values.
     76 
     77 ```javascript
     78 var accumulator = incrmeanvar();
     79 
     80 var mv = accumulator();
     81 // returns null
     82 
     83 mv = accumulator( 2.0 );
     84 // returns [ 2.0, 0.0 ]
     85 
     86 mv = accumulator( 1.0 );
     87 // returns [ 1.5, 0.5 ]
     88 
     89 mv = accumulator( 3.0 );
     90 // returns [ 2.0, 1.0 ]
     91 
     92 mv = accumulator( -7.0 );
     93 // returns [ -0.25, ~20.92 ]
     94 
     95 mv = accumulator( -5.0 );
     96 // returns [ -1.2, 20.2 ]
     97 
     98 mv = accumulator();
     99 // returns [ -1.2, 20.2 ]
    100 ```
    101 
    102 </section>
    103 
    104 <!-- /.usage -->
    105 
    106 <section class="notes">
    107 
    108 ## Notes
    109 
    110 -   Input values are **not** type checked. If provided `NaN`, the accumulated values are equal to `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
    111 
    112 </section>
    113 
    114 <!-- /.notes -->
    115 
    116 <section class="examples">
    117 
    118 ## Examples
    119 
    120 <!-- eslint no-undef: "error" -->
    121 
    122 ```javascript
    123 var randu = require( '@stdlib/random/base/randu' );
    124 var Float64Array = require( '@stdlib/array/float64' );
    125 var ArrayBuffer = require( '@stdlib/array/buffer' );
    126 var incrmeanvar = require( '@stdlib/stats/incr/meanvar' );
    127 
    128 var offset;
    129 var acc;
    130 var buf;
    131 var out;
    132 var mv;
    133 var N;
    134 var v;
    135 var i;
    136 var j;
    137 
    138 // Define the number of accumulators:
    139 N = 5;
    140 
    141 // Create an array buffer for storing accumulator output:
    142 buf = new ArrayBuffer( N*2*8 ); // 8 bytes per element
    143 
    144 // Initialize accumulators:
    145 acc = [];
    146 for ( i = 0; i < N; i++ ) {
    147     // Compute the byte offset:
    148     offset = i * 2 * 8; // stride=2, bytes_per_element=8
    149 
    150     // Create a new view for storing accumulated values:
    151     out = new Float64Array( buf, offset, 2 );
    152 
    153     // Initialize an accumulator which will write results to the view:
    154     acc.push( incrmeanvar( out ) );
    155 }
    156 
    157 // Simulate data and update the sample means and variances...
    158 for ( i = 0; i < 100; i++ ) {
    159     for ( j = 0; j < N; j++ ) {
    160         v = randu() * 100.0 * (j+1);
    161         acc[ j ]( v );
    162     }
    163 }
    164 
    165 // Print the final results:
    166 console.log( 'Mean\tVariance' );
    167 for ( i = 0; i < N; i++ ) {
    168     mv = acc[ i ]();
    169     console.log( '%d\t%d', mv[ 0 ].toFixed( 3 ), mv[ 1 ].toFixed( 3 ) );
    170 }
    171 ```
    172 
    173 </section>
    174 
    175 <!-- /.examples -->
    176 
    177 <section class="links">
    178 
    179 [arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
    180 
    181 [sample-variance]: https://en.wikipedia.org/wiki/Variance
    182 
    183 </section>
    184 
    185 <!-- /.links -->