public void add ( double x , double weight ) { //See http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance if ( weight < 0 ) throw new ArithmeticException ( "Can not add a negative weight" ) ; else if ( weight == 0 ) return ; double n1 = n ; n += weight ; double delta = x - mean ; double delta_n = delta * weight / n ; double delta_n2 = delta_n * delta_n ; double term1 = delta * delta_n * n1 ; mean += delta_n ; m4 += term1 * delta_n2 * ( n * n - 3 * n + 3 ) + 6 * delta_n2 * m2 - 4 * delta_n * m3 ; m3 += term1 * delta_n * ( n - 2 ) - 3 * delta_n * m2 ; m2 += weight * delta * ( x - mean ) ; if ( min == null ) min = max = x ; else { min = Math . min ( min , x ) ; max = Math . max ( max , x ) ; } }
Adds a data sample the the counts with the provided weight of influence .