Variance and an Algorithm

I was handed this task that basically said that it wanted a market variance percentage. I was like cool, I have not played with variances since my prob and stat days in college. So I was miserable to realize that they simple wanted the percent different ((A/B)* 100). So after a day the blog I go.

I am camping out at Vesta (a new pizza place by KapStone) opened up my surface and started working on this article. Pizza, beer and math… life is good.

To start lets define a variance:

A variance is a measurement of how far a set of numbers are spread out.

Some real world applications for variances:

Determine the best location to distribute goods from. Used to grade performance on machinery and people. Used to determine attendance forecasts (got this from my movie theatre days.)

Here is some code I wrote to calculate simple variance. Nothing fancy, and feel free to check it out on github.


class Variance
{
   public static int simple_variance(int[] values) {
     float total = 0;
     float totaledSquared = 0;
     float counter = 0;

     foreach (int value in values) {

        counter ++;
        total += value;
        totaledSquared += value * value;

     }


     return Convert.ToInt32((totaledSquared ((total * total) / counter)) / (counter 1));
  }
}

And the main program.


class Program
{

    static void Main(string[] args) {


         int[] myArray = {1,4,5,3,5,6,7,8,4};

         Console.WriteLine(Variance.simple_variance(myArray));

     }
}

 

Leave a Reply

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

12 − = 8

Post Navigation