Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 9 of 9

07112007, 07:20 PM #1
 Join Date
 May 2007
 Posts
 6
 Thanks
 0
 Thanked 0 Times in 0 Posts
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.
07112007, 10:20 PM
#2
 Join Date
 May 2006
 Location
 England
 Posts
 664
 Thanks
 0
 Thanked 84 Times in 84 Posts
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.
Code:<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>
07112007, 10:50 PM
#3
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,079
 Thanks
 203
 Thanked 2,542 Times in 2,520 Posts
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>
Last edited by Philip M; 07122007 at 07:54 AM. Reason: Improved version
07122007, 08:39 AM
#4
 Join Date
 May 2007
 Posts
 6
 Thanks
 0
 Thanked 0 Times in 0 Posts
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
07122007, 03:20 PM
#5
 Join Date
 May 2007
 Posts
 6
 Thanks
 0
 Thanked 0 Times in 0 Posts
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.
07122007, 04:35 PM
#6
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,079
 Thanks
 203
 Thanked 2,542 Times in 2,520 Posts
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.
Last edited by Philip M; 07122007 at 04:42 PM.
07122007, 04:42 PM
#7
 Join Date
 May 2007
 Posts
 6
 Thanks
 0
 Thanked 0 Times in 0 Posts
07122007, 05:17 PM
#8
 Join Date
 May 2006
 Location
 England
 Posts
 664
 Thanks
 0
 Thanked 84 Times in 84 Posts
I don't agree, the sd is 39. Your calculation divides by the wrong value:I assumed that the mean was known already, but that's easily fixed:Code:Math.sqrt(devnsum/(len1))
Code:<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>
07122007, 06:54 PM
#9
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,079
 Thanks
 203
 Thanked 2,542 Times in 2,520 Posts
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.