|
| 1 | +package com.thealgorithms.maths; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.params.ParameterizedTest; |
| 6 | +import org.junit.jupiter.params.provider.CsvSource; |
| 7 | + |
| 8 | +/** |
| 9 | + * Test class for Correlation class |
| 10 | + */ |
| 11 | +public class CorrelationTest { |
| 12 | + |
| 13 | + public static final double DELTA = 1e-9; |
| 14 | + |
| 15 | + public void testCorrelationFirst() { |
| 16 | + double[] x = {1, 2, 3, 4}; |
| 17 | + double[] y = {7, 1, 4, 9}; |
| 18 | + int n = 4; |
| 19 | + assertEquals(0.3319700011, Correlation.correlation(x, y, n), DELTA); |
| 20 | + } |
| 21 | + |
| 22 | + public void testCorrelationSecond() { |
| 23 | + double[] x = {1, 2, 3, 4}; |
| 24 | + double[] y = {5, 0, 9, 2}; |
| 25 | + int n = 4; |
| 26 | + assertEquals(0, Correlation.correlation(x, y, n), DELTA); |
| 27 | + } |
| 28 | + |
| 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 | + public void testCorrelationLinearDependence() { |
| 36 | + double[] x = {1, 2, 3, 4}; |
| 37 | + double[] y = {6, 8, 10, 12}; |
| 38 | + int n = 4; |
| 39 | + assertEquals(1, Correlation.correlation(x, y, n), DELTA); |
| 40 | + } |
| 41 | + |
| 42 | + public void testCorrelationInverseLinearDependence() { |
| 43 | + double[] x = {1, 2, 3, 4, 5}; |
| 44 | + double[] y = {18, 15, 12, 9, 6}; |
| 45 | + int n = 5; |
| 46 | + assertEquals(-1, Correlation.correlation(x, y, n), DELTA); |
| 47 | + } |
| 48 | +} |
0 commit comments