I am trying to convert the following one-pass standard deviation code in C to Python:
double std_dev(double a[], int n) {
if(n == 0)
return 0.0;
int i = 0;
double meanSum = a[0];
double stdDevSum = 0.0;
for(i = 1; i < n; ++i) {
double stepSum = a[i] - meanSum;
double stepMean = ((i - 1) * stepSum) / i;
meanSum += stepMean;
stdDevSum += stepMean * stepSum;
}
// for poulation variance: return sqrt(stdDevSum / n);
return sqrt(stdDevSum / (n));
Here is what I have in Python so far:
def std_dev(a,n):
if n == 0:
return 0.0
i = 0
meanSum = float(a[0])
stdDevSum = float(0.0)
for i in range(1,n,1):
stepSum = float(float(a[i]) - meanSum)
stepMean = float(((i - 1)*stepSum)/i)
meanSum += stepMean
stdDevSum += stepMean*stepSum
print(stdDevSum)
value = float(sqrt(stdDevSum/(n)))
print(value)
However, I am not arriving at the correct result for Standard Deviation of the population. For instance, the program returns the standard deviation of the set [10,20,500,40,50] as 175.33, whereas an online calculator or by-hand calculation returns 188.53. How can we account for the difference?
Thanks!
C Algorithm Source: https://www.strchr.com/standard_deviation_in_one_pass