|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# incrnanmapcorr |
| 22 | + |
| 23 | +> Compute a moving sample absolute [Pearson product-moment correlation coefficient][pearson-correlation] incrementally, ignoring `NaN` value. |
| 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 | +```math |
| 32 | +\rho_{X,Y} = \frac{\mathop{\mathrm{cov}}(X,Y)}{\sigma_X \sigma_Y} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <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"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@e7c0cbc398c5e64614baf47cf5c6259b93c0ffce/lib/node_modules/@stdlib/stats/incr/mapcorr/docs/img/equation_pearson_correlation_coefficient.svg" alt="Equation for the Pearson product-moment correlation coefficient."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +where the numerator is the [covariance][covariance] and the denominator is the product of the respective standard deviations. |
| 43 | + |
| 44 | +For a sample of size `W`, the sample [Pearson product-moment correlation coefficient][pearson-correlation] is defined as |
| 45 | + |
| 46 | +<!-- <equation class="equation" label="eq:sample_pearson_correlation_coefficient" align="center" raw="r = \frac{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})(y_i - \bar{y})}{\displaystyle\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."> --> |
| 47 | + |
| 48 | +```math |
| 49 | +r = \frac{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})(y_i - \bar{y})}{\displaystyle\sqrt{\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \sqrt{\sum_{i=0}^{n-1} (y_i - \bar{y})^2}} |
| 50 | +``` |
| 51 | + |
| 52 | +<!-- <div class="equation" align="center" data-raw-text="r = \frac{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})(y_i - \bar{y})}{\displaystyle\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"> |
| 53 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@e7c0cbc398c5e64614baf47cf5c6259b93c0ffce/lib/node_modules/@stdlib/stats/incr/mapcorr/docs/img/equation_sample_pearson_correlation_coefficient.svg" alt="Equation for the sample Pearson product-moment correlation coefficient."> |
| 54 | + <br> |
| 55 | +</div> --> |
| 56 | + |
| 57 | +<!-- </equation> --> |
| 58 | + |
| 59 | +The sample **absolute** [Pearson product-moment correlation coefficient][pearson-correlation] is thus defined as the absolute value of the sample [Pearson product-moment correlation coefficient][pearson-correlation]. |
| 60 | + |
| 61 | +</section> |
| 62 | + |
| 63 | +<!-- /.intro --> |
| 64 | + |
| 65 | +<section class="usage"> |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +```javascript |
| 70 | +var incrnanmapcorr = require( '@stdlib/stats/incr/nanmapcorr' ); |
| 71 | +``` |
| 72 | + |
| 73 | +#### incrnanmapcorr( window\[, mx, my] ) |
| 74 | + |
| 75 | +Returns an accumulator `function` which incrementally computes a moving sample absolute [Pearson product-moment correlation coefficient][pearson-correlation]. The `window` parameter defines the number of values over which to compute the moving sample absolute [Pearson product-moment correlation coefficient][pearson-correlation]. |
| 76 | + |
| 77 | +```javascript |
| 78 | +var accumulator = incrnanmapcorr( 3 ); |
| 79 | +``` |
| 80 | + |
| 81 | +If means are already known, provide `mx` and `my` arguments. |
| 82 | + |
| 83 | +```javascript |
| 84 | +var accumulator = incrnanmapcorr( 3, 5.0, -3.14 ); |
| 85 | +``` |
| 86 | + |
| 87 | +#### accumulator( \[x, y] ) |
| 88 | + |
| 89 | +If provided input values `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. |
| 90 | + |
| 91 | +```javascript |
| 92 | +var accumulator = incrnanmapcorr( 3 ); |
| 93 | + |
| 94 | +var ar = accumulator(); |
| 95 | +// returns null |
| 96 | + |
| 97 | +// Fill the window... |
| 98 | +ar = accumulator( 2.0, 1.0 ); // [(2.0, 1.0)] |
| 99 | +// returns 0.0 |
| 100 | + |
| 101 | +ar = accumulator( -5.0, 3.14 ); // [(2.0, 1.0), (-5.0, 3.14)] |
| 102 | +// returns ~1.0 |
| 103 | + |
| 104 | +ar = accumulator( 3.0, -1.0 ); // [(2.0, 1.0), (-5.0, 3.14), (3.0, -1.0)] |
| 105 | +// returns ~0.925 |
| 106 | + |
| 107 | +ar = accumulator( 3.0, NaN ); // [(2.0, 1.0), (-5.0, 3.14), (3.0, -1.0)] |
| 108 | +// returns ~0.925 |
| 109 | + |
| 110 | +// Window begins sliding... |
| 111 | +ar = accumulator( 5.0, -9.5 ); // [(-5.0, 3.14), (3.0, -1.0), (5.0, -9.5)] |
| 112 | +// returns ~0.863 |
| 113 | + |
| 114 | +ar = accumulator( -5.0, 1.5 ); // [(3.0, -1.0), (5.0, -9.5), (-5.0, 1.5)] |
| 115 | +// returns ~0.803 |
| 116 | + |
| 117 | +ar = accumulator(); |
| 118 | +// returns ~0.803 |
| 119 | +``` |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.usage --> |
| 124 | + |
| 125 | +<section class="notes"> |
| 126 | + |
| 127 | +## Notes |
| 128 | + |
| 129 | +- 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. |
| 130 | +- In comparison to the sample [Pearson product-moment correlation coefficient][pearson-correlation], the sample absolute [Pearson product-moment correlation coefficient][pearson-correlation] is useful when only concerned with the strength of the correlation and not the direction. |
| 131 | + |
| 132 | +</section> |
| 133 | + |
| 134 | +<!-- /.notes --> |
| 135 | + |
| 136 | +<section class="examples"> |
| 137 | + |
| 138 | +## Examples |
| 139 | + |
| 140 | +<!-- eslint no-undef: "error" --> |
| 141 | + |
| 142 | +```javascript |
| 143 | +var randu = require( '@stdlib/random/base/randu' ); |
| 144 | +var incrnanmapcorr = require( '@stdlib/stats/incr/nanmapcorr' ); |
| 145 | + |
| 146 | +var accumulator; |
| 147 | +var x; |
| 148 | +var y; |
| 149 | +var i; |
| 150 | + |
| 151 | +// Initialize an accumulator: |
| 152 | +accumulator = incrnanmapcorr( 5 ); |
| 153 | + |
| 154 | +// For each simulated datum, update the moving sample absolute correlation coefficient... |
| 155 | +for ( i = 0; i < 100; i++ ) { |
| 156 | + if ( randu() < 0.2 ) { |
| 157 | + x = NaN; |
| 158 | + } else { |
| 159 | + x = randu() * 100.0; |
| 160 | + } |
| 161 | + if ( randu() < 0.2 ) { |
| 162 | + y = NaN; |
| 163 | + } else { |
| 164 | + y = randu() * 100.0; |
| 165 | + } |
| 166 | + accumulator( x, y ); |
| 167 | +} |
| 168 | +console.log( accumulator() ); |
| 169 | +``` |
| 170 | + |
| 171 | +</section> |
| 172 | + |
| 173 | +<!-- /.examples --> |
| 174 | + |
| 175 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 176 | + |
| 177 | +<section class="related"> |
| 178 | + |
| 179 | +* * * |
| 180 | + |
| 181 | +## See Also |
| 182 | + |
| 183 | +- <span class="package-name">[`@stdlib/stats/incr/apcorr`][@stdlib/stats/incr/apcorr]</span><span class="delimiter">: </span><span class="description">compute a sample absolute Pearson product-moment correlation coefficient.</span> |
| 184 | +- <span class="package-name">[`@stdlib/stats/incr/mpcorr`][@stdlib/stats/incr/mpcorr]</span><span class="delimiter">: </span><span class="description">compute a moving sample Pearson product-moment correlation coefficient incrementally.</span> |
| 185 | +- <span class="package-name">[`@stdlib/stats/incr/mpcorr2`][@stdlib/stats/incr/mpcorr2]</span><span class="delimiter">: </span><span class="description">compute a moving squared sample Pearson product-moment correlation coefficient incrementally.</span> |
| 186 | + |
| 187 | +</section> |
| 188 | + |
| 189 | +<!-- /.related --> |
| 190 | + |
| 191 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 192 | + |
| 193 | +<section class="links"> |
| 194 | + |
| 195 | +[pearson-correlation]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient |
| 196 | + |
| 197 | +[covariance]: https://en.wikipedia.org/wiki/Covariance |
| 198 | + |
| 199 | +<!-- <related-links> --> |
| 200 | + |
| 201 | +[@stdlib/stats/incr/apcorr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/apcorr |
| 202 | + |
| 203 | +[@stdlib/stats/incr/mpcorr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mpcorr |
| 204 | + |
| 205 | +[@stdlib/stats/incr/mpcorr2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mpcorr2 |
| 206 | + |
| 207 | +<!-- </related-links> --> |
| 208 | + |
| 209 | +</section> |
| 210 | + |
| 211 | +<!-- /.links --> |
0 commit comments