I need to know if a number compared to a set of numbers is outside of 1 stddev from the mean, etc..
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.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With