time-to-botec

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

README.md (4102B)


      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 # incrmvariance
     22 
     23 > Compute a moving [unbiased sample variance][sample-variance] incrementally.
     24 
     25 <section class="intro">
     26 
     27 For a window of size `W`, the [unbiased sample variance][sample-variance] is defined as
     28 
     29 <!-- <equation class="equation" label="eq:unbiased_sample_variance" align="center" raw="s^2 = \frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2" alt="Equation for the unbiased sample variance."> -->
     30 
     31 <div class="equation" align="center" data-raw-text="s^2 = \frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2" data-equation="eq:unbiased_sample_variance">
     32     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mvariance/docs/img/equation_unbiased_sample_variance.svg" alt="Equation for the unbiased sample variance.">
     33     <br>
     34 </div>
     35 
     36 <!-- </equation> -->
     37 
     38 </section>
     39 
     40 <!-- /.intro -->
     41 
     42 <section class="usage">
     43 
     44 ## Usage
     45 
     46 ```javascript
     47 var incrmvariance = require( '@stdlib/stats/incr/mvariance' );
     48 ```
     49 
     50 #### incrmvariance( window\[, mean] )
     51 
     52 Returns an accumulator `function` which incrementally computes a moving [unbiased sample variance][sample-variance]. The `window` parameter defines the number of values over which to compute the moving [unbiased sample variance][sample-variance].
     53 
     54 ```javascript
     55 var accumulator = incrmvariance( 3 );
     56 ```
     57 
     58 If the mean is already known, provide a `mean` argument.
     59 
     60 ```javascript
     61 var accumulator = incrmvariance( 3, 5.0 );
     62 ```
     63 
     64 #### accumulator( \[x] )
     65 
     66 If provided an input value `x`, the accumulator function returns an updated [unbiased sample variance][sample-variance]. If not provided an input value `x`, the accumulator function returns the current [unbiased sample variance][sample-variance].
     67 
     68 ```javascript
     69 var accumulator = incrmvariance( 3 );
     70 
     71 var s2 = accumulator();
     72 // returns null
     73 
     74 // Fill the window...
     75 s2 = accumulator( 2.0 ); // [2.0]
     76 // returns 0.0
     77 
     78 s2 = accumulator( 1.0 ); // [2.0, 1.0]
     79 // returns 0.5
     80 
     81 s2 = accumulator( 3.0 ); // [2.0, 1.0, 3.0]
     82 // returns 1.0
     83 
     84 // Window begins sliding...
     85 s2 = accumulator( -7.0 ); // [1.0, 3.0, -7.0]
     86 // returns 28.0
     87 
     88 s2 = accumulator( -5.0 ); // [3.0, -7.0, -5.0]
     89 // returns 28.0
     90 
     91 s2 = accumulator();
     92 // returns 28.0
     93 ```
     94 
     95 </section>
     96 
     97 <!-- /.usage -->
     98 
     99 <section class="notes">
    100 
    101 ## Notes
    102 
    103 -   Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **at least** `W-1` 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.
    104 -   As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values.
    105 
    106 </section>
    107 
    108 <!-- /.notes -->
    109 
    110 <section class="examples">
    111 
    112 ## Examples
    113 
    114 <!-- eslint no-undef: "error" -->
    115 
    116 ```javascript
    117 var randu = require( '@stdlib/random/base/randu' );
    118 var incrmvariance = require( '@stdlib/stats/incr/mvariance' );
    119 
    120 var accumulator;
    121 var v;
    122 var i;
    123 
    124 // Initialize an accumulator:
    125 accumulator = incrmvariance( 5 );
    126 
    127 // For each simulated datum, update the moving unbiased sample variance...
    128 for ( i = 0; i < 100; i++ ) {
    129     v = randu() * 100.0;
    130     accumulator( v );
    131 }
    132 console.log( accumulator() );
    133 ```
    134 
    135 </section>
    136 
    137 <!-- /.examples -->
    138 
    139 <section class="links">
    140 
    141 [sample-variance]: https://en.wikipedia.org/wiki/Variance
    142 
    143 </section>
    144 
    145 <!-- /.links -->