Comparing version 1.0.1 to 1.1.0
382
array.js
'use strict'; | ||
// https://github.com/accord-net/framework/blob/development/Sources/Accord.Statistics/Tools.cs | ||
function max(values) { | ||
var max = -Infinity, l = values.length; | ||
function compareNumbers(a, b) { | ||
return a - b; | ||
} | ||
/** | ||
* Computes the sum of the given values | ||
* @param {Array} values | ||
* @returns {number} | ||
*/ | ||
exports.sum = function sum(values) { | ||
var sum = 0; | ||
for (var i = 0; i < values.length; i++) { | ||
sum += values[i]; | ||
} | ||
return sum; | ||
}; | ||
/** | ||
* Computes the maximum of the given values | ||
* @param {Array} values | ||
* @returns {number} | ||
*/ | ||
exports.max = function max(values) { | ||
var max = -Infinity; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
@@ -10,6 +32,12 @@ if (values[i] > max) max = values[i]; | ||
return max; | ||
} | ||
}; | ||
function min(values) { | ||
var min = Infinity, l = values.length; | ||
/** | ||
* Computes the minimum of the given values | ||
* @param {Array} values | ||
* @returns {number} | ||
*/ | ||
exports.min = function min(values) { | ||
var min = Infinity; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
@@ -19,8 +47,13 @@ if (values[i] < min) min = values[i]; | ||
return min; | ||
} | ||
}; | ||
function minMax(values) { | ||
var min = Infinity, | ||
max = -Infinity, | ||
l = values.length; | ||
/** | ||
* Computes the min and max of the given values | ||
* @param {Array} values | ||
* @returns {{min: number, max: number}} | ||
*/ | ||
exports.minMax = function minMax(values) { | ||
var min = Infinity; | ||
var max = -Infinity; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
@@ -34,27 +67,63 @@ if (values[i] < min) min = values[i]; | ||
}; | ||
} | ||
}; | ||
function mean(values) { | ||
var sum = 0, l = values.length; | ||
for (var i = 0; i < l; i++) | ||
/** | ||
* Computes the arithmetic mean of the given values | ||
* @param {Array} values | ||
* @returns {number} | ||
*/ | ||
exports.arithmeticMean = function arithmeticMean(values) { | ||
var sum = 0; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
sum += values[i]; | ||
} | ||
return sum / l; | ||
} | ||
}; | ||
function geometricMean(values) { | ||
var sum = 0, l = values.length; | ||
for (var i = 0; i < l; i++) | ||
sum *= values[i]; | ||
return Math.pow(sum, 1 / l); | ||
} | ||
/** | ||
* {@link arithmeticMean} | ||
*/ | ||
exports.mean = exports.arithmeticMean; | ||
function logGeometricMean(values) { | ||
var lnsum = 0, l = values.length; | ||
for (var i = 0; i < l; i++) | ||
/** | ||
* Computes the geometric mean of the given values | ||
* @param {Array} values | ||
* @returns {number} | ||
*/ | ||
exports.geometricMean = function geometricMean(values) { | ||
var mul = 1; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
mul *= values[i]; | ||
} | ||
return Math.pow(mul, 1 / l); | ||
}; | ||
/** | ||
* Computes the mean of the log of the given values | ||
* If the return value is exponentiated, it gives the same result as the | ||
* geometric mean. | ||
* @param {Array} values | ||
* @returns {number} | ||
*/ | ||
exports.logMean = function logMean(values) { | ||
var lnsum = 0; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
lnsum += Math.log(values[i]); | ||
} | ||
return lnsum / l; | ||
} | ||
}; | ||
function grandMean(means, samples) { | ||
var sum = 0, n = 0, l = means.length; | ||
/** | ||
* Computes the weighted grand mean for a list of means and sample sizes | ||
* @param {Array} means - Mean values for each set of samples | ||
* @param {Array} samples - Number of original values for each set of samples | ||
* @returns {number} | ||
*/ | ||
exports.grandMean = function grandMean(means, samples) { | ||
var sum = 0; | ||
var n = 0; | ||
var l = means.length; | ||
for (var i = 0; i < l; i++) { | ||
@@ -65,57 +134,124 @@ sum += samples[i] * means[i]; | ||
return sum / n; | ||
} | ||
}; | ||
function truncatedMean(values, percent, inPlace) { | ||
if (typeof(inPlace) === 'undefined') inPlace = false; | ||
values = inPlace ? values : values.slice(); | ||
values.sort(); | ||
/** | ||
* Computes the truncated mean of the given values using a given percentage | ||
* @param {Array} values | ||
* @param {number} percent - The percentage of values to keep (range: [0,1]) | ||
* @param {boolean} [alreadySorted=false] | ||
* @returns {number} | ||
*/ | ||
exports.truncatedMean = function truncatedMean(values, percent, alreadySorted) { | ||
if (alreadySorted === undefined) alreadySorted = false; | ||
if (!alreadySorted) { | ||
values = values.slice().sort(compareNumbers); | ||
} | ||
var l = values.length; | ||
var k = Math.floor(l * percent); | ||
var sum = 0; | ||
for (var i = k; i < l - k; i++) | ||
for (var i = k; i < (l - k); i++) { | ||
sum += values[i]; | ||
} | ||
return sum / (l - 2 * k); | ||
} | ||
}; | ||
function contraHarmonicMean(values, order) { | ||
if (typeof(order) === 'undefined') order = 1; | ||
var r1 = 0, r2 = 0, l = values.length; | ||
/** | ||
* Computes the harmonic mean of the given values | ||
* @param {Array} values | ||
* @returns {number} | ||
*/ | ||
exports.harmonicMean = function harmonicMean(values) { | ||
var sum = 0; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
r1 += Math.pow(values[i], order + 1); | ||
r2 += Math.pow(values[i], order); | ||
if (values[i] === 0) { | ||
throw new RangeError('value at index ' + i + 'is zero'); | ||
} | ||
sum += 1 / values[i]; | ||
} | ||
return l / sum; | ||
}; | ||
/** | ||
* Computes the contraharmonic mean of the given values | ||
* @param {Array} values | ||
* @returns {number} | ||
*/ | ||
exports.contraHarmonicMean = function contraHarmonicMean(values) { | ||
var r1 = 0; | ||
var r2 = 0; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
r1 += values[i] * values[i]; | ||
r2 += values[i]; | ||
} | ||
if (r2 < 0) { | ||
throw new RangeError('sum of values is negative'); | ||
} | ||
return r1 / r2; | ||
} | ||
}; | ||
function standardDeviation(values, unbiased) { | ||
return Math.sqrt(variance(values, unbiased)); | ||
} | ||
function standardError(values) { | ||
return standardDeviation(values) / Math.sqrt(values.length); | ||
} | ||
function median(values, alreadySorted) { | ||
if (typeof(alreadySorted) === 'undefined') alreadySorted = false; | ||
/** | ||
* Computes the median of the given values | ||
* @param {Array} values | ||
* @param {boolean} [alreadySorted=false] | ||
* @returns {number} | ||
*/ | ||
exports.median = function median(values, alreadySorted) { | ||
if (alreadySorted === undefined) alreadySorted = false; | ||
if (!alreadySorted) { | ||
values = values.slice(); | ||
values.sort(); | ||
values = values.slice().sort(compareNumbers); | ||
} | ||
var l = values.length; | ||
var half = Math.floor(l / 2); | ||
if (l % 2 === 0) | ||
if (l % 2 === 0) { | ||
return (values[half - 1] + values[half]) * 0.5; | ||
return values[half]; | ||
} | ||
} else { | ||
return values[half]; | ||
} | ||
}; | ||
function quartiles(values, alreadySorted) { | ||
/** | ||
* Computes the variance of the given values | ||
* @param {Array} values | ||
* @param {boolean} [unbiased=true] - if true, divide by (n-1); if false, divide by n. | ||
* @returns {number} | ||
*/ | ||
exports.variance = function variance(values, unbiased) { | ||
if (unbiased === undefined) unbiased = true; | ||
var theMean = exports.mean(values); | ||
var theVariance = 0; | ||
var l = values.length; | ||
for (var i = 0; i < l; i++) { | ||
var x = values[i] - theMean; | ||
theVariance += x * x; | ||
} | ||
if (unbiased) { | ||
return theVariance / (l - 1); | ||
} else { | ||
return theVariance / l; | ||
} | ||
}; | ||
/** | ||
* Computes the standard deviation of the given values | ||
* @param {Array} values | ||
* @param {boolean} [unbiased=true] - if true, divide by (n-1); if false, divide by n. | ||
* @returns {number} | ||
*/ | ||
exports.standardDeviation = function standardDeviation(values, unbiased) { | ||
return Math.sqrt(exports.variance(values, unbiased)); | ||
}; | ||
exports.standardError = function standardError(values) { | ||
return exports.standardDeviation(values) / Math.sqrt(values.length); | ||
}; | ||
exports.quartiles = function quartiles(values, alreadySorted) { | ||
if (typeof(alreadySorted) === 'undefined') alreadySorted = false; | ||
if (!alreadySorted) { | ||
values = values.slice(); | ||
values.sort(); | ||
values.sort(compareNumbers); | ||
} | ||
@@ -125,29 +261,13 @@ | ||
var q1 = values[Math.ceil(quart) - 1]; | ||
var q2 = median(values, true); | ||
var q2 = exports.median(values, true); | ||
var q3 = values[Math.ceil(quart * 3) - 1]; | ||
return {q1: q1, q2: q2, q3: q3}; | ||
} | ||
}; | ||
function variance(values, unbiased) { | ||
if (typeof(unbiased) === 'undefined') unbiased = true; | ||
var theMean = mean(values); | ||
var theVariance = 0, l = values.length; | ||
exports.pooledStandardDeviation = function pooledStandardDeviation(samples, unbiased) { | ||
return Math.sqrt(exports.pooledVariance(samples, unbiased)); | ||
}; | ||
for (var i = 0; i < l; i++) { | ||
var x = values[i] - theMean; | ||
theVariance += x * x; | ||
} | ||
if (unbiased) | ||
return theVariance / (l - 1); | ||
else | ||
return theVariance / l; | ||
} | ||
function pooledStandardDeviation(samples, unbiased) { | ||
return Math.sqrt(pooledVariance(samples, unbiased)); | ||
} | ||
function pooledVariance(samples, unbiased) { | ||
exports.pooledVariance = function pooledVariance(samples, unbiased) { | ||
if (typeof(unbiased) === 'undefined') unbiased = true; | ||
@@ -158,3 +278,3 @@ var sum = 0; | ||
var values = samples[i]; | ||
var vari = variance(values); | ||
var vari = exports.variance(values); | ||
@@ -169,5 +289,5 @@ sum += (values.length - 1) * vari; | ||
return sum / length; | ||
} | ||
}; | ||
function mode(values) { | ||
exports.mode = function mode(values) { | ||
var l = values.length, | ||
@@ -202,8 +322,8 @@ itemCount = new Array(l), | ||
return itemArray[maxIndex]; | ||
} | ||
}; | ||
function covariance(vector1, vector2, unbiased) { | ||
exports.covariance = function covariance(vector1, vector2, unbiased) { | ||
if (typeof(unbiased) === 'undefined') unbiased = true; | ||
var mean1 = mean(vector1); | ||
var mean2 = mean(vector2); | ||
var mean1 = exports.mean(vector1); | ||
var mean2 = exports.mean(vector2); | ||
@@ -224,7 +344,7 @@ if (vector1.length !== vector2.length) | ||
return cov / l; | ||
} | ||
}; | ||
function skewness(values, unbiased) { | ||
exports.skewness = function skewness(values, unbiased) { | ||
if (typeof(unbiased) === 'undefined') unbiased = true; | ||
var theMean = mean(values); | ||
var theMean = exports.mean(values); | ||
@@ -249,7 +369,7 @@ var s2 = 0, s3 = 0, l = values.length; | ||
} | ||
} | ||
}; | ||
function kurtosis(values, unbiased) { | ||
exports.kurtosis = function kurtosis(values, unbiased) { | ||
if (typeof(unbiased) === 'undefined') unbiased = true; | ||
var theMean = mean(values); | ||
var theMean = exports.mean(values); | ||
var n = values.length, s2 = 0, s4 = 0; | ||
@@ -276,5 +396,5 @@ | ||
} | ||
} | ||
}; | ||
function entropy(values, eps) { | ||
exports.entropy = function entropy(values, eps) { | ||
if (typeof(eps) === 'undefined') eps = 0; | ||
@@ -285,5 +405,5 @@ var sum = 0, l = values.length; | ||
return -sum; | ||
} | ||
}; | ||
function weightedMean(values, weights) { | ||
exports.weightedMean = function weightedMean(values, weights) { | ||
var sum = 0, l = values.length; | ||
@@ -293,10 +413,10 @@ for (var i = 0; i < l; i++) | ||
return sum; | ||
} | ||
}; | ||
function weightedStandardDeviation(values, weights) { | ||
return Math.sqrt(weightedVariance(values, weights)); | ||
} | ||
exports.weightedStandardDeviation = function weightedStandardDeviation(values, weights) { | ||
return Math.sqrt(exports.weightedVariance(values, weights)); | ||
}; | ||
function weightedVariance(values, weights) { | ||
var theMean = weightedMean(values, weights); | ||
exports.weightedVariance = function weightedVariance(values, weights) { | ||
var theMean = exports.weightedMean(values, weights); | ||
var vari = 0, l = values.length; | ||
@@ -315,5 +435,5 @@ var a = 0, b = 0; | ||
return vari * (b / (b * b - a)); | ||
} | ||
}; | ||
function center(values, inPlace) { | ||
exports.center = function center(values, inPlace) { | ||
if (typeof(inPlace) === 'undefined') inPlace = false; | ||
@@ -325,9 +445,9 @@ | ||
var theMean = mean(result), l = result.length; | ||
var theMean = exports.mean(result), l = result.length; | ||
for (var i = 0; i < l; i++) | ||
result[i] -= theMean; | ||
} | ||
}; | ||
function standardize(values, standardDev, inPlace) { | ||
if (typeof(standardDev) === 'undefined') standardDev = standardDeviation(values); | ||
exports.standardize = function standardize(values, standardDev, inPlace) { | ||
if (typeof(standardDev) === 'undefined') standardDev = exports.standardDeviation(values); | ||
if (typeof(inPlace) === 'undefined') inPlace = false; | ||
@@ -339,5 +459,5 @@ var l = values.length; | ||
return result; | ||
} | ||
}; | ||
function cumulativeSum(array) { | ||
exports.cumulativeSum = function cumulativeSum(array) { | ||
var l = array.length; | ||
@@ -349,32 +469,2 @@ var result = new Array(l); | ||
return result; | ||
} | ||
module.exports = { | ||
min: min, | ||
max: max, | ||
minMax: minMax, | ||
mean: mean, | ||
geometricMean: geometricMean, | ||
logGeometricMean: logGeometricMean, | ||
grandMean: grandMean, | ||
truncatedMean: truncatedMean, | ||
contraHarmonicMean: contraHarmonicMean, | ||
standardDeviation: standardDeviation, | ||
standardError: standardError, | ||
median: median, | ||
quartiles: quartiles, | ||
variance: variance, | ||
pooledStandardDeviation: pooledStandardDeviation, | ||
pooledVariance: pooledVariance, | ||
mode: mode, | ||
covariance: covariance, | ||
skewness: skewness, | ||
kurtosis: kurtosis, | ||
entropy: entropy, | ||
weightedMean: weightedMean, | ||
weightedStandardDeviation: weightedStandardDeviation, | ||
weightedVariance: weightedVariance, | ||
center: center, | ||
standardize: standardize, | ||
cumulativeSum: cumulativeSum | ||
}; |
@@ -0,1 +1,8 @@ | ||
1.1.0 / 2015-09-02 | ||
================== | ||
* add array.sum | ||
* fix array sorting tu use proper number comparator | ||
* add array.harmonicMean | ||
1.0.1 / 2015-02-02 | ||
@@ -2,0 +9,0 @@ ================== |
@@ -0,3 +1,4 @@ | ||
'use strict'; | ||
exports.array = require('./array'); | ||
exports.matrix = require('./matrix'); |
'use strict'; | ||
var arrayStat = require('./array'); | ||
// https://github.com/accord-net/framework/blob/development/Sources/Accord.Statistics/Tools.cs | ||
@@ -427,3 +429,3 @@ | ||
var weightSum = sum(weights); | ||
var weightSum = arrayStat.sum(weights); | ||
if (weightSum !== 0) { | ||
@@ -499,11 +501,2 @@ for (i = 0, ii = means.length; i < ii; i++) { | ||
// private | ||
function sum(vector) { | ||
var sum = 0, l = vector.length; | ||
for (var i = 0; i < l; i++) { | ||
sum += vector[i]; | ||
} | ||
return sum; | ||
} | ||
module.exports = { | ||
@@ -510,0 +503,0 @@ entropy: entropy, |
{ | ||
"name": "ml-stat", | ||
"version": "1.0.1", | ||
"description": "Functions for computing stats on arrays and matrices", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha --require should --reporter mocha-better-spec-reporter --recursive" | ||
}, | ||
"repository": "mljs/stat", | ||
"keywords": [ | ||
"stat", | ||
"stats", | ||
"vector", | ||
"matrix", | ||
"data", | ||
"mining", | ||
"datamining", | ||
"machine", | ||
"learning" | ||
], | ||
"contributors": [ | ||
"Michaël Zasso" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mljs/stat/issues" | ||
}, | ||
"homepage": "https://github.com/mljs/stat", | ||
"devDependencies": { | ||
"mocha-better-spec-reporter": "latest", | ||
"mocha": "latest", | ||
"should": "latest" | ||
} | ||
"name": "ml-stat", | ||
"version": "1.1.0", | ||
"description": "Functions for computing stats on arrays and matrices", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha --require should --reporter mocha-better-spec-reporter --recursive" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mljs/stat.git" | ||
}, | ||
"keywords": [ | ||
"stat", | ||
"stats", | ||
"vector", | ||
"matrix", | ||
"data", | ||
"mining", | ||
"datamining", | ||
"machine", | ||
"learning" | ||
], | ||
"author": "Michaël Zasso", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mljs/stat/issues" | ||
}, | ||
"homepage": "https://github.com/mljs/stat", | ||
"devDependencies": { | ||
"mocha-better-spec-reporter": "latest", | ||
"mocha": "latest", | ||
"should": "latest" | ||
} | ||
} |
@@ -5,2 +5,3 @@ # ml-stat | ||
[![build status][travis-image]][travis-url] | ||
[![npm download][download-image]][download-url] | ||
@@ -11,13 +12,4 @@ Functions for computing stats on arrays and matrices | ||
```js | ||
$ npm install ml-stat | ||
``` | ||
`$ npm install ml-stat` | ||
## Test | ||
```js | ||
$ npm install | ||
$ npm test | ||
``` | ||
## Authors | ||
@@ -30,7 +22,9 @@ | ||
MIT | ||
[MIT](./LICENSE) | ||
[npm-image]: https://img.shields.io/npm/v/ml-stat.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/ml-stat | ||
[npm-url]: https://www.npmjs.com/package/ml-stat | ||
[travis-image]: https://img.shields.io/travis/mljs/stat/master.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/mljs/stat | ||
[download-image]: https://img.shields.io/npm/dm/ml-stat.svg?style=flat-square | ||
[download-url]: https://www.npmjs.com/package/ml-stat |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
28665
874
0
0
28