Average //JavaScript Repository
Description
Calculates the mean, standard deviation and variance in an array.
Created: 2005.12.24
Created: 2005.12.24
Code (Download)
//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/array/average [rev. #1]
average = function(a){
var r = {mean: 0, variance: 0, deviation: 0}, t = a.length;
for(var m, s = 0, l = t; l--; s += a[l]);
for(m = r.mean = s / t, l = t, s = 0; l--; s += Math.pow(a[l] - m, 2));
return r.deviation = Math.sqrt(r.variance = s / t), r;
}
Example (Example)
<script type="text/javascript">
//<![CDATA[
var x = average([2, 3, 4]);
document.write(
"average([2, 3, 4]).mean = ", x.mean, "<br />",
"average([2, 3, 4]).deviation = ", x.deviation, "<br />",
"average([2, 3, 4]).variance = ", x.variance
);
//]]>
</script>
Help
- shuffle(vector: Array): Array
-
Returns an object with 3 properties, they are:
- mean: arithmetic mean of the elements
- variance: variance
- deviation: standard deviation
- vector
- array containing numbers
Rank (Votes: 67)
2.91