-
-
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
Closed
+910
−309
Closed
Statistical Functions #2920
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6e3baf2
Stats Function -- SUM,Median,Mean,Standarddeviation,mode,standardErro…
ca454d9
Comments added
4cf984c
Changes Requested has been changed as per the sugegstion
d97a93d
STatistics Test JS - In progress
c9f39e1
Statistics - Test Cases added
47f192d
Removed the build file
3cf8880
Changes Requested
bbaa44c
Percentile function
9f2207b
Removed Whitespaces
dc6eb04
Merge branch 'master' of https://github.com/jashkenas/underscore
5317dcb
Changes Implemented
65cdbbc
Changed to 2 white spaces
e0bc2c8
Line Break added
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import size from './size.js'; | ||
| import sum from './sum.js'; | ||
|
|
||
| // Compute the average of the numbers obtained from the collection | ||
| export default function mean(collection, iteratee, context) { | ||
| var length = size(collection); | ||
|
|
||
| if (length < 1) return 0; | ||
|
|
||
| return sum(collection, iteratee, context) / length; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import map from './map.js' | ||
| import isEmpty from './isEmpty'; | ||
|
|
||
| // https://en.wikipedia.org/wiki/Median | ||
| // Calulation of median is done using the following method. | ||
| // If the array has odd numbers then median is the middle element. | ||
| // If the array has even numbers then average of middle two numbers is the median value. | ||
| 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(collection, iteratee, context).sort(); | ||
|
|
||
| return tmpArr.length % 2 ? | ||
| tmpArr[Math.floor(tmpArr.length / 2)] : | ||
| (tmpArr[tmpArr.length / 2 - 1] + tmpArr[tmpArr.length / 2]) / 2; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import isEmpty from './isEmpty'; | ||
| import groupBy from './groupBy.js'; | ||
| import max from './max.js'; | ||
| import first from './first.js'; | ||
|
|
||
| // Return the element (or element-based computation) that appears most frequently in the collection. | ||
| // https://en.wikipedia.org/wiki/Mode_(statistics) | ||
| 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')); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import isEmpty from './isEmpty'; | ||
| import sortBy from './sortBy.js'; | ||
|
|
||
| // Return the percentile value of the numeric elements from the collection | ||
| //Ex : 50th,75th,99th etc. | ||
| //https://en.wikipedia.org/wiki/Percentile | ||
| export default function percentile(collection, percentile) { | ||
| if (isEmpty(collection)) return 0; | ||
| if (typeof percentile !== 'number') throw new TypeError('Percentile must be a number between 0 - 100'); | ||
| if (percentile <= 0) return collection[0]; | ||
| if (percentile >= 100) return collection[collection.length - 1]; | ||
|
|
||
| collection = sortBy(collection); | ||
| var index = (percentile / 100) * (collection.length - 1), | ||
| lowerIndex = Math.floor(index), | ||
| upperIndex = lowerIndex + 1, | ||
| weight = index % 1; | ||
|
|
||
| if (upperIndex >= collection.length) return collection[lowerIndex]; | ||
| return collection[lowerIndex] * (1 - weight) + collection[upperIndex] * weight; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import variance from './variance.js'; | ||
|
|
||
| // Return the standardDeviation based on element-based computation. | ||
| // https://en.wikipedia.org/wiki/Standard_deviation | ||
| export default function standardDeviation(collection, iteratee, context) { | ||
| return Math.sqrt(variance(collection, iteratee, context)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import variance from './variance.js'; | ||
| import size from './size.js'; | ||
|
|
||
| // Return the standard error of the mean based on element-based computation. | ||
| // https://en.wikipedia.org/wiki/Standard_error | ||
| export default function standardError(collection, iteratee, context) { | ||
| return Math.sqrt(variance(collection, iteratee, context) / (size(collection) - 1)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import cb from './_cb.js'; | ||
| import mean from './mean.js'; | ||
|
|
||
| // Return the variance of the numeric elements of the collection, | ||
| // optionally after transforming them through `iteratee`. | ||
| // https://en.wikipedia.org/wiki/Variance | ||
| export default function variance(collection, iteratee, context) { | ||
| if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { | ||
| iteratee = null; | ||
| } | ||
|
|
||
| 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; | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| (function() { | ||
| var _ = typeof require == 'function' ? require('..') : window._; | ||
| QUnit.module('Statistics'); | ||
| QUnit.test('mean', function(assert) { | ||
|
|
||
| assert.strictEqual(_.mean(null), 0, 'can handle null/undefined'); | ||
| assert.strictEqual(_.mean(void 0), 0, 'can handle 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'); | ||
| }); | ||
|
|
||
| QUnit.test('median', function(assert) { | ||
|
|
||
| assert.strictEqual(_.median(null), undefined, 'can handle null/undefined'); | ||
| assert.strictEqual(_.median(void 0), undefined, 'can handle undefined'); | ||
| assert.strictEqual(_.median([1, 2, 3]), 2, "Median of the numbers in the collection"); | ||
| assert.strictEqual(_.median([1, 2, 3, 4]), 2.5, "Median of the numbers in the collection"); | ||
| assert.strictEqual(_.median({}), undefined, 'Median value of an empty object'); | ||
| assert.strictEqual(_.median([]), undefined, 'Median value of an empty array'); | ||
| }); | ||
|
|
||
| QUnit.test('sum', function(assert) { | ||
|
|
||
| assert.strictEqual(_.sum(null), 0, 'can handle null/undefined'); | ||
| assert.strictEqual(_.sum(void 0), 0, 'can handle undefined'); | ||
| assert.strictEqual(_.sum([1, 2, 3]), 6, "SUM of the numbers in the collection"); | ||
| assert.strictEqual(_.sum([1, 2, 3, 4]), 10, "SUM of the numbers in the collection"); | ||
| assert.strictEqual(_.sum({}), 0, 'sum value of an empty object'); | ||
| assert.strictEqual(_.sum([]), 0, 'sum value of an empty array'); | ||
| }); | ||
|
|
||
| QUnit.test('variance', function(assert) { | ||
|
|
||
| assert.strictEqual(_.variance(null), 0, 'can handle null/undefined'); | ||
| assert.strictEqual(_.variance(void 0), 0, 'can handle undefined'); | ||
| assert.strictEqual(_.variance([0, 1, 2, 3, 4]), 2, "variance of the numbers in the collection"); | ||
| assert.strictEqual(_.variance([1, 2, 3, 4]), 1.25, "variance of the numbers in the collection"); | ||
| assert.strictEqual(_.variance({}), 0, 'variance value of an empty object'); | ||
| assert.strictEqual(_.variance([]), 0, 'variance value of an empty array'); | ||
| }); | ||
|
|
||
| QUnit.test('standardDeviation', function(assert) { | ||
|
|
||
| assert.strictEqual(_.standardDeviation(null), 0, 'can handle null/undefined'); | ||
| assert.strictEqual(_.standardDeviation(void 0), 0, 'can handle undefined'); | ||
| assert.strictEqual(_.standardDeviation([0, 1, 2, 3, 4]), 1.4142135623730951, "Standard Deviation of the numbers in the collection"); | ||
| assert.strictEqual(_.standardDeviation([1, 2, 3, 4]), 1.118033988749895, "Standard Deviation of the numbers in the collection"); | ||
| assert.strictEqual(_.standardDeviation({}), 0, 'Standard Deviation value of an empty object'); | ||
| assert.strictEqual(_.standardDeviation([]), 0, 'Standard Deviation value of an empty array'); | ||
| }); | ||
|
|
||
| QUnit.test('standardError', function(assert) { | ||
|
|
||
| assert.strictEqual(_.standardError(null), 0, 'can handle null/undefined'); | ||
| assert.strictEqual(_.standardError(void 0), 0, 'can handle undefined'); | ||
| assert.strictEqual(_.standardError([0, 1, 2, 3, 4]), 0.7071067811865476, "Standard Error of the numbers in the collection"); | ||
| assert.strictEqual(_.standardError([1, 2, 3, 4]), 0.6454972243679028, "Standard Error of the numbers in the collection"); | ||
| assert.strictEqual(_.standardError({}), 0, 'Standard Error value of an empty object'); | ||
| assert.strictEqual(_.standardError([]), 0, 'Standard Error value of an empty array'); | ||
| }); | ||
|
|
||
| QUnit.test('mode', function(assert) { | ||
|
|
||
| assert.strictEqual(_.mode(null), undefined, 'can handle null/undefined'); | ||
| assert.strictEqual(_.mode(void 0), undefined, 'can handle undefined'); | ||
| assert.strictEqual(_.mode([0, 1, 2, 3, 4]), 0, "Mode of the numbers in the collection"); | ||
| assert.strictEqual(_.mode([1, 1, 3, 4]), 1, "Mode of the numbers in the collection"); | ||
| assert.strictEqual(_.mode({}), undefined, 'Mode value of an empty object'); | ||
| assert.strictEqual(_.mode([]), undefined, 'Mode value of an empty array'); | ||
| }); | ||
|
|
||
| QUnit.test('statRange', function(assert) { | ||
|
|
||
| assert.strictEqual(_.statRange(null), -Infinity, 'can handle null/undefined'); | ||
| assert.strictEqual(_.statRange(void 0), -Infinity, 'can handle undefined'); | ||
| assert.strictEqual(_.statRange([0, 1, 2, 3, 4]), 4, "Stat Range of the numbers in the collection"); | ||
| assert.strictEqual(_.statRange([1, 1, 3, 4]), 3, "Stat Range of the numbers in the collection"); | ||
| assert.strictEqual(_.statRange({}), -Infinity, 'Stat Range value of an empty object'); | ||
| assert.strictEqual(_.statRange([]), -Infinity, 'Stat Range value of an empty array'); | ||
| }); | ||
|
|
||
| QUnit.test('percentile', function(assert) { | ||
|
|
||
| assert.strictEqual(_.percentile(null, 25), 0, 'can handle null/undefined'); | ||
| assert.strictEqual(_.percentile(void 0, 50), 0, 'can handle undefined'); | ||
| assert.strictEqual(_.percentile([0, 1, 2, 3, 4], 75), 3, "75th percentile of the numbers in the collection"); | ||
| assert.strictEqual(_.percentile([1, 1, 3, 4], 50), 2, "50th of the numbers in the collection"); | ||
|
|
||
| assert.strictEqual(_.percentile({}, 10), 0, 'Percentile value of an empty object'); | ||
| assert.strictEqual(_.percentile([], 50), 0, 'Percentile value of an empty array'); | ||
| assert.raises(function() { | ||
| _.percentile([1, 1, 3, 4], "50") | ||
| }, TypeError, 'Percentile must be a number between 0 - 100'); | ||
| }); | ||
| }()); | ||
jgonggrijp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.