Skip to content

Commit d64593a

Browse files
committed
build: Add incremental absolute Pearson correlation function ignoring NaN
1 parent 5fd8af8 commit d64593a

File tree

12 files changed

+1339
-0
lines changed

12 files changed

+1339
-0
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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+
# incrnanapcorr
22+
23+
> Compute a sample absolute [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+
```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@80f96253bf726f33bc71d8eb68037ab203ae4cf9/lib/node_modules/@stdlib/stats/incr/nanapcorr/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 `n`, 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@80f96253bf726f33bc71d8eb68037ab203ae4cf9/lib/node_modules/@stdlib/stats/incr/nanapcorr/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 incrnanapcorr = require( '@stdlib/stats/incr/nanapcorr' );
71+
```
72+
73+
#### incrnanapcorr( \[mx, my] )
74+
75+
Returns an accumulator `function` which incrementally computes a sample absolute [Pearson product-moment correlation coefficient][pearson-correlation].
76+
77+
```javascript
78+
var accumulator = incrnanapcorr();
79+
```
80+
81+
If the means are already known, provide `mx` and `my` arguments.
82+
83+
```javascript
84+
var accumulator = incrnanapcorr( 3.0, -5.5 );
85+
```
86+
87+
#### accumulator( \[x, y] )
88+
89+
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.
90+
91+
```javascript
92+
var accumulator = incrnanapcorr();
93+
94+
var v = accumulator( 2.0, 1.0 );
95+
// returns 0.0
96+
97+
var v = accumulator( NaN, 1.0 ); //ignore NaN
98+
// returns 0.0
99+
100+
v = accumulator( 1.0, -5.0 );
101+
// returns 1.0
102+
103+
v = accumulator( 3.0, 3.14 );
104+
// returns ~0.965
105+
106+
v = accumulator( 3.0, NaN ); //ignore NaN
107+
// returns ~0.965
108+
109+
v = accumulator();
110+
// returns ~0.965
111+
```
112+
113+
</section>
114+
115+
<!-- /.usage -->
116+
117+
<section class="notes">
118+
119+
## Notes
120+
121+
- Input values are type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, then it will be Ignored but you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
122+
- 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.
123+
124+
</section>
125+
126+
<!-- /.notes -->
127+
128+
<section class="examples">
129+
130+
## Examples
131+
132+
<!-- eslint no-undef: "error" -->
133+
134+
```javascript
135+
var randu = require( '@stdlib/random/base/randu' );
136+
var incrnanapcorr = require( '@stdlib/stats/incr/nanapcorr' );
137+
138+
var accumulator;
139+
var x;
140+
var y;
141+
var i;
142+
143+
// Initialize an accumulator:
144+
accumulator = incrnanapcorr();
145+
146+
// For each simulated datum, update the sample absolute correlation coefficient...
147+
for ( i = 0; i < 100; i++ ) {
148+
149+
x = (randu() < 0.2) ? NaN : randu() * 100.0; // 20% of the time, generate a NaN
150+
y = (randu() < 0.1) ? NaN : randu() * 100.0; // 10% of the time, generate a NaN
151+
152+
ar = accumulator( x, y );
153+
console.log( '%d\t%d\t%d', isnan(x) ? 'NaN' : x.toFixed( 4 ), isnan(y) ? 'NaN' : y.toFixed( 4 ), ar.toFixed( 4 ) );
154+
}
155+
```
156+
157+
</section>
158+
159+
<!-- /.examples -->
160+
161+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
162+
163+
<section class="related">
164+
165+
* * *
166+
167+
## See Also
168+
169+
- <span class="package-name">[`@stdlib/stats/incr/mapcorr`][@stdlib/stats/incr/mapcorr]</span><span class="delimiter">: </span><span class="description">compute a moving sample absolute Pearson product-moment correlation coefficient incrementally.</span>
170+
- <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>
171+
- <span class="package-name">[`@stdlib/stats/incr/pcorr2`][@stdlib/stats/incr/pcorr2]</span><span class="delimiter">: </span><span class="description">compute a squared sample Pearson product-moment correlation coefficient.</span>
172+
173+
</section>
174+
175+
<!-- /.related -->
176+
177+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
178+
179+
<section class="links">
180+
181+
[pearson-correlation]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient
182+
183+
[covariance]: https://en.wikipedia.org/wiki/Covariance
184+
185+
<!-- <related-links> -->
186+
187+
[@stdlib/stats/incr/mapcorr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mapcorr
188+
189+
[@stdlib/stats/incr/pcorr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/pcorr
190+
191+
[@stdlib/stats/incr/pcorr2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/pcorr2
192+
193+
<!-- </related-links> -->
194+
195+
</section>
196+
197+
<!-- /.links -->
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrnanapcorr = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnanapcorr();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanapcorr();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu(), randu() );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
70+
71+
bench( pkg+'::accumulator,known_means', function benchmark( b ) {
72+
var acc;
73+
var v;
74+
var i;
75+
76+
acc = incrnanapcorr( 3.0, -2.0 );
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
v = acc( randu(), randu() );
81+
if ( v !== v ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
}
85+
b.toc();
86+
if ( v !== v ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
});
Lines changed: 48 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)