README.md (4934B)
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 # incrpcorr 22 23 > Compute a [sample Pearson product-moment correlation coefficient][pearson-correlation] incrementally. 24 25 <section class="intro"> 26 27 The [Pearson product-moment correlation coefficient][pearson-correlation] between random variables `X` and `Y` is defined as 28 29 <!-- <equation class="equation" label="eq:pearson_correlation_coefficient" align="center" raw="\rho_{X,Y} = \frac{\operatorname{cov}(X,Y)}{\sigma_X \sigma_Y}" alt="Equation for the Pearson product-moment correlation coefficient."> --> 30 31 <div class="equation" align="center" data-raw-text="\rho_{X,Y} = \frac{\operatorname{cov}(X,Y)}{\sigma_X \sigma_Y}" data-equation="eq:pearson_correlation_coefficient"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/pcorr/docs/img/equation_pearson_correlation_coefficient.svg" alt="Equation for the Pearson product-moment correlation coefficient."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 where the numerator is the [covariance][covariance] and the denominator is the product of the respective standard deviations. 39 40 For a sample of size `n`, the [sample Pearson product-moment correlation coefficient][pearson-correlation] is defined as 41 42 <!-- <equation class="equation" label="eq:sample_pearson_correlation_coefficient" align="center" raw="r = \frac{\sum_{i=0}^{n-1} (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \sqrt{\sum_{i=0}^{n-1} (y_i - \bar{y})^2}}" alt="Equation for the sample Pearson product-moment correlation coefficient."> --> 43 44 <div class="equation" align="center" data-raw-text="r = \frac{\sum_{i=0}^{n-1} (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \sqrt{\sum_{i=0}^{n-1} (y_i - \bar{y})^2}}" data-equation="eq:sample_pearson_correlation_coefficient"> 45 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/pcorr/docs/img/equation_sample_pearson_correlation_coefficient.svg" alt="Equation for the sample Pearson product-moment correlation coefficient."> 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 incrpcorr = require( '@stdlib/stats/incr/pcorr' ); 61 ``` 62 63 #### incrpcorr( \[mx, my] ) 64 65 Returns an accumulator `function` which incrementally computes a [sample Pearson product-moment correlation coefficient][pearson-correlation]. 66 67 ```javascript 68 var accumulator = incrpcorr(); 69 ``` 70 71 If the means are already known, provide `mx` and `my` arguments. 72 73 ```javascript 74 var accumulator = incrpcorr( 3.0, -5.5 ); 75 ``` 76 77 #### accumulator( \[x, y] ) 78 79 If provided input value `x` and `y`, the accumulator function returns an updated [sample correlation coefficient][pearson-correlation]. If not provided input values `x` and `y`, the accumulator function returns the current [sample correlation coefficient][pearson-correlation]. 80 81 ```javascript 82 var accumulator = incrpcorr(); 83 84 var v = accumulator( 2.0, 1.0 ); 85 // returns 0.0 86 87 v = accumulator( 1.0, -5.0 ); 88 // returns 1.0 89 90 v = accumulator( 3.0, 3.14 ); 91 // returns ~0.965 92 93 v = accumulator(); 94 // returns ~0.965 95 ``` 96 97 </section> 98 99 <!-- /.usage --> 100 101 <section class="notes"> 102 103 ## Notes 104 105 - 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. 106 107 </section> 108 109 <!-- /.notes --> 110 111 <section class="examples"> 112 113 ## Examples 114 115 <!-- eslint no-undef: "error" --> 116 117 ```javascript 118 var randu = require( '@stdlib/random/base/randu' ); 119 var incrpcorr = require( '@stdlib/stats/incr/pcorr' ); 120 121 var accumulator; 122 var x; 123 var y; 124 var i; 125 126 // Initialize an accumulator: 127 accumulator = incrpcorr(); 128 129 // For each simulated datum, update the sample correlation coefficient... 130 for ( i = 0; i < 100; i++ ) { 131 x = randu() * 100.0; 132 y = randu() * 100.0; 133 accumulator( x, y ); 134 } 135 console.log( accumulator() ); 136 ``` 137 138 </section> 139 140 <!-- /.examples --> 141 142 <section class="links"> 143 144 [pearson-correlation]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient 145 146 [covariance]: https://en.wikipedia.org/wiki/Covariance 147 148 </section> 149 150 <!-- /.links -->