Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stats/incr): add nancovariance accumulator #5980

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nancovariance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<!--
@license Apache-2.0
Copyright (c) 2020 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# incrnancovariance

> Compute an incremental sample covariance while ignoring `NaN` values.
<section class="usage">

## Usage

```javascript
var nancovariance = require( '@stdlib/stats/incr/nancovariance' );
```

#### incrnancovariance

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.

```javascript
var accumulator = nancovariance();
```

#### accumulator(`[x, y]`)

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.

```javascript
var accumulator = nancovariance();

var cov = accumulator();
// returns null

cov = accumulator( 2.0, 1.0 );
// returns null

cov = accumulator( NaN, 1.0 );
// returns null

cov = accumulator( 4.0, 5.0 );
// returns 4.0

cov = accumulator();
// returns 4.0
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var nancovariance = require( '@stdlib/stats/incr/nancovariance' );

var accumulator;
var i;
var x;
var y;
accumulator = nancovariance();
for ( i = 0; i < 100; i++ ) {
x = ( randu() < 0.2 ) ? NaN : randu() * 100.0;
y = ( randu() < 0.2 ) ? NaN : randu() * 100.0;
accumulator( x, y );
}
console.log( accumulator() );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/stats/incr/nansum`][@stdlib/stats/incr/nansum]</span><span class="delimiter">: </span><span class="description">compute a sum incrementally, ignoring NaN values.</span>

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

<!-- <related-links> -->

[@stdlib/stats/incr/nansum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nansum

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var pkg = require( './../package.json' ).name;
var nancovariance = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var acc;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
acc = nancovariance();
if ( typeof acc !== 'function' ) {
b.fail( 'should return a function' );
}
}
b.toc();
if ( typeof acc !== 'function' ) {
b.fail( 'should return a function' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::accumulator', function benchmark( b ) {
var acc;
var x;
var y;
var v;
var i;

acc = nancovariance();

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu() < 0.1 ) ? NaN : randu()*10; // ~10% NaN
y = ( randu() < 0.1 ) ? NaN : randu()*10; // ~10% NaN
v = acc( x, y );
if ( v !== v && i > 1 ) {
b.fail( 'accumulator should not return NaN once enough valid data' );
}
}
b.toc();
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
28 changes: 28 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nancovariance/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{alias}}()
Returns an accumulator function which incrementally updates a count,
ignoring `NaN` values.

If provided a value, the accumulator function returns an updated count. If
not provided a value, the accumulator function returns the current count.

Returns
-------
acc: Function
Accumulator function.

Examples
--------
> var accumulator = {{alias}}();
> var v = accumulator()
null
> v = accumulator( 2.0 )
null
> v = accumulator( NaN )
null
> v = accumulator( -5.0 )
NaN
> v = accumulator()
NaN

See Also
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

/**
* If provided a pair of values, updates and returns the sample covariance;
* otherwise, returns the current sample covariance.
*
* @param x - first numeric value
* @param y - second numeric value
* @returns sample covariance or null
*/
interface Accumulator {
( x: number, y: number ): number | null;
(): number | null;
}


/**
* Returns an accumulator function which incrementally computes a count, ignoring `NaN` values.
*
* @returns accumulator function
*
* @example
* var nancovariance = require( '@stdlib/stats/incr/nancovariance' );
*
* var acc = nancovariance();
*
* var cov = acc();
* // returns null
*
* cov = acc( 2.0, 1.0 );
* // returns null
*
* cov = acc( NaN, 5.0 );
* // returns null
*
* cov = acc( 4.0, 5.0 );
* // returns 4.0
*
* cov = acc();
* // returns 4.0
*/

declare function nancovariance( ...args: [] ): Accumulator;


// EXPORTS //

export = nancovariance;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import nancovariance = require( '@stdlib/stats/incr/nancovariance' );

// TESTS //

// The function returns an accumulator function...
{
nancovariance(); // $ExpectType Accumulator
}

// The compiler throws an error if the function is provided any arguments...
{
nancovariance( 3 ); // $ExpectError
nancovariance( 3, 0.0 ); // $ExpectError
}

// The function returns an accumulator function which returns an accumulated result...
{
const acc = nancovariance();
acc(); // $ExpectType number | null
acc( 2.0, 1.0 ); // $ExpectType number | null
}

// The compiler throws an error if the returned accumulator function is provided invalid arguments...
{
const acc = nancovariance();
acc( '5', 2.0 ); // $ExpectError
acc( true, 2.0 ); // $ExpectError
acc( 2.0 ); // $ExpectError
acc( null, 2.0 ); // $ExpectError
acc( [], {} ); // $ExpectError
acc( ( x: number ): number => x ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var nancovariance = require( './../lib' );

var acc = nancovariance();

var dataX = [ 2.0, 2.0, NaN, 3.0, 5.0 ];
var dataY = [ 1.0, 5.0, 4.0, NaN, 5.0 ];

var i;
var cov;
for ( i = 0; i < dataX.length; i++ ) {
cov = acc( dataX[ i ], dataY[ i ] );
console.log( 'x=%s, y=%s, cov=%s', dataX[ i ], dataY[ i ], ( cov === null ) ? 'null' : cov.toString() );
}

console.log( '\nFinal sample covariance:', acc() );
Loading