incrmvariance

!NPM versionnpm-imagenpm-url !Build Statustravis-imagetravis-url !Coverage Statuscoveralls-imagecoveralls-url !Dependenciesdependencies-imagedependencies-urlProvides a method to compute a moving sample variance incrementally.

## Installation

`$ npm install compute-incrmvariance`

For use in the browser, use browserify.

## Usage

`var incrmvariance = require( 'compute-incrmvariance' );`

#### incrmvariance( window )

Returns an initialized method to compute a moving sample variance incrementally.`window`

sets the window size, i.e., the number of values over which to compute a moving sample variance.`var mvariance = incrmvariance( 3 );`

#### mvariance( value )

If provided a`value`

, the method updates and returns the sample variance of the current window. If not provided a `value`

, the method returns the current sample variance.```
var s2;
// Filling window...
s2 = mvariance( 2 );
// variance is 0
mvariance( 4 );
// variance is 2
mvariance( 0 );
// variance is 4
// Window starts sliding...
mvariance( -2 );
// variance is 9.5
mvariance( -1 );
// variance is 1
s2 = mvariance();
// returns 1
```

## Notes

- If values have not yet been provided to
`mvariance`

,`mvariance`

returns`null`

. - The first
`W-1`

returned sample variances will have less statistical support than subsequent sample variances, as`W`

values are needed to fill the window buffer. Until the window is full, the sample variance returned equals the sample variance of all values provided thus far.

The use case for this module differs from the conventional vector implementation and the stream implementation. Namely, this module decouples the act of updating the moving sample variance from the act of consuming the moving sample variance.

## Examples

```
var incrmvariance = require( 'compute-incrmvariance' );
// Initialize a method to calculate the moving sample variance incrementally:
var mvariance = incrmvariance( 5 ),
s2;
// Simulate some data...
for ( var i = 0; i < 1000; i++ ) {
s2 = mvariance( Math.random()*100 );
console.log( s2 );
}
s2 = mvariance();
console.log( s2 );
```

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`