Skip to content

Commit 0561d70

Browse files
committed
feat(stats): add nanmpcorr package
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent f29381d commit 0561d70

File tree

12 files changed

+1898
-0
lines changed

12 files changed

+1898
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
```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@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mpcorr/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{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \displaystyle\sqrt{\displaystyle\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})}{\sqrt{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \displaystyle\sqrt{\displaystyle\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{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \displaystyle\sqrt{\displaystyle\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@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.">
54+
<br>
55+
</div> -->
56+
57+
<!-- </equation> -->
58+
59+
</section>
60+
61+
<!-- /.intro -->
62+
63+
<section class="usage">
64+
65+
## Usage
66+
67+
```javascript
68+
var incrmpcorr = require( '@stdlib/stats/incr/mpcorr' );
69+
```
70+
71+
#### incrmpcorr( window\[, mx, my] )
72+
73+
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].
74+
75+
```javascript
76+
var accumulator = incrmpcorr( 3 );
77+
```
78+
79+
If means are already known, provide `mx` and `my` arguments.
80+
81+
```javascript
82+
var accumulator = incrmpcorr( 3, 5.0, -3.14 );
83+
```
84+
85+
#### accumulator( \[x, y] )
86+
87+
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].
88+
89+
```javascript
90+
var accumulator = incrmpcorr( 3 );
91+
92+
var r = accumulator();
93+
// returns null
94+
95+
// Fill the window...
96+
r = accumulator( 2.0, 1.0 ); // [(2.0, 1.0)]
97+
// returns 0.0
98+
99+
r = accumulator( -5.0, 3.14 ); // [(2.0, 1.0), (-5.0, 3.14)]
100+
// returns ~-1.0
101+
102+
r = accumulator( 3.0, -1.0 ); // [(2.0, 1.0), (-5.0, 3.14), (3.0, -1.0)]
103+
// returns ~-0.925
104+
105+
// Window begins sliding...
106+
r = accumulator( 5.0, -9.5 ); // [(-5.0, 3.14), (3.0, -1.0), (5.0, -9.5)]
107+
// returns ~-0.863
108+
109+
r = accumulator( -5.0, 1.5 ); // [(3.0, -1.0), (5.0, -9.5), (-5.0, 1.5)]
110+
// returns ~-0.803
111+
112+
r = accumulator();
113+
// returns ~-0.803
114+
```
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<section class="notes">
121+
122+
## Notes
123+
124+
- 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.
125+
- 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.
126+
127+
</section>
128+
129+
<!-- /.notes -->
130+
131+
<section class="examples">
132+
133+
## Examples
134+
135+
<!-- eslint no-undef: "error" -->
136+
137+
```javascript
138+
var randu = require( '@stdlib/random/base/randu' );
139+
var incrmpcorr = require( '@stdlib/stats/incr/mpcorr' );
140+
141+
var accumulator;
142+
var x;
143+
var y;
144+
var i;
145+
146+
// Initialize an accumulator:
147+
accumulator = incrmpcorr( 5 );
148+
149+
// For each simulated datum, update the moving sample correlation coefficient...
150+
for ( i = 0; i < 100; i++ ) {
151+
x = randu() * 100.0;
152+
y = randu() * 100.0;
153+
accumulator( x, y );
154+
}
155+
console.log( accumulator() );
156+
```
157+
158+
</section>
159+
160+
<!-- /.examples -->
161+
162+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
163+
164+
<section class="related">
165+
166+
* * *
167+
168+
## See Also
169+
170+
- <span class="package-name">[`@stdlib/stats/incr/mcovariance`][@stdlib/stats/incr/mcovariance]</span><span class="delimiter">: </span><span class="description">compute a moving unbiased sample covariance incrementally.</span>
171+
- <span class="package-name">[`@stdlib/stats/incr/mpcorrdist`][@stdlib/stats/incr/mpcorrdist]</span><span class="delimiter">: </span><span class="description">compute a moving sample Pearson product-moment correlation distance incrementally.</span>
172+
- <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>
173+
174+
</section>
175+
176+
<!-- /.related -->
177+
178+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
179+
180+
<section class="links">
181+
182+
[pearson-correlation]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient
183+
184+
[covariance]: https://en.wikipedia.org/wiki/Covariance
185+
186+
<!-- <related-links> -->
187+
188+
[@stdlib/stats/incr/mcovariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mcovariance
189+
190+
[@stdlib/stats/incr/mpcorrdist]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mpcorrdist
191+
192+
[@stdlib/stats/incr/pcorr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/pcorr
193+
194+
<!-- </related-links> -->
195+
196+
</section>
197+
198+
<!-- /.links -->
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 incrnanmpcorr = 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 = incrnanmpcorr( (i%5)+1 );
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 = incrnanmpcorr( 5 );
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 = incrnanmpcorr( 5, 3.0, -1.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+
});
92+
93+
bench( pkg+'::accumulator,with_nan', function benchmark( b ) {
94+
var acc;
95+
var v;
96+
var i;
97+
acc = incrnanmpcorr( 5 );
98+
99+
b.tic();
100+
for ( i = 0; i < b.iterations; i++ ) {
101+
if ( randu() < 0.2 ) {
102+
if ( randu() < 0.5 ) {
103+
v = acc( NaN, randu() );
104+
} else {
105+
v = acc( randu(), NaN );
106+
}
107+
} else {
108+
v = acc( randu(), randu() );
109+
}
110+
// Ensure that even with occasional NaN inputs the accumulator does not return NaN:
111+
if ( v !== v ) {
112+
b.fail( 'should not return NaN' );
113+
}
114+
}
115+
b.toc();
116+
if ( v !== v ) {
117+
b.fail( 'should not return NaN' );
118+
}
119+
b.pass( 'benchmark finished' );
120+
b.end();
121+
});
Lines changed: 48 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)