Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine the standard deviation (stddev) of a set of values?

I need to know if a number compared to a set of numbers is outside of 1 stddev from the mean, etc..

like image 838
dead and bloated Avatar asked May 22 '09 00:05

dead and bloated


People also ask

How do you find the standard deviation of a set of values?

Step 1: Find the mean. Step 2: For each data point, find the square of its distance to the mean. Step 3: Sum the values from Step 2. Step 4: Divide by the number of data points.

What is the formula for standard deviation?

If a random variable has a binomial distribution, its standard deviation is given by: 𝜎= √npq, where mean: 𝜇 = np, n = number of trials, p = probability of success and 1-p =q is the probability of failure.

How do you find the standard deviation of a large data set?

To do this, add up all the numbers in a data set and divide by the total number of pieces of data. For example, if you have four numbers in a data set, divide the sum by four. This is the mean of the data set. Subtract the deviance of each piece of data by subtracting the mean from each number.


1 Answers

While the sum of squares algorithm works fine most of the time, it can cause big trouble if you are dealing with very large numbers. You basically may end up with a negative variance...

Plus, don't never, ever, ever, compute a^2 as pow(a,2), a * a is almost certainly faster.

By far the best way of computing a standard deviation is Welford's method. My C is very rusty, but it could look something like:

public static double StandardDeviation(List<double> valueList) {     double M = 0.0;     double S = 0.0;     int k = 1;     foreach (double value in valueList)      {         double tmpM = M;         M += (value - tmpM) / k;         S += (value - tmpM) * (value - M);         k++;     }     return Math.Sqrt(S / (k-2)); } 

If you have the whole population (as opposed to a sample population), then use return Math.Sqrt(S / (k-1));.

EDIT: I've updated the code according to Jason's remarks...

EDIT: I've also updated the code according to Alex's remarks...

like image 153
Jaime Avatar answered Sep 28 '22 02:09

Jaime