From 07ebb2fb73261614441e5459fb5cebbb7fb73fce Mon Sep 17 00:00:00 2001 From: vrushaket Date: Wed, 23 Aug 2023 15:16:24 +0530 Subject: [PATCH 1/9] added correlation function to statistics --- package-lock.json | 4 +- package.json | 4 +- src/expression/embeddedDocs/embeddedDocs.js | 2 + .../function/statistics/correlation.js | 21 +++++++ src/factoriesAny.js | 1 + src/factoriesNumber.js | 1 + src/function/statistics/correlation.js | 60 +++++++++++++++++++ .../function/statistics/correlation.test.js | 18 ++++++ types/index.d.ts | 15 ++++- 9 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 src/expression/embeddedDocs/function/statistics/correlation.js create mode 100644 src/function/statistics/correlation.js create mode 100644 test/unit-tests/function/statistics/correlation.test.js diff --git a/package-lock.json b/package-lock.json index 1521300166..f42cd050cb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,7 +40,7 @@ "core-js": "3.31.1", "del": "6.1.1", "dtslint": "4.2.1", - "eslint": "8.45.0", + "eslint": "^8.45.0", "eslint-config-prettier": "8.8.0", "eslint-config-standard": "17.1.0", "eslint-plugin-import": "2.27.5", @@ -63,7 +63,7 @@ "karma-mocha-reporter": "2.2.5", "karma-webpack": "5.0.0", "mkdirp": "3.0.1", - "mocha": "10.2.0", + "mocha": "^10.2.0", "mocha-junit-reporter": "2.2.1", "ndarray": "1.0.19", "ndarray-determinant": "1.0.0", diff --git a/package.json b/package.json index 7ba78e5d34..379b4223ac 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "core-js": "3.31.1", "del": "6.1.1", "dtslint": "4.2.1", - "eslint": "8.45.0", + "eslint": "^8.45.0", "eslint-config-prettier": "8.8.0", "eslint-config-standard": "17.1.0", "eslint-plugin-import": "2.27.5", @@ -76,7 +76,7 @@ "karma-mocha-reporter": "2.2.5", "karma-webpack": "5.0.0", "mkdirp": "3.0.1", - "mocha": "10.2.0", + "mocha": "^10.2.0", "mocha-junit-reporter": "2.2.1", "ndarray": "1.0.19", "ndarray-determinant": "1.0.0", diff --git a/src/expression/embeddedDocs/embeddedDocs.js b/src/expression/embeddedDocs/embeddedDocs.js index e2adde8a95..d70a5c8a5e 100644 --- a/src/expression/embeddedDocs/embeddedDocs.js +++ b/src/expression/embeddedDocs/embeddedDocs.js @@ -197,6 +197,7 @@ import { stdDocs } from './function/statistics/std.js' import { cumSumDocs } from './function/statistics/cumsum.js' import { sumDocs } from './function/statistics/sum.js' import { varianceDocs } from './function/statistics/variance.js' +import { correlationDocs } from './function/statistics/correlation.js' import { acosDocs } from './function/trigonometry/acos.js' import { acoshDocs } from './function/trigonometry/acosh.js' import { acotDocs } from './function/trigonometry/acot.js' @@ -543,6 +544,7 @@ export const embeddedDocs = { std: stdDocs, sum: sumDocs, variance: varianceDocs, + corr: correlationDocs, // functions - trigonometry acos: acosDocs, diff --git a/src/expression/embeddedDocs/function/statistics/correlation.js b/src/expression/embeddedDocs/function/statistics/correlation.js new file mode 100644 index 0000000000..3bcedf3d13 --- /dev/null +++ b/src/expression/embeddedDocs/function/statistics/correlation.js @@ -0,0 +1,21 @@ +export const correlationDocs = { + name: 'corr', + category: 'Statistics', + syntax: [ + 'corr(A,B)' + ], + description: 'Compute the colleration of a two list with values', + examples: [ + 'corr([2, 4, 6, 8],[1, 2, 3, 6])' + ], + seealso: [ + 'max', + 'mean', + 'min', + 'median', + 'min', + 'prod', + 'std', + 'sum' + ] +} diff --git a/src/factoriesAny.js b/src/factoriesAny.js index 8d79c67555..824a19a84e 100644 --- a/src/factoriesAny.js +++ b/src/factoriesAny.js @@ -237,6 +237,7 @@ export { createMad } from './function/statistics/mad.js' export { createVariance } from './function/statistics/variance.js' export { createQuantileSeq } from './function/statistics/quantileSeq.js' export { createStd } from './function/statistics/std.js' +export { createCorrelation } from './function/statistics/correlation.js' export { createCombinations } from './function/probability/combinations.js' export { createCombinationsWithRep } from './function/probability/combinationsWithRep.js' export { createGamma } from './function/probability/gamma.js' diff --git a/src/factoriesNumber.js b/src/factoriesNumber.js index 47f9a37e17..c7ba59d5c6 100644 --- a/src/factoriesNumber.js +++ b/src/factoriesNumber.js @@ -263,6 +263,7 @@ export { createMad } from './function/statistics/mad.js' export { createVariance } from './function/statistics/variance.js' export { createQuantileSeq } from './function/statistics/quantileSeq.js' export { createStd } from './function/statistics/std.js' +export { createCorrelation } from './function/statistics/correlation.js' // string export { createFormat } from './function/string/format.js' diff --git a/src/function/statistics/correlation.js b/src/function/statistics/correlation.js new file mode 100644 index 0000000000..cd0b764387 --- /dev/null +++ b/src/function/statistics/correlation.js @@ -0,0 +1,60 @@ +import { factory } from '../../utils/factory.js' + +const name = 'corr' +const dependencies = ['typed'] + +export const createCorrelation = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => { + /** + * Compute the colleration of a two list with values. + * + * 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 + * + */ + return typed(name, { + // correlation(xArray, yArray) + 'Array, Array': function (xArray, yArray) { + return _corr(xArray, yArray) + } + }) + /** + * Calculate the correlation coefficient between two arrays. + * @param {Array} xArray + * @param {Array} yArray + * @return {number | BigNumber} correlation coefficient + * @private + */ + function _corr (xArray, yArray, normalization) { + if (xArray.length !== yArray.length) { + throw new SyntaxError('Input arrays must have the same length') + } + if (xArray.length < 2) { + throw new SyntaxError('Function corr requires two or more parameters (' + xArray.length + ' provided) in first input array') + } + if (yArray.length < 2) { + throw new SyntaxError('Function corr requires two or more parameters (' + yArray.length + ' provided) in second input array') + } + + const n = xArray.length + + const sumX = xArray.reduce((acc, x) => acc + x, 0) + const sumY = yArray.reduce((acc, y) => acc + y, 0) + + const sumXY = xArray.reduce((acc, x, index) => acc + x * yArray[index], 0) + const sumXSquare = xArray.reduce((acc, x) => acc + x ** 2, 0) + const sumYSquare = yArray.reduce((acc, y) => acc + y ** 2, 0) + + const numerator = n * sumXY - sumX * sumY + const denominator = Math.sqrt((n * sumXSquare - sumX ** 2) * (n * sumYSquare - sumY ** 2)) + + const correlation = numerator / denominator + + return correlation + } +}) diff --git a/test/unit-tests/function/statistics/correlation.test.js b/test/unit-tests/function/statistics/correlation.test.js new file mode 100644 index 0000000000..5ff8c7106a --- /dev/null +++ b/test/unit-tests/function/statistics/correlation.test.js @@ -0,0 +1,18 @@ +import assert from 'assert' +import math from '../../../../src/defaultInstance.js' +const corr = math.corr + +describe('correlation', function () { + it('should return the correlation coefficient from an array', function () { + assert.strictEqual(corr([1, 2, 3, 4, 5], [4, 5, 6, 7, 8]), 1) + assert.strictEqual(corr([1, 2.2, 3, 4.8, 5], [4, 5.3, 6.6, 7, 8]), 0.9569941688503644) + }) + + it('should throw an error if called with invalid number of arguments', function () { + assert.throws(function () { corr() }) + }) + + it('should throw an error if called with an empty array', function () { + assert.throws(function () { corr([]) }) + }) +}) diff --git a/types/index.d.ts b/types/index.d.ts index 0001222955..54ef9fabdd 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2915,8 +2915,8 @@ declare namespace math { cumsum(...args: MathType[]): MathType[] /** * @param array A single matrix - * @param dim The dimension along which to sum (defaults to 0) - * @returns The cumulative sums along the given dimension + * @p corr(xArray: MathCollection, yArray: MathCollection): MathNumericType +ms along the given dimension */ cumsum(array: MathCollection, dim?: number): MathCollection @@ -2970,6 +2970,17 @@ declare namespace math { normalization: 'unbiased' | 'uncorrected' | 'biased' ): MathNumericType + /** + * Calculate the correlation coefficient between two matrix. + * @param {Array} xArray A matrix to compute correlation coefficient + * @param {Array} yArray A matrix to compute correlation coefficient + * @returns correlation coefficient + */ + corr( + xArray: MathCollection, + yArray: MathCollection + ): MathNumericType + /************************************************************************* * String functions ************************************************************************/ From d7abc9a3a18787dc00b3f340ab8383f888c79a88 Mon Sep 17 00:00:00 2001 From: vrushaket Date: Wed, 23 Aug 2023 23:06:32 +0530 Subject: [PATCH 2/9] added implemenation for signature Matrix, Matrix and support for BigNumbers --- AUTHORS | 1 + .../function/statistics/correlation.js | 5 +- src/function/statistics/correlation.js | 69 +++++++++---------- .../function/statistics/correlation.test.js | 3 + types/index.d.ts | 13 ++-- 5 files changed, 45 insertions(+), 46 deletions(-) diff --git a/AUTHORS b/AUTHORS index 8eaa345e4b..b7a19b5d27 100644 --- a/AUTHORS +++ b/AUTHORS @@ -231,5 +231,6 @@ MaybePixem <47889538+MaybePixem@users.noreply.github.com> Aly Khaled BuildTools Anik Patel <74193405+Bobingstern@users.noreply.github.com> +vrushaket # Generated by tools/update-authors.js diff --git a/src/expression/embeddedDocs/function/statistics/correlation.js b/src/expression/embeddedDocs/function/statistics/correlation.js index 3bcedf3d13..6e59defdeb 100644 --- a/src/expression/embeddedDocs/function/statistics/correlation.js +++ b/src/expression/embeddedDocs/function/statistics/correlation.js @@ -4,9 +4,10 @@ export const correlationDocs = { syntax: [ 'corr(A,B)' ], - description: 'Compute the colleration of a two list with values', + description: 'Compute the correlation coefficient of a two list with values, For matrices, the matrix correlation coefficient is calculated.', examples: [ - 'corr([2, 4, 6, 8],[1, 2, 3, 6])' + 'corr([2, 4, 6, 8],[1, 2, 3, 6])', + 'corr(matrix([[1, 2.2, 3, 4.8, 5], [1, 2, 3, 4, 5]]), matrix([[4, 5.3, 6.6, 7, 8], [1, 2, 3, 4, 5]]))' ], seealso: [ 'max', diff --git a/src/function/statistics/correlation.js b/src/function/statistics/correlation.js index cd0b764387..5beb828400 100644 --- a/src/function/statistics/correlation.js +++ b/src/function/statistics/correlation.js @@ -1,11 +1,10 @@ import { factory } from '../../utils/factory.js' - const name = 'corr' -const dependencies = ['typed'] +const dependencies = ['typed', 'matrix', 'mean', 'sqrt', 'sum', 'add', 'subtract', 'multiply', 'pow', 'divide'] -export const createCorrelation = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => { +export const createCorrelation = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, sqrt, sum, add, subtract, multiply, pow, divide }) => { /** - * Compute the colleration of a two list with values. + * Compute the correlation coefficient of a two list with values, For matrices, the matrix correlation coefficient is calculated. * * Syntax: * @@ -15,46 +14,44 @@ export const createCorrelation = /* #__PURE__ */ factory(name, dependencies, ({ * * 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(matrix([[1, 2.2, 3, 4.8, 5], [1, 2, 3, 4, 5]]), matrix([[4, 5.3, 6.6, 7, 8], [1, 2, 3, 4, 5]])) // returns [0.9569941688503644, 1]) * */ return typed(name, { - // correlation(xArray, yArray) - 'Array, Array': function (xArray, yArray) { - return _corr(xArray, yArray) + 'Array, Array': function (A, B) { + return _corr(A, B) + }, + 'Matrix, Matrix': function (xMatrix, yMatrix) { + return matrix(_corr(xMatrix.toArray(), yMatrix.toArray())) } }) /** - * Calculate the correlation coefficient between two arrays. - * @param {Array} xArray - * @param {Array} yArray - * @return {number | BigNumber} correlation coefficient + * Calculate the correlation coefficient between two arrays or matrices. + * @param {Array | Matrix} A + * @param {Array | Matrix} B + * @return {*} correlation coefficient * @private */ - function _corr (xArray, yArray, normalization) { - if (xArray.length !== yArray.length) { - throw new SyntaxError('Input arrays must have the same length') - } - if (xArray.length < 2) { - throw new SyntaxError('Function corr requires two or more parameters (' + xArray.length + ' provided) in first input array') - } - if (yArray.length < 2) { - throw new SyntaxError('Function corr requires two or more parameters (' + yArray.length + ' provided) in second input array') + function _corr (A, B) { + if (Array.isArray(A[0]) && Array.isArray(B[0])) { + const correlations = [] + for (let i = 0; i < A.length; i++) { + correlations.push(correlation(A[i], B[i])) + } + return correlations + } else { + return correlation(A, B) } - - const n = xArray.length - - const sumX = xArray.reduce((acc, x) => acc + x, 0) - const sumY = yArray.reduce((acc, y) => acc + y, 0) - - const sumXY = xArray.reduce((acc, x, index) => acc + x * yArray[index], 0) - const sumXSquare = xArray.reduce((acc, x) => acc + x ** 2, 0) - const sumYSquare = yArray.reduce((acc, y) => acc + y ** 2, 0) - - const numerator = n * sumXY - sumX * sumY - const denominator = Math.sqrt((n * sumXSquare - sumX ** 2) * (n * sumYSquare - sumY ** 2)) - - const correlation = numerator / denominator - - return correlation + } + 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) } }) diff --git a/test/unit-tests/function/statistics/correlation.test.js b/test/unit-tests/function/statistics/correlation.test.js index 5ff8c7106a..38071158c2 100644 --- a/test/unit-tests/function/statistics/correlation.test.js +++ b/test/unit-tests/function/statistics/correlation.test.js @@ -1,11 +1,14 @@ import assert from 'assert' import math from '../../../../src/defaultInstance.js' const corr = math.corr +const BigNumber = math.BigNumber describe('correlation', function () { it('should return the correlation coefficient from an array', function () { + assert.strictEqual(corr([new BigNumber(1), new BigNumber(2.2), new BigNumber(3), new BigNumber(4.8), new BigNumber(5)], [new BigNumber(4), new BigNumber(5.3), new BigNumber(6.6), new BigNumber(7), new BigNumber(8)]).toNumber(), 0.9569941688503653) assert.strictEqual(corr([1, 2, 3, 4, 5], [4, 5, 6, 7, 8]), 1) assert.strictEqual(corr([1, 2.2, 3, 4.8, 5], [4, 5.3, 6.6, 7, 8]), 0.9569941688503644) + assert.deepStrictEqual(corr(math.matrix([[1, 2.2, 3, 4.8, 5], [1, 2, 3, 4, 5]]), math.matrix([[4, 5.3, 6.6, 7, 8], [1, 2, 3, 4, 5]]))._data, [0.9569941688503644, 1]) }) it('should throw an error if called with invalid number of arguments', function () { diff --git a/types/index.d.ts b/types/index.d.ts index 54ef9fabdd..441777ac09 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2915,8 +2915,8 @@ declare namespace math { cumsum(...args: MathType[]): MathType[] /** * @param array A single matrix - * @p corr(xArray: MathCollection, yArray: MathCollection): MathNumericType -ms along the given dimension + * @param dim The dimension along which to sum (defaults to 0) + * @returns The cumulative sums along the given dimension */ cumsum(array: MathCollection, dim?: number): MathCollection @@ -2972,14 +2972,11 @@ ms along the given dimension /** * Calculate the correlation coefficient between two matrix. - * @param {Array} xArray A matrix to compute correlation coefficient - * @param {Array} yArray A matrix to compute correlation coefficient + * @param {Array} x The first matrix to compute correlation coefficient + * @param {Array} y The second matrix to compute correlation coefficient * @returns correlation coefficient */ - corr( - xArray: MathCollection, - yArray: MathCollection - ): MathNumericType + corr(xArray: MathCollection, yArray: MathCollection): MathType /************************************************************************* * String functions From f76bd302beaa7c45d44b76eb88ce2acaa6fffb5b Mon Sep 17 00:00:00 2001 From: vrushaket Date: Wed, 23 Aug 2023 23:19:02 +0530 Subject: [PATCH 3/9] reverted changes to default for version numbers of devDepenencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 379b4223ac..7ba78e5d34 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "core-js": "3.31.1", "del": "6.1.1", "dtslint": "4.2.1", - "eslint": "^8.45.0", + "eslint": "8.45.0", "eslint-config-prettier": "8.8.0", "eslint-config-standard": "17.1.0", "eslint-plugin-import": "2.27.5", @@ -76,7 +76,7 @@ "karma-mocha-reporter": "2.2.5", "karma-webpack": "5.0.0", "mkdirp": "3.0.1", - "mocha": "^10.2.0", + "mocha": "10.2.0", "mocha-junit-reporter": "2.2.1", "ndarray": "1.0.19", "ndarray-determinant": "1.0.0", From 34852b7f4dcbe3babdbc9b1962f62bb6fbf06869 Mon Sep 17 00:00:00 2001 From: vrushaket Date: Wed, 23 Aug 2023 23:20:27 +0530 Subject: [PATCH 4/9] reverted changes to default for version numbers of devDepenencies in package-lock.json --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index f42cd050cb..1521300166 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,7 +40,7 @@ "core-js": "3.31.1", "del": "6.1.1", "dtslint": "4.2.1", - "eslint": "^8.45.0", + "eslint": "8.45.0", "eslint-config-prettier": "8.8.0", "eslint-config-standard": "17.1.0", "eslint-plugin-import": "2.27.5", @@ -63,7 +63,7 @@ "karma-mocha-reporter": "2.2.5", "karma-webpack": "5.0.0", "mkdirp": "3.0.1", - "mocha": "^10.2.0", + "mocha": "10.2.0", "mocha-junit-reporter": "2.2.1", "ndarray": "1.0.19", "ndarray-determinant": "1.0.0", From a24c1df34e749066272d1471aaf17e1da2677269 Mon Sep 17 00:00:00 2001 From: vrushaket Date: Thu, 24 Aug 2023 15:11:01 +0530 Subject: [PATCH 5/9] change variable name from xArray, yArray to x and y --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 441777ac09..eed294e074 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2976,7 +2976,7 @@ declare namespace math { * @param {Array} y The second matrix to compute correlation coefficient * @returns correlation coefficient */ - corr(xArray: MathCollection, yArray: MathCollection): MathType + corr(x: MathCollection, y: MathCollection): MathType /************************************************************************* * String functions From b2a175e9bf075e36edc1f7153c9017e656193167 Mon Sep 17 00:00:00 2001 From: vrushaket Date: Fri, 25 Aug 2023 16:21:18 +0530 Subject: [PATCH 6/9] added Matrix as param in index.d.ts --- types/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index eed294e074..c14a05d3f9 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2972,8 +2972,8 @@ declare namespace math { /** * Calculate the correlation coefficient between two matrix. - * @param {Array} x The first matrix to compute correlation coefficient - * @param {Array} y The second matrix to compute correlation coefficient + * @param {Array | Matrix} x The first array or matrix to compute correlation coefficient + * @param {Array | Matrix} y The second array or matrix to compute correlation coefficient * @returns correlation coefficient */ corr(x: MathCollection, y: MathCollection): MathType From 37359fbebad43eb17b7971a4671e94f07b10556a Mon Sep 17 00:00:00 2001 From: vrushaket Date: Fri, 1 Sep 2023 09:19:41 +0530 Subject: [PATCH 7/9] corrected the file and function names for correlation function --- src/expression/embeddedDocs/embeddedDocs.js | 4 ++-- .../function/statistics/{correlation.js => corr.js} | 2 +- src/factoriesAny.js | 2 +- src/function/statistics/{correlation.js => corr.js} | 2 +- .../function/statistics/{correlation.test.js => corr.test.js} | 0 5 files changed, 5 insertions(+), 5 deletions(-) rename src/expression/embeddedDocs/function/statistics/{correlation.js => corr.js} (93%) rename src/function/statistics/{correlation.js => corr.js} (92%) rename test/unit-tests/function/statistics/{correlation.test.js => corr.test.js} (100%) diff --git a/src/expression/embeddedDocs/embeddedDocs.js b/src/expression/embeddedDocs/embeddedDocs.js index d70a5c8a5e..9dd8a4e527 100644 --- a/src/expression/embeddedDocs/embeddedDocs.js +++ b/src/expression/embeddedDocs/embeddedDocs.js @@ -197,7 +197,7 @@ import { stdDocs } from './function/statistics/std.js' import { cumSumDocs } from './function/statistics/cumsum.js' import { sumDocs } from './function/statistics/sum.js' import { varianceDocs } from './function/statistics/variance.js' -import { correlationDocs } from './function/statistics/correlation.js' +import { corrDocs } from './function/statistics/corr.js' import { acosDocs } from './function/trigonometry/acos.js' import { acoshDocs } from './function/trigonometry/acosh.js' import { acotDocs } from './function/trigonometry/acot.js' @@ -544,7 +544,7 @@ export const embeddedDocs = { std: stdDocs, sum: sumDocs, variance: varianceDocs, - corr: correlationDocs, + corr: corrDocs, // functions - trigonometry acos: acosDocs, diff --git a/src/expression/embeddedDocs/function/statistics/correlation.js b/src/expression/embeddedDocs/function/statistics/corr.js similarity index 93% rename from src/expression/embeddedDocs/function/statistics/correlation.js rename to src/expression/embeddedDocs/function/statistics/corr.js index 6e59defdeb..397dcbdf88 100644 --- a/src/expression/embeddedDocs/function/statistics/correlation.js +++ b/src/expression/embeddedDocs/function/statistics/corr.js @@ -1,4 +1,4 @@ -export const correlationDocs = { +export const corrDocs = { name: 'corr', category: 'Statistics', syntax: [ diff --git a/src/factoriesAny.js b/src/factoriesAny.js index 824a19a84e..d353f70c70 100644 --- a/src/factoriesAny.js +++ b/src/factoriesAny.js @@ -237,7 +237,7 @@ export { createMad } from './function/statistics/mad.js' export { createVariance } from './function/statistics/variance.js' export { createQuantileSeq } from './function/statistics/quantileSeq.js' export { createStd } from './function/statistics/std.js' -export { createCorrelation } from './function/statistics/correlation.js' +export { createCorr } from './function/statistics/corr.js' export { createCombinations } from './function/probability/combinations.js' export { createCombinationsWithRep } from './function/probability/combinationsWithRep.js' export { createGamma } from './function/probability/gamma.js' diff --git a/src/function/statistics/correlation.js b/src/function/statistics/corr.js similarity index 92% rename from src/function/statistics/correlation.js rename to src/function/statistics/corr.js index 5beb828400..de8c564518 100644 --- a/src/function/statistics/correlation.js +++ b/src/function/statistics/corr.js @@ -2,7 +2,7 @@ import { factory } from '../../utils/factory.js' const name = 'corr' const dependencies = ['typed', 'matrix', 'mean', 'sqrt', 'sum', 'add', 'subtract', 'multiply', 'pow', 'divide'] -export const createCorrelation = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, sqrt, sum, add, subtract, multiply, pow, divide }) => { +export const createCorr = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, sqrt, sum, add, subtract, multiply, pow, divide }) => { /** * Compute the correlation coefficient of a two list with values, For matrices, the matrix correlation coefficient is calculated. * diff --git a/test/unit-tests/function/statistics/correlation.test.js b/test/unit-tests/function/statistics/corr.test.js similarity index 100% rename from test/unit-tests/function/statistics/correlation.test.js rename to test/unit-tests/function/statistics/corr.test.js From efe751dd322a447e29f17e73617b7aee391bf55e Mon Sep 17 00:00:00 2001 From: vrushaket Date: Fri, 1 Sep 2023 09:24:26 +0530 Subject: [PATCH 8/9] renamed createCorrelation to createCorr in factoriesNumber.js --- src/factoriesNumber.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/factoriesNumber.js b/src/factoriesNumber.js index c7ba59d5c6..988eb27e0e 100644 --- a/src/factoriesNumber.js +++ b/src/factoriesNumber.js @@ -263,7 +263,7 @@ export { createMad } from './function/statistics/mad.js' export { createVariance } from './function/statistics/variance.js' export { createQuantileSeq } from './function/statistics/quantileSeq.js' export { createStd } from './function/statistics/std.js' -export { createCorrelation } from './function/statistics/correlation.js' +export { createCorr } from './function/statistics/corr.js' // string export { createFormat } from './function/string/format.js' From 4bbaff005c39579ff94f53a7de1cd4f6c710317a Mon Sep 17 00:00:00 2001 From: vrushaket Date: Fri, 1 Sep 2023 16:49:20 +0530 Subject: [PATCH 9/9] fixed failing test case for matrix and added params and return in corr --- src/function/statistics/corr.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/function/statistics/corr.js b/src/function/statistics/corr.js index de8c564518..efaa905d69 100644 --- a/src/function/statistics/corr.js +++ b/src/function/statistics/corr.js @@ -14,8 +14,15 @@ export const createCorr = /* #__PURE__ */ factory(name, dependencies, ({ typed, * * 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(matrix([[1, 2.2, 3, 4.8, 5], [1, 2, 3, 4, 5]]), matrix([[4, 5.3, 6.6, 7, 8], [1, 2, 3, 4, 5]])) // returns [0.9569941688503644, 1]) + * math.corr(math.matrix([[1, 2.2, 3, 4.8, 5], [1, 2, 3, 4, 5]]), math.matrix([[4, 5.3, 6.6, 7, 8], [1, 2, 3, 4, 5]])) // returns DenseMatrix [0.9569941688503644, 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) {