README.md (5793B)
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 # incrmpcorr 22 23 > Compute a moving [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/mpcorr/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 `W`, 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/mpcorr/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 incrmpcorr = require( '@stdlib/stats/incr/mpcorr' ); 61 ``` 62 63 #### incrmpcorr( window\[, mx, my] ) 64 65 Returns an accumulator `function` which incrementally computes a moving [sample Pearson product-moment correlation coefficient][pearson-correlation]. The `window` parameter defines the number of values over which to compute the moving [sample Pearson product-moment correlation coefficient][pearson-correlation]. 66 67 ```javascript 68 var accumulator = incrmpcorr( 3 ); 69 ``` 70 71 If means are already known, provide `mx` and `my` arguments. 72 73 ```javascript 74 var accumulator = incrmpcorr( 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 [sample Pearson product-moment correlation coefficient][pearson-correlation]. If not provided input values `x` and `y`, the accumulator function returns the current [sample Pearson product-moment correlation coefficient][pearson-correlation]. 80 81 ```javascript 82 var accumulator = incrmpcorr( 3 ); 83 84 var r = accumulator(); 85 // returns null 86 87 // Fill the window... 88 r = accumulator( 2.0, 1.0 ); // [(2.0, 1.0)] 89 // returns 0.0 90 91 r = accumulator( -5.0, 3.14 ); // [(2.0, 1.0), (-5.0, 3.14)] 92 // returns ~-1.0 93 94 r = accumulator( 3.0, -1.0 ); // [(2.0, 1.0), (-5.0, 3.14), (3.0, -1.0)] 95 // returns ~-0.925 96 97 // Window begins sliding... 98 r = accumulator( 5.0, -9.5 ); // [(-5.0, 3.14), (3.0, -1.0), (5.0, -9.5)] 99 // returns ~-0.863 100 101 r = accumulator( -5.0, 1.5 ); // [(3.0, -1.0), (5.0, -9.5), (-5.0, 1.5)] 102 // returns ~-0.803 103 104 r = accumulator(); 105 // returns ~-0.803 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 incrmpcorr = require( '@stdlib/stats/incr/mpcorr' ); 132 133 var accumulator; 134 var x; 135 var y; 136 var i; 137 138 // Initialize an accumulator: 139 accumulator = incrmpcorr( 5 ); 140 141 // For each simulated datum, update the moving sample correlation coefficient... 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 [pearson-correlation]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient 157 158 [covariance]: https://en.wikipedia.org/wiki/Covariance 159 160 </section> 161 162 <!-- /.links -->