public void Push(double value)
{
count++;
// See Knuth TAOCP vol 2, 3rd edition, page 232
// http://www.johndcook.com/standard_deviation.html
if (count == 1)
{
mean = lastMean = value;
sigma = lastSigma = 0.0;
}
else
{
mean = lastMean + (value - lastMean) / count;
sigma = (lastSigma + (value - lastMean) * (value - Mean));
lastMean = mean;
lastSigma = sigma;
}
}
public void ClearTest() { double[] values = { 0.5, -1.2, 0.7, 0.2, 1.1 }; RunningNormalStatistics target = new RunningNormalStatistics(); for (int i = 0; i < values.Length; i++) target.Push(values[i]); target.Clear(); double[] values2 = { 1.5, -5.2, 0.7, 1.2, 9.1 }; for (int i = 0; i < values.Length; i++) target.Push(values2[i]); Assert.AreEqual(values2.Mean(), target.Mean, 1e-10); Assert.AreEqual(values2.StandardDeviation(), target.StandardDeviation, 1e-10); Assert.AreEqual(values2.Variance(), target.Variance, 1e-10); }