Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 './mean.js';
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';
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).
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
export default function mean(collection, iteratee, context) {
var length = size(collection);

if (length < 1) return 0;

return sum(collection, iteratee, context) / length;
}
39 changes: 39 additions & 0 deletions modules/median.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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).
// First arrays is sorted in ascending order
// Then middle element is the median in the given array
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
// 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
*/
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
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)] :
(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];
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
}
19 changes: 19 additions & 0 deletions modules/mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import isEmpty from './isEmpty';
import groupBy from './groupBy.js';
import max from './max.js';
import first from './first.js';

// https://en.wikipedia.org/wiki/Mode_(statistics)
// Mode is the value that appears most number of times in an array;

// Array is sorted and traversed to find the most frequent element in the array
// Return the mode element (or element-based computation).
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
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'));
}
11 changes: 11 additions & 0 deletions modules/standardDeviation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import variance from './variance.js';

// https://en.wikipedia.org/wiki/Standard_deviation

// 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.

Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
export default function standardDeviation(collection, iteratee, context) {
return Math.sqrt(variance(collection, iteratee, context));
}
12 changes: 12 additions & 0 deletions modules/standardError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import variance from './variance.js';
import size from './size.js';

//https://en.wikipedia.org/wiki/Standard_error

Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
// 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.
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
export default function standardError(collection, iteratee, context) {
return Math.sqrt(variance(collection, iteratee, context)/(size(collection) - 1));
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
}
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(collection,iteratee,context){
return max(collection,iteratee,context) - min(collection,iteratee,context);
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
}
21 changes: 21 additions & 0 deletions modules/sum.js
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;
}
27 changes: 27 additions & 0 deletions modules/variance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import cb from './_cb.js';
import mean from './mean.js';

// 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.
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
export default function variance(collection, iteratee, context) {
if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') iteratee = null;
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated

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;
});
}
82 changes: 82 additions & 0 deletions test/statistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(function() {
var _ = typeof require == 'function' ? require('..') : window._;
QUnit.module('Statistics');
QUnit.test('mean', function(assert) {

assert.strictEqual(_.mean(null), 0, 'can handle null/undefined');
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
assert.strictEqual(_.mean(void 0), 0, 'can handle null/undefined');
Comment thread
jgonggrijp marked this conversation as resolved.
Outdated
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 null/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 null/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 null/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 null/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 null/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 null/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 null/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');
});
}());
4 changes: 2 additions & 2 deletions underscore-esm-min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion underscore-esm-min.js.map

Large diffs are not rendered by default.

Loading