# Standard Deviation problem

• 09-23-2007
Suchy
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
• 09-23-2007
Salem
Initialise the sum to zero perhaps?
• 10-04-2007
TriKri
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.
• 10-04-2007
brewbuck
Quote:

Originally Posted by TriKri
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.
• 10-04-2007
TriKri
Quote:

Originally Posted by brewbuck
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.
• 10-05-2007
crazyheavyd
calculating the stardard diviation
i think the problem is that you haven't initialised your variables therefore set
Quote:

squared_sum=0;
square=0;
• 10-05-2007
sh3rpa
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.
• 10-07-2007
TriKri
Quote:

Originally Posted by sh3rpa
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; }```