Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion modules/mean.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import size from './size.js';
import sum from './sum.js';

// Return the average/mean element (or element-based computation).
// Compute the average of the numbers obtained from the collection
export default function mean(collection, iteratee, context) {
var length = size(collection);

Expand Down
20 changes: 5 additions & 15 deletions modules/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ import isEmpty from './isEmpty';
// 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
If the array has odd numbers then median is the middle element
*/

/* 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
If the array has even numbers then average of middle two numbers is the median value
*/
export default function median(collection, iteratee, context) {
if (isEmpty(collection)) return undefined;
Expand All @@ -31,9 +23,7 @@ export default function median(collection, iteratee, context) {
}
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];
return tmpArr.length % 2 ?
tmpArr[Math.floor(tmpArr.length / 2)] :
(tmpArr[tmpArr.length / 2 - 1] + tmpArr[tmpArr.length / 2]) / 2
}
6 changes: 2 additions & 4 deletions modules/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ 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;
// Return the element (or element-based computation) that appears most frequently in the collection.

// Array is sorted and traversed to find the most frequent element in the array
// Return the mode element (or element-based computation).
// https://en.wikipedia.org/wiki/Mode_(statistics)
export default function mode(collection, iteratee, context) {
if (isEmpty(collection)) return;

Expand Down
6 changes: 1 addition & 5 deletions modules/standardDeviation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
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.

// https://en.wikipedia.org/wiki/Standard_deviation
export default function standardDeviation(collection, iteratee, context) {
return Math.sqrt(variance(collection, iteratee, context));
}
7 changes: 2 additions & 5 deletions modules/standardError.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import variance from './variance.js';
import size from './size.js';

//https://en.wikipedia.org/wiki/Standard_error
// Return the standard error of the mean based on element-based computation.

// 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.
// https://en.wikipedia.org/wiki/Standard_error
export default function standardError(collection, iteratee, context) {
return Math.sqrt(variance(collection, iteratee, context)/(size(collection) - 1));
}
4 changes: 2 additions & 2 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';

export default function statRange(collection,iteratee,context){
return max(collection,iteratee,context) - min(collection,iteratee,context);
export default function statRange(collection, iteratee, context){
return max(collection, iteratee, context) - min(collection, iteratee, context);
}
10 changes: 3 additions & 7 deletions modules/variance.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
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 of the numeric elements of the collection,
// optionally after transforming them through `iteratee`.

// Return the variance based on the computation.
// https://en.wikipedia.org/wiki/Variance
export default function variance(collection, iteratee, context) {
if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') iteratee = null;

Expand Down
16 changes: 8 additions & 8 deletions test/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
QUnit.test('mean', function(assert) {

assert.strictEqual(_.mean(null), 0, 'can handle null/undefined');
assert.strictEqual(_.mean(void 0), 0, 'can handle null/undefined');
assert.strictEqual(_.mean(void 0), 0, 'can handle undefined');
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');
Expand All @@ -13,7 +13,7 @@
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(void 0), undefined, 'can handle 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');
Expand All @@ -23,7 +23,7 @@
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(void 0), 0, 'can handle 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');
Expand All @@ -33,7 +33,7 @@
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(void 0), 0, 'can handle 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');
Expand All @@ -43,7 +43,7 @@
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(void 0), 0, 'can handle 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');
Expand All @@ -53,7 +53,7 @@
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(void 0), 0, 'can handle 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');
Expand All @@ -63,7 +63,7 @@
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(void 0), undefined, 'can handle 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');
Expand All @@ -73,7 +73,7 @@
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(void 0), -Infinity, 'can handle 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');
Expand Down
55 changes: 16 additions & 39 deletions underscore-esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Large diffs are not rendered by default.

55 changes: 16 additions & 39 deletions underscore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore.js.map

Large diffs are not rendered by default.