Even Simpler Stats in Javascript

Last week I wrote about simple statistics in javascript. However it turned out I needed something else. First of all, the only values I needed was the mean (arithmetic mean) and the standard deviation - but I needed these values from a large set of large numbers. However this turned out to be a problem, because the sum of all these numbers could possibly be greater than the largest number representable by javascript.

The solution was to make these values available as running calculations. Each time you add an element, the mean and standard deviation is calculated from the existing values, set size and the new value. This means for large sets, this code will use much less memory.

Like last time, the complete source is embedded from a gist at github.com

To check the values - I have tested with the same values as last time:

var stat = SimpleStats([12, 13, 15, 18, 19, 4, 59, 100, 6, 129, 83, 5]);
console.log(stat.getArithmeticMean()); //38.583333333333336
console.log(stat.getStandardDeviation()); //41.27844137346058

However now you should also be able to add a number to the set, and have the new values instantly available to you:

stat.addValues([1,2,3,4,5,6,7]);
console.log(stat.getArithmeticMean()); //25.842105263157894
console.log(stat.getStandardDeviation()); //36.82285211214811