#! /usr/bin/perl -w
##
## Oldham, Jeffrey D.
## 2000Mar13
## util
##
## Compute the Mean and Variance Using Welford's Formula

## Compute the mean and variance of a stream of floating-point
## numbers.  The numbers should appear at the beginning of each line.
$mean = 0.0;
$variance = 0.0;
$count = 0;

# Compute the mean and maximum.
while (<ARGV>)  {
    ($number, $_) = split;
    ++$count;
    $diff = $number - $mean;
    $mean += $diff / $count;
    $variance += $diff * $diff * ($count -1) / $count;
}
$variance /= $count;

printf("The mean is %g.\n", $mean);
printf("The variance is %g.\n", $variance);
printf("The standard deviation is %g.\n", sqrt($variance));
