Compute the arithmetic mean of a one-dimensional ndarray, ignoring
NaNvalues and using Welford's algorithm.
The arithmetic mean is defined as
var nanmeanwd = require( '@stdlib/stats/base/ndarray/nanmeanwd' );Computes the arithmetic mean of a one-dimensional ndarray, ignoring NaN values and using Welford's algorithm.
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var xbuf = [ 1.0, 3.0, NaN, 2.0 ];
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
var v = nanmeanwd( [ x ] );
// returns 2.0The function has the following parameters:
- arrays: array-like object containing a one-dimensional input ndarray.
- If provided an empty one-dimensional ndarray, the function returns
NaN.
var uniform = require( '@stdlib/random/base/uniform' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var nanmeanwd = require( '@stdlib/stats/base/ndarray/nanmeanwd' );
function rand() {
if ( bernoulli( 0.8 ) < 1 ) {
return NaN;
}
return uniform( -50.0, 50.0 );
}
var xbuf = filledarrayBy( 10, 'generic', rand );
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
console.log( ndarray2array( x ) );
var v = nanmeanwd( [ x ] );
console.log( v );- Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." Technometrics 4 (3). Taylor & Francis: 419–20. doi:10.1080/00401706.1962.10490022.
- van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." Communications of the ACM 11 (3): 149–50. doi:10.1145/362929.362961.