JSPM

compute-variance

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 234
  • Score
    100M100P100Q87274F

Computes the variance of an array.

Package Exports

  • compute-variance

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (compute-variance) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Variance

NPM version Build Status Coverage Status Dependencies

Computes the variance of an array.

Installation

$ npm install compute-variance

For use in the browser, use browserify.

Usage

var variance = require( 'compute-variance' );

variance( arr[, opts] )

Computes the variance of an array. For numeric arrays,

var data = [ 2, 4, 5, 3, 4, 3, 1, 5, 6, 9 ];

var s2 = variance( data );
// returns 5.067

The function accepts two options:

  • accessor: accessor function for accessing array values
  • bias: boolean indicating whether to compute the population variance (biased sample variance) or the (unbiased) sample variance. Default: false; i.e., the unbiased sample variance.

For non-numeric arrays, provide an accessor function for accessing numeric array values

var data = [
    {'x':2},
    {'x':4},
    {'x':5},
    {'x':3},
    {'x':4},
    {'x':3},
    {'x':1},
    {'x':5},
    {'x':6},
    {'x':9}
];

function getValue( d ) {
    return d.x;
}

var s2 = variance( data, {
    'accessor': getValue
});
// returns 5.067

By default, the function calculates the unbiased sample variance. To calculate the population variance (or a biased sample variance), set the bias option to true.

var data = [ 2, 4, 5, 3, 4, 3, 1, 5, 6, 9 ];

var value = variance( data, {
    'bias': true	
});
// returns 4.56

Note: if provided an empty array, the function returns null.

Examples

var variance = require( 'compute-variance' );

var data = new Array( 1000 );
for ( var i = 0; i < data.length; i++ ) {
    data[ i ] = Math.random() * 100;
}

console.log( variance( data ) );

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

MIT license.

Copyright © 2014. Athan Reines.