time-to-botec

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

README.md (5682B)


      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 # incrmcovariance
     22 
     23 > Compute a moving [unbiased sample covariance][covariance] incrementally.
     24 
     25 <section class="intro">
     26 
     27 For unknown population means, the [unbiased sample covariance][covariance] for a window `n` of size `W` is defined as
     28 
     29 <!-- <equation class="equation" label="eq:unbiased_sample_covariance_unknown_means" align="center" raw="\operatorname{cov_n} = \frac{1}{n-1} \sum_{i=j}^{j+W-1} (x_i - \bar{x}_n)(y_i - \bar{y}_n)" alt="Equation for the unbiased sample covariance for unknown population means."> -->
     30 
     31 <div class="equation" align="center" data-raw-text="\operatorname{cov_n} = \frac{1}{n-1} \sum_{i=j}^{j+W-1} (x_i - \bar{x}_n)(y_i - \bar{y}_n)" data-equation="eq:unbiased_sample_covariance_unknown_means">
     32     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mcovariance/docs/img/equation_unbiased_sample_covariance_unknown_means.svg" alt="Equation for the unbiased sample covariance for unknown population means.">
     33     <br>
     34 </div>
     35 
     36 <!-- </equation> -->
     37 
     38 where `j` specifies the index of the value at which the window begins. For example, for a trailing (i.e., non-centered) window using zero-based indexing and `j` greater than or equal to `W`, `j` is the `n-W`th value with `n` being the number of values thus analyzed.
     39 
     40 For known population means, the [unbiased sample covariance][covariance] for a window `n` of size `W` is defined as
     41 
     42 <!-- <equation class="equation" label="eq:unbiased_sample_covariance_known_means" align="center" raw="\operatorname{cov_n} = \frac{1}{n} \sum_{i=j}^{j+W-1} (x_i - \mu_x)(y_i - \mu_y)" alt="Equation for the unbiased sample covariance for known population means."> -->
     43 
     44 <div class="equation" align="center" data-raw-text="\operatorname{cov_n} = \frac{1}{n} \sum_{i=j}^{j+W-1} (x_i - \mu_x)(y_i - \mu_y)" data-equation="eq:unbiased_sample_covariance_known_means">
     45     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@27e2a43c70db648bb5bbc3fd0cdee050c25adc0b/lib/node_modules/@stdlib/stats/incr/mcovariance/docs/img/equation_unbiased_sample_covariance_known_means.svg" alt="Equation for the unbiased sample covariance for known population means.">
     46     <br>
     47 </div>
     48 
     49 <!-- </equation> --> 
     50 
     51 </section>
     52 
     53 <!-- /.intro -->
     54 
     55 <section class="usage">
     56 
     57 ## Usage
     58 
     59 ```javascript
     60 var incrmcovariance = require( '@stdlib/stats/incr/mcovariance' );
     61 ```
     62 
     63 #### incrmcovariance( window\[, mx, my] )
     64 
     65 Returns an accumulator `function` which incrementally computes a moving [unbiased sample covariance][covariance]. The `window` parameter defines the number of values over which to compute the moving [unbiased sample covariance][covariance].
     66 
     67 ```javascript
     68 var accumulator = incrmcovariance( 3 );
     69 ```
     70 
     71 If means are already known, provide `mx` and `my` arguments.
     72 
     73 ```javascript
     74 var accumulator = incrmcovariance( 3, 5.0, -3.14 );
     75 ```
     76 
     77 #### accumulator( \[x, y] )
     78 
     79 If provided input values `x` and `y`, the accumulator function returns an updated [unbiased sample covariance][covariance]. If not provided input values `x` and `y`, the accumulator function returns the current [unbiased sample covariance][covariance].
     80 
     81 ```javascript
     82 var accumulator = incrmcovariance( 3 );
     83 
     84 var v = accumulator();
     85 // returns null
     86 
     87 // Fill the window...
     88 v = accumulator( 2.0, 1.0 ); // [(2.0, 1.0)]
     89 // returns 0.0
     90 
     91 v = accumulator( -5.0, 3.14 ); // [(2.0, 1.0), (-5.0, 3.14)]
     92 // returns ~-7.49
     93 
     94 v = accumulator( 3.0, -1.0 ); // [(2.0, 1.0), (-5.0, 3.14), (3.0, -1.0)]
     95 // returns -8.35
     96 
     97 // Window begins sliding...
     98 v = accumulator( 5.0, -9.5 ); // [(-5.0, 3.14), (3.0, -1.0), (5.0, -9.5)]
     99 // returns -29.42
    100 
    101 v = accumulator( -5.0, 1.5 ); // [(3.0, -1.0), (5.0, -9.5), (-5.0, 1.5)]
    102 // returns -24.5
    103 
    104 v = accumulator();
    105 // returns -24.5
    106 ```
    107 
    108 </section>
    109 
    110 <!-- /.usage -->
    111 
    112 <section class="notes">
    113 
    114 ## Notes
    115 
    116 -   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.
    117 -   As `W` (x,y) pairs 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.
    118 
    119 </section>
    120 
    121 <!-- /.notes -->
    122 
    123 <section class="examples">
    124 
    125 ## Examples
    126 
    127 <!-- eslint no-undef: "error" -->
    128 
    129 ```javascript
    130 var randu = require( '@stdlib/random/base/randu' );
    131 var incrmcovariance = require( '@stdlib/stats/incr/mcovariance' );
    132 
    133 var accumulator;
    134 var x;
    135 var y;
    136 var i;
    137 
    138 // Initialize an accumulator:
    139 accumulator = incrmcovariance( 5 );
    140 
    141 // For each simulated datum, update the moving unbiased sample covariance...
    142 for ( i = 0; i < 100; i++ ) {
    143     x = randu() * 100.0;
    144     y = randu() * 100.0;
    145     accumulator( x, y );
    146 }
    147 console.log( accumulator() );
    148 ```
    149 
    150 </section>
    151 
    152 <!-- /.examples -->
    153 
    154 <section class="links">
    155 
    156 [covariance]: https://en.wikipedia.org/wiki/Covariance
    157 
    158 </section>
    159 
    160 <!-- /.links -->