jwo.utils
Class VarianceCalculator

java.lang.Object
  extended by jwo.utils.VarianceCalculator

public class VarianceCalculator
extends Object

Class for efficient calculation of variance and standard deviation and mean of a list of numbers. Uses the one-pass method described in Hoemmen (2007) www.cs.berkeley.edu/~mhoemmen/cs194/Tutorials/variance.pdf

Version:
2.3, 10th September, 2008
Author:
Jo Wood

Constructor Summary
VarianceCalculator()
          Initialises the calculator.
 
Method Summary
 void add(double x)
          Adds a value to the values to be described.
 double getMean()
          Reports the mean of the numbers that have been added to the calculator.
 double getNumValues()
          Reports the number of values that have been added to the calculator.
 double getSampleStdev()
          Reports the sample standard deviation (applying Bessel's correction) of the numbers that have been added to the calculator.
 double getSampleVariance()
          Reports the sample variance (applying Bessel's correction) of the numbers that have been added to the calculator.
 double getStdev()
          Reports the population standard deviation of the numbers that have been added to the calculator.
 double getVariance()
          Reports the population variance of the numbers that have been added to the calculator.
 void reset()
          Resets the calculator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VarianceCalculator

public VarianceCalculator()
Initialises the calculator.

Method Detail

add

public void add(double x)
Adds a value to the values to be described. Note that the complete set of values is never stored, so this method can be called large numbers of times for very large samples without incurring a memory overhead.

Parameters:
x - Value to add.

reset

public void reset()
Resets the calculator.


getNumValues

public double getNumValues()
Reports the number of values that have been added to the calculator.

Returns:
Number of values added.

getMean

public double getMean()
Reports the mean of the numbers that have been added to the calculator.

Returns:
Mean of the numbers added to the calculator or NaN if no numbers have been added.

getVariance

public double getVariance()
Reports the population variance of the numbers that have been added to the calculator.

Returns:
Population variance of the numbers added to the calculator or 0 if only one number added or NaN if no numbers have been added.

getSampleVariance

public double getSampleVariance()
Reports the sample variance (applying Bessel's correction) of the numbers that have been added to the calculator.

Returns:
Sample variance of the numbers added to the calculator or 0 if only one number added or NaN if no numbers have been added.

getStdev

public double getStdev()
Reports the population standard deviation of the numbers that have been added to the calculator.

Returns:
Population standard deviation of the numbers added to the calculator or 0 if only one number added or NaN if no numbers have been added.

getSampleStdev

public double getSampleStdev()
Reports the sample standard deviation (applying Bessel's correction) of the numbers that have been added to the calculator.

Returns:
Sample standard deviation of the numbers added to the calculator or 0 if only one number added or NaN if no numbers have been added.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009