Compute a geometric mean incrementally, ignoring
NaNvalues.
The geometric mean is defined as the nth root of a product of n numbers.
var incrnangmean = require( '@stdlib/stats/incr/nangmean' );Returns an accumulator function which incrementally computes a geometric mean, ignoring NaN values.
var accumulator = incrnangmean();If provided an input value x, the accumulator function returns an updated geometric mean. If not provided an input value x, the accumulator function returns the current geometric mean.
var accumulator = incrnangmean();
var prod = accumulator( 2.0 );
// returns 2.0
prod = accumulator( 1.0 );
// returns ~1.414
prod = accumulator( NaN );
// returns ~1.414
prod = accumulator( 3.0 );
// returns ~1.817
prod = accumulator();
// returns ~1.817- 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.
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var uniform = require( '@stdlib/random/base/uniform' );
var incrnangmean = require( '@stdlib/stats/incr/nangmean' );
var accumulator;
var v;
var i;
// Initialize an accumulator:
accumulator = incrnangmean();
// For each simulated value, update the geometric mean...
for ( i = 0; i < 100; i++ ) {
if ( bernoulli( 0.2 ) ) {
v = NaN;
} else {
v = uniform( 0.0, 100.0 );
}
accumulator( v );
}
console.log( accumulator() );