bcds.tools
Class RunningStdDev

java.lang.Object
  extended by bcds.tools.RunningStdDev

public class RunningStdDev
extends java.lang.Object

Computes a running (or online, or continuous) standard deviation, that is, as a new value is available, it is pushed into an instance of this class and, at any time, the standard deviation can be obtained with a call to stdDev.

This is an adaptation of the C++ code given in http://www.johndcook.com/standard_deviation.html. Another variation is given in http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance.

Unlike the original code, the standard deviation (method devStd) is the population standard deviation, not the sample's. To compensate the possible effects of this decision, both populationVariance and sampleVariance are provided. Beware: The maximum number of items (samples) that can be pushed is 2^31. Beyond that, the returned values will be wrong.

Author:
The authors of the above referenced URLs, Juan Segovia S.

Constructor Summary
RunningStdDev()
          Default constructor.
 
Method Summary
 void clear()
          Resets this object.
 double mean()
          Returns the mean of the elements pushed up to now.
 double populationVariance()
          Returns the population's variance.
 void push(double x)
          Includes x in the computation of the standard deviation and increments the counter of the number of items pushed.
 double sampleVariance()
          Returns the sample's variance.
 int size()
          Returns the number of items pushed up to now.
 double stdDev()
          Returns the square root of the population's variance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RunningStdDev

public RunningStdDev()
Default constructor.

Method Detail

clear

public void clear()
Resets this object.


push

public void push(double x)
Includes x in the computation of the standard deviation and increments the counter of the number of items pushed.


size

public int size()
Returns the number of items pushed up to now.


mean

public double mean()
Returns the mean of the elements pushed up to now.


populationVariance

public double populationVariance()
Returns the population's variance.


sampleVariance

public double sampleVariance()
Returns the sample's variance.


stdDev

public double stdDev()
Returns the square root of the population's variance.