View Full Version : Looking for standard deviation function
Hi all,
I'm trying to find a js function that will calculate the standard deviation from a set of numbers. It doesn't need to be wildly accurate as it will be rounded to an integer anyway.
Does anyone know of one that I could use?
I did do a search on the forums but have come up with nothing. Surprisingly google hasn't turned up anything either.
Arty Effem 07112007, 10:20 PM Hi all,
I'm trying to find a js function that will calculate the standard deviation from a set of numbers. It doesn't need to be wildly accurate as it will be rounded to an integer anyway.
Does anyone know of one that I could use?
I did do a search on the forums but have come up with nothing. Surprisingly google hasn't turned up anything either.
I think I've got the algorithm right. The first parameter is the mean and you can add as many points as you want. The returned value is unrounded.
<script type='text/javascript'>
function sigma()
{
var mean=arguments[0], squareSum=0;
for(var i=1, len=arguments.length; i<len; i++)
squareSum+=Math.pow(arguments[i]mean, 2);
return Math.sqrt(squareSum/(i1));
}
alert( sigma(6, 4,8) )
</script>
Philip M 07112007, 10:50 PM A simple script:
<script type = "text/javascript">
var data = new Array("73", "58", "67", "93", "33", "18", "147");
var deviation = new Array();
var sum = 0;
var devnsum = 0;
var stddevn = 0;
var len = data.length;
for (var i=0; i<len; i++) {
sum = sum + (parseFloat(data[i],10)); // ensure number to base 10
}
var mean = (sum/len).toFixed(6); // 6 decimal places
for (i=0; i<len; i++) {
deviation[i] = data[i]  mean;
deviation[i] = deviation[i] * deviation[i];
devnsum = devnsum + deviation[i];
}
stddevn = Math.sqrt(devnsum/(len1)).toFixed(6); // 6 decimal places
alert ("The mean is: " + mean + " The standard deviation is: " + stddevn)
</script>
Thanks ever so much for your help with this folks, I really appreciate it as I have a work deadline looming of which this calculation is an integral part. It would have taken me a while to get my head around standard deviation and then write a function myself. I also appear to be developing RSI which makes typing rather uncomfortable so you've saved me a fair few keystrokes there too.
Thanks again,
John
A simple script:
<script type = "text/javascript">
var data = new Array("73", "58", "67", "93", "33", "18", "147");
var deviation = new Array();
var sum = 0;
var devnsum = 0;
var stddevn = 0;
var len = data.length;
for (var i=0; i<len; i++) {
sum = sum + (parseFloat(data[i],10)); // ensure number to base 10
}
var mean = (sum/len).toFixed(6); // 6 decimal places
for (i=0; i<len; i++) {
deviation[i] = data[i]  mean;
deviation[i] = deviation[i] * deviation[i];
devnsum = devnsum + deviation[i];
}
stddevn = Math.sqrt(devnsum/(len1)).toFixed(6); // 6 decimal places
alert ("The mean is: " + mean + " The standard deviation is: " + stddevn)
</script>
Script wasn't quite right due to the length and for loops counting from 0. Made a couple of small changes as follows:
<script type = "text/javascript">
var data = new Array("73", "58", "67", "93", "33", "18", "147");
var deviation = new Array();
var sum = 0;
var devnsum = 0;
var stddevn = 0;
var len = data.length  1;
alert(len);
for (var i=1; i<len; i++)
{
sum = sum + (parseFloat(data[i],10)); // ensure number to base 10
}
var mean = (sum/len).toFixed(2); // 2 decimal places
for (i=1; i<len; i++)
{
deviation[i] = data[i]  mean;
deviation[i] = deviation[i] * deviation[i];
devnsum = devnsum + deviation[i];
}
stddevn = Math.sqrt(devnsum/(len1)).toFixed(2); // 2 decimal places
alert ("The mean is: " + mean + " The standard deviation is: " + stddevn)
}
</script>
Works fine now, thanks all for your help.
Philip M 07122007, 04:35 PM Sorry, you have messed it up.
The array data() contains 7 elements, indexed 0  6.
So var len = data.length  1; (i.e. 6) and for (var i=1....) means that you only include five values 15 in the calculation.
Suggest you put it back as it was and study the script more carefully.
The correct answers are mean = 69.85 and standard deviation = 42.21 on the sample values given.
Why have you added a } here? It stops the script working.
alert ("The mean is: " + mean + " The standard deviation is: " + stddevn)
}
Obviously
toFixed(2); // 2 decimal places
is fine.
Sorry, you have messed it up.
The array data() contains 7 elements, indexed 0  6.
So var len = data.length  1; (i.e. 6) and for (i=1....) means that you only include five values 15 in the calculation.
Suggest you put it back as it was and study the script more carefully.
Obviously
toFixed(2); // 2 decimal places
is fine.
Yeah sorry, my mistake.
I do appreciate the help but you could have replied in a slightly less patronising fashion. Not everyone is born a **** hot coder.
Arty Effem 07122007, 05:17 PM Sorry, you have messed it up.
The array data() contains 7 elements, indexed 0  6.
So var len = data.length  1; (i.e. 6) and for (var i=1....) means that you only include five values 15 in the calculation.
Suggest you put it back as it was and study the script more carefully.
The correct answers are mean = 69.85 and standard deviation = 42.21 on the sample values given.
I don't agree, the sd is 39. Your calculation divides by the wrong value:
Math.sqrt(devnsum/(len1))
I assumed that the mean was known already, but that's easily fixed:
<script type='text/javascript'>
function sigma()
{
for(var i=0, mean=0, len=arguments.length; i<len; i++)
mean+=arguments[i];
mean/=i;
for(var i=0, squareSum=0, len=arguments.length; i<len; i++)
squareSum+=Math.pow(arguments[i]mean, 2);
return {sigma:Math.sqrt(squareSum/i), mean:mean}
}
var data=sigma(73, 58, 67, 93, 33, 18, 147);
alert( 'sd:' + data.sigma +' Mean:'+ data.mean )
</script>
Philip M 07122007, 06:54 PM I don't agree, the sd is 39. Your calculation divides by the wrong value:
Math.sqrt(devnsum/(len1))
I am afraid that I do not agree with you. len = 7 and len  1 = 6, which is one less than the number of data items.
When I was at school the standard deviation was calculated as follows:
A. Compute the mean for the data set.
B. Compute the deviation by subtracting the mean from each value.
C. Square each individual deviation.
D. Add up the squared deviations.
E. Divide by one less than the sample size.
F. Take the square root.

