Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

incrnanmstdev

Compute a moving corrected sample standard deviation incrementally, ignoring NaN values.

For a window of size W, the corrected sample standard deviation is defined as

$$s = \sqrt{\frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2}$$

Usage

var incrnanmstdev = require( '@stdlib/stats/incr/nanmstdev' );

incrnanmstdev( window[, mean] )

Returns an accumulator function which incrementally computes a moving corrected sample standard deviation, ignoring NaN values. The window parameter defines the number of values over which to compute the moving corrected sample standard deviation.

var accumulator = incrnanmstdev( 3 );

If the mean is already known, provide a mean argument.

var accumulator = incrnanmstdev( 3, 5.0 );

accumulator( [x] )

If provided an input value x, the accumulator function returns an updated corrected sample standard deviation. If not provided an input value x, the accumulator function returns the current corrected sample standard deviation. If provided NaN, the accumulator function ignores the value and returns the current corrected sample standard deviation without updating the window.

var accumulator = incrnanmstdev( 3 );

var s = accumulator();
// returns null

// Fill the window with non-NaN values...
s = accumulator( 2.0 ); // [2.0]
// returns 0.0

s = accumulator( NaN ); // [2.0]
// returns 0.0

s = accumulator( 3.0 ); // [2.0, 3.0]
// returns ~0.7071

s = accumulator( 5.0 ); // [2.0, 3.0, 5.0]
// returns ~1.53

// Window begins sliding...
s = accumulator( -7.0 ); // [3.0, 5.0, -7.0]
// returns ~6.43

s = accumulator( NaN ); // [3.0, 5.0, -7.0]
// returns ~6.43

s = accumulator( -5.0 ); // [5.0, -7.0, -5.0]
// returns ~6.43

s = accumulator();
// returns ~6.43

Notes

  • Input values are not type checked. If provided NaN, the value is ignored and the accumulator function returns the current corrected sample standard deviation without updating the window. If non-numeric inputs are possible, you are advised to type check and handle accordingly before passing the value to the accumulator function.
  • As W values are needed to fill the window buffer, the first W-1 returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values (excluding NaN values).

Examples

var randu = require( '@stdlib/random/base/randu' );
var incrnanmstdev = require( '@stdlib/stats/incr/nanmstdev' );

var accumulator;
var v;
var i;

// Initialize an accumulator:
accumulator = incrnanmstdev( 5 );

// For each simulated datum, update the moving corrected sample standard deviation...
for ( i = 0; i < 100; i++ ) {
    if ( randu() < 0.2 ) {
        v = NaN;
    } else {
        v = randu() * 100.0;
    }
    accumulator( v );
}
console.log( accumulator() );