public class Accumulator extends Object
Accumulator
class is a data type for computing the running
mean, sample standard deviation, and sample variance of a stream of real
numbers. It provides an example of a mutable data type and a streaming
algorithm.
This implementation uses a one-pass algorithm that is less susceptible to floating-point roundoff error than the more straightforward implementation based on saving the sum of the squares of the numbers. This technique is due to B. P. Welford. Each operation takes constant time in the worst case. The amount of memory is constant - the data values are not stored.
For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
Constructor and Description |
---|
Accumulator()
Initializes an accumulator.
|
Modifier and Type | Method and Description |
---|---|
void |
addDataValue(double x)
Adds the specified data value to the accumulator.
|
int |
count()
Returns the number of data values.
|
static void |
main(String[] args)
Unit tests the
Accumulator data type. |
double |
mean()
Returns the mean of the data values.
|
double |
stddev()
Returns the sample standard deviation of the data values.
|
String |
toString()
Returns a string representation of this accumulator.
|
double |
var()
Returns the sample variance of the data values.
|
public void addDataValue(double x)
x
- the data valuepublic double mean()
public double var()
public double stddev()
public int count()
public String toString()
public static void main(String[] args)
Accumulator
data type.
Reads in a stream of real number from standard input;
adds them to the accumulator; and prints the mean,
sample standard deviation, and sample variance to standard
output.args
- the command-line arguments