Maspear / jbooktrader

Automatically exported from code.google.com/p/jbooktrader
0 stars 0 forks source link

Incorrect standard deviation calculation #27

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In class MovingWindowStDev, the standard deviation calculation is incorrect.

For example the standard deviation of the array {0 , 2} is 1.
But:
MovingWindowStDev stdDev = new MovingWindowStDev(2);
stdDev.add(0.);
stdDev.add(2.);
System.out.println(stdDev.getStdev());

will display a result of 1.41[..] instead.
Here's a fix:

public double getStdev() {
    int capacity = getCapacity();
    double num = sumSquared - (sum * sum) / capacity;
    return Math.sqrt(num / capacity);
}

Original issue reported on code.google.com by donnef...@gmail.com on 5 Feb 2011 at 3:44

GoogleCodeExporter commented 9 years ago
I've revised the implementation and added the JUnit tests which pass. 

Original comment by eugene.k...@gmail.com on 23 Feb 2011 at 8:22