Comparing version 1.1.0 to 1.2.0
49
array.js
@@ -26,5 +26,5 @@ 'use strict'; | ||
exports.max = function max(values) { | ||
var max = -Infinity; | ||
var max = values[0]; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
for (var i = 1; i < l; i++) { | ||
if (values[i] > max) max = values[i]; | ||
@@ -41,5 +41,5 @@ } | ||
exports.min = function min(values) { | ||
var min = Infinity; | ||
var min = values[0]; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
for (var i = 1; i < l; i++) { | ||
if (values[i] < min) min = values[i]; | ||
@@ -56,6 +56,6 @@ } | ||
exports.minMax = function minMax(values) { | ||
var min = Infinity; | ||
var max = -Infinity; | ||
var min = values[0]; | ||
var max = values[0]; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
for (var i = 1; i < l; i++) { | ||
if (values[i] < min) min = values[i]; | ||
@@ -146,3 +146,3 @@ if (values[i] > max) max = values[i]; | ||
if (!alreadySorted) { | ||
values = values.slice().sort(compareNumbers); | ||
values = [].concat(values).sort(compareNumbers); | ||
} | ||
@@ -203,3 +203,3 @@ var l = values.length; | ||
if (!alreadySorted) { | ||
values = values.slice().sort(compareNumbers); | ||
values = [].concat(values).sort(compareNumbers); | ||
} | ||
@@ -253,7 +253,32 @@ var l = values.length; | ||
/** | ||
* IEEE Transactions on biomedical engineering, vol. 52, no. 1, january 2005, p. 76- | ||
* Calculate the standard deviation via the Median of the absolute deviation | ||
* The formula for the standard deviation only holds for Gaussian random variables. | ||
* @returns {{mean: number, stdev: number}} | ||
*/ | ||
exports.robustMeanAndStdev = function robustMeanAndStdev(y) { | ||
var mean = 0, stdev = 0; | ||
var length = y.length, i = 0; | ||
for (i = 0; i < length; i++) { | ||
mean += y[i]; | ||
} | ||
mean /= length; | ||
var averageDeviations = new Array(length); | ||
for (i = 0; i < length; i++) | ||
averageDeviations[i] = Math.abs(y[i] - mean); | ||
averageDeviations.sort(compareNumbers); | ||
if (length % 2 == 1) { | ||
stdev = averageDeviations[(length - 1) / 2] / 0.6745; | ||
} else { | ||
stdev = 0.5 * (averageDeviations[length / 2] + averageDeviations[length / 2 - 1]) / 0.6745; | ||
} | ||
return {mean, stdev}; | ||
}; | ||
exports.quartiles = function quartiles(values, alreadySorted) { | ||
if (typeof(alreadySorted) === 'undefined') alreadySorted = false; | ||
if (!alreadySorted) { | ||
values = values.slice(); | ||
values.sort(compareNumbers); | ||
values = [].concat(values).sort(compareNumbers); | ||
} | ||
@@ -435,3 +460,3 @@ | ||
if (!inPlace) | ||
result = values.slice(); | ||
result = [].concat(values); | ||
@@ -438,0 +463,0 @@ var theMean = exports.mean(result), l = result.length; |
@@ -0,1 +1,7 @@ | ||
1.2.0 / 2016-06-16 | ||
================== | ||
* add array.robustMeanAndStdev | ||
* add matrix.min, matrix.max and matrix.minMax | ||
1.1.0 / 2015-09-02 | ||
@@ -2,0 +8,0 @@ ================== |
176
matrix.js
'use strict'; | ||
var arrayStat = require('./array'); | ||
// https://github.com/accord-net/framework/blob/development/Sources/Accord.Statistics/Tools.cs | ||
function compareNumbers(a, b) { | ||
return a - b; | ||
} | ||
function entropy(matrix, eps) { | ||
exports.max = function max(matrix) { | ||
var max = -Infinity; | ||
for (var i = 0; i < matrix.length; i++) { | ||
for (var j = 0; j < matrix[i].length; j++) { | ||
if (matrix[i][j] > max) max = matrix[i][j]; | ||
} | ||
} | ||
return max; | ||
}; | ||
exports.min = function min(matrix) { | ||
var min = Infinity; | ||
for (var i = 0; i < matrix.length; i++) { | ||
for (var j = 0; j < matrix[i].length; j++) { | ||
if (matrix[i][j] < min) min = matrix[i][j]; | ||
} | ||
} | ||
return min; | ||
}; | ||
exports.minMax = function minMax(matrix) { | ||
var min = Infinity; | ||
var max = -Infinity; | ||
for (var i = 0; i < matrix.length; i++) { | ||
for (var j = 0; j < matrix[i].length; j++) { | ||
if (matrix[i][j] < min) min = matrix[i][j]; | ||
if (matrix[i][j] > max) max = matrix[i][j]; | ||
} | ||
} | ||
return {min, max}; | ||
}; | ||
exports.entropy = function entropy(matrix, eps) { | ||
if (typeof(eps) === 'undefined') { | ||
@@ -19,5 +54,5 @@ eps = 0; | ||
return -sum; | ||
} | ||
}; | ||
function mean(matrix, dimension) { | ||
exports.mean = function mean(matrix, dimension) { | ||
if (typeof(dimension) === 'undefined') { | ||
@@ -63,6 +98,6 @@ dimension = 0; | ||
return theMean; | ||
} | ||
}; | ||
function standardDeviation(matrix, means, unbiased) { | ||
var vari = variance(matrix, means, unbiased), l = vari.length; | ||
exports.standardDeviation = function standardDeviation(matrix, means, unbiased) { | ||
var vari = exports.variance(matrix, means, unbiased), l = vari.length; | ||
for (var i = 0; i < l; i++) { | ||
@@ -72,9 +107,9 @@ vari[i] = Math.sqrt(vari[i]); | ||
return vari; | ||
} | ||
}; | ||
function variance(matrix, means, unbiased) { | ||
exports.variance = function variance(matrix, means, unbiased) { | ||
if (typeof(unbiased) === 'undefined') { | ||
unbiased = true; | ||
} | ||
means = means || mean(matrix); | ||
means = means || exports.mean(matrix); | ||
var rows = matrix.length; | ||
@@ -99,5 +134,5 @@ if (rows === 0) return []; | ||
return vari; | ||
} | ||
}; | ||
function median(matrix) { | ||
exports.median = function median(matrix) { | ||
var rows = matrix.length, cols = matrix[0].length; | ||
@@ -111,3 +146,3 @@ var medians = new Array(cols); | ||
} | ||
data.sort(); | ||
data.sort(compareNumbers); | ||
var N = data.length; | ||
@@ -121,5 +156,5 @@ if (N % 2 === 0) { | ||
return medians; | ||
} | ||
}; | ||
function mode(matrix) { | ||
exports.mode = function mode(matrix) { | ||
var rows = matrix.length, | ||
@@ -159,7 +194,7 @@ cols = matrix[0].length, | ||
return modes; | ||
} | ||
}; | ||
function skewness(matrix, unbiased) { | ||
exports.skewness = function skewness(matrix, unbiased) { | ||
if (typeof(unbiased) === 'undefined') unbiased = true; | ||
var means = mean(matrix); | ||
var means = exports.mean(matrix); | ||
var n = matrix.length, l = means.length; | ||
@@ -189,7 +224,7 @@ var skew = new Array(l); | ||
return skew; | ||
} | ||
}; | ||
function kurtosis(matrix, unbiased) { | ||
exports.kurtosis = function kurtosis(matrix, unbiased) { | ||
if (typeof(unbiased) === 'undefined') unbiased = true; | ||
var means = mean(matrix); | ||
var means = exports.mean(matrix); | ||
var n = matrix.length, m = matrix[0].length; | ||
@@ -219,7 +254,8 @@ var kurt = new Array(m); | ||
return kurt; | ||
} | ||
}; | ||
function standardError(matrix) { | ||
exports.standardError = function standardError(matrix) { | ||
var samples = matrix.length; | ||
var standardDeviations = standardDeviation(matrix), l = standardDeviations.length; | ||
var standardDeviations = exports.standardDeviation(matrix) | ||
var l = standardDeviations.length; | ||
var standardErrors = new Array(l); | ||
@@ -232,9 +268,9 @@ var sqrtN = Math.sqrt(samples); | ||
return standardErrors; | ||
} | ||
}; | ||
function covariance(matrix, dimension) { | ||
return scatter(matrix, undefined, dimension); | ||
} | ||
exports.covariance = function covariance(matrix, dimension) { | ||
return exports.scatter(matrix, undefined, dimension); | ||
}; | ||
function scatter(matrix, divisor, dimension) { | ||
exports.scatter = function scatter(matrix, divisor, dimension) { | ||
if (typeof(dimension) === 'undefined') { | ||
@@ -250,4 +286,4 @@ dimension = 0; | ||
} | ||
var means = mean(matrix, dimension), | ||
rows = matrix.length; | ||
var means = exports.mean(matrix, dimension); | ||
var rows = matrix.length; | ||
if (rows === 0) { | ||
@@ -296,8 +332,8 @@ return [[]]; | ||
return cov; | ||
} | ||
}; | ||
function correlation(matrix) { | ||
var means = mean(matrix), | ||
standardDeviations = standardDeviation(matrix, true, means), | ||
scores = zScores(matrix, means, standardDeviations), | ||
exports.correlation = function correlation(matrix) { | ||
var means = exports.mean(matrix), | ||
standardDeviations = exports.standardDeviation(matrix, true, means), | ||
scores = exports.zScores(matrix, means, standardDeviations), | ||
rows = matrix.length, | ||
@@ -323,12 +359,12 @@ cols = matrix[0].length, | ||
return cor; | ||
} | ||
}; | ||
function zScores(matrix, means, standardDeviations) { | ||
means = means || mean(matrix); | ||
if (typeof(standardDeviations) === 'undefined') standardDeviations = standardDeviation(matrix, true, means); | ||
return standardize(center(matrix, means, false), standardDeviations, true); | ||
} | ||
exports.zScores = function zScores(matrix, means, standardDeviations) { | ||
means = means || exports.mean(matrix); | ||
if (typeof(standardDeviations) === 'undefined') standardDeviations = exports.standardDeviation(matrix, true, means); | ||
return exports.standardize(exports.center(matrix, means, false), standardDeviations, true); | ||
}; | ||
function center(matrix, means, inPlace) { | ||
means = means || mean(matrix); | ||
exports.center = function center(matrix, means, inPlace) { | ||
means = means || exports.mean(matrix); | ||
var result = matrix, | ||
@@ -352,6 +388,6 @@ l = matrix.length, | ||
return result; | ||
} | ||
}; | ||
function standardize(matrix, standardDeviations, inPlace) { | ||
if (typeof(standardDeviations) === 'undefined') standardDeviations = standardDeviation(matrix); | ||
exports.standardize = function standardize(matrix, standardDeviations, inPlace) { | ||
if (typeof(standardDeviations) === 'undefined') standardDeviations = exports.standardDeviation(matrix); | ||
var result = matrix, | ||
@@ -378,6 +414,6 @@ l = matrix.length, | ||
return result; | ||
} | ||
}; | ||
function weightedVariance(matrix, weights) { | ||
var means = mean(matrix); | ||
exports.weightedVariance = function weightedVariance(matrix, weights) { | ||
var means = exports.mean(matrix); | ||
var rows = matrix.length; | ||
@@ -405,5 +441,5 @@ if (rows === 0) return []; | ||
return vari; | ||
} | ||
}; | ||
function weightedMean(matrix, weights, dimension) { | ||
exports.weightedMean = function weightedMean(matrix, weights, dimension) { | ||
if (typeof(dimension) === 'undefined') { | ||
@@ -452,7 +488,7 @@ dimension = 0; | ||
return means; | ||
} | ||
}; | ||
function weightedCovariance(matrix, weights, means, dimension) { | ||
exports.weightedCovariance = function weightedCovariance(matrix, weights, means, dimension) { | ||
dimension = dimension || 0; | ||
means = means || weightedMean(matrix, weights, dimension); | ||
means = means || exports.weightedMean(matrix, weights, dimension); | ||
var s1 = 0, s2 = 0; | ||
@@ -464,8 +500,8 @@ for (var i = 0, ii = weights.length; i < ii; i++) { | ||
var factor = s1 / (s1 * s1 - s2); | ||
return weightedScatter(matrix, weights, means, factor, dimension); | ||
} | ||
return exports.weightedScatter(matrix, weights, means, factor, dimension); | ||
}; | ||
function weightedScatter(matrix, weights, means, factor, dimension) { | ||
exports.weightedScatter = function weightedScatter(matrix, weights, means, factor, dimension) { | ||
dimension = dimension || 0; | ||
means = means || weightedMean(matrix, weights, dimension); | ||
means = means || exports.weightedMean(matrix, weights, dimension); | ||
if (typeof(factor) === 'undefined') { | ||
@@ -516,24 +552,2 @@ factor = 1; | ||
return cov; | ||
} | ||
module.exports = { | ||
entropy: entropy, | ||
mean: mean, | ||
standardDeviation: standardDeviation, | ||
variance: variance, | ||
median: median, | ||
mode: mode, | ||
skewness: skewness, | ||
kurtosis: kurtosis, | ||
standardError: standardError, | ||
covariance: covariance, | ||
scatter: scatter, | ||
correlation: correlation, | ||
zScores: zScores, | ||
center: center, | ||
standardize: standardize, | ||
weightedVariance: weightedVariance, | ||
weightedMean: weightedMean, | ||
weightedCovariance: weightedCovariance, | ||
weightedScatter: weightedScatter | ||
}; |
{ | ||
"name": "ml-stat", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Functions for computing stats on arrays and matrices", | ||
@@ -31,6 +31,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha-better-spec-reporter": "latest", | ||
"mocha": "latest", | ||
"should": "latest" | ||
"mocha-better-spec-reporter": "^3.0.1", | ||
"mocha": "^2.5.1", | ||
"should": "^8.2.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30568
908