-
-
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.
Stats Function -- SUM,Median,Mean,Standarddeviation,mode,standardErro…
…r,variance
- Loading branch information
in0068
committed
Apr 7, 2021
1 parent
b713f5a
commit 6e3baf2
Showing
9 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
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,18 @@ | ||
import isArrayLike from './_isArrayLike.js'; | ||
import values from './values.js'; | ||
import sum from './sum.js' | ||
|
||
// Return the maximum element (or element-based computation). | ||
export default function mean(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 = sum(obj)/obj.length; | ||
} else { | ||
result = sum(obj, iteratee, context)/obj.length | ||
} | ||
return result; | ||
} |
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,28 @@ | ||
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' | ||
|
||
// Return the maximum 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(); | ||
}); | ||
} | ||
|
||
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]; | ||
} |
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,35 @@ | ||
import isArrayLike from './_isArrayLike.js'; | ||
import values from './values.js'; | ||
import sortBy from './sortBy.js' | ||
|
||
// Return the maximum 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]; | ||
} | ||
|
||
currentStreak++; | ||
} | ||
result = currentStreak > bestStreak ? currentElem : bestElem; | ||
return result | ||
} |
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,19 @@ | ||
import isArrayLike from './_isArrayLike.js'; | ||
import values from './values.js'; | ||
import variance from './variance.js' | ||
|
||
// Return the maximum 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)); | ||
} | ||
|
||
return result; | ||
} |
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,15 @@ | ||
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; | ||
} |
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,6 @@ | ||
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); | ||
} |
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,25 @@ | ||
import isArrayLike from './_isArrayLike.js'; | ||
import values from './values.js'; | ||
import cb from './_cb.js'; | ||
import each from './each.js'; | ||
|
||
// Return the sum of elements (or element-based computation). | ||
export default function sum(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); | ||
for (var i = 0, length = obj.length; i < length; i++) { | ||
result += obj[i]; | ||
} | ||
} else { | ||
iteratee = cb(iteratee, context); | ||
each(obj, function(v, index, list) { | ||
computed = iteratee(v, index, list); | ||
result += computed; | ||
}); | ||
} | ||
return result; | ||
} |
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,34 @@ | ||
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' | ||
|
||
// Return the maximum 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; | ||
} |