From 6baf77d51af32f847345c471cfafea3630fabb7d Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:01:48 +0200 Subject: [PATCH 01/12] Correlation function for discrete variable correlation --- .../com/thealgorithms/maths/Correlation.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/Correlation.java diff --git a/src/main/java/com/thealgorithms/maths/Correlation.java b/src/main/java/com/thealgorithms/maths/Correlation.java new file mode 100644 index 000000000000..310ea124495e --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/Correlation.java @@ -0,0 +1,46 @@ +package com.thealgorithms.maths; + +/** + * Class for correlation of two discrete variables + */ + +public final class Correlation { + private Correlation() { + } + + public static final double DELTA = 1e-9; + + /** + * Discrete correlation function. + * Correlation between two discrete variables is calculated. + * Correlation with a constant variable is taken to be zero. + * + * @param x The first discrete variable + * @param y The second discrete variable + * @param n The number of values for each variable + * @return The result of the correlation of variables x,y. + */ + public static double correlation(double[] x, double[] y, int n) { + double exy = 0, ex = 0, exx = 0, ey = 0, eyy = 0; + for (int i = 0; i < n; i++) { + exy += x[i] * y[i]; + ex += x[i]; + exx += x[i] * x[i]; + ey += y[i]; + eyy += y[i] * y[i]; + } + exy /= n; + ex /= n; + exx /= n; + ey /= n; + eyy /= n; + double cov = exy - ex * ey; + double varx = Math.sqrt(exx - ex * ex); + double vary = Math.sqrt(eyy - ey * ey); + if (varx * vary < DELTA) { + return 0; + } else { + return cov / Math.sqrt(varx * vary); + } + } +} From 62cc3f42839eb77bae07d6338b17db2c184509a9 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:06:20 +0200 Subject: [PATCH 02/12] 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. --- .../thealgorithms/maths/CorrelationTest.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/test/java/com/thealgorithms/maths/CorrelationTest.java diff --git a/src/test/java/com/thealgorithms/maths/CorrelationTest.java b/src/test/java/com/thealgorithms/maths/CorrelationTest.java new file mode 100644 index 000000000000..899391fcf7d0 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/CorrelationTest.java @@ -0,0 +1,48 @@ +package com.thealgorithms.maths; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +/** + * Test class for Correlation class + */ +public class CorrelationTest { + + public static final double DELTA = 1e-9; + + public void testCorrelationFirst() { + double[] x = {1, 2, 3, 4}; + double[] y = {7, 1, 4, 9}; + int n = 4; + assertEquals(0.3319700011, Correlation.correlation(x, y, n), DELTA); + } + + public void testCorrelationSecond() { + double[] x = {1, 2, 3, 4}; + double[] y = {5, 0, 9, 2}; + int n = 4; + assertEquals(0, Correlation.correlation(x, y, n), DELTA); + } + + public void testCorrelationConstant() { + double[] x = {1, 2, 3}; + double[] y = {4, 4, 4}; + int n = 3; + assertEquals(0, Correlation.correlation(x, y, n), DELTA); + + public void testCorrelationLinearDependence() { + double[] x = {1, 2, 3, 4}; + double[] y = {6, 8, 10, 12}; + int n = 4; + assertEquals(1, Correlation.correlation(x, y, n), DELTA); + } + + public void testCorrelationInverseLinearDependence() { + double[] x = {1, 2, 3, 4, 5}; + double[] y = {18, 15, 12, 9, 6}; + int n = 5; + assertEquals(-1, Correlation.correlation(x, y, n), DELTA); + } +} From e88fd72c6f1722d6df63095255b257b7503710bf Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:14:10 +0200 Subject: [PATCH 03/12] Added missing bracket --- src/test/java/com/thealgorithms/maths/CorrelationTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/maths/CorrelationTest.java b/src/test/java/com/thealgorithms/maths/CorrelationTest.java index 899391fcf7d0..23bfc9832340 100644 --- a/src/test/java/com/thealgorithms/maths/CorrelationTest.java +++ b/src/test/java/com/thealgorithms/maths/CorrelationTest.java @@ -31,6 +31,7 @@ public void testCorrelationConstant() { double[] y = {4, 4, 4}; int n = 3; assertEquals(0, Correlation.correlation(x, y, n), DELTA); + } public void testCorrelationLinearDependence() { double[] x = {1, 2, 3, 4}; From b36660720507a33a7db1757abab9c16fb70356bf Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:22:20 +0200 Subject: [PATCH 04/12] Refactor variable initialization in correlation method --- src/main/java/com/thealgorithms/maths/Correlation.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/Correlation.java b/src/main/java/com/thealgorithms/maths/Correlation.java index 310ea124495e..7c04a4076278 100644 --- a/src/main/java/com/thealgorithms/maths/Correlation.java +++ b/src/main/java/com/thealgorithms/maths/Correlation.java @@ -21,7 +21,11 @@ private Correlation() { * @return The result of the correlation of variables x,y. */ public static double correlation(double[] x, double[] y, int n) { - double exy = 0, ex = 0, exx = 0, ey = 0, eyy = 0; + double exy = 0; + double ex = 0; + double exx = 0; + double ey = 0; + double eyy = 0; for (int i = 0; i < n; i++) { exy += x[i] * y[i]; ex += x[i]; From 1ef23b21e81135371b8ffbf84daff04e14547ada Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:23:38 +0200 Subject: [PATCH 05/12] Remove unused imports and clean up CorrelationTest --- src/test/java/com/thealgorithms/maths/CorrelationTest.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/CorrelationTest.java b/src/test/java/com/thealgorithms/maths/CorrelationTest.java index 23bfc9832340..363bf854b154 100644 --- a/src/test/java/com/thealgorithms/maths/CorrelationTest.java +++ b/src/test/java/com/thealgorithms/maths/CorrelationTest.java @@ -2,9 +2,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; - /** * Test class for Correlation class */ @@ -18,13 +15,13 @@ public void testCorrelationFirst() { int n = 4; assertEquals(0.3319700011, Correlation.correlation(x, y, n), DELTA); } - + public void testCorrelationSecond() { double[] x = {1, 2, 3, 4}; double[] y = {5, 0, 9, 2}; int n = 4; assertEquals(0, Correlation.correlation(x, y, n), DELTA); - } + } public void testCorrelationConstant() { double[] x = {1, 2, 3}; From 48f837caa4676e230f0f668aabd369a1b4928f0a Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:30:06 +0200 Subject: [PATCH 06/12] Fix formatting of variable declarations in correlation method --- src/main/java/com/thealgorithms/maths/Correlation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Correlation.java b/src/main/java/com/thealgorithms/maths/Correlation.java index 7c04a4076278..fcd99b40b1ff 100644 --- a/src/main/java/com/thealgorithms/maths/Correlation.java +++ b/src/main/java/com/thealgorithms/maths/Correlation.java @@ -21,8 +21,8 @@ private Correlation() { * @return The result of the correlation of variables x,y. */ public static double correlation(double[] x, double[] y, int n) { - double exy = 0; - double ex = 0; + double exy = 0; + double ex = 0; double exx = 0; double ey = 0; double eyy = 0; From df97f4cde356d983563af9ee1de5e255b45fd9cf Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:36:12 +0200 Subject: [PATCH 07/12] Update Correlation.java --- src/main/java/com/thealgorithms/maths/Correlation.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Correlation.java b/src/main/java/com/thealgorithms/maths/Correlation.java index fcd99b40b1ff..d3081bec2563 100644 --- a/src/main/java/com/thealgorithms/maths/Correlation.java +++ b/src/main/java/com/thealgorithms/maths/Correlation.java @@ -22,10 +22,10 @@ private Correlation() { */ public static double correlation(double[] x, double[] y, int n) { double exy = 0; - double ex = 0; - double exx = 0; - double ey = 0; - double eyy = 0; + double ex = 0; + double exx = 0; + double ey = 0; + double eyy = 0; for (int i = 0; i < n; i++) { exy += x[i] * y[i]; ex += x[i]; From e037c7b19901dc5d040d88279d271d465df05509 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:47:17 +0200 Subject: [PATCH 08/12] Fix formatting in CorrelationTest.java --- src/test/java/com/thealgorithms/maths/CorrelationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/maths/CorrelationTest.java b/src/test/java/com/thealgorithms/maths/CorrelationTest.java index 363bf854b154..e5b810840283 100644 --- a/src/test/java/com/thealgorithms/maths/CorrelationTest.java +++ b/src/test/java/com/thealgorithms/maths/CorrelationTest.java @@ -28,7 +28,7 @@ public void testCorrelationConstant() { double[] y = {4, 4, 4}; int n = 3; assertEquals(0, Correlation.correlation(x, y, n), DELTA); - } + } public void testCorrelationLinearDependence() { double[] x = {1, 2, 3, 4}; From 832ae4976ce017c46d6baa177b653f34a7e0cf06 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Sun, 22 Mar 2026 15:18:06 +0200 Subject: [PATCH 09/12] Enhance comments in correlation function Added detailed comments to the correlation function for better understanding. --- .../com/thealgorithms/maths/Correlation.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Correlation.java b/src/main/java/com/thealgorithms/maths/Correlation.java index d3081bec2563..ea753934c849 100644 --- a/src/main/java/com/thealgorithms/maths/Correlation.java +++ b/src/main/java/com/thealgorithms/maths/Correlation.java @@ -12,7 +12,8 @@ private Correlation() { /** * Discrete correlation function. - * Correlation between two discrete variables is calculated. + * Correlation between two discrete variables is calculated + * according to the formula: Cor(x, y)=Cov(x, y)/sqrt(Var(x)*Var(y)). * Correlation with a constant variable is taken to be zero. * * @param x The first discrete variable @@ -21,11 +22,11 @@ private Correlation() { * @return The result of the correlation of variables x,y. */ public static double correlation(double[] x, double[] y, int n) { - double exy = 0; - double ex = 0; - double exx = 0; - double ey = 0; - double eyy = 0; + double exy = 0; //E(XY) + double ex = 0; //E(X) + double exx = 0; //E(X^2) + double ey = 0; //E(Y) + double eyy = 0; //E(Y^2) for (int i = 0; i < n; i++) { exy += x[i] * y[i]; ex += x[i]; @@ -38,10 +39,10 @@ public static double correlation(double[] x, double[] y, int n) { exx /= n; ey /= n; eyy /= n; - double cov = exy - ex * ey; - double varx = Math.sqrt(exx - ex * ex); - double vary = Math.sqrt(eyy - ey * ey); - if (varx * vary < DELTA) { + double cov = exy - ex * ey; //Cov(X, Y) = E(XY)-E(X)E(Y) + double varx = Math.sqrt(exx - ex * ex); //Var(X) = sqrt(E(X^2)-E(X)^2) + double vary = Math.sqrt(eyy - ey * ey); //Var(Y) = sqrt(E(Y^2)-E(Y)^2) + if (varx * vary < DELTA) { //Var(X) = 0 means X = const, the same about Y return 0; } else { return cov / Math.sqrt(varx * vary); From 1b35e4b0d3ea68a7d2a138570e1ba5fd72d62b96 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Sun, 22 Mar 2026 15:21:39 +0200 Subject: [PATCH 10/12] Add correlation tests for various scenarios --- src/test/java/com/thealgorithms/maths/CorrelationTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/com/thealgorithms/maths/CorrelationTest.java b/src/test/java/com/thealgorithms/maths/CorrelationTest.java index e5b810840283..96867d56ad5e 100644 --- a/src/test/java/com/thealgorithms/maths/CorrelationTest.java +++ b/src/test/java/com/thealgorithms/maths/CorrelationTest.java @@ -9,6 +9,7 @@ public class CorrelationTest { public static final double DELTA = 1e-9; + // Regular correlation test public void testCorrelationFirst() { double[] x = {1, 2, 3, 4}; double[] y = {7, 1, 4, 9}; @@ -16,6 +17,7 @@ public void testCorrelationFirst() { assertEquals(0.3319700011, Correlation.correlation(x, y, n), DELTA); } + // Regular correlation test (zero correlation) public void testCorrelationSecond() { double[] x = {1, 2, 3, 4}; double[] y = {5, 0, 9, 2}; @@ -23,6 +25,7 @@ public void testCorrelationSecond() { assertEquals(0, Correlation.correlation(x, y, n), DELTA); } + // Correlation with a constant variable is taken to be zero public void testCorrelationConstant() { double[] x = {1, 2, 3}; double[] y = {4, 4, 4}; @@ -30,6 +33,7 @@ public void testCorrelationConstant() { assertEquals(0, Correlation.correlation(x, y, n), DELTA); } + // Linear dependence gives correlation 1 public void testCorrelationLinearDependence() { double[] x = {1, 2, 3, 4}; double[] y = {6, 8, 10, 12}; @@ -37,6 +41,7 @@ public void testCorrelationLinearDependence() { assertEquals(1, Correlation.correlation(x, y, n), DELTA); } + // Inverse linear dependence gives correlation -1 public void testCorrelationInverseLinearDependence() { double[] x = {1, 2, 3, 4, 5}; double[] y = {18, 15, 12, 9, 6}; From 82ddaa25569dfe295f0ef8b60e89efa288896461 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Sun, 22 Mar 2026 15:25:44 +0200 Subject: [PATCH 11/12] Format comments for clarity in correlation method --- .../com/thealgorithms/maths/Correlation.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Correlation.java b/src/main/java/com/thealgorithms/maths/Correlation.java index ea753934c849..c16676f6e5c2 100644 --- a/src/main/java/com/thealgorithms/maths/Correlation.java +++ b/src/main/java/com/thealgorithms/maths/Correlation.java @@ -22,11 +22,11 @@ private Correlation() { * @return The result of the correlation of variables x,y. */ public static double correlation(double[] x, double[] y, int n) { - double exy = 0; //E(XY) - double ex = 0; //E(X) - double exx = 0; //E(X^2) - double ey = 0; //E(Y) - double eyy = 0; //E(Y^2) + double exy = 0; // E(XY) + double ex = 0; // E(X) + double exx = 0; // E(X^2) + double ey = 0; // E(Y) + double eyy = 0; // E(Y^2) for (int i = 0; i < n; i++) { exy += x[i] * y[i]; ex += x[i]; @@ -39,9 +39,9 @@ public static double correlation(double[] x, double[] y, int n) { exx /= n; ey /= n; eyy /= n; - double cov = exy - ex * ey; //Cov(X, Y) = E(XY)-E(X)E(Y) - double varx = Math.sqrt(exx - ex * ex); //Var(X) = sqrt(E(X^2)-E(X)^2) - double vary = Math.sqrt(eyy - ey * ey); //Var(Y) = sqrt(E(Y^2)-E(Y)^2) + double cov = exy - ex * ey; // Cov(X, Y) = E(XY)-E(X)E(Y) + double varx = Math.sqrt(exx - ex * ex); // Var(X) = sqrt(E(X^2)-E(X)^2) + double vary = Math.sqrt(eyy - ey * ey); // Var(Y) = sqrt(E(Y^2)-E(Y)^2) if (varx * vary < DELTA) { //Var(X) = 0 means X = const, the same about Y return 0; } else { From 3efcd172137ab776e647e5f67831d972485395b1 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Sun, 22 Mar 2026 15:28:06 +0200 Subject: [PATCH 12/12] Fix formatting and comments in correlation method --- src/main/java/com/thealgorithms/maths/Correlation.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Correlation.java b/src/main/java/com/thealgorithms/maths/Correlation.java index c16676f6e5c2..a46445fb23b7 100644 --- a/src/main/java/com/thealgorithms/maths/Correlation.java +++ b/src/main/java/com/thealgorithms/maths/Correlation.java @@ -23,9 +23,9 @@ private Correlation() { */ public static double correlation(double[] x, double[] y, int n) { double exy = 0; // E(XY) - double ex = 0; // E(X) + double ex = 0; // E(X) double exx = 0; // E(X^2) - double ey = 0; // E(Y) + double ey = 0; // E(Y) double eyy = 0; // E(Y^2) for (int i = 0; i < n; i++) { exy += x[i] * y[i]; @@ -39,10 +39,10 @@ public static double correlation(double[] x, double[] y, int n) { exx /= n; ey /= n; eyy /= n; - double cov = exy - ex * ey; // Cov(X, Y) = E(XY)-E(X)E(Y) + double cov = exy - ex * ey; // Cov(X, Y) = E(XY)-E(X)E(Y) double varx = Math.sqrt(exx - ex * ex); // Var(X) = sqrt(E(X^2)-E(X)^2) double vary = Math.sqrt(eyy - ey * ey); // Var(Y) = sqrt(E(Y^2)-E(Y)^2) - if (varx * vary < DELTA) { //Var(X) = 0 means X = const, the same about Y + if (varx * vary < DELTA) { // Var(X) = 0 means X = const, the same about Y return 0; } else { return cov / Math.sqrt(varx * vary);