@stdlib/stats-incr-nanmsum
v0.1.1
Published
Compute a moving sum incrementally, ignoring NaN values.
Readme
incrnanmsum
Compute a moving sum incrementally, ignoring
NaNvalues.
For a window of size W, the moving sum is defined as
Installation
npm install @stdlib/stats-incr-nanmsumUsage
var incrnanmsum = require( '@stdlib/stats-incr-nanmsum' );incrnanmsum( window )
Returns an accumulator function which incrementally computes a moving sum. The window parameter defines the number of values over which to compute the moving sum.
var accumulator = incrnanmsum( 3 );accumulator( [x] )
If provided an input value x, the accumulator function returns an updated sum. If not provided an input value x, the accumulator function returns the current sum.
var accumulator = incrnanmsum( 3 );
var sum = accumulator();
// returns null
sum = accumulator( 2.0 );
// returns 2.0
sum = accumulator( NaN );
// returns 2.0
sum = accumulator( -5.0 );
// returns -3.0
sum = accumulator( 3.0 );
// returns 0.0
sum = accumulator( 5.0 );
// returns 3.0
sum = accumulator();
// returns 3.0Notes
- Input values are not type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly before passing the value to the accumulator function.
- As
Wvalues are needed to fill the window buffer, the firstW-1returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values.
Examples
var uniform = require( '@stdlib/random-base-uniform' );
var bernoulli = require( '@stdlib/random-base-bernoulli' );
var incrnanmsum = require( '@stdlib/stats-incr-nanmsum' );
// Initialize an accumulator:
var accumulator = incrnanmsum( 5 );
// For each simulated datum, update the moving sum...
var i;
for ( i = 0; i < 100; i++ ) {
accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -50.0, 50.0 ) );
}
console.log( accumulator() );Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
