-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes Requested has been changed as per the sugegstion
- Loading branch information
in0068
committed
Apr 8, 2021
1 parent
ca454d9
commit 4cf984c
Showing
11 changed files
with
124 additions
and
154 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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 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'; | ||
|
||
// 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; | ||
} |
This file contains 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 |
---|---|---|
@@ -1,28 +1,39 @@ | ||
import isArrayLike from './_isArrayLike.js'; | ||
import values from './values.js'; | ||
import cb from './_cb.js'; | ||
import each from './each.js'; | ||
import clone from './clone.js' | ||
import isNumber from './isNumber.js' | ||
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). | ||
export default function median(obj, iteratee, context) { | ||
if (!iteratee && _.isEmpty(obj)){ | ||
return 0; | ||
} | ||
var tmpObj = []; | ||
if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { | ||
obj = isArrayLike(obj) ? obj : values(obj); | ||
tmpObj = clone(obj); | ||
tmpObj.sort(function(f,s){return f-s;}); | ||
} else { | ||
iteratee = cb(iteratee, context); | ||
each(obj, function(v, index, list) { | ||
computed = iteratee(v, index, list); | ||
tmpObj.push(iteratee ? computed : v); | ||
tmpObj.sort(); | ||
}); | ||
// First arrays is sorted in ascending order | ||
// Then middle element is the median in the given array | ||
// 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 | ||
*/ | ||
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 tmpObj.length%2 ? tmpObj[Math.floor(tmpObj.length/2)] : (isNumber(tmpObj[tmpObj.length/2-1]) && isNumber(tmpObj[tmpObj.length/2])) ? (tmpObj[tmpObj.length/2-1]+tmpObj[tmpObj.length/2]) /2 : tmpObj[tmpObj.length/2-1]; | ||
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]; | ||
} |
This file contains 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 |
---|---|---|
@@ -1,35 +1,19 @@ | ||
import isArrayLike from './_isArrayLike.js'; | ||
import values from './values.js'; | ||
import sortBy from './sortBy.js' | ||
import isEmpty from './isEmpty'; | ||
import groupBy from './groupBy.js'; | ||
import max from './max.js'; | ||
import first from './first.js'; | ||
|
||
// Return the mode element (or element-based computation). | ||
export default function mode(obj, iteratee, context) { | ||
if (!iteratee && _.isEmpty(obj)){ | ||
return 0; | ||
} | ||
var result = 0; | ||
if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { | ||
obj = isArrayLike(obj) ? obj : values(obj); | ||
obj = sortBy(obj); | ||
}else{ | ||
obj = sortBy(tmpObj,iteratee,context); | ||
} | ||
var bestStreak = 1; | ||
var bestElem = obj[0]; | ||
var currentStreak = 1; | ||
var currentElem = obj[0]; | ||
for (var i = 1; i < obj.length; i++) { | ||
if (obj[i-1] !== obj[i]) { | ||
if (currentStreak > bestStreak) { | ||
bestStreak = currentStreak; | ||
bestElem = currentElem; | ||
} | ||
currentStreak = 0; | ||
currentElem = obj[i]; | ||
} | ||
// https://en.wikipedia.org/wiki/Mode_(statistics) | ||
// Mode is the value that appears most number of times in an array; | ||
|
||
currentStreak++; | ||
} | ||
result = currentStreak > bestStreak ? currentElem : bestElem; | ||
return result | ||
} | ||
// Array is sorted and traversed to find the most frequent element in the array | ||
// Return the mode element (or element-based computation). | ||
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 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 |
---|---|---|
@@ -1,19 +1,11 @@ | ||
import isArrayLike from './_isArrayLike.js'; | ||
import values from './values.js'; | ||
import variance from './variance.js' | ||
import variance from './variance.js'; | ||
|
||
// Return the standardDeviation element (or element-based computation). | ||
export default function standardDeviation(obj, iteratee, context) { | ||
if (!iteratee && _.isEmpty(obj)){ | ||
return 0; | ||
} | ||
var result = 0; | ||
if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { | ||
obj = isArrayLike(obj) ? obj : values(obj); | ||
result = Math.sqrt(variance(obj)); | ||
} else { | ||
result = Math.sqrt(variance(obj,iteratee,context)); | ||
} | ||
// https://en.wikipedia.org/wiki/Standard_deviation | ||
|
||
return result; | ||
// 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. | ||
|
||
export default function standardDeviation(collection, iteratee, context) { | ||
return Math.sqrt(variance(collection, iteratee, context)); | ||
} |
This file contains 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 |
---|---|---|
@@ -1,15 +1,12 @@ | ||
import isArrayLike from './_isArrayLike.js'; | ||
import values from './values.js'; | ||
import variance from './variance.js' | ||
export default function standardError(obj, iteratee, context) { | ||
if (!iteratee && _.isEmpty(obj)) return 0; | ||
var result = 0; | ||
if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { | ||
obj = isArrayLike(obj) ? obj : values(obj); | ||
result = Math.sqrt(variance(obj)/(obj.length-1)); | ||
} | ||
else{ | ||
result = Math.sqrt(variance(obj,iterator,context)/(obj.length-1)); | ||
} | ||
return result; | ||
} | ||
import variance from './variance.js'; | ||
import size from './size.js'; | ||
|
||
//https://en.wikipedia.org/wiki/Standard_error | ||
|
||
// 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. | ||
export default function standardError(collection, iteratee, context) { | ||
return Math.sqrt(variance(collection, iteratee, context)/(size(collection) - 1)); | ||
} |
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import max from './max.js' | ||
import min from './min.js' | ||
import max from './max.js'; | ||
import min from './min.js'; | ||
|
||
export default function statRange(obj,iteratee,context){ | ||
return max(obj,iteratee,context) - min(obj,iteratee,context); | ||
} | ||
export default function statRange(collection,iteratee,context){ | ||
return max(collection,iteratee,context) - min(collection,iteratee,context); | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,34 +1,27 @@ | ||
import isArrayLike from './_isArrayLike.js'; | ||
import values from './values.js'; | ||
import cb from './_cb.js'; | ||
import each from './each.js'; | ||
import mean from './avg.js' | ||
import mean from './mean.js'; | ||
|
||
// Return the variance element (or element-based computation). | ||
export default function variance(obj, iteratee, context) { | ||
if (!iteratee && _.isEmpty(obj)){ | ||
return 0; | ||
} | ||
var result = 0; | ||
if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { | ||
obj = isArrayLike(obj) ? obj : values(obj); | ||
var avg = mean(obj); | ||
var squareDiffs = obj.map(function(value){ | ||
return (value - avg) * (value - avg);; | ||
}); | ||
result = mean(squareDiffs); | ||
} else { | ||
var tmpObj; | ||
iteratee = cb(iteratee, context); | ||
each(obj, function(v, index, list) { | ||
computed = iteratee(v, index, list); | ||
tmpObj.push(iteratee ? computed : v); | ||
}); | ||
var avg = mean(tmpObj); | ||
var squareDiffs = tmpObj.map(function(value){ | ||
return (value - avg) * (value - avg);; | ||
}); | ||
result = mean(squareDiffs); | ||
} | ||
return result; | ||
// 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. | ||
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 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,4 @@ | ||
(function() { | ||
var _ = typeof require == 'function' ? require('..') : window._; | ||
QUnit.module('Statistics'); | ||
}); |