How can I simplify the following code that calculates standard deviation Before I get started I have spent much time searching for the answer. It seems the most referenced way is here, http://www.developer.com/print.php/3794146. I am not quite understanding how to optimize. See below.

public void CalculateStdDev()
{
    arrayItemCount = 0; // integer
    average = 0; // double
    double itemsSum = 0;
    double[] values = new double[DS.Tables[0].Rows.Count];
    double[] deviations = new double[DS.Tables[0].Rows.Count];
    double[] squareDeviations = new double[DS.Tables[0].Rows.Count];
    double squareDeviationsSum = 0;
    double squareDeviationsAverage = 0;
    stdDeviation = 0; // double

    for (int i = 0; i &lt; DS.Tables[0].Rows.Count; i++)
    {
        if (DS.Tables[0].Rows[i].ItemArray.GetValue(1).ToString() != "")
        {
            values[arrayItemCount] = Convert.ToDouble(DS.Tables[0].Rows[i].ItemArray.GetValue(1));
            itemsSum += Convert.ToDouble(DS.Tables[0].Rows[i].ItemArray.GetValue(1));
            arrayItemCount += 1;
        }
    }
    average = itemsSum / arrayItemCount;
    for (int i = 0; i &lt; arrayItemCount; i++)
    {
        deviations[i] = Math.Abs(values[i] - average);
        squareDeviations[i] = deviations[i] * deviations[i];
        squareDeviationsSum += squareDeviations[i];
    }
    squareDeviationsAverage = squareDeviationsSum / Convert.ToDouble(arrayItemCount - 1);
    stdDeviation = Math.Round(Convert.ToDouble(Math.Sqrt(squareDeviationsAverage)), 3);
}
If the method calculate standard desviation, it should return the value, not assign a global variable or instance variable.

Regards,