Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Looking for standard deviation function

1. ## 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.

• Originally Posted by jhob
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.
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/(i-1));
}

</script>```

• 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/(len-1)).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

• Originally Posted by Philip M
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/(len-1)).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;
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/(len-1)).toFixed(2); // 2 decimal places
alert ("The mean is: " + mean + " The standard deviation is: " + stddevn)
}
</script>

Works fine now, thanks all for your help.

• 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 1-5 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.

• Originally Posted by Philip M
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 1-5 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.

• Originally Posted by Philip M
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 1-5 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:
Code:
`Math.sqrt(devnsum/(len-1))`
I assumed that the mean was known already, but that's easily fixed:
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>```

• Originally Posted by Arty Effem
I don't agree, the sd is 39. Your calculation divides by the wrong value:
Math.sqrt(devnsum/(len-1))
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.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•