|
| 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 | +# incrnanpcorrdist |
| 22 | + |
| 23 | +> Compute a [sample Pearson product-moment correlation distance][pearson-correlation] incrementally, ignoring `NaN` value. |
| 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@7e0a95722efd9c771b129597380c63dc6715508b/lib/node_modules/@stdlib/stats/incr/pcorrdist/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 incrnanpcorrdist = require( '@stdlib/stats/incr/nanpcorrdist' ); |
| 54 | +``` |
| 55 | + |
| 56 | +#### incrnanpcorrdist( \[mx, my] ) |
| 57 | + |
| 58 | +Returns an accumulator `function` which incrementally computes a [sample Pearson product-moment correlation distance][pearson-correlation], ignoring `NaN` value. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var accumulator = incrnanpcorrdist(); |
| 62 | +``` |
| 63 | + |
| 64 | +If the means are already known, provide `mx` and `my` arguments. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var accumulator = incrnanpcorrdist( 3.0, -5.5 ); |
| 68 | +``` |
| 69 | + |
| 70 | +#### accumulator( \[x, y] ) |
| 71 | + |
| 72 | +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]. |
| 73 | + |
| 74 | +```javascript |
| 75 | +var accumulator = incrnanpcorrdist(); |
| 76 | + |
| 77 | +var d = accumulator( 2.0, 1.0 ); |
| 78 | +// returns 1.0 |
| 79 | + |
| 80 | +d = accumulator( NaN, 1.0 ); |
| 81 | +// returns 1.0 |
| 82 | + |
| 83 | +d = accumulator( 1.0, -5.0 ); |
| 84 | +// returns 0.0 |
| 85 | + |
| 86 | +d = accumulator( 1.0, NaN ); |
| 87 | +// returns 0.0 |
| 88 | + |
| 89 | +d = accumulator( 3.0, 3.14 ); |
| 90 | +// returns ~0.035 |
| 91 | + |
| 92 | +d = accumulator(); |
| 93 | +// returns ~0.035 |
| 94 | +``` |
| 95 | + |
| 96 | +</section> |
| 97 | + |
| 98 | +<!-- /.usage --> |
| 99 | + |
| 100 | +<section class="notes"> |
| 101 | + |
| 102 | +## Notes |
| 103 | + |
| 104 | +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulator function skips updating its state and continues to return the most recent valid accumulated value. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. |
| 105 | + |
| 106 | +</section> |
| 107 | + |
| 108 | +<!-- /.notes --> |
| 109 | + |
| 110 | +<section class="examples"> |
| 111 | + |
| 112 | +## Examples |
| 113 | + |
| 114 | +<!-- eslint no-undef: "error" --> |
| 115 | + |
| 116 | +```javascript |
| 117 | +var randu = require( '@stdlib/random/base/randu' ); |
| 118 | +var incrnanpcorrdist = require( '@stdlib/stats/incr/nanpcorrdist' ); |
| 119 | + |
| 120 | +var accumulator; |
| 121 | +var x; |
| 122 | +var y; |
| 123 | +var i; |
| 124 | + |
| 125 | +// Initialize an accumulator: |
| 126 | +accumulator = incrnanpcorrdist(); |
| 127 | + |
| 128 | +// For each simulated datum, update the sample correlation distance... |
| 129 | +for ( i = 0; i < 100; i++ ) { |
| 130 | + if ( randu() < 0.2 ) { |
| 131 | + x = NaN; |
| 132 | + } else { |
| 133 | + x = randu() * 100.0; |
| 134 | + } |
| 135 | + if ( randu() < 0.2 ) { |
| 136 | + y = NaN; |
| 137 | + } else { |
| 138 | + y = randu() * 100.0; |
| 139 | + } |
| 140 | + accumulator( x, y ); |
| 141 | +} |
| 142 | +console.log( accumulator() ); |
| 143 | +``` |
| 144 | + |
| 145 | +</section> |
| 146 | + |
| 147 | +<!-- /.examples --> |
| 148 | + |
| 149 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 150 | + |
| 151 | +<section class="related"> |
| 152 | + |
| 153 | +* * * |
| 154 | + |
| 155 | +## See Also |
| 156 | + |
| 157 | +- <span class="package-name">[`@stdlib/stats/incr/covariance`][@stdlib/stats/incr/covariance]</span><span class="delimiter">: </span><span class="description">compute an unbiased sample covariance incrementally.</span> |
| 158 | +- <span class="package-name">[`@stdlib/stats/incr/pcorr`][@stdlib/stats/incr/pcorr]</span><span class="delimiter">: </span><span class="description">compute a sample Pearson product-moment correlation coefficient.</span> |
| 159 | +- <span class="package-name">[`@stdlib/stats/incr/summary`][@stdlib/stats/incr/summary]</span><span class="delimiter">: </span><span class="description">compute a statistical summary incrementally.</span> |
| 160 | + |
| 161 | +</section> |
| 162 | + |
| 163 | +<!-- /.related --> |
| 164 | + |
| 165 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 166 | + |
| 167 | +<section class="links"> |
| 168 | + |
| 169 | +[pearson-correlation]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient |
| 170 | + |
| 171 | +<!-- <related-links> --> |
| 172 | + |
| 173 | +[@stdlib/stats/incr/covariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/covariance |
| 174 | + |
| 175 | +[@stdlib/stats/incr/pcorr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/pcorr |
| 176 | + |
| 177 | +[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary |
| 178 | + |
| 179 | +<!-- </related-links> --> |
| 180 | + |
| 181 | +</section> |
| 182 | + |
| 183 | +<!-- /.links --> |
0 commit comments