Skip to content

Commit d7869fb

Browse files
Adithya-jhPlaneshifterstdlib-bot
authored
feat: add stats/incr/nancovariance
PR-URL: #5980 Closes: #5554 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: stdlib-bot <noreply@stdlib.io> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 4da47fe commit d7869fb

10 files changed

Lines changed: 709 additions & 0 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# incrnancovariance
22+
23+
> Compute a sample covariance incrementally, ignoring `NaN` values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var nancovariance = require( '@stdlib/stats/incr/nancovariance' );
31+
```
32+
33+
#### incrnancovariance
34+
35+
Returns an accumulator function which incrementally computes a sample covariance, ignoring any input pairs that contain NaN values. The accumulator returns null until at least two valid data pairs have been provided.
36+
37+
```javascript
38+
var accumulator = nancovariance();
39+
```
40+
41+
#### accumulator(`[x, y]`)
42+
43+
If provided a pair of numeric input values `[x, y]`, the accumulator updates and returns the sample covariance. If not provided any input values, the accumulator returns the current sample covariance.
44+
45+
```javascript
46+
var accumulator = nancovariance();
47+
48+
var cov = accumulator();
49+
// returns null
50+
51+
cov = accumulator( 2.0, 1.0 );
52+
// returns null
53+
54+
cov = accumulator( NaN, 1.0 );
55+
// returns null
56+
57+
cov = accumulator( 4.0, 5.0 );
58+
// returns 4.0
59+
60+
cov = accumulator();
61+
// returns 4.0
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="examples">
69+
70+
## Examples
71+
72+
<!-- eslint no-undef: "error" -->
73+
74+
```javascript
75+
var randu = require( '@stdlib/random/base/randu' );
76+
var nancovariance = require( '@stdlib/stats/incr/nancovariance' );
77+
78+
var accumulator;
79+
var i;
80+
var x;
81+
var y;
82+
accumulator = nancovariance();
83+
for ( i = 0; i < 100; i++ ) {
84+
x = ( randu() < 0.2 ) ? NaN : randu() * 100.0;
85+
y = ( randu() < 0.2 ) ? NaN : randu() * 100.0;
86+
accumulator( x, y );
87+
}
88+
console.log( accumulator() );
89+
```
90+
91+
</section>
92+
93+
<!-- /.examples -->
94+
95+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
96+
97+
<section class="related">
98+
99+
</section>
100+
101+
<!-- /.related -->
102+
103+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
104+
105+
<section class="links">
106+
107+
<!-- <related-links> -->
108+
109+
<!-- </related-links> -->
110+
111+
</section>
112+
113+
<!-- /.links -->
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
26+
var pkg = require( './../package.json' ).name;
27+
var nancovariance = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var acc;
34+
var i;
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
acc = nancovariance();
38+
if ( typeof acc !== 'function' ) {
39+
b.fail( 'should return a function' );
40+
}
41+
}
42+
b.toc();
43+
if ( typeof acc !== 'function' ) {
44+
b.fail( 'should return a function' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( format( '%s::accumulator', pkg ), function benchmark( b ) {
51+
var acc;
52+
var x;
53+
var y;
54+
var v;
55+
var i;
56+
57+
acc = nancovariance();
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
x = ( randu() < 0.1 ) ? NaN : randu() * 10; // ~10% NaN
62+
y = ( randu() < 0.1 ) ? NaN : randu() * 10; // ~10% NaN
63+
v = acc( x, y );
64+
if ( v !== v && i > 1 ) {
65+
b.fail( 'accumulator should not return NaN once enough valid data' );
66+
}
67+
}
68+
b.toc();
69+
if ( v !== v ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes a sample
4+
covariance, ignoring `NaN` values.
5+
6+
If provided input values, the accumulator function returns an updated
7+
sample covariance. If not provided input values, the accumulator function
8+
returns the current sample covariance.
9+
10+
If an input value is `NaN`, the input pair is ignored and the accumulated
11+
value remains unchanged.
12+
13+
Until at least two valid data pairs have been provided, the accumulator
14+
function returns `null`.
15+
16+
Returns
17+
-------
18+
acc: Function
19+
Accumulator function.
20+
21+
Examples
22+
--------
23+
> var accumulator = {{alias}}();
24+
> var v = accumulator()
25+
null
26+
> v = accumulator( 2.0, 1.0 )
27+
null
28+
> v = accumulator( NaN, 5.0 )
29+
null
30+
> v = accumulator( 4.0, 5.0 )
31+
4.0
32+
> v = accumulator()
33+
4.0
34+
35+
See Also
36+
--------
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* If provided arguments, returns an updated sample covariance; otherwise, returns the current sample covariance.
25+
*
26+
* ## Notes
27+
*
28+
* - If provided `NaN`, the input is ignored and the accumulated value remains unchanged.
29+
*
30+
* @param x - value
31+
* @param y - value
32+
* @returns sample covariance or null
33+
*/
34+
type accumulator = ( x?: number, y?: number ) => number | null;
35+
36+
/**
37+
* Returns an accumulator function which incrementally computes a sample covariance, ignoring `NaN` values.
38+
*
39+
* @returns accumulator function
40+
*
41+
* @example
42+
* var acc = nancovariance();
43+
*
44+
* var cov = acc();
45+
* // returns null
46+
*
47+
* cov = acc( 2.0, 1.0 );
48+
* // returns null
49+
*
50+
* cov = acc( NaN, 5.0 );
51+
* // returns null
52+
*
53+
* cov = acc( 4.0, 5.0 );
54+
* // returns 4.0
55+
*
56+
* cov = acc();
57+
* // returns 4.0
58+
*/
59+
declare function nancovariance(): accumulator;
60+
61+
62+
// EXPORTS //
63+
64+
export = nancovariance;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
import nancovariance = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
nancovariance(); // $ExpectType accumulator
27+
}
28+
29+
// The compiler throws an error if the function is provided any arguments...
30+
{
31+
nancovariance( 3 ); // $ExpectError
32+
nancovariance( 3, 0.0 ); // $ExpectError
33+
}
34+
35+
// The function returns an accumulator function which returns an accumulated result...
36+
{
37+
const acc = nancovariance();
38+
acc(); // $ExpectType number | null
39+
acc( 2.0, 1.0 ); // $ExpectType number | null
40+
}
41+
42+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
43+
{
44+
const acc = nancovariance();
45+
46+
acc( '5', 2.0 ); // $ExpectError
47+
acc( true, 2.0 ); // $ExpectError
48+
acc( false, 2.0 ); // $ExpectError
49+
acc( null, 2.0 ); // $ExpectError
50+
acc( [], 2.0 ); // $ExpectError
51+
acc( {}, 2.0 ); // $ExpectError
52+
acc( ( x: number ): number => x, 2.0 ); // $ExpectError
53+
54+
acc( 2.0, '5' ); // $ExpectError
55+
acc( 2.0, true ); // $ExpectError
56+
acc( 2.0, false ); // $ExpectError
57+
acc( 2.0, null ); // $ExpectError
58+
acc( 2.0, [] ); // $ExpectError
59+
acc( 2.0, {} ); // $ExpectError
60+
acc( 2.0, ( x: number ): number => x ); // $ExpectError
61+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
var nancovariance = require( './../lib' );
22+
23+
var acc = nancovariance();
24+
25+
var dataX = [ 2.0, 2.0, NaN, 3.0, 5.0 ];
26+
var dataY = [ 1.0, 5.0, 4.0, NaN, 5.0 ];
27+
28+
var i;
29+
var cov;
30+
for ( i = 0; i < dataX.length; i++ ) {
31+
cov = acc( dataX[ i ], dataY[ i ] );
32+
console.log( 'x=%s, y=%s, cov=%s', dataX[ i ], dataY[ i ], ( cov === null ) ? 'null' : cov.toString() );
33+
}
34+
35+
console.log( '\nFinal sample covariance:', acc() );

0 commit comments

Comments
 (0)