Skip to content

Commit

Permalink
Changes Requested has been changed as per the sugegstion
Browse files Browse the repository at this point in the history
  • Loading branch information
in0068 committed Apr 8, 2021
1 parent ca454d9 commit 4cf984c
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 154 deletions.
18 changes: 0 additions & 18 deletions modules/avg.js

This file was deleted.

2 changes: 1 addition & 1 deletion modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export { default } from './underscore-array-methods.js';

//Statistical Function
export { default as sum } from './sum.js';
export { default as mean } from './avg.js';
export { default as mean } from './mean.js';
export { default as median } from './median.js';
export { default as standardDeviation } from './standardDeviation.js';
export { default as variance } from './variance.js';
Expand Down
11 changes: 11 additions & 0 deletions modules/mean.js
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;
}
57 changes: 34 additions & 23 deletions modules/median.js
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];
}
50 changes: 17 additions & 33 deletions modules/mode.js
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'));
}
24 changes: 8 additions & 16 deletions modules/standardDeviation.js
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));
}
27 changes: 12 additions & 15 deletions modules/standardError.js
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));
}
10 changes: 5 additions & 5 deletions modules/statRange.js
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);
}
20 changes: 8 additions & 12 deletions modules/sum.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import isArrayLike from './_isArrayLike.js';
import values from './values.js';
import cb from './_cb.js';
import each from './each.js';
import find from './find.js';

// Return the sum of elements (or element-based computation).
export default function sum(obj, iteratee, context) {
if (!iteratee && _.isEmpty(obj)){
return 0;
}
export default function sum(collection, iteratee, context) {
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];
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);
each(obj, function(v, index, list) {
computed = iteratee(v, index, list);
result += computed;
find(collection, function(v, index, list) {
result += iteratee(v, index, list);;
});
}
return result;
Expand Down
55 changes: 24 additions & 31 deletions modules/variance.js
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;
});
}
4 changes: 4 additions & 0 deletions test/statistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(function() {
var _ = typeof require == 'function' ? require('..') : window._;
QUnit.module('Statistics');
});

0 comments on commit 4cf984c

Please sign in to comment.