compute-covariance
v1.0.1
Published
Computes the covariance between one or more numeric arrays.
Downloads
2,051
Maintainers
Readme
Covariance
Computes the covariance between one or more numeric arrays.
Installation
$ npm install compute-covariance
For use in the browser, use browserify.
Usage
To use the module,
var cov = require( 'compute-covariance' );
cov( arr1[, arr2,...,opts] )
Computes the covariance between one or more numeric arrays.
var x = [ 1, 2, 3, 4, 5 ],
y = [ 5, 4, 3, 2, 1 ];
var mat = cov( x, y );
// returns [[2.5,-2.5],[-2.5,2.5]]
Note: for univariate input, the returned covariance matrix contains a single element equal to the variance.
If the number of arrays is dynamic, you may want the flexibility to compute the covariance of an arbitrary array
collection. To this end, cov
also accepts an array
of arrays
.
var mat = cov( [x,y] );
// returns [[2.5,-2.5],[-2.5,2.5]]
By default, each element of the covariance matrix is an unbiased covariance estimate. Hence, the covariance matrix is the sample covariance matrix. For those cases where you want a biased estimate (i.e., population statistics), set the bias
option to true
.
var mat = cov( x, y, {'bias': true});
// returns [[2,-2],[-2,2]]
Examples
var cov = require( 'compute-covariance' );
// Simulate some data...
var N = 100,
x = new Array( N ),
y = new Array( N ),
z = new Array( N );
for ( var i = 0; i < N; i++ ) {
x[ i ] = Math.round( Math.random()*100 );
y[ i ] = Math.round( Math.random()*100 );
z[ i ] = 100 - x[ i ];
}
var mat = cov( x, y, z );
console.log( mat );
To run the example code from the top-level application directory,
$ node ./examples/index.js
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
License
Copyright
Copyright © 2014. Athan Reines.