Skip to content
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

Closed
wants to merge 13 commits into from
18 changes: 18 additions & 0 deletions modules/avg.js
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 average 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;
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 10 additions & 0 deletions modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,13 @@ export { default as chunk } from './chunk.js';
// `underscore.js` and `index-default.js`.
export { default as mixin } from './mixin.js';
export { default } from './underscore-array-methods.js';

//Statistical Function
export { default as sum } from './sum.js';
export { default as mean } from './avg.js';
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
export { default as median } from './median.js';
export { default as standardDeviation } from './standardDeviation.js';
export { default as variance } from './variance.js';
export { default as mode } from './mode.js';
export { default as standardError } from './standardError.js';
export { default as statRange } from './statRange.js';
28 changes: 28 additions & 0 deletions modules/median.js
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 median element (or element-based computation).
export default function median(obj, iteratee, context) {
if (!iteratee && _.isEmpty(obj)){
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
return 0;
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
}
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
var tmpObj = [];
if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) {
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
obj = isArrayLike(obj) ? obj : values(obj);
tmpObj = clone(obj);
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
tmpObj.sort(function(f,s){return f-s;});
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
} else {
iteratee = cb(iteratee, context);
each(obj, function(v, index, list) {
computed = iteratee(v, index, list);
tmpObj.push(iteratee ? computed : v);
tmpObj.sort();
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
});
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
}
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved

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];
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
}
35 changes: 35 additions & 0 deletions modules/mode.js
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 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];
}

jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
currentStreak++;
}
result = currentStreak > bestStreak ? currentElem : bestElem;
return result
}
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions modules/standardDeviation.js
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 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));
}

return result;
}
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions modules/standardError.js
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;
}
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions modules/statRange.js
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);
}
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions modules/sum.js
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;
}
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
var result = 0;
if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) {
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
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) {
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
computed = iteratee(v, index, list);
result += computed;
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
});
}
return result;
}
34 changes: 34 additions & 0 deletions modules/variance.js
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 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;
}
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved