Skip to content

Commit 7d1cf21

Browse files
feat: add new package to the stats/incr/* namespace: @stdlib/stats/incr/nanmpcorrdist
--- 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 633dc6b commit 7d1cf21

File tree

11 files changed

+1580
-0
lines changed

11 files changed

+1580
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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 -->
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 incrnanmpcorrdist = 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 = incrnanmpcorrdist( (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 = incrnanmpcorrdist( 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 = incrnanmpcorrdist( 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+
});
Lines changed: 70 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)