Compute a weighted variance incrementally.
The weighted variance is defined as
where μ_w is the weighted arithmetic mean.
var incrwvariance = require( '@stdlib/stats/incr/wvariance' );Returns an accumulator function which incrementally computes a weighted variance.
var accumulator = incrwvariance();If provided an input value x and a weight w, the accumulator function returns an updated weighted variance. If not provided any input values, the accumulator function returns the current variance.
var accumulator = incrwvariance();
var s2 = accumulator( 2.0, 1.0 );
// returns 0.0
s2 = accumulator( 2.0, 0.5 );
// returns 0.0
s2 = accumulator( 3.0, 1.5 );
// returns 0.25
s2 = accumulator();
// returns 0.25- Input values are not type checked. If provided
NaNor a value which, when used in computations, results inNaN, the accumulated value isNaNfor all future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly before passing the value to the accumulator function.
var randu = require( '@stdlib/random/base/randu' );
var incrwvariance = require( '@stdlib/stats/incr/wvariance' );
var accumulator;
var v;
var w;
var i;
// Initialize an accumulator:
accumulator = incrwvariance();
// For each simulated datum, update the weighted variance...
for ( i = 0; i < 100; i++ ) {
v = randu() * 100.0;
w = randu() * 100.0;
accumulator( v, w );
}
console.log( accumulator() );