In this post I would like to talk a little bit about making statistics with javascript. I am not an expert neither in javascript nor math, but sometimes I need to recalculate, standardize or analyse some values. I have created some function, all of them will have input of array with values. Output will be a number or an array with numbers as a result of chosen statistic operation.

**Sum**

The first operation I would like to talk about is sum, because it is very simple. First option is to just iterate through all the values in array:

1 2 3 4 5 6 7 |
function getSum(arr){ var sum = 0 for (a in arr){ sum +=arr[a] } return sum } |

The second option is to use “Array.prototype.reduce” method:

1 2 3 |
function getSum(arr){ return arr.reduce(function(a,b) {return a+b} ); } |

**Arithmetic mean
**

Most of people would just say “average”. But in statistics, there are lot of so called “measures of central tendency” that define center of the distribution. Arithmetic mean is very easy to calculate, it is just a sum divided by number of elements:

1 2 3 4 5 |
function getAvg(arr){ var sum = getSum(arr); var len = arr.length; return sum/len } |

**Median**

Another “measure of central tendency” is median. It is the middle value that separates the higher half from the lower half of the data set. If the number of values is odd, we just pick the middle one after sorting the array. The problem is, when we have even number of values. In this case we just sum two “middle” values and divide them by two. Even number will return “0” after %2.

1 2 3 4 5 6 7 8 |
function getMedian(arr){ arr.sort( function(a,b) {return a - b;} ); var half = Math.floor(arr.length/2); if(arr.length % 2) return arr[half]; else return (arr[half-1] + arr[half]) / 2.0; } |

**Mode **

Mode is the next central tendency value and shows the number that is in the set as most frequent one. If there are more values as most frequent and you need just one, this is one option:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function getMode(arr){ if(arr.length == 0) return null; var modeMap = {}; var maxEl = arr[0], maxCount = 1; for(var i = 0; i < arr.length; i++){ var el = arr[i]; if(modeMap[el] == null) modeMap[el] = 1; else modeMap[el]++; if(modeMap[el] > maxCount) { maxEl = el; maxCount = modeMap[el]; } } return maxEl; } |

all the values are saved in js object “modeMap” and variable “maxEl” is the most frequent value.

There is a possibility, more values are most frequent and in this case you would like to return an array of this values:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function getMode(arr){ if(arr.length == 0) return null; var modeMap = {}; var maxEl = []; for(var i = 0; i < arr.length; i++){ var el = arr[i]; if(modeMap[el] == null) modeMap[el] = 1; else modeMap[el]++; } var vals = Object.keys(modeMap).map(function (key) { return modeMap[key]; }); var keys = Object.keys(modeMap); var max = Math.max.apply(Math, vals); for (i in vals){ if (vals[i] == max) maxEl.push(keys[i]) } return maxEl; } |

In our code we added lines for getting all the values and keys from object. Then we found maximal frequency and iterated all the values to found all the keys.

There are more “measure of central tendency” but really dont know why would one use them.