Skip to content

Commit 1ee8733

Browse files
authored
added correlation function to statistics (#3015)
* added correlation function to statistics * added implemenation for signature Matrix, Matrix and support for BigNumbers * reverted changes to default for version numbers of devDepenencies * reverted changes to default for version numbers of devDepenencies in package-lock.json * change variable name from xArray, yArray to x and y * added Matrix as param in index.d.ts * corrected the file and function names for correlation function * renamed createCorrelation to createCorr in factoriesNumber.js * fixed failing test case for matrix and added params and return in corr
1 parent 2a3c99d commit 1ee8733

File tree

8 files changed

+120
-0
lines changed

8 files changed

+120
-0
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,6 @@ MaybePixem <47889538+MaybePixem@users.noreply.github.com>
231231
Aly Khaled <alykhaled2001@live.com>
232232
BuildTools <anikpatel1322@gmail.com>
233233
Anik Patel <74193405+Bobingstern@users.noreply.github.com>
234+
vrushaket <vrushu00@gmail.com>
234235

235236
# Generated by tools/update-authors.js

src/expression/embeddedDocs/embeddedDocs.js

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ import { stdDocs } from './function/statistics/std.js'
197197
import { cumSumDocs } from './function/statistics/cumsum.js'
198198
import { sumDocs } from './function/statistics/sum.js'
199199
import { varianceDocs } from './function/statistics/variance.js'
200+
import { corrDocs } from './function/statistics/corr.js'
200201
import { acosDocs } from './function/trigonometry/acos.js'
201202
import { acoshDocs } from './function/trigonometry/acosh.js'
202203
import { acotDocs } from './function/trigonometry/acot.js'
@@ -543,6 +544,7 @@ export const embeddedDocs = {
543544
std: stdDocs,
544545
sum: sumDocs,
545546
variance: varianceDocs,
547+
corr: corrDocs,
546548

547549
// functions - trigonometry
548550
acos: acosDocs,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const corrDocs = {
2+
name: 'corr',
3+
category: 'Statistics',
4+
syntax: [
5+
'corr(A,B)'
6+
],
7+
description: 'Compute the correlation coefficient of a two list with values, For matrices, the matrix correlation coefficient is calculated.',
8+
examples: [
9+
'corr([2, 4, 6, 8],[1, 2, 3, 6])',
10+
'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]]))'
11+
],
12+
seealso: [
13+
'max',
14+
'mean',
15+
'min',
16+
'median',
17+
'min',
18+
'prod',
19+
'std',
20+
'sum'
21+
]
22+
}

src/factoriesAny.js

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ export { createMad } from './function/statistics/mad.js'
237237
export { createVariance } from './function/statistics/variance.js'
238238
export { createQuantileSeq } from './function/statistics/quantileSeq.js'
239239
export { createStd } from './function/statistics/std.js'
240+
export { createCorr } from './function/statistics/corr.js'
240241
export { createCombinations } from './function/probability/combinations.js'
241242
export { createCombinationsWithRep } from './function/probability/combinationsWithRep.js'
242243
export { createGamma } from './function/probability/gamma.js'

src/factoriesNumber.js

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export { createMad } from './function/statistics/mad.js'
263263
export { createVariance } from './function/statistics/variance.js'
264264
export { createQuantileSeq } from './function/statistics/quantileSeq.js'
265265
export { createStd } from './function/statistics/std.js'
266+
export { createCorr } from './function/statistics/corr.js'
266267

267268
// string
268269
export { createFormat } from './function/string/format.js'

src/function/statistics/corr.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { factory } from '../../utils/factory.js'
2+
const name = 'corr'
3+
const dependencies = ['typed', 'matrix', 'mean', 'sqrt', 'sum', 'add', 'subtract', 'multiply', 'pow', 'divide']
4+
5+
export const createCorr = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, sqrt, sum, add, subtract, multiply, pow, divide }) => {
6+
/**
7+
* Compute the correlation coefficient of a two list with values, For matrices, the matrix correlation coefficient is calculated.
8+
*
9+
* Syntax:
10+
*
11+
* math.corr(A, B)
12+
*
13+
* Examples:
14+
*
15+
* math.corr([1, 2, 3, 4, 5], [4, 5, 6, 7, 8]) // returns 1
16+
* math.corr([1, 2.2, 3, 4.8, 5], [4, 5.3, 6.6, 7, 8]) // returns 0.9569941688503644
17+
* 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]
18+
*
19+
* See also:
20+
*
21+
* median, mean, min, max, sum, prod, std, variance
22+
*
23+
* @param {Array | Matrix} A The first array or matrix to compute correlation coefficient
24+
* @param {Array | Matrix} B The second array or matrix to compute correlation coefficient
25+
* @return {*} The correlation coefficient
26+
*/
27+
return typed(name, {
28+
'Array, Array': function (A, B) {
29+
return _corr(A, B)
30+
},
31+
'Matrix, Matrix': function (xMatrix, yMatrix) {
32+
return matrix(_corr(xMatrix.toArray(), yMatrix.toArray()))
33+
}
34+
})
35+
/**
36+
* Calculate the correlation coefficient between two arrays or matrices.
37+
* @param {Array | Matrix} A
38+
* @param {Array | Matrix} B
39+
* @return {*} correlation coefficient
40+
* @private
41+
*/
42+
function _corr (A, B) {
43+
if (Array.isArray(A[0]) && Array.isArray(B[0])) {
44+
const correlations = []
45+
for (let i = 0; i < A.length; i++) {
46+
correlations.push(correlation(A[i], B[i]))
47+
}
48+
return correlations
49+
} else {
50+
return correlation(A, B)
51+
}
52+
}
53+
function correlation (A, B) {
54+
const n = A.length
55+
const sumX = sum(A)
56+
const sumY = sum(B)
57+
const sumXY = A.reduce((acc, x, index) => add(acc, multiply(x, B[index])), 0)
58+
const sumXSquare = sum(A.map(x => pow(x, 2)))
59+
const sumYSquare = sum(B.map(y => pow(y, 2)))
60+
const numerator = subtract(multiply(n, sumXY), multiply(sumX, sumY))
61+
const denominator = sqrt(multiply(subtract(multiply(n, sumXSquare), pow(sumX, 2)), subtract(multiply(n, sumYSquare), pow(sumY, 2))))
62+
return divide(numerator, denominator)
63+
}
64+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import assert from 'assert'
2+
import math from '../../../../src/defaultInstance.js'
3+
const corr = math.corr
4+
const BigNumber = math.BigNumber
5+
6+
describe('correlation', function () {
7+
it('should return the correlation coefficient from an array', function () {
8+
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)
9+
assert.strictEqual(corr([1, 2, 3, 4, 5], [4, 5, 6, 7, 8]), 1)
10+
assert.strictEqual(corr([1, 2.2, 3, 4.8, 5], [4, 5.3, 6.6, 7, 8]), 0.9569941688503644)
11+
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])
12+
})
13+
14+
it('should throw an error if called with invalid number of arguments', function () {
15+
assert.throws(function () { corr() })
16+
})
17+
18+
it('should throw an error if called with an empty array', function () {
19+
assert.throws(function () { corr([]) })
20+
})
21+
})

types/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -2970,6 +2970,14 @@ declare namespace math {
29702970
normalization: 'unbiased' | 'uncorrected' | 'biased'
29712971
): MathNumericType
29722972

2973+
/**
2974+
* Calculate the correlation coefficient between two matrix.
2975+
* @param {Array | Matrix} x The first array or matrix to compute correlation coefficient
2976+
* @param {Array | Matrix} y The second array or matrix to compute correlation coefficient
2977+
* @returns correlation coefficient
2978+
*/
2979+
corr(x: MathCollection, y: MathCollection): MathType
2980+
29732981
/*************************************************************************
29742982
* String functions
29752983
************************************************************************/

0 commit comments

Comments
 (0)