Skip to content

Latest commit

 

History

History
149 lines (92 loc) · 3.73 KB

File metadata and controls

149 lines (92 loc) · 3.73 KB

incrwvariance

Compute a weighted variance incrementally.

The weighted variance is defined as

$$\sigma^2_w = \frac{\sum_{i=1}^N w_i (x_i - \mu_w)^2}{\sum_{i=1}^N w_i}$$

where μ_w is the weighted arithmetic mean.

Usage

var incrwvariance = require( '@stdlib/stats/incr/wvariance' );

incrwvariance()

Returns an accumulator function which incrementally computes a weighted variance.

var accumulator = incrwvariance();

accumulator( [x, w] )

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

Notes

  • Input values are not type checked. If provided NaN or a value which, when used in computations, results in NaN, the accumulated value is NaN for 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.

Examples

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