feat:node-modules
This commit is contained in:
90
node_modules/mathjs/lib/cjs/function/statistics/corr.js
generated
vendored
Normal file
90
node_modules/mathjs/lib/cjs/function/statistics/corr.js
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createCorr = void 0;
|
||||
var _factory = require("../../utils/factory.js");
|
||||
const name = 'corr';
|
||||
const dependencies = ['typed', 'matrix', 'mean', 'sqrt', 'sum', 'add', 'subtract', 'multiply', 'pow', 'divide'];
|
||||
const createCorr = exports.createCorr = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
matrix,
|
||||
sqrt,
|
||||
sum,
|
||||
add,
|
||||
subtract,
|
||||
multiply,
|
||||
pow,
|
||||
divide
|
||||
} = _ref;
|
||||
/**
|
||||
* Compute the correlation coefficient of a two list with values, For matrices, the matrix correlation coefficient is calculated.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.corr(A, B)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.corr([1, 2, 3, 4, 5], [4, 5, 6, 7, 8]) // returns 1
|
||||
* math.corr([1, 2.2, 3, 4.8, 5], [4, 5.3, 6.6, 7, 8]) //returns 0.9569941688503644
|
||||
* math.corr([[1, 2.2, 3, 4.8, 5], [4, 5.3, 6.6, 7, 8]],[[1, 2.2, 3, 4.8, 5], [4, 5.3, 6.6, 7, 8]]) // returns [1,1]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* median, mean, min, max, sum, prod, std, variance
|
||||
*
|
||||
* @param {Array | Matrix} A The first array or matrix to compute correlation coefficient
|
||||
* @param {Array | Matrix} B The second array or matrix to compute correlation coefficient
|
||||
* @return {*} The correlation coefficient
|
||||
*/
|
||||
return typed(name, {
|
||||
'Array, Array': function (A, B) {
|
||||
return _corr(A, B);
|
||||
},
|
||||
'Matrix, Matrix': function (A, B) {
|
||||
const res = _corr(A.toArray(), B.toArray());
|
||||
return Array.isArray(res) ? matrix(res) : res;
|
||||
}
|
||||
});
|
||||
/**
|
||||
* Calculate the correlation coefficient between two arrays or matrices.
|
||||
* @param {Array | Matrix} A
|
||||
* @param {Array | Matrix} B
|
||||
* @return {*} correlation coefficient
|
||||
* @private
|
||||
*/
|
||||
function _corr(A, B) {
|
||||
const correlations = [];
|
||||
if (Array.isArray(A[0]) && Array.isArray(B[0])) {
|
||||
if (A.length !== B.length) {
|
||||
throw new SyntaxError('Dimension mismatch. Array A and B must have the same length.');
|
||||
}
|
||||
for (let i = 0; i < A.length; i++) {
|
||||
if (A[i].length !== B[i].length) {
|
||||
throw new SyntaxError('Dimension mismatch. Array A and B must have the same number of elements.');
|
||||
}
|
||||
correlations.push(correlation(A[i], B[i]));
|
||||
}
|
||||
return correlations;
|
||||
} else {
|
||||
if (A.length !== B.length) {
|
||||
throw new SyntaxError('Dimension mismatch. Array A and B must have the same number of elements.');
|
||||
}
|
||||
return correlation(A, B);
|
||||
}
|
||||
}
|
||||
function correlation(A, B) {
|
||||
const n = A.length;
|
||||
const sumX = sum(A);
|
||||
const sumY = sum(B);
|
||||
const sumXY = A.reduce((acc, x, index) => add(acc, multiply(x, B[index])), 0);
|
||||
const sumXSquare = sum(A.map(x => pow(x, 2)));
|
||||
const sumYSquare = sum(B.map(y => pow(y, 2)));
|
||||
const numerator = subtract(multiply(n, sumXY), multiply(sumX, sumY));
|
||||
const denominator = sqrt(multiply(subtract(multiply(n, sumXSquare), pow(sumX, 2)), subtract(multiply(n, sumYSquare), pow(sumY, 2))));
|
||||
return divide(numerator, denominator);
|
||||
}
|
||||
});
|
||||
129
node_modules/mathjs/lib/cjs/function/statistics/cumsum.js
generated
vendored
Normal file
129
node_modules/mathjs/lib/cjs/function/statistics/cumsum.js
generated
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createCumSum = void 0;
|
||||
var _collection = require("../../utils/collection.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _switch2 = require("../../utils/switch.js");
|
||||
var _improveErrorMessage = require("./utils/improveErrorMessage.js");
|
||||
var _array = require("../../utils/array.js");
|
||||
var _IndexError = require("../../error/IndexError.js");
|
||||
const name = 'cumsum';
|
||||
const dependencies = ['typed', 'add', 'unaryPlus'];
|
||||
const createCumSum = exports.createCumSum = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
add,
|
||||
unaryPlus
|
||||
} = _ref;
|
||||
/**
|
||||
* Compute the cumulative sum of a matrix or a list with values.
|
||||
* In case of a (multi dimensional) array or matrix, the cumulative sums
|
||||
* along a specified dimension (defaulting to the first) will be calculated.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.cumsum(a, b, c, ...)
|
||||
* math.cumsum(A)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.cumsum(2, 1, 4, 3) // returns [2, 3, 7, 10]
|
||||
* math.cumsum([2, 1, 4, 3]) // returns [2, 3, 7, 10]
|
||||
* math.cumsum([[1, 2], [3, 4]]) // returns [[1, 2], [4, 6]]
|
||||
* math.cumsum([[1, 2], [3, 4]], 0) // returns [[1, 2], [4, 6]]
|
||||
* math.cumsum([[1, 2], [3, 4]], 1) // returns [[1, 3], [3, 7]]
|
||||
* math.cumsum([[2, 5], [4, 3], [1, 7]]) // returns [[2, 5], [6, 8], [7, 15]]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* mean, median, min, max, prod, std, variance, sum
|
||||
*
|
||||
* @param {... *} args A single matrix or or multiple scalar values
|
||||
* @return {*} The cumulative sum of all values
|
||||
*/
|
||||
return typed(name, {
|
||||
// sum([a, b, c, d, ...])
|
||||
Array: _cumsum,
|
||||
Matrix: function (matrix) {
|
||||
return matrix.create(_cumsum(matrix.valueOf(), matrix.datatype()));
|
||||
},
|
||||
// sum([a, b, c, d, ...], dim)
|
||||
'Array, number | BigNumber': _ncumSumDim,
|
||||
'Matrix, number | BigNumber': function (matrix, dim) {
|
||||
return matrix.create(_ncumSumDim(matrix.valueOf(), dim), matrix.datatype());
|
||||
},
|
||||
// cumsum(a, b, c, d, ...)
|
||||
'...': function (args) {
|
||||
if ((0, _collection.containsCollections)(args)) {
|
||||
throw new TypeError('All values expected to be scalar in function cumsum');
|
||||
}
|
||||
return _cumsum(args);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Recursively calculate the cumulative sum of an n-dimensional array
|
||||
* @param {Array} array
|
||||
* @return {number} cumsum
|
||||
* @private
|
||||
*/
|
||||
function _cumsum(array) {
|
||||
try {
|
||||
return _cumsummap(array);
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, name);
|
||||
}
|
||||
}
|
||||
function _cumsummap(array) {
|
||||
if (array.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const sums = [unaryPlus(array[0])]; // unaryPlus converts to number if need be
|
||||
for (let i = 1; i < array.length; ++i) {
|
||||
// Must use add below and not addScalar for the case of summing a
|
||||
// 2+-dimensional array along the 0th dimension (the row vectors,
|
||||
// or higher-d analogues, are literally added to each other).
|
||||
sums.push(add(sums[i - 1], array[i]));
|
||||
}
|
||||
return sums;
|
||||
}
|
||||
function _ncumSumDim(array, dim) {
|
||||
const size = (0, _array.arraySize)(array);
|
||||
if (dim < 0 || dim >= size.length) {
|
||||
// TODO: would be more clear when throwing a DimensionError here
|
||||
throw new _IndexError.IndexError(dim, size.length);
|
||||
}
|
||||
try {
|
||||
return _cumsumDimensional(array, dim);
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, name);
|
||||
}
|
||||
}
|
||||
|
||||
/* Possible TODO: Refactor _reduce in collection.js to be able to work here as well */
|
||||
function _cumsumDimensional(mat, dim) {
|
||||
let i, ret, tran;
|
||||
if (dim <= 0) {
|
||||
const initialValue = mat[0][0];
|
||||
if (!Array.isArray(initialValue)) {
|
||||
return _cumsummap(mat);
|
||||
} else {
|
||||
tran = (0, _switch2._switch)(mat);
|
||||
ret = [];
|
||||
for (i = 0; i < tran.length; i++) {
|
||||
ret[i] = _cumsumDimensional(tran[i], dim - 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
} else {
|
||||
ret = [];
|
||||
for (i = 0; i < mat.length; i++) {
|
||||
ret[i] = _cumsumDimensional(mat[i], dim - 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
});
|
||||
70
node_modules/mathjs/lib/cjs/function/statistics/mad.js
generated
vendored
Normal file
70
node_modules/mathjs/lib/cjs/function/statistics/mad.js
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createMad = void 0;
|
||||
var _array = require("../../utils/array.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _improveErrorMessage = require("./utils/improveErrorMessage.js");
|
||||
const name = 'mad';
|
||||
const dependencies = ['typed', 'abs', 'map', 'median', 'subtract'];
|
||||
const createMad = exports.createMad = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
abs,
|
||||
map,
|
||||
median,
|
||||
subtract
|
||||
} = _ref;
|
||||
/**
|
||||
* Compute the median absolute deviation of a matrix or a list with values.
|
||||
* The median absolute deviation is defined as the median of the absolute
|
||||
* deviations from the median.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.mad(a, b, c, ...)
|
||||
* math.mad(A)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.mad(10, 20, 30) // returns 10
|
||||
* math.mad([1, 2, 3]) // returns 1
|
||||
* math.mad([[1, 2, 3], [4, 5, 6]]) // returns 1.5
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* median, mean, std, abs
|
||||
*
|
||||
* @param {Array | Matrix} array
|
||||
* A single matrix or multiple scalar values.
|
||||
* @return {*} The median absolute deviation.
|
||||
*/
|
||||
return typed(name, {
|
||||
// mad([a, b, c, d, ...])
|
||||
'Array | Matrix': _mad,
|
||||
// mad(a, b, c, d, ...)
|
||||
'...': function (args) {
|
||||
return _mad(args);
|
||||
}
|
||||
});
|
||||
function _mad(array) {
|
||||
array = (0, _array.flatten)(array.valueOf());
|
||||
if (array.length === 0) {
|
||||
throw new Error('Cannot calculate median absolute deviation (mad) of an empty array');
|
||||
}
|
||||
try {
|
||||
const med = median(array);
|
||||
return median(map(array, function (value) {
|
||||
return abs(subtract(value, med));
|
||||
}));
|
||||
} catch (err) {
|
||||
if (err instanceof TypeError && err.message.includes('median')) {
|
||||
throw new TypeError(err.message.replace('median', 'mad'));
|
||||
} else {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'mad');
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
111
node_modules/mathjs/lib/cjs/function/statistics/max.js
generated
vendored
Normal file
111
node_modules/mathjs/lib/cjs/function/statistics/max.js
generated
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createMax = void 0;
|
||||
var _collection = require("../../utils/collection.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _number = require("../../utils/number.js");
|
||||
var _improveErrorMessage = require("./utils/improveErrorMessage.js");
|
||||
const name = 'max';
|
||||
const dependencies = ['typed', 'config', 'numeric', 'larger'];
|
||||
const createMax = exports.createMax = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
config,
|
||||
numeric,
|
||||
larger
|
||||
} = _ref;
|
||||
/**
|
||||
* Compute the maximum value of a matrix or a list with values.
|
||||
* In case of a multidimensional array, the maximum of the flattened array
|
||||
* will be calculated. When `dim` is provided, the maximum over the selected
|
||||
* dimension will be calculated. Parameter `dim` is zero-based.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.max(a, b, c, ...)
|
||||
* math.max(A)
|
||||
* math.max(A, dimension)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.max(2, 1, 4, 3) // returns 4
|
||||
* math.max([2, 1, 4, 3]) // returns 4
|
||||
*
|
||||
* // maximum over a specified dimension (zero-based)
|
||||
* math.max([[2, 5], [4, 3], [1, 7]], 0) // returns [4, 7]
|
||||
* math.max([[2, 5], [4, 3], [1, 7]], 1) // returns [5, 4, 7]
|
||||
*
|
||||
* math.max(2.7, 7.1, -4.5, 2.0, 4.1) // returns 7.1
|
||||
* math.min(2.7, 7.1, -4.5, 2.0, 4.1) // returns -4.5
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* mean, median, min, prod, std, sum, variance
|
||||
*
|
||||
* @param {... *} args A single matrix or or multiple scalar values
|
||||
* @return {*} The maximum value
|
||||
*/
|
||||
return typed(name, {
|
||||
// max([a, b, c, d, ...])
|
||||
'Array | Matrix': _max,
|
||||
// max([a, b, c, d, ...], dim)
|
||||
'Array | Matrix, number | BigNumber': function (array, dim) {
|
||||
return (0, _collection.reduce)(array, dim.valueOf(), _largest);
|
||||
},
|
||||
// max(a, b, c, d, ...)
|
||||
'...': function (args) {
|
||||
if ((0, _collection.containsCollections)(args)) {
|
||||
throw new TypeError('Scalar values expected in function max');
|
||||
}
|
||||
return _max(args);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Return the largest of two values
|
||||
* @param {*} x
|
||||
* @param {*} y
|
||||
* @returns {*} Returns x when x is largest, or y when y is largest
|
||||
* @private
|
||||
*/
|
||||
function _largest(x, y) {
|
||||
try {
|
||||
return larger(x, y) ? x : y;
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'max', y);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively calculate the maximum value in an n-dimensional array
|
||||
* @param {Array} array
|
||||
* @return {number} max
|
||||
* @private
|
||||
*/
|
||||
function _max(array) {
|
||||
let res;
|
||||
(0, _collection.deepForEach)(array, function (value) {
|
||||
try {
|
||||
if (isNaN(value) && typeof value === 'number') {
|
||||
res = NaN;
|
||||
} else if (res === undefined || larger(value, res)) {
|
||||
res = value;
|
||||
}
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'max', value);
|
||||
}
|
||||
});
|
||||
if (res === undefined) {
|
||||
throw new Error('Cannot calculate max of an empty array');
|
||||
}
|
||||
|
||||
// make sure returning numeric value: parse a string into a numeric value
|
||||
if (typeof res === 'string') {
|
||||
res = numeric(res, (0, _number.safeNumberType)(res, config));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
});
|
||||
100
node_modules/mathjs/lib/cjs/function/statistics/mean.js
generated
vendored
Normal file
100
node_modules/mathjs/lib/cjs/function/statistics/mean.js
generated
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createMean = void 0;
|
||||
var _collection = require("../../utils/collection.js");
|
||||
var _array = require("../../utils/array.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _improveErrorMessage = require("./utils/improveErrorMessage.js");
|
||||
const name = 'mean';
|
||||
const dependencies = ['typed', 'add', 'divide'];
|
||||
const createMean = exports.createMean = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
add,
|
||||
divide
|
||||
} = _ref;
|
||||
/**
|
||||
* Compute the mean value of matrix or a list with values.
|
||||
* In case of a multidimensional array, the mean of the flattened array
|
||||
* will be calculated. When `dim` is provided, the maximum over the selected
|
||||
* dimension will be calculated. Parameter `dim` is zero-based.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.mean(a, b, c, ...)
|
||||
* math.mean(A)
|
||||
* math.mean(A, dimension)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.mean(2, 1, 4, 3) // returns 2.5
|
||||
* math.mean([1, 2.7, 3.2, 4]) // returns 2.725
|
||||
*
|
||||
* math.mean([[2, 5], [6, 3], [1, 7]], 0) // returns [3, 5]
|
||||
* math.mean([[2, 5], [6, 3], [1, 7]], 1) // returns [3.5, 4.5, 4]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* median, min, max, sum, prod, std, variance
|
||||
*
|
||||
* @param {... *} args A single matrix or or multiple scalar values
|
||||
* @return {*} The mean of all values
|
||||
*/
|
||||
return typed(name, {
|
||||
// mean([a, b, c, d, ...])
|
||||
'Array | Matrix': _mean,
|
||||
// mean([a, b, c, d, ...], dim)
|
||||
'Array | Matrix, number | BigNumber': _nmeanDim,
|
||||
// mean(a, b, c, d, ...)
|
||||
'...': function (args) {
|
||||
if ((0, _collection.containsCollections)(args)) {
|
||||
throw new TypeError('Scalar values expected in function mean');
|
||||
}
|
||||
return _mean(args);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Calculate the mean value in an n-dimensional array, returning a
|
||||
* n-1 dimensional array
|
||||
* @param {Array} array
|
||||
* @param {number} dim
|
||||
* @return {number} mean
|
||||
* @private
|
||||
*/
|
||||
function _nmeanDim(array, dim) {
|
||||
try {
|
||||
const sum = (0, _collection.reduce)(array, dim, add);
|
||||
const s = Array.isArray(array) ? (0, _array.arraySize)(array) : array.size();
|
||||
return divide(sum, s[dim]);
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'mean');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively calculate the mean value in an n-dimensional array
|
||||
* @param {Array} array
|
||||
* @return {number} mean
|
||||
* @private
|
||||
*/
|
||||
function _mean(array) {
|
||||
let sum;
|
||||
let num = 0;
|
||||
(0, _collection.deepForEach)(array, function (value) {
|
||||
try {
|
||||
sum = sum === undefined ? value : add(sum, value);
|
||||
num++;
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'mean', value);
|
||||
}
|
||||
});
|
||||
if (num === 0) {
|
||||
throw new Error('Cannot calculate the mean of an empty array');
|
||||
}
|
||||
return divide(sum, num);
|
||||
}
|
||||
});
|
||||
114
node_modules/mathjs/lib/cjs/function/statistics/median.js
generated
vendored
Normal file
114
node_modules/mathjs/lib/cjs/function/statistics/median.js
generated
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createMedian = void 0;
|
||||
var _collection = require("../../utils/collection.js");
|
||||
var _array = require("../../utils/array.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _improveErrorMessage = require("./utils/improveErrorMessage.js");
|
||||
const name = 'median';
|
||||
const dependencies = ['typed', 'add', 'divide', 'compare', 'partitionSelect'];
|
||||
const createMedian = exports.createMedian = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
add,
|
||||
divide,
|
||||
compare,
|
||||
partitionSelect
|
||||
} = _ref;
|
||||
/**
|
||||
* Recursively calculate the median of an n-dimensional array
|
||||
* @param {Array} array
|
||||
* @return {Number} median
|
||||
* @private
|
||||
*/
|
||||
function _median(array) {
|
||||
try {
|
||||
array = (0, _array.flatten)(array.valueOf());
|
||||
const num = array.length;
|
||||
if (num === 0) {
|
||||
throw new Error('Cannot calculate median of an empty array');
|
||||
}
|
||||
if (num % 2 === 0) {
|
||||
// even: return the average of the two middle values
|
||||
const mid = num / 2 - 1;
|
||||
const right = partitionSelect(array, mid + 1);
|
||||
|
||||
// array now partitioned at mid + 1, take max of left part
|
||||
let left = array[mid];
|
||||
for (let i = 0; i < mid; ++i) {
|
||||
if (compare(array[i], left) > 0) {
|
||||
left = array[i];
|
||||
}
|
||||
}
|
||||
return middle2(left, right);
|
||||
} else {
|
||||
// odd: return the middle value
|
||||
const m = partitionSelect(array, (num - 1) / 2);
|
||||
return middle(m);
|
||||
}
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'median');
|
||||
}
|
||||
}
|
||||
|
||||
// helper function to type check the middle value of the array
|
||||
const middle = typed({
|
||||
'number | BigNumber | Complex | Unit': function (value) {
|
||||
return value;
|
||||
}
|
||||
});
|
||||
|
||||
// helper function to type check the two middle value of the array
|
||||
const middle2 = typed({
|
||||
'number | BigNumber | Complex | Unit, number | BigNumber | Complex | Unit': function (left, right) {
|
||||
return divide(add(left, right), 2);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Compute the median of a matrix or a list with values. The values are
|
||||
* sorted and the middle value is returned. In case of an even number of
|
||||
* values, the average of the two middle values is returned.
|
||||
* Supported types of values are: Number, BigNumber, Unit
|
||||
*
|
||||
* In case of a (multi dimensional) array or matrix, the median of all
|
||||
* elements will be calculated.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.median(a, b, c, ...)
|
||||
* math.median(A)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.median(5, 2, 7) // returns 5
|
||||
* math.median([3, -1, 5, 7]) // returns 4
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* mean, min, max, sum, prod, std, variance, quantileSeq
|
||||
*
|
||||
* @param {... *} args A single matrix or or multiple scalar values
|
||||
* @return {*} The median
|
||||
*/
|
||||
return typed(name, {
|
||||
// median([a, b, c, d, ...])
|
||||
'Array | Matrix': _median,
|
||||
// median([a, b, c, d, ...], dim)
|
||||
'Array | Matrix, number | BigNumber': function (array, dim) {
|
||||
// TODO: implement median(A, dim)
|
||||
throw new Error('median(A, dim) is not yet supported');
|
||||
// return reduce(arguments[0], arguments[1], ...)
|
||||
},
|
||||
// median(a, b, c, d, ...)
|
||||
'...': function (args) {
|
||||
if ((0, _collection.containsCollections)(args)) {
|
||||
throw new TypeError('Scalar values expected in function median');
|
||||
}
|
||||
return _median(args);
|
||||
}
|
||||
});
|
||||
});
|
||||
111
node_modules/mathjs/lib/cjs/function/statistics/min.js
generated
vendored
Normal file
111
node_modules/mathjs/lib/cjs/function/statistics/min.js
generated
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createMin = void 0;
|
||||
var _collection = require("../../utils/collection.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _number = require("../../utils/number.js");
|
||||
var _improveErrorMessage = require("./utils/improveErrorMessage.js");
|
||||
const name = 'min';
|
||||
const dependencies = ['typed', 'config', 'numeric', 'smaller'];
|
||||
const createMin = exports.createMin = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
config,
|
||||
numeric,
|
||||
smaller
|
||||
} = _ref;
|
||||
/**
|
||||
* Compute the minimum value of a matrix or a list of values.
|
||||
* In case of a multidimensional array, the minimum of the flattened array
|
||||
* will be calculated. When `dim` is provided, the minimum over the selected
|
||||
* dimension will be calculated. Parameter `dim` is zero-based.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.min(a, b, c, ...)
|
||||
* math.min(A)
|
||||
* math.min(A, dimension)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.min(2, 1, 4, 3) // returns 1
|
||||
* math.min([2, 1, 4, 3]) // returns 1
|
||||
*
|
||||
* // minimum over a specified dimension (zero-based)
|
||||
* math.min([[2, 5], [4, 3], [1, 7]], 0) // returns [1, 3]
|
||||
* math.min([[2, 5], [4, 3], [1, 7]], 1) // returns [2, 3, 1]
|
||||
*
|
||||
* math.max(2.7, 7.1, -4.5, 2.0, 4.1) // returns 7.1
|
||||
* math.min(2.7, 7.1, -4.5, 2.0, 4.1) // returns -4.5
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* mean, median, max, prod, std, sum, variance
|
||||
*
|
||||
* @param {... *} args A single matrix or or multiple scalar values
|
||||
* @return {*} The minimum value
|
||||
*/
|
||||
return typed(name, {
|
||||
// min([a, b, c, d, ...])
|
||||
'Array | Matrix': _min,
|
||||
// min([a, b, c, d, ...], dim)
|
||||
'Array | Matrix, number | BigNumber': function (array, dim) {
|
||||
return (0, _collection.reduce)(array, dim.valueOf(), _smallest);
|
||||
},
|
||||
// min(a, b, c, d, ...)
|
||||
'...': function (args) {
|
||||
if ((0, _collection.containsCollections)(args)) {
|
||||
throw new TypeError('Scalar values expected in function min');
|
||||
}
|
||||
return _min(args);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Return the smallest of two values
|
||||
* @param {*} x
|
||||
* @param {*} y
|
||||
* @returns {*} Returns x when x is smallest, or y when y is smallest
|
||||
* @private
|
||||
*/
|
||||
function _smallest(x, y) {
|
||||
try {
|
||||
return smaller(x, y) ? x : y;
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'min', y);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively calculate the minimum value in an n-dimensional array
|
||||
* @param {Array} array
|
||||
* @return {number} min
|
||||
* @private
|
||||
*/
|
||||
function _min(array) {
|
||||
let min;
|
||||
(0, _collection.deepForEach)(array, function (value) {
|
||||
try {
|
||||
if (isNaN(value) && typeof value === 'number') {
|
||||
min = NaN;
|
||||
} else if (min === undefined || smaller(value, min)) {
|
||||
min = value;
|
||||
}
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'min', value);
|
||||
}
|
||||
});
|
||||
if (min === undefined) {
|
||||
throw new Error('Cannot calculate min of an empty array');
|
||||
}
|
||||
|
||||
// make sure returning numeric value: parse a string into a numeric value
|
||||
if (typeof min === 'string') {
|
||||
min = numeric(min, (0, _number.safeNumberType)(min, config));
|
||||
}
|
||||
return min;
|
||||
}
|
||||
});
|
||||
82
node_modules/mathjs/lib/cjs/function/statistics/mode.js
generated
vendored
Normal file
82
node_modules/mathjs/lib/cjs/function/statistics/mode.js
generated
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createMode = void 0;
|
||||
var _array = require("../../utils/array.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
const name = 'mode';
|
||||
const dependencies = ['typed', 'isNaN', 'isNumeric'];
|
||||
const createMode = exports.createMode = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
isNaN,
|
||||
isNumeric
|
||||
} = _ref;
|
||||
/**
|
||||
* Computes the mode of a set of numbers or a list with values(numbers or characters).
|
||||
* If there are multiple modes, it returns a list of those values.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.mode(a, b, c, ...)
|
||||
* math.mode(A)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.mode(2, 1, 4, 3, 1) // returns [1]
|
||||
* math.mode([1, 2.7, 3.2, 4, 2.7]) // returns [2.7]
|
||||
* math.mode(1, 4, 6, 1, 6) // returns [1, 6]
|
||||
* math.mode('a','a','b','c') // returns ["a"]
|
||||
* math.mode(1, 1.5, 'abc') // returns [1, 1.5, "abc"]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* median,
|
||||
* mean
|
||||
*
|
||||
* @param {... *} args A single matrix
|
||||
* @return {*} The mode of all values
|
||||
*/
|
||||
return typed(name, {
|
||||
'Array | Matrix': _mode,
|
||||
'...': function (args) {
|
||||
return _mode(args);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Calculates the mode in an 1-dimensional array
|
||||
* @param {Array} values
|
||||
* @return {Array} mode
|
||||
* @private
|
||||
*/
|
||||
function _mode(values) {
|
||||
values = (0, _array.flatten)(values.valueOf());
|
||||
const num = values.length;
|
||||
if (num === 0) {
|
||||
throw new Error('Cannot calculate mode of an empty array');
|
||||
}
|
||||
const count = {};
|
||||
let mode = [];
|
||||
let max = 0;
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
const value = values[i];
|
||||
if (isNumeric(value) && isNaN(value)) {
|
||||
throw new Error('Cannot calculate mode of an array containing NaN values');
|
||||
}
|
||||
if (!(value in count)) {
|
||||
count[value] = 0;
|
||||
}
|
||||
count[value]++;
|
||||
if (count[value] === max) {
|
||||
mode.push(value);
|
||||
} else if (count[value] > max) {
|
||||
max = count[value];
|
||||
mode = [value];
|
||||
}
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
});
|
||||
85
node_modules/mathjs/lib/cjs/function/statistics/prod.js
generated
vendored
Normal file
85
node_modules/mathjs/lib/cjs/function/statistics/prod.js
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createProd = void 0;
|
||||
var _collection = require("../../utils/collection.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _number = require("../../utils/number.js");
|
||||
var _improveErrorMessage = require("./utils/improveErrorMessage.js");
|
||||
const name = 'prod';
|
||||
const dependencies = ['typed', 'config', 'multiplyScalar', 'numeric'];
|
||||
const createProd = exports.createProd = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
config,
|
||||
multiplyScalar,
|
||||
numeric
|
||||
} = _ref;
|
||||
/**
|
||||
* Compute the product of a matrix or a list with values.
|
||||
* In case of a multidimensional array or matrix, the sum of all
|
||||
* elements will be calculated.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.prod(a, b, c, ...)
|
||||
* math.prod(A)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.multiply(2, 3) // returns 6
|
||||
* math.prod(2, 3) // returns 6
|
||||
* math.prod(2, 3, 4) // returns 24
|
||||
* math.prod([2, 3, 4]) // returns 24
|
||||
* math.prod([[2, 5], [4, 3]]) // returns 120
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* mean, median, min, max, sum, std, variance
|
||||
*
|
||||
* @param {... *} args A single matrix or or multiple scalar values
|
||||
* @return {*} The product of all values
|
||||
*/
|
||||
return typed(name, {
|
||||
// prod([a, b, c, d, ...])
|
||||
'Array | Matrix': _prod,
|
||||
// prod([a, b, c, d, ...], dim)
|
||||
'Array | Matrix, number | BigNumber': function (array, dim) {
|
||||
// TODO: implement prod(A, dim)
|
||||
throw new Error('prod(A, dim) is not yet supported');
|
||||
// return reduce(arguments[0], arguments[1], math.prod)
|
||||
},
|
||||
// prod(a, b, c, d, ...)
|
||||
'...': function (args) {
|
||||
return _prod(args);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Recursively calculate the product of an n-dimensional array
|
||||
* @param {Array} array
|
||||
* @return {number} prod
|
||||
* @private
|
||||
*/
|
||||
function _prod(array) {
|
||||
let prod;
|
||||
(0, _collection.deepForEach)(array, function (value) {
|
||||
try {
|
||||
prod = prod === undefined ? value : multiplyScalar(prod, value);
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'prod', value);
|
||||
}
|
||||
});
|
||||
|
||||
// make sure returning numeric value: parse a string into a numeric value
|
||||
if (typeof prod === 'string') {
|
||||
prod = numeric(prod, (0, _number.safeNumberType)(prod, config));
|
||||
}
|
||||
if (prod === undefined) {
|
||||
throw new Error('Cannot calculate prod of an empty array');
|
||||
}
|
||||
return prod;
|
||||
}
|
||||
});
|
||||
172
node_modules/mathjs/lib/cjs/function/statistics/quantileSeq.js
generated
vendored
Normal file
172
node_modules/mathjs/lib/cjs/function/statistics/quantileSeq.js
generated
vendored
Normal file
@@ -0,0 +1,172 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createQuantileSeq = void 0;
|
||||
var _is = require("../../utils/is.js");
|
||||
var _array = require("../../utils/array.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _apply = require("../matrix/apply.js");
|
||||
const name = 'quantileSeq';
|
||||
const dependencies = ['typed', '?bignumber', 'add', 'subtract', 'divide', 'multiply', 'partitionSelect', 'compare', 'isInteger', 'smaller', 'smallerEq', 'larger'];
|
||||
const createQuantileSeq = exports.createQuantileSeq = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
bignumber,
|
||||
add,
|
||||
subtract,
|
||||
divide,
|
||||
multiply,
|
||||
partitionSelect,
|
||||
compare,
|
||||
isInteger,
|
||||
smaller,
|
||||
smallerEq,
|
||||
larger
|
||||
} = _ref;
|
||||
const apply = (0, _apply.createApply)({
|
||||
typed,
|
||||
isInteger
|
||||
});
|
||||
|
||||
/**
|
||||
* Compute the prob order quantile of a matrix or a list with values.
|
||||
* The sequence is sorted and the middle value is returned.
|
||||
* Supported types of sequence values are: Number, BigNumber, Unit
|
||||
* Supported types of probability are: Number, BigNumber
|
||||
*
|
||||
* In case of a multidimensional array or matrix, the prob order quantile
|
||||
* of all elements will be calculated.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.quantileSeq(A, prob[, sorted])
|
||||
* math.quantileSeq(A, [prob1, prob2, ...][, sorted])
|
||||
* math.quantileSeq(A, N[, sorted])
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.quantileSeq([3, -1, 5, 7], 0.5) // returns 4
|
||||
* math.quantileSeq([3, -1, 5, 7], [1/3, 2/3]) // returns [3, 5]
|
||||
* math.quantileSeq([3, -1, 5, 7], 2) // returns [3, 5]
|
||||
* math.quantileSeq([-1, 3, 5, 7], 0.5, true) // returns 4
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* median, mean, min, max, sum, prod, std, variance
|
||||
*
|
||||
* @param {Array, Matrix} data A single matrix or Array
|
||||
* @param {Number, BigNumber, Array} probOrN prob is the order of the quantile, while N is
|
||||
* the amount of evenly distributed steps of
|
||||
* probabilities; only one of these options can
|
||||
* be provided
|
||||
* @param {Boolean} sorted=false is data sorted in ascending order
|
||||
* @return {Number, BigNumber, Unit, Array} Quantile(s)
|
||||
*/
|
||||
return typed(name, {
|
||||
'Array | Matrix, number | BigNumber': (data, p) => _quantileSeqProbNumber(data, p, false),
|
||||
'Array | Matrix, number | BigNumber, number': (data, prob, dim) => _quantileSeqDim(data, prob, false, dim, _quantileSeqProbNumber),
|
||||
'Array | Matrix, number | BigNumber, boolean': _quantileSeqProbNumber,
|
||||
'Array | Matrix, number | BigNumber, boolean, number': (data, prob, sorted, dim) => _quantileSeqDim(data, prob, sorted, dim, _quantileSeqProbNumber),
|
||||
'Array | Matrix, Array | Matrix': (data, p) => _quantileSeqProbCollection(data, p, false),
|
||||
'Array | Matrix, Array | Matrix, number': (data, prob, dim) => _quantileSeqDim(data, prob, false, dim, _quantileSeqProbCollection),
|
||||
'Array | Matrix, Array | Matrix, boolean': _quantileSeqProbCollection,
|
||||
'Array | Matrix, Array | Matrix, boolean, number': (data, prob, sorted, dim) => _quantileSeqDim(data, prob, sorted, dim, _quantileSeqProbCollection)
|
||||
});
|
||||
function _quantileSeqDim(data, prob, sorted, dim, fn) {
|
||||
return apply(data, dim, x => fn(x, prob, sorted));
|
||||
}
|
||||
function _quantileSeqProbNumber(data, probOrN, sorted) {
|
||||
let probArr;
|
||||
const dataArr = data.valueOf();
|
||||
if (smaller(probOrN, 0)) {
|
||||
throw new Error('N/prob must be non-negative');
|
||||
}
|
||||
if (smallerEq(probOrN, 1)) {
|
||||
// quantileSeq([a, b, c, d, ...], prob[,sorted])
|
||||
return (0, _is.isNumber)(probOrN) ? _quantileSeq(dataArr, probOrN, sorted) : bignumber(_quantileSeq(dataArr, probOrN, sorted));
|
||||
}
|
||||
if (larger(probOrN, 1)) {
|
||||
// quantileSeq([a, b, c, d, ...], N[,sorted])
|
||||
if (!isInteger(probOrN)) {
|
||||
throw new Error('N must be a positive integer');
|
||||
}
|
||||
|
||||
// largest possible Array length is 2^32-1
|
||||
// 2^32 < 10^15, thus safe conversion guaranteed
|
||||
if (larger(probOrN, 4294967295)) {
|
||||
throw new Error('N must be less than or equal to 2^32-1, as that is the maximum length of an Array');
|
||||
}
|
||||
const nPlusOne = add(probOrN, 1);
|
||||
probArr = [];
|
||||
for (let i = 0; smaller(i, probOrN); i++) {
|
||||
const prob = divide(i + 1, nPlusOne);
|
||||
probArr.push(_quantileSeq(dataArr, prob, sorted));
|
||||
}
|
||||
return (0, _is.isNumber)(probOrN) ? probArr : bignumber(probArr);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the prob order quantile of an n-dimensional array.
|
||||
*
|
||||
* @param {Array, Matrix} array
|
||||
* @param {Array, Matrix} prob
|
||||
* @param {Boolean} sorted
|
||||
* @return {Number, BigNumber, Unit} prob order quantile
|
||||
* @private
|
||||
*/
|
||||
|
||||
function _quantileSeqProbCollection(data, probOrN, sorted) {
|
||||
const dataArr = data.valueOf();
|
||||
// quantileSeq([a, b, c, d, ...], [prob1, prob2, ...][,sorted])
|
||||
const probOrNArr = probOrN.valueOf();
|
||||
const probArr = [];
|
||||
for (let i = 0; i < probOrNArr.length; ++i) {
|
||||
probArr.push(_quantileSeq(dataArr, probOrNArr[i], sorted));
|
||||
}
|
||||
return probArr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the prob order quantile of an n-dimensional array.
|
||||
*
|
||||
* @param {Array} array
|
||||
* @param {Number, BigNumber} prob
|
||||
* @param {Boolean} sorted
|
||||
* @return {Number, BigNumber, Unit} prob order quantile
|
||||
* @private
|
||||
*/
|
||||
function _quantileSeq(array, prob, sorted) {
|
||||
const flat = (0, _array.flatten)(array);
|
||||
const len = flat.length;
|
||||
if (len === 0) {
|
||||
throw new Error('Cannot calculate quantile of an empty sequence');
|
||||
}
|
||||
const index = (0, _is.isNumber)(prob) ? prob * (len - 1) : prob.times(len - 1);
|
||||
const integerPart = (0, _is.isNumber)(prob) ? Math.floor(index) : index.floor().toNumber();
|
||||
const fracPart = (0, _is.isNumber)(prob) ? index % 1 : index.minus(integerPart);
|
||||
if (isInteger(index)) {
|
||||
return sorted ? flat[index] : partitionSelect(flat, (0, _is.isNumber)(prob) ? index : index.valueOf());
|
||||
}
|
||||
let left;
|
||||
let right;
|
||||
if (sorted) {
|
||||
left = flat[integerPart];
|
||||
right = flat[integerPart + 1];
|
||||
} else {
|
||||
right = partitionSelect(flat, integerPart + 1);
|
||||
|
||||
// max of partition is kth largest
|
||||
left = flat[integerPart];
|
||||
for (let i = 0; i < integerPart; ++i) {
|
||||
if (compare(flat[i], left) > 0) {
|
||||
left = flat[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
// Q(prob) = (1-f)*A[floor(index)] + f*A[floor(index)+1]
|
||||
return add(multiply(left, subtract(1, fracPart)), multiply(right, fracPart));
|
||||
}
|
||||
});
|
||||
103
node_modules/mathjs/lib/cjs/function/statistics/std.js
generated
vendored
Normal file
103
node_modules/mathjs/lib/cjs/function/statistics/std.js
generated
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createStd = void 0;
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _is = require("../../utils/is.js");
|
||||
const name = 'std';
|
||||
const dependencies = ['typed', 'map', 'sqrt', 'variance'];
|
||||
const createStd = exports.createStd = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
map,
|
||||
sqrt,
|
||||
variance
|
||||
} = _ref;
|
||||
/**
|
||||
* Compute the standard deviation of a matrix or a list with values.
|
||||
* The standard deviations is defined as the square root of the variance:
|
||||
* `std(A) = sqrt(variance(A))`.
|
||||
* In case of a (multi dimensional) array or matrix, the standard deviation
|
||||
* over all elements will be calculated by default, unless an axis is specified
|
||||
* in which case the standard deviation will be computed along that axis.
|
||||
*
|
||||
* Additionally, it is possible to compute the standard deviation along the rows
|
||||
* or columns of a matrix by specifying the dimension as the second argument.
|
||||
*
|
||||
* Optionally, the type of normalization can be specified as the final
|
||||
* parameter. The parameter `normalization` can be one of the following values:
|
||||
*
|
||||
* - 'unbiased' (default) The sum of squared errors is divided by (n - 1)
|
||||
* - 'uncorrected' The sum of squared errors is divided by n
|
||||
* - 'biased' The sum of squared errors is divided by (n + 1)
|
||||
*
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.std(a, b, c, ...)
|
||||
* math.std(A)
|
||||
* math.std(A, normalization)
|
||||
* math.std(A, dimension)
|
||||
* math.std(A, dimension, normalization)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.std(2, 4, 6) // returns 2
|
||||
* math.std([2, 4, 6, 8]) // returns 2.581988897471611
|
||||
* math.std([2, 4, 6, 8], 'uncorrected') // returns 2.23606797749979
|
||||
* math.std([2, 4, 6, 8], 'biased') // returns 2
|
||||
*
|
||||
* math.std([[1, 2, 3], [4, 5, 6]]) // returns 1.8708286933869707
|
||||
* math.std([[1, 2, 3], [4, 6, 8]], 0) // returns [2.1213203435596424, 2.8284271247461903, 3.5355339059327378]
|
||||
* math.std([[1, 2, 3], [4, 6, 8]], 1) // returns [1, 2]
|
||||
* math.std([[1, 2, 3], [4, 6, 8]], 1, 'biased') // returns [0.7071067811865476, 1.4142135623730951]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* mean, median, max, min, prod, sum, variance
|
||||
*
|
||||
* @param {Array | Matrix} array
|
||||
* A single matrix or or multiple scalar values
|
||||
* @param {string} [normalization='unbiased']
|
||||
* Determines how to normalize the variance.
|
||||
* Choose 'unbiased' (default), 'uncorrected', or 'biased'.
|
||||
* @param dimension {number | BigNumber}
|
||||
* Determines the axis to compute the standard deviation for a matrix
|
||||
* @return {*} The standard deviation
|
||||
*/
|
||||
return typed(name, {
|
||||
// std([a, b, c, d, ...])
|
||||
'Array | Matrix': _std,
|
||||
// std([a, b, c, d, ...], normalization)
|
||||
'Array | Matrix, string': _std,
|
||||
// std([a, b, c, c, ...], dim)
|
||||
'Array | Matrix, number | BigNumber': _std,
|
||||
// std([a, b, c, c, ...], dim, normalization)
|
||||
'Array | Matrix, number | BigNumber, string': _std,
|
||||
// std(a, b, c, d, ...)
|
||||
'...': function (args) {
|
||||
return _std(args);
|
||||
}
|
||||
});
|
||||
function _std(array, normalization) {
|
||||
if (array.length === 0) {
|
||||
throw new SyntaxError('Function std requires one or more parameters (0 provided)');
|
||||
}
|
||||
try {
|
||||
const v = variance.apply(null, arguments);
|
||||
if ((0, _is.isCollection)(v)) {
|
||||
return map(v, sqrt);
|
||||
} else {
|
||||
return sqrt(v);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof TypeError && err.message.includes(' variance')) {
|
||||
throw new TypeError(err.message.replace(' variance', ' std'));
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
91
node_modules/mathjs/lib/cjs/function/statistics/sum.js
generated
vendored
Normal file
91
node_modules/mathjs/lib/cjs/function/statistics/sum.js
generated
vendored
Normal file
@@ -0,0 +1,91 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createSum = void 0;
|
||||
var _collection = require("../../utils/collection.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _number = require("../../utils/number.js");
|
||||
var _improveErrorMessage = require("./utils/improveErrorMessage.js");
|
||||
const name = 'sum';
|
||||
const dependencies = ['typed', 'config', 'add', 'numeric'];
|
||||
const createSum = exports.createSum = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
config,
|
||||
add,
|
||||
numeric
|
||||
} = _ref;
|
||||
/**
|
||||
* Compute the sum of a matrix or a list with values.
|
||||
* In case of a multidimensional array or matrix, the sum of all
|
||||
* elements will be calculated.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.sum(a, b, c, ...)
|
||||
* math.sum(A)
|
||||
* math.sum(A, dimension)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.sum(2, 1, 4, 3) // returns 10
|
||||
* math.sum([2, 1, 4, 3]) // returns 10
|
||||
* math.sum([[2, 5], [4, 3], [1, 7]]) // returns 22
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* mean, median, min, max, prod, std, variance, cumsum
|
||||
*
|
||||
* @param {... *} args A single matrix or multiple scalar values
|
||||
* @return {*} The sum of all values
|
||||
*/
|
||||
return typed(name, {
|
||||
// sum([a, b, c, d, ...])
|
||||
'Array | Matrix': _sum,
|
||||
// sum([a, b, c, d, ...], dim)
|
||||
'Array | Matrix, number | BigNumber': _nsumDim,
|
||||
// sum(a, b, c, d, ...)
|
||||
'...': function (args) {
|
||||
if ((0, _collection.containsCollections)(args)) {
|
||||
throw new TypeError('Scalar values expected in function sum');
|
||||
}
|
||||
return _sum(args);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Recursively calculate the sum of an n-dimensional array
|
||||
* @param {Array | Matrix} array
|
||||
* @return {number} sum
|
||||
* @private
|
||||
*/
|
||||
function _sum(array) {
|
||||
let sum;
|
||||
(0, _collection.deepForEach)(array, function (value) {
|
||||
try {
|
||||
sum = sum === undefined ? value : add(sum, value);
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'sum', value);
|
||||
}
|
||||
});
|
||||
|
||||
// make sure returning numeric value: parse a string into a numeric value
|
||||
if (sum === undefined) {
|
||||
sum = numeric(0, config.number);
|
||||
}
|
||||
if (typeof sum === 'string') {
|
||||
sum = numeric(sum, (0, _number.safeNumberType)(sum, config));
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
function _nsumDim(array, dim) {
|
||||
try {
|
||||
const sum = (0, _collection.reduce)(array, dim, add);
|
||||
return sum;
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'sum');
|
||||
}
|
||||
}
|
||||
});
|
||||
30
node_modules/mathjs/lib/cjs/function/statistics/utils/improveErrorMessage.js
generated
vendored
Normal file
30
node_modules/mathjs/lib/cjs/function/statistics/utils/improveErrorMessage.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.improveErrorMessage = improveErrorMessage;
|
||||
var _is = require("../../../utils/is.js");
|
||||
/**
|
||||
* Improve error messages for statistics functions. Errors are typically
|
||||
* thrown in an internally used function like larger, causing the error
|
||||
* not to mention the function (like max) which is actually used by the user.
|
||||
*
|
||||
* @param {Error} err
|
||||
* @param {String} fnName
|
||||
* @param {*} [value]
|
||||
* @return {Error}
|
||||
*/
|
||||
function improveErrorMessage(err, fnName, value) {
|
||||
// TODO: add information with the index (also needs transform in expression parser)
|
||||
let details;
|
||||
if (String(err).includes('Unexpected type')) {
|
||||
details = arguments.length > 2 ? ' (type: ' + (0, _is.typeOf)(value) + ', value: ' + JSON.stringify(value) + ')' : ' (type: ' + err.data.actual + ')';
|
||||
return new TypeError('Cannot calculate ' + fnName + ', unexpected type of argument' + details);
|
||||
}
|
||||
if (String(err).includes('complex numbers')) {
|
||||
details = arguments.length > 2 ? ' (type: ' + (0, _is.typeOf)(value) + ', value: ' + JSON.stringify(value) + ')' : '';
|
||||
return new TypeError('Cannot calculate ' + fnName + ', no ordering relation is defined for complex numbers' + details);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
159
node_modules/mathjs/lib/cjs/function/statistics/variance.js
generated
vendored
Normal file
159
node_modules/mathjs/lib/cjs/function/statistics/variance.js
generated
vendored
Normal file
@@ -0,0 +1,159 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createVariance = void 0;
|
||||
var _collection = require("../../utils/collection.js");
|
||||
var _is = require("../../utils/is.js");
|
||||
var _factory = require("../../utils/factory.js");
|
||||
var _improveErrorMessage = require("./utils/improveErrorMessage.js");
|
||||
const DEFAULT_NORMALIZATION = 'unbiased';
|
||||
const name = 'variance';
|
||||
const dependencies = ['typed', 'add', 'subtract', 'multiply', 'divide', 'apply', 'isNaN'];
|
||||
const createVariance = exports.createVariance = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
add,
|
||||
subtract,
|
||||
multiply,
|
||||
divide,
|
||||
apply,
|
||||
isNaN
|
||||
} = _ref;
|
||||
/**
|
||||
* Compute the variance of a matrix or a list with values.
|
||||
* In case of a multidimensional array or matrix, the variance over all
|
||||
* elements will be calculated.
|
||||
*
|
||||
* Additionally, it is possible to compute the variance along the rows
|
||||
* or columns of a matrix by specifying the dimension as the second argument.
|
||||
*
|
||||
* Optionally, the type of normalization can be specified as the final
|
||||
* parameter. The parameter `normalization` can be one of the following values:
|
||||
*
|
||||
* - 'unbiased' (default) The sum of squared errors is divided by (n - 1)
|
||||
* - 'uncorrected' The sum of squared errors is divided by n
|
||||
* - 'biased' The sum of squared errors is divided by (n + 1)
|
||||
*
|
||||
*
|
||||
* Note that older browser may not like the variable name `var`. In that
|
||||
* case, the function can be called as `math['var'](...)` instead of
|
||||
* `math.var(...)`.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.variance(a, b, c, ...)
|
||||
* math.variance(A)
|
||||
* math.variance(A, normalization)
|
||||
* math.variance(A, dimension)
|
||||
* math.variance(A, dimension, normalization)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.variance(2, 4, 6) // returns 4
|
||||
* math.variance([2, 4, 6, 8]) // returns 6.666666666666667
|
||||
* math.variance([2, 4, 6, 8], 'uncorrected') // returns 5
|
||||
* math.variance([2, 4, 6, 8], 'biased') // returns 4
|
||||
*
|
||||
* math.variance([[1, 2, 3], [4, 5, 6]]) // returns 3.5
|
||||
* math.variance([[1, 2, 3], [4, 6, 8]], 0) // returns [4.5, 8, 12.5]
|
||||
* math.variance([[1, 2, 3], [4, 6, 8]], 1) // returns [1, 4]
|
||||
* math.variance([[1, 2, 3], [4, 6, 8]], 1, 'biased') // returns [0.5, 2]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* mean, median, max, min, prod, std, sum
|
||||
*
|
||||
* @param {Array | Matrix} array
|
||||
* A single matrix or or multiple scalar values
|
||||
* @param {string} [normalization='unbiased']
|
||||
* Determines how to normalize the variance.
|
||||
* Choose 'unbiased' (default), 'uncorrected', or 'biased'.
|
||||
* @param dimension {number | BigNumber}
|
||||
* Determines the axis to compute the variance for a matrix
|
||||
* @return {*} The variance
|
||||
*/
|
||||
return typed(name, {
|
||||
// variance([a, b, c, d, ...])
|
||||
'Array | Matrix': function (array) {
|
||||
return _var(array, DEFAULT_NORMALIZATION);
|
||||
},
|
||||
// variance([a, b, c, d, ...], normalization)
|
||||
'Array | Matrix, string': _var,
|
||||
// variance([a, b, c, c, ...], dim)
|
||||
'Array | Matrix, number | BigNumber': function (array, dim) {
|
||||
return _varDim(array, dim, DEFAULT_NORMALIZATION);
|
||||
},
|
||||
// variance([a, b, c, c, ...], dim, normalization)
|
||||
'Array | Matrix, number | BigNumber, string': _varDim,
|
||||
// variance(a, b, c, d, ...)
|
||||
'...': function (args) {
|
||||
return _var(args, DEFAULT_NORMALIZATION);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Recursively calculate the variance of an n-dimensional array
|
||||
* @param {Array} array
|
||||
* @param {string} normalization
|
||||
* Determines how to normalize the variance:
|
||||
* - 'unbiased' The sum of squared errors is divided by (n - 1)
|
||||
* - 'uncorrected' The sum of squared errors is divided by n
|
||||
* - 'biased' The sum of squared errors is divided by (n + 1)
|
||||
* @return {number | BigNumber} variance
|
||||
* @private
|
||||
*/
|
||||
function _var(array, normalization) {
|
||||
let sum;
|
||||
let num = 0;
|
||||
if (array.length === 0) {
|
||||
throw new SyntaxError('Function variance requires one or more parameters (0 provided)');
|
||||
}
|
||||
|
||||
// calculate the mean and number of elements
|
||||
(0, _collection.deepForEach)(array, function (value) {
|
||||
try {
|
||||
sum = sum === undefined ? value : add(sum, value);
|
||||
num++;
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'variance', value);
|
||||
}
|
||||
});
|
||||
if (num === 0) throw new Error('Cannot calculate variance of an empty array');
|
||||
const mean = divide(sum, num);
|
||||
|
||||
// calculate the variance
|
||||
sum = undefined;
|
||||
(0, _collection.deepForEach)(array, function (value) {
|
||||
const diff = subtract(value, mean);
|
||||
sum = sum === undefined ? multiply(diff, diff) : add(sum, multiply(diff, diff));
|
||||
});
|
||||
if (isNaN(sum)) {
|
||||
return sum;
|
||||
}
|
||||
switch (normalization) {
|
||||
case 'uncorrected':
|
||||
return divide(sum, num);
|
||||
case 'biased':
|
||||
return divide(sum, num + 1);
|
||||
case 'unbiased':
|
||||
{
|
||||
const zero = (0, _is.isBigNumber)(sum) ? sum.mul(0) : 0;
|
||||
return num === 1 ? zero : divide(sum, num - 1);
|
||||
}
|
||||
default:
|
||||
throw new Error('Unknown normalization "' + normalization + '". ' + 'Choose "unbiased" (default), "uncorrected", or "biased".');
|
||||
}
|
||||
}
|
||||
function _varDim(array, dim, normalization) {
|
||||
try {
|
||||
if (array.length === 0) {
|
||||
throw new SyntaxError('Function variance requires one or more parameters (0 provided)');
|
||||
}
|
||||
return apply(array, dim, x => _var(x, normalization));
|
||||
} catch (err) {
|
||||
throw (0, _improveErrorMessage.improveErrorMessage)(err, 'variance');
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user