|
| 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 | +# incrmpcorrdist |
| 22 | + |
| 23 | +> Compute a moving [sample Pearson product-moment correlation distance][pearson-correlation] incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [sample Pearson product-moment correlation distance][pearson-correlation] is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:pearson_distance" align="center" raw="d_{x,y} = 1 - r_{x,y} = 1 - \frac{\operatorname{cov_n(x,y)}}{\sigma_x \sigma_y}" alt="Equation for the Pearson product-moment correlation distance."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +d_{x,y} = 1 - r_{x,y} = 1 - \frac{\mathop{\mathrm{cov_n(x,y)}}}{\sigma_x \sigma_y} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="d_{x,y} = 1 - r_{x,y} = 1 - \frac{\operatorname{cov_n(x,y)}}{\sigma_x \sigma_y}" data-equation="eq:pearson_distance"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mpcorrdist/docs/img/equation_pearson_distance.svg" alt="Equation for the Pearson product-moment correlation distance."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +where `r` is the [sample Pearson product-moment correlation coefficient][pearson-correlation], `cov(x,y)` is the sample covariance, and `σ` corresponds to the sample standard deviation. As `r` resides on the interval `[-1,1]`, `d` resides on the interval `[0,2]`. |
| 43 | + |
| 44 | +</section> |
| 45 | + |
| 46 | +<!-- /.intro --> |
| 47 | + |
| 48 | +<section class="usage"> |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +```javascript |
| 53 | +var incrnanmpcorrdist = require( '@stdlib/stats/incr/nanmpcorrdist' ); |
| 54 | +``` |
| 55 | + |
| 56 | +#### incrmpcorrdist( window\[, mx, my] ) |
| 57 | + |
| 58 | +Returns an accumulator `function` which incrementally computes a moving [sample Pearson product-moment correlation distance][pearson-correlation] while ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving [sample Pearson product-moment correlation distance][pearson-correlation]. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var accumulator = incrnanmpcorrdist( 3 ); |
| 62 | +``` |
| 63 | + |
| 64 | +If means are already known, provide `mx` and `my` arguments. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var accumulator = incrnanmpcorrdist( 3, 5.0, -3.14 ); |
| 68 | +``` |
| 69 | + |
| 70 | +#### accumulator( \[x, y] ) |
| 71 | + |
| 72 | +If provided input values `x` and `y`, the accumulator function returns an updated [sample Pearson product-moment correlation distance][pearson-correlation]. If not provided input values `x` and `y`, the accumulator function returns the current [sample Pearson product-moment correlation distance][pearson-correlation]. |
| 73 | + |
| 74 | +```javascript |
| 75 | +var accumulator = incrnanmpcorrdist( 3 ); |
| 76 | + |
| 77 | +var r = accumulator(); |
| 78 | +// returns null |
| 79 | + |
| 80 | +// Fill the window... |
| 81 | +r = accumulator( 2.0, 1.0 ); // [(2.0, 1.0)] |
| 82 | +// returns 1.0 |
| 83 | + |
| 84 | +r = accumulator( NaN, 3.0 ); // [(2.0, 1.0), (NaN, 3.0)] |
| 85 | +// returns 1.0 |
| 86 | + |
| 87 | +r = accumulator( 4.0, NaN ); // [(2.0, 1.0), (NaN, 3.0), (4.0, NaN)] |
| 88 | +// returns 1.0 |
| 89 | + |
| 90 | +// Window begins sliding... |
| 91 | +r = accumulator( NaN, NaN ); // [(NaN, 3.0), (4.0, NaN), (NaN, NaN)] |
| 92 | +// returns 1.0 |
| 93 | + |
| 94 | +r = accumulator( 5.0, 2.0 ); // [(4.0, NaN), (NaN, NaN), 5.0, 2.0] |
| 95 | +// returns 0.0 |
| 96 | + |
| 97 | +r = accumulator(); |
| 98 | +// returns 0.0 |
| 99 | +``` |
| 100 | + |
| 101 | +</section> |
| 102 | + |
| 103 | +<!-- /.usage --> |
| 104 | + |
| 105 | +<section class="notes"> |
| 106 | + |
| 107 | +## Notes |
| 108 | + |
| 109 | +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, it will be ignored, and the accumulated value is the last one. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. |
| 110 | +- 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. |
| 111 | +- Due to limitations inherent in representing numeric values using floating-point format (i.e., the inability to represent numeric values with infinite precision), the [sample correlation distance][pearson-correlation] between perfectly correlated random variables may **not** be `0` or `2`. In fact, the [sample correlation distance][pearson-correlation] is **not** guaranteed to be strictly on the interval `[0,2]`. Any computed distance should, however, be within floating-point roundoff error. |
| 112 | + |
| 113 | +</section> |
| 114 | + |
| 115 | +<!-- /.notes --> |
| 116 | + |
| 117 | +<section class="examples"> |
| 118 | + |
| 119 | +## Examples |
| 120 | + |
| 121 | +<!-- eslint no-undef: "error" --> |
| 122 | + |
| 123 | +```javascript |
| 124 | +var randu = require( '@stdlib/random/base/randu' ); |
| 125 | +var incrnanmpcorrdist = require( '@stdlib/stats/incr/nanmpcorrdist' ); |
| 126 | + |
| 127 | +var accumulator; |
| 128 | +var x; |
| 129 | +var y; |
| 130 | +var i; |
| 131 | + |
| 132 | +// Initialize an accumulator: |
| 133 | +accumulator = incrnanmpcorrdist( 5 ); |
| 134 | +var d; |
| 135 | + |
| 136 | +// For each simulated datum, update the moving sample correlation distance... |
| 137 | +for ( i = 0; i < 100; i++ ) { |
| 138 | + if ( randu() < 0.2 ) { |
| 139 | + x = NaN; |
| 140 | + } |
| 141 | + else { |
| 142 | + x = randu() * 100.0; |
| 143 | + } |
| 144 | + if ( randu() < 0.2 ) { |
| 145 | + y = NaN; |
| 146 | + } |
| 147 | + else { |
| 148 | + y = randu() * 100.0; |
| 149 | + } |
| 150 | + d = accumulator( x, y ); |
| 151 | + console.log( '%d\t%d\t%d', x.toFixed( 4 ), y.toFixed( 4 ), ( d === null ) ? NaN : d.toFixed( 4 ) ); |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +</section> |
| 156 | + |
| 157 | +<!-- /.examples --> |
| 158 | + |
| 159 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 160 | + |
| 161 | +<section class="related"> |
| 162 | + |
| 163 | +* * * |
| 164 | + |
| 165 | +## See Also |
| 166 | + |
| 167 | +- <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> |
| 168 | +- <span class="package-name">[`@stdlib/stats/incr/pcorrdist`][@stdlib/stats/incr/pcorrdist]</span><span class="delimiter">: </span><span class="description">compute a sample Pearson product-moment correlation distance.</span> |
| 169 | + |
| 170 | +</section> |
| 171 | + |
| 172 | +<!-- /.related --> |
| 173 | + |
| 174 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 175 | + |
| 176 | +<section class="links"> |
| 177 | + |
| 178 | +[pearson-correlation]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient |
| 179 | + |
| 180 | +<!-- <related-links> --> |
| 181 | + |
| 182 | +[@stdlib/stats/incr/mpcorr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mpcorr |
| 183 | + |
| 184 | +[@stdlib/stats/incr/pcorrdist]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/pcorrdist |
| 185 | + |
| 186 | +<!-- </related-links> --> |
| 187 | + |
| 188 | +</section> |
| 189 | + |
| 190 | +<!-- /.links --> |
0 commit comments