Skip to content

Latest commit

 

History

History
152 lines (93 loc) · 4.02 KB

File metadata and controls

152 lines (93 loc) · 4.02 KB

nanmeanwd

Compute the arithmetic mean of a one-dimensional ndarray, ignoring NaN values and using Welford's algorithm.

The arithmetic mean is defined as

$$\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i$$

Usage

var nanmeanwd = require( '@stdlib/stats/base/ndarray/nanmeanwd' );

nanmeanwd( arrays )

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.0

The function has the following parameters:

  • arrays: array-like object containing a one-dimensional input ndarray.

Notes

  • If provided an empty one-dimensional ndarray, the function returns NaN.

Examples

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 );

References

  • 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.