Skip to content

Commit 6ddadde

Browse files
committed
feat(stats): add nanpcorrdist
--- 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 9b2b107 commit 6ddadde

File tree

11 files changed

+1193
-0
lines changed

11 files changed

+1193
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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 -->
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 incrnanpcorrdist = 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 = incrnanpcorrdist();
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 = incrnanpcorrdist();
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 = incrnanpcorrdist( 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: 70 additions & 0 deletions
Loading
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
{{alias}}( [mx, my] )
3+
Returns an accumulator function which incrementally computes a sample
4+
Pearson product-moment correlation distance, ignoring `NaN` value.
5+
6+
The correlation distance is defined as one minus the Pearson product-moment
7+
correlation coefficient and, thus, resides on the interval [0,2].
8+
9+
If provided values, the accumulator function returns an updated sample
10+
correlation distance. If not provided values, the accumulator function
11+
returns the current sample correlation distance.
12+
13+
If provided `NaN` or a value which, when used in computations, results in
14+
`NaN`, the accumulated value is `NaN` for all future invocations.
15+
16+
Parameters
17+
----------
18+
mx: number (optional)
19+
Known mean.
20+
21+
my: number (optional)
22+
Known mean.
23+
24+
Returns
25+
-------
26+
acc: Function
27+
Accumulator function.
28+
29+
Examples
30+
--------
31+
> var accumulator = {{alias}}();
32+
> var d = accumulator()
33+
null
34+
> d = accumulator( 2.0, 1.0 )
35+
~1.0
36+
> d = accumulator( NaN, 1.0 )
37+
~1.0
38+
> d = accumulator( -5.0, 3.14 )
39+
~2.0
40+
> d = accumulator( -5.0, NaN )
41+
~2.0
42+
> d = accumulator()
43+
~2.0
44+
45+
See Also
46+
--------
47+

0 commit comments

Comments
 (0)