/*
#############################################################################
This function returns the standard deviation statistics in a space-delimited
numeric text string.
Population =
The total count of all data points in the general population.
This value may or not be known. It could be estimated, like the population
of city or state at any given moment.
It could be an exactly known population size, like the exact number of people
occupying a large classroom or auditorium making up the entire population
under study and nothing outside matters.
Sample Population =
A sample drawn from a much larger general population, like 10,000 people
picked at random from a total population of multiple millions.
-----------------------------------------------
THE RETURNED STANDARD DEVIATION STATISTICS ARE:
N = Number or total count of all data points
sum = Sum of all the data points
mean = Arithmetic mean of the data points
SumOfSquaresOfDiffs = Sum of squares of all differences from the mean
PopVariance = Population variance
SamVariance = Sample variance
PopStdDev = Population standard deviation
SamStdDev = Sample standard deviation
NO DEPENDENCIES
ERRORS:
No special error checking is done.
#############################################################################
*/
function Std_Dev_Stats ($NumericDataTableStr)
{
$w = Str_Replace(',', ' ', $NumericDataTableStr);
$w = Str_Replace(';', ' ', $w);
$w = Str_Replace('|', ' ', $w);
$w = Str_Replace("\n", ' ', $w);
$WorkString = PReg_Replace("/\s+/", ' ', trim($w));
$N = $SumAllData = 0;
while ($WorkString <> '')
{
$N++;
$i = StrPos($WorkString, ' ');
if ($i === FALSE)
{
$SumAllData += $WorkString;
$mean = $SumAllData / $N;
break;
}
$xi = trim(substr($WorkString, 0, $i));
$SumAllData += $xi;
$mean = $SumAllData / $N;
$WorkString = trim(substr($WorkString, $i, StrLen($WorkString)));
}
// Cycle through data computing the sum of the squares of all differences
// from mean. The mean of this summation of squares is the population
// variance.
$w = Str_Replace(',', ' ', $NumericDataTableStr);
$w = Str_Replace(';', ' ', $w);
$WorkString = PReg_Replace("/\s+/", ' ', trim($w));
$N = $SumOfSquaresOfDiffs = 0;
while ($WorkString != '')
{
$N++;
$i = StrPos($WorkString, ' ');
if ($i === FALSE)
{
$diff = $WorkString - $mean;
$SumOfSquaresOfDiffs += ($diff * $diff);
break;
}
$xi = trim(substr($WorkString, 0, $i));
$diff = $xi - $mean;
$SumOfSquaresOfDiffs += ($diff * $diff);
$WorkString = trim(substr($WorkString, $i, StrLen($WorkString)));
}
$PopVariance = $SumOfSquaresOfDiffs / ($N-0); // sigma
$SamVariance = $SumOfSquaresOfDiffs / ($N-1); // s
$PopStdDev = sqrt($PopVariance); // sqrt(sigma)
$SamStdDev = sqrt($SamVariance); // sqrt(s)
$StdDevstats = "$N $SumAllData $mean $SumOfSquaresOfDiffs $PopVariance $SamVariance $PopStdDev $SamStdDev";
return $StdDevstats;
} // End of Std_Dev_Stats(...)