Help with stats method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jane507
    New Member
    • Apr 2007
    • 1

    Help with stats method

    As I guess is standard in today's undergrad education, I've got to code a stats class with various statistical methods. I have them all under control except the one for standard deviation. We're supposed to use a specific formula, the one that take the square root of everything. I don't know how to put it in here like my teacher has it on her website, so hopefully this makes sense:
    Sqrt [ (E (x - x) ^2) / n]
    where E is the summation symbol, x is an array value, x is the mean and n is the number of values in the array.

    I have tried to write this myself so don't think I'm wanting somebody out there to write the whole thing for me. I did however just get frustrated and deleted everything I had. So my code currently looks like this:
    Code:
    float StandardDeviation( int array[], int n)
    {
    }
    I have the mean method coded already, if that helps. I tried to bring it down and use it and it didn't work. Any help at all would be so appreciated!!
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    I wrote something similar to this recently, and it's tough, but doable. I broke everything down into a separate mathematical function - even simple additions. Is the array of a fixed length? I'm pretty sure you'll have to pass the mean, you won't be able to access it from a function - that's out of scope.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by jane507
      As I guess is standard in today's undergrad education, I've got to code a stats class with various statistical methods. I have them all under control except the one for standard deviation. We're supposed to use a specific formula, the one that take the square root of everything. I don't know how to put it in here like my teacher has it on her website, so hopefully this makes sense:
      Sqrt [ (E (x - x) ^2) / n]
      where E is the summation symbol, x is an array value, x is the mean and n is the number of values in the array.

      I have tried to write this myself so don't think I'm wanting somebody out there to write the whole thing for me. I did however just get frustrated and deleted everything I had. So my code currently looks like this:
      Code:
      float StandardDeviation( int array[], int n)
      {
      }
      I have the mean method coded already, if that helps. I tried to bring it down and use it and it didn't work. Any help at all would be so appreciated!!
      This is not very complicated if you make use of a simple for loop that for the summation

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by jane507
        I have tried to write this myself so don't think I'm wanting somebody out there to write the whole thing for me. I did however just get frustrated and deleted everything I had.
        Remember those old calculators? Some of them could even do some stats
        functions (such as the standard deviation). You could find those numbers for
        thousands of samples but you can bet on it that those old calculators didn't
        have many kilo/mega-bytes of ram avaialble to store those big arrays.

        This is how they did it:

        the square of the standard deviation is defined as:

        sum(((x-m)^2)/n) where m is the average or mean of all values x and n is
        the number of samples x. (identical to your formula).

        the value m itself is defined as sum(x)/n

        Let's fiddle a bit with that formula:

        sum(((x-m)^2)/n) --> sum((x^2-2.x.m+m^2)/n) -->

        sum(x^2/n)-sum(2.x.m/n)+sum(m^2/n) -->

        sum(x^2/n)-m.sum(2.x)/n+sum(m^2/n) -->

        sum(x^2/n)-2.m.m+m.m --> sum(x^2/n)-m.m --> sum(x^2)/n-(sum(x)/n)^2

        The latest form requires just sum(x^2) and the mean of all those x's squared.
        Suppose you have three variables:

        Code:
        double sx; // sum of x's
        double sxx; // sum of squares of x's
        int n; // number of sampes
        you can add a new x value as follows:
        Code:
        sx+= x;
        sxx+= x*x;
        n++;
        And you can find the square of the standard deviation as follows:

        Code:
        double ssd= ssx/n-(sx/n)*(sx/n);
        All you need are those three variables sx, sxx and n, keep on adding new
        x values and you can have the ssd at any time you want.

        kind regards,

        Jos

        Comment

        Working...