Standard Deviation and Algorithm

Seeing how my last algorithm article was on Variance it just seems natural that my next algorithm be about Standard Deviation. Standard Deviation is used in finance. The standard deviation on a company’s return of investment defines the company’s volatility. Another usage is to help define your restaurants par list. Tracking your attendance and sales Monday through Friday can define your mean which will allow for you to calculate a daily deviation. Now you know the minimum amount of pizzas you will need prep.

Standard Deviation for a data set is the square root of the data sets variance.

Standard Deviation shows how much volatility exists from the mean. A low result shows that the data points are close to the mean. A large standard deviation results when the data sets are spread out over a large range in values. Utilizing my code from variance I will now provide you with the standard deviation algorithm. For readability I will include a copy of the variance class instead of including a reference to that project.

Made a change to the variance class from using int’s to using doubles:

namespace VarianceAlgorithm {
  class Variance {

    public static double simple_variance(double[] values) 
{

     double total = 0;
     double totaledSquared = 0;
     double counter = 0;

     foreach (double value in values) {
       counter ++;
       total += value;
       totaledSquared += value * value;
     }

    return (totaledSquared ((total * total) / counter)) / (counter 1);
   }
}
}

The standard deviation class is now really simple:

namespace StandardDeviationAlgorithm {
   class StandardDeviation {

     public static double SimpleStandardDeviation(double[] values) {
       return Math.Sqrt(VarianceAlgorithm.Variance.simple_variance(values));
     }
   }
}

This is how the program is executed:

class Program 
{

  static void Main(string[] args) 
{

     double[] myArray = { 34.7, 23.6, 28.5, 37.8, 35.4, 28.6, 37.3, 48.9, 24.0 };

     Console.WriteLine(StandardDeviation.SimpleStandardDeviation(myArray));

   }
}

Let me know what you think and any other creative uses you can see for Standard Deviation.

Leave a Reply

Your email address will not be published. Required fields are marked *

48 − = 42

Post Navigation