Monday, February 25, 2013

Online Incremental and Decremental Stats

Thinking about implementing this in forwards and reverse.

Now to figure out skew and kurtosis... Also I note that the wikipedia article has parallel version so I can do it in parts. (Which would come in useful on a hadoop cluster for sure).. Also I should have paid more attention to recurrence relations in math class..




public static class Variance {
        private long numEntries;
        private double mean;
        private double secondMoment;

         public void add(double value ) {
            this.numEntries++;
            double delta = value - this.mean;
            this.mean = this.mean + (delta/this.numEntries);
            this.secondMoment = this.secondMoment + (delta * (value - this.mean) );
          }


public void remove(double value) {
            double oldMean = mean;
            this.mean *= numEntries;
            this.numEntries--;
            this.mean = (this.mean - value) / this.numEntries;
            this.secondMoment -= (value - oldMean)*(value - this.mean);
}


public double getVariance() {
            return( this.secondMoment / (this.numEntries - 1 ) );
        }
}

0 Comments:

Post a Comment

<< Home