Standard Deviation problem

This is a discussion on Standard Deviation problem within the C++ Programming forums, part of the General Programming Boards category; I am trying to calculate standard deviation of all the elements in a vector, so far I have: Code: double ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Standard Deviation problem

    I am trying to calculate standard deviation of all the elements in a vector, so far I have:

    Code:
       double x;
       double squared_sum;
       double squared;
       
       for (int i = 0 ; i < num.size () ; i++ )
       {
           x = num[i] - mean; // mean = 4.2857
           squared = x*x;
           squared_sum += squared;
       }
      cout << "squared_sum" << x << endl;
    But when I run this I get something like: squared_sum.#INF

    What seems to be the problem and haw can I fix this?

    The numbers in the vector are: 2 5 3 2 10 1 7
    Last edited by Suchy; 09-23-2007 at 02:46 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Initialise the sum to zero perhaps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Another way to calculate the standard derivation each afters the numbers keep droping in:

    1. Count all numbers in variable n.
    2. Sum all numbers in variable sum.
    3. Sum all squares of the numbers in variable sqrsum.

    Now, at any time, the standard derivation of all numbers that has droped in so far is equal to sqrsum/n - (sum/n)^2. So, the standard derivation equals to the average of the squares minus the average squared.
    Come on, you can do it! b( ~_')

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by TriKri View Post
    Another way to calculate the standard derivation each afters the numbers keep droping in:

    1. Count all numbers in variable n.
    2. Sum all numbers in variable sum.
    3. Sum all squares of the numbers in variable sqrsum.

    Now, at any time, the standard derivation of all numbers that has droped in so far is equal to sqrsum/n - (sum/n)^2. So, the standard derivation equals to the average of the squares minus the average squared.
    This is also known as the "calculator method" because it requires fewer button presses when computing variances by hand.

  5. #5
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by brewbuck View Post
    This is also known as the "calculator method" because it requires fewer button presses when computing variances by hand.
    Also good if you get another number and want to calculate the new standard derivation in constant time, and not in O(n) time.
    Come on, you can do it! b( ~_')

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    3

    calculating the stardard diviation

    i think the problem is that you haven't initialised your variables therefore set
    squared_sum=0;
    square=0;
    Last edited by Salem; 10-06-2007 at 04:55 AM. Reason: Snip email solicitation - this is a forum, not a dating service

  7. #7
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Code:
    double normal(const double &mean, const double &sdiv)
    {
    
        static const double r_max = RAND_MAX +1;
        
        return sdiv * sqrt( -2 * log( (rand() + 1) / r_max ) ) * sin ( 2 * PI * rand() / r_max) + mean;
    }
    this is the standard deviation function i've used in my AI classes. Now, this returns a *random* number that is within the deviation that you set (as const double &sdiv)

    But it might help, shoot, I dunno.

  8. #8
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by sh3rpa View Post
    Code:
    double normal(const double &mean, const double &sdiv)
    {
    
        static const double r_max = RAND_MAX +1;
        
        return sdiv * sqrt( -2 * log( (rand() + 1) / r_max ) ) * sin ( 2 * PI * rand() / r_max) + mean;
    }
    this is the standard deviation function i've used in my AI classes. Now, this returns a *random* number that is within the deviation that you set (as const double &sdiv)

    But it might help, shoot, I dunno.
    It's funny I didn't know there was such an easy method to get a normal distributed number, by just inserting two "ordinary" random numbers into a short formula. Since I saw your post I've been bussy trying to find the probably distribution of the numbers generated, eventually I found an article at wikipedia.

    This method is called Box-Muller transform. What it does is that it generates a normal distributed complex number in polar form, then it extracts the imaginary part of it. But since the real and the imaginary part of the kind of normal distributed complex numbers this method is generating is independent, you can actually get two perfectly normal distributed and independent real numbers, the other is the real part of the number from it, and you get it by taking cosinus of the angle instead of sinus.

    So, you could have a function which takes two random numbers between 0 and 1 and transforms them into normal distributed numbers:

    Code:
    int normal(const double &mean, const double &sdiv,
               double &r1, double &r2) { //r1 and r2 contains the real return values, the int is just for error handling
        if (r1 <= 0 or r1 > 1) return 1;
        double abs = sdiv * sqrt(-2*log(r1));
        r1 = mean + abs*sin(2*PI*r2);
        r2 = mean + abs*cos(2*PI*r2);
        return 0;
    }
    Come on, you can do it! b( ~_')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard deviation
    By Dontgiveup in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2009, 02:46 PM
  2. How to calculate standard deviation
    By hobilla in forum C Programming
    Replies: 13
    Last Post: 03-14-2009, 07:41 AM
  3. help with standard deviation
    By belkins in forum C Programming
    Replies: 3
    Last Post: 10-29-2008, 12:04 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21