Online Incremental and Decremental Stats
Thinking about implementing this in forwards and reverse.
http://www.mymathforum.com/viewtopic.php?f=44&t=14057
http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
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 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 ) );
}
}
public double getVariance() {
return( this.secondMoment / (this.numEntries - 1 ) );
}
}
0 Comments:
Post a Comment
<< Home