README.md (4576B)
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 # incrcovariance 22 23 > Compute an [unbiased sample covariance][covariance] incrementally. 24 25 <section class="intro"> 26 27 For unknown population means, the [unbiased sample covariance][covariance] 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=0}^{n-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=0}^{n-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/covariance/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 For known population means, the [unbiased sample covariance][covariance] is defined as 39 40 <!-- <equation class="equation" label="eq:unbiased_sample_covariance_known_means" align="center" raw="\operatorname{cov_n} = \frac{1}{n} \sum_{i=0}^{n-1} (x_i - \mu_x)(y_i - \mu_y)" alt="Equation for the unbiased sample covariance for known population means."> --> 41 42 <div class="equation" align="center" data-raw-text="\operatorname{cov_n} = \frac{1}{n} \sum_{i=0}^{n-1} (x_i - \mu_x)(y_i - \mu_y)" data-equation="eq:unbiased_sample_covariance_known_means"> 43 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@27e2a43c70db648bb5bbc3fd0cdee050c25adc0b/lib/node_modules/@stdlib/stats/incr/covariance/docs/img/equation_unbiased_sample_covariance_known_means.svg" alt="Equation for the unbiased sample covariance for known population means."> 44 <br> 45 </div> 46 47 <!-- </equation> --> 48 49 </section> 50 51 <!-- /.intro --> 52 53 <section class="usage"> 54 55 ## Usage 56 57 ```javascript 58 var incrcovariance = require( '@stdlib/stats/incr/covariance' ); 59 ``` 60 61 #### incrcovariance( \[mx, my] ) 62 63 Returns an accumulator `function` which incrementally computes an [unbiased sample covariance][covariance]. 64 65 ```javascript 66 var accumulator = incrcovariance(); 67 ``` 68 69 If the means are already known, provide `mx` and `my` arguments. 70 71 ```javascript 72 var accumulator = incrcovariance( 3.0, -5.5 ); 73 ``` 74 75 #### accumulator( \[x, y] ) 76 77 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]. 78 79 ```javascript 80 var accumulator = incrcovariance(); 81 82 var v = accumulator( 2.0, 1.0 ); 83 // returns 0.0 84 85 v = accumulator( 1.0, -5.0 ); 86 // returns 3.0 87 88 v = accumulator( 3.0, 3.14 ); 89 // returns 4.07 90 91 v = accumulator(); 92 // returns 4.07 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 **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. 104 105 </section> 106 107 <!-- /.notes --> 108 109 <section class="examples"> 110 111 ## Examples 112 113 <!-- eslint no-undef: "error" --> 114 115 ```javascript 116 var randu = require( '@stdlib/random/base/randu' ); 117 var incrcovariance = require( '@stdlib/stats/incr/covariance' ); 118 119 var accumulator; 120 var x; 121 var y; 122 var i; 123 124 // Initialize an accumulator: 125 accumulator = incrcovariance(); 126 127 // For each simulated datum, update the unbiased sample covariance... 128 for ( i = 0; i < 100; i++ ) { 129 x = randu() * 100.0; 130 y = randu() * 100.0; 131 accumulator( x, y ); 132 } 133 console.log( accumulator() ); 134 ``` 135 136 </section> 137 138 <!-- /.examples --> 139 140 <section class="links"> 141 142 [covariance]: https://en.wikipedia.org/wiki/Covariance 143 144 </section> 145 146 <!-- /.links -->