- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.5k
Statistical Functions #2920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Statistical Functions #2920
Changes from 4 commits
6e3baf2
              ca454d9
              4cf984c
              d97a93d
              c9f39e1
              47f192d
              3cf8880
              bbaa44c
              9f2207b
              dc6eb04
              5317dcb
              65cdbbc
              e0bc2c8
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import size from './size.js'; | ||
| import sum from './sum.js'; | ||
|  | ||
| // Return the average/mean element (or element-based computation). | ||
| export default function mean(collection, iteratee, context) { | ||
| var length = size(collection); | ||
|  | ||
| if (length < 1) return 0; | ||
|  | ||
| return sum(collection, iteratee, context) / length; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import map from './map.js' | ||
| import isNumber from './isNumber.js'; | ||
| import isEmpty from './isEmpty'; | ||
|  | ||
| // https://en.wikipedia.org/wiki/Median | ||
| // Return the median element (or element-based computation). | ||
| // First arrays is sorted in ascending order | ||
| // Then middle element is the median in the given array | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| // Calulation of median is done using the following method; | ||
|  | ||
| /* Odd elements | ||
| If the array has odd numbers then value is the middle element | ||
| example: [1,2,3,4,5,6,7] | ||
| length: 7 | ||
| middle value: (length+1)/2 = 4 | ||
| median : array[4] = 4 | ||
| */ | ||
|  | ||
| /* Even elements | ||
| If the array has odd numbers then value is the middle element | ||
| example: [1,5,5,8,10,12,13,15] | ||
| length: 8 | ||
| middle value: ((length/2) + ((length/2)+1))/2 = | ||
| median : (8+10)/2 = 9 | ||
| */ | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| export default function median(collection, iteratee, context) { | ||
| if (isEmpty(collection)) return undefined; | ||
|  | ||
| if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { | ||
| iteratee = null; | ||
| } | ||
| var tmpArr = map(obj, iteratee, context).sort(); | ||
|  | ||
| return tmpArr.length%2 ? | ||
| tmpArr[Math.floor(tmpArr.length/2)] : | ||
| (isNumber(tmpArr[tmpArr.length/2-1]) && isNumber(tmpArr[tmpArr.length/2])) ? | ||
| (tmpArr[tmpArr.length/2-1]+tmpArr[tmpArr.length/2]) /2 : | ||
| tmpArr[tmpArr.length/2-1]; | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import isEmpty from './isEmpty'; | ||
| import groupBy from './groupBy.js'; | ||
| import max from './max.js'; | ||
| import first from './first.js'; | ||
|  | ||
| // https://en.wikipedia.org/wiki/Mode_(statistics) | ||
| // Mode is the value that appears most number of times in an array; | ||
|  | ||
| // Array is sorted and traversed to find the most frequent element in the array | ||
| // Return the mode element (or element-based computation). | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| export default function mode(collection, iteratee, context) { | ||
| if (isEmpty(collection)) return; | ||
|  | ||
| if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { | ||
| iteratee = null; | ||
| } | ||
| var groups = groupBy(collection, iteratee, context); | ||
| return first(max(groups, 'length')); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import variance from './variance.js'; | ||
|  | ||
| // https://en.wikipedia.org/wiki/Standard_deviation | ||
|  | ||
| // Suare root of the variance value | ||
| // Variance is calulation can go through the variance function for description (https://en.wikipedia.org/wiki/Variance) | ||
| // Return the standardDeviation based on element-based computation. | ||
|  | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| export default function standardDeviation(collection, iteratee, context) { | ||
| return Math.sqrt(variance(collection, iteratee, context)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import variance from './variance.js'; | ||
| import size from './size.js'; | ||
|  | ||
| //https://en.wikipedia.org/wiki/Standard_error | ||
|  | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| // Square root of variance divided by the number of elements (length -1) | ||
| // Variance is calulation can go through the variance function for description (https://en.wikipedia.org/wiki/Variance) | ||
|  | ||
| // Return the standardError based on element-based computation. | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| export default function standardError(collection, iteratee, context) { | ||
| return Math.sqrt(variance(collection, iteratee, context)/(size(collection) - 1)); | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import max from './max.js'; | ||
| import min from './min.js'; | ||
|  | ||
| export default function statRange(collection,iteratee,context){ | ||
| return max(collection,iteratee,context) - min(collection,iteratee,context); | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import isArrayLike from './_isArrayLike.js'; | ||
| import values from './values.js'; | ||
| import cb from './_cb.js'; | ||
| import find from './find.js'; | ||
|  | ||
| // Return the sum of elements (or element-based computation). | ||
| export default function sum(collection, iteratee, context) { | ||
| var result = 0; | ||
| if (iteratee == null || typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { | ||
| collection = isArrayLike(collection) ? collection : values(collection); | ||
| for (var i = 0, length = collection.length; i < length; i++) { | ||
| result += collection[i]; | ||
| } | ||
| } else { | ||
| iteratee = cb(iteratee, context); | ||
| find(collection, function(v, index, list) { | ||
| result += iteratee(v, index, list);; | ||
| }); | ||
| } | ||
| return result; | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import cb from './_cb.js'; | ||
| import mean from './mean.js'; | ||
|  | ||
| // https://en.wikipedia.org/wiki/Variance | ||
|  | ||
| // Steps to calculate variance | ||
| // 1. Average value of the array | ||
| // 2. New array is calulated by negating the value with the average value and to the power of 2. | ||
| // 3. Average value of the new array is the variance | ||
|  | ||
| // Return the variance based on the computation. | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| export default function variance(collection, iteratee, context) { | ||
| if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') iteratee = null; | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| iteratee = cb(iteratee, context); | ||
|  | ||
| var computed = []; | ||
| var avg = mean(collection, function(value, key, collection) { | ||
| var result = iteratee(value, key, collection); | ||
| computed.push(result); | ||
| return result; | ||
| }); | ||
| return mean(computed, function(value) { | ||
| var difference = value - avg; | ||
| return difference * difference; | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| (function() { | ||
| var _ = typeof require == 'function' ? require('..') : window._; | ||
| QUnit.module('Statistics'); | ||
| QUnit.test('mean', function(assert) { | ||
|  | ||
| assert.strictEqual(_.mean(null), 0, 'can handle null/undefined'); | ||
|         
                  jgonggrijp marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| assert.strictEqual(_.mean(void 0), -Infinity, 'can handle null/undefined'); | ||
| assert.strictEqual(_.mean([1, 2, 3]),2,"Avearge of the numbers in the collection"); | ||
|  | ||
| assert.strictEqual(_.mean({}), 0, 'Avearge value of an empty object'); | ||
| assert.strictEqual(_.mean([]), 0, 'Avearge value of an empty array'); | ||
| }); | ||
| }); | ||
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.