Skip to content

Commit ee34336

Browse files
authored
Correlation function for discrete variable correlation (#7326)
* Correlation function for discrete variable correlation * Add unit tests for Correlation class Added unit tests for the Correlation class to validate correlation calculations under various scenarios, including linear dependence and constant values. * Added missing bracket * Refactor variable initialization in correlation method * Remove unused imports and clean up CorrelationTest * Fix formatting of variable declarations in correlation method * Update Correlation.java * Fix formatting in CorrelationTest.java * Enhance comments in correlation function Added detailed comments to the correlation function for better understanding. * Add correlation tests for various scenarios * Format comments for clarity in correlation method * Fix formatting and comments in correlation method
1 parent d2744b5 commit ee34336

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* Class for correlation of two discrete variables
5+
*/
6+
7+
public final class Correlation {
8+
private Correlation() {
9+
}
10+
11+
public static final double DELTA = 1e-9;
12+
13+
/**
14+
* Discrete correlation function.
15+
* Correlation between two discrete variables is calculated
16+
* according to the formula: Cor(x, y)=Cov(x, y)/sqrt(Var(x)*Var(y)).
17+
* Correlation with a constant variable is taken to be zero.
18+
*
19+
* @param x The first discrete variable
20+
* @param y The second discrete variable
21+
* @param n The number of values for each variable
22+
* @return The result of the correlation of variables x,y.
23+
*/
24+
public static double correlation(double[] x, double[] y, int n) {
25+
double exy = 0; // E(XY)
26+
double ex = 0; // E(X)
27+
double exx = 0; // E(X^2)
28+
double ey = 0; // E(Y)
29+
double eyy = 0; // E(Y^2)
30+
for (int i = 0; i < n; i++) {
31+
exy += x[i] * y[i];
32+
ex += x[i];
33+
exx += x[i] * x[i];
34+
ey += y[i];
35+
eyy += y[i] * y[i];
36+
}
37+
exy /= n;
38+
ex /= n;
39+
exx /= n;
40+
ey /= n;
41+
eyy /= n;
42+
double cov = exy - ex * ey; // Cov(X, Y) = E(XY)-E(X)E(Y)
43+
double varx = Math.sqrt(exx - ex * ex); // Var(X) = sqrt(E(X^2)-E(X)^2)
44+
double vary = Math.sqrt(eyy - ey * ey); // Var(Y) = sqrt(E(Y^2)-E(Y)^2)
45+
if (varx * vary < DELTA) { // Var(X) = 0 means X = const, the same about Y
46+
return 0;
47+
} else {
48+
return cov / Math.sqrt(varx * vary);
49+
}
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
/**
6+
* Test class for Correlation class
7+
*/
8+
public class CorrelationTest {
9+
10+
public static final double DELTA = 1e-9;
11+
12+
// Regular correlation test
13+
public void testCorrelationFirst() {
14+
double[] x = {1, 2, 3, 4};
15+
double[] y = {7, 1, 4, 9};
16+
int n = 4;
17+
assertEquals(0.3319700011, Correlation.correlation(x, y, n), DELTA);
18+
}
19+
20+
// Regular correlation test (zero correlation)
21+
public void testCorrelationSecond() {
22+
double[] x = {1, 2, 3, 4};
23+
double[] y = {5, 0, 9, 2};
24+
int n = 4;
25+
assertEquals(0, Correlation.correlation(x, y, n), DELTA);
26+
}
27+
28+
// Correlation with a constant variable is taken to be zero
29+
public void testCorrelationConstant() {
30+
double[] x = {1, 2, 3};
31+
double[] y = {4, 4, 4};
32+
int n = 3;
33+
assertEquals(0, Correlation.correlation(x, y, n), DELTA);
34+
}
35+
36+
// Linear dependence gives correlation 1
37+
public void testCorrelationLinearDependence() {
38+
double[] x = {1, 2, 3, 4};
39+
double[] y = {6, 8, 10, 12};
40+
int n = 4;
41+
assertEquals(1, Correlation.correlation(x, y, n), DELTA);
42+
}
43+
44+
// Inverse linear dependence gives correlation -1
45+
public void testCorrelationInverseLinearDependence() {
46+
double[] x = {1, 2, 3, 4, 5};
47+
double[] y = {18, 15, 12, 9, 6};
48+
int n = 5;
49+
assertEquals(-1, Correlation.correlation(x, y, n), DELTA);
50+
}
51+
}

0 commit comments

Comments
 (0)