README.md (5344B)
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 # incrpcorr2 22 23 > Compute a squared 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@0086d9dadd17859fedeb3c5acc3a80d7011970e1/lib/node_modules/@stdlib/stats/incr/pcorr2/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@0086d9dadd17859fedeb3c5acc3a80d7011970e1/lib/node_modules/@stdlib/stats/incr/pcorr2/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 The squared sample [Pearson product-moment correlation coefficient][pearson-correlation] is thus defined as the square of the sample [Pearson product-moment correlation coefficient][pearson-correlation]. 52 53 </section> 54 55 <!-- /.intro --> 56 57 <section class="usage"> 58 59 ## Usage 60 61 ```javascript 62 var incrpcorr2 = require( '@stdlib/stats/incr/pcorr2' ); 63 ``` 64 65 #### incrpcorr2( \[mx, my] ) 66 67 Returns an accumulator `function` which incrementally computes a squared sample [Pearson product-moment correlation coefficient][pearson-correlation]. 68 69 ```javascript 70 var accumulator = incrpcorr2(); 71 ``` 72 73 If the means are already known, provide `mx` and `my` arguments. 74 75 ```javascript 76 var accumulator = incrpcorr2( 3.0, -5.5 ); 77 ``` 78 79 #### accumulator( \[x, y] ) 80 81 If provided input value `x` and `y`, the accumulator function returns an updated accumulated value. If not provided input values `x` and `y`, the accumulator function returns the current accumulated value. 82 83 ```javascript 84 var accumulator = incrpcorr2(); 85 86 var r2 = accumulator( 2.0, 1.0 ); 87 // returns 0.0 88 89 r2 = accumulator( 1.0, -5.0 ); 90 // returns 1.0 91 92 r2 = accumulator( 3.0, 3.14 ); 93 // returns ~0.93 94 95 r2 = accumulator(); 96 // returns ~0.93 97 ``` 98 99 </section> 100 101 <!-- /.usage --> 102 103 <section class="notes"> 104 105 ## Notes 106 107 - 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. 108 - In comparison to the sample [Pearson product-moment correlation coefficient][pearson-correlation], the squared sample [Pearson product-moment correlation coefficient][pearson-correlation] is useful for emphasizing strong correlations. 109 110 </section> 111 112 <!-- /.notes --> 113 114 <section class="examples"> 115 116 ## Examples 117 118 <!-- eslint no-undef: "error" --> 119 120 ```javascript 121 var randu = require( '@stdlib/random/base/randu' ); 122 var incrpcorr2 = require( '@stdlib/stats/incr/pcorr2' ); 123 124 var accumulator; 125 var x; 126 var y; 127 var i; 128 129 // Initialize an accumulator: 130 accumulator = incrpcorr2(); 131 132 // For each simulated datum, update the squared sample correlation coefficient... 133 for ( i = 0; i < 100; i++ ) { 134 x = randu() * 100.0; 135 y = randu() * 100.0; 136 accumulator( x, y ); 137 } 138 console.log( accumulator() ); 139 ``` 140 141 </section> 142 143 <!-- /.examples --> 144 145 <section class="links"> 146 147 [pearson-correlation]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient 148 149 [covariance]: https://en.wikipedia.org/wiki/Covariance 150 151 </section> 152 153 <!-- /.links -->