Skip to content

Commit 444bd9e

Browse files
authored
Merge pull request #94 from cicirello/development
Added test cases to improve test coverage
2 parents fe6bf59 + ef8e93c commit 444bd9e

File tree

3 files changed

+140
-9
lines changed

3 files changed

+140
-9
lines changed

CHANGELOG.md

+16-6
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,36 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased] - 2021-02-13
7+
## [Unreleased] - 2021-02-15
88

99
### Added
1010

11+
### Changed
12+
13+
### Deprecated
14+
15+
### Removed
16+
17+
### Fixed
18+
19+
### CI/CD
20+
21+
22+
## [2.4.0] - 2021-02-15
23+
1124
### Changed
1225
* Minor optimizations to ReversalDistance and ReinsertionDistance.
1326
* Refactored the RandomVariates.nextCauchy methods to remove redundancy.
1427
* Refactored org.cicirello.math.rand.BTPE to remove redundancy.
1528
* Refactored various methods of RandomIndexer to remove redundancy.
1629
* Minor optimizations to RandomIndexer.sample methods.
1730

18-
### Deprecated
19-
20-
### Removed
21-
2231
### Fixed
2332
* Fixed large magnitude negative input case in MathFunctions.logGamma.
33+
* Fixed but in JacobiDiagonalization in case when default epsilon is not used.
2434

2535
### CI/CD
26-
* Test cases added to improve test coverage.
36+
* Added test cases to improve test coverage to 100%.
2737

2838

2939
## [2.3.0] - 2021-01-30

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Central since version 2.1.2).
8080
<dependency>
8181
<groupId>org.cicirello</groupId>
8282
<artifactId>jpt</artifactId>
83-
<version>2.2.0</version>
83+
<version>2.4.0</version>
8484
</dependency>
8585
```
8686

tests/org/cicirello/math/stats/StatisticsTests.java

+123-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Vincent A. Cicirello, <https://www.cicirello.org/>.
2+
* Copyright 2018, 2021 Vincent A. Cicirello, <https://www.cicirello.org/>.
33
*
44
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
55
*
@@ -168,6 +168,10 @@ public void testCovarianceOfInts() {
168168
assertEquals("covariance of X with itself", Statistics.variance(data), Statistics.covariance(data, data.clone()), EPSILON);
169169
assertEquals("covariance of X with itself", 3*4*Statistics.variance(data), Statistics.covariance(data2, data3), EPSILON);
170170
}
171+
IllegalArgumentException thrown = assertThrows(
172+
IllegalArgumentException.class,
173+
() -> Statistics.covariance(new int[3], new int[4])
174+
);
171175
}
172176

173177
@Test
@@ -192,7 +196,11 @@ public void testCovarianceOfDoubles() {
192196
// assumes variance method already tested... uses it in expected result
193197
assertEquals("covariance of X with itself", Statistics.variance(data), Statistics.covariance(data, data.clone()), EPSILON);
194198
assertEquals("covariance of X with itself", 3.3*1.4*Statistics.variance(data), Statistics.covariance(data2, data3), EPSILON);
195-
}
199+
}
200+
IllegalArgumentException thrown = assertThrows(
201+
IllegalArgumentException.class,
202+
() -> Statistics.covariance(new double[3], new double[4])
203+
);
196204
}
197205

198206

@@ -218,6 +226,12 @@ public void testCorrelationOfInts() {
218226
}
219227
assertEquals("Y = -alpha * X", -1.0, Statistics.correlation(x, y), EPSILON);
220228
}
229+
int[] x = { 2, 2, 2 };
230+
int[] y = { 2, 1, 3 };
231+
assertEquals(0.0, Statistics.correlation(x, y), 0.0);
232+
assertEquals(0.0, Statistics.correlation(y, x), 0.0);
233+
int[] z = { 4, 1, 1 };
234+
assertEquals(0.0, Statistics.correlation(y, z), 0.0);
221235
}
222236

223237
@Test
@@ -242,9 +256,116 @@ public void testCorrelationOfDoubles() {
242256
}
243257
assertEquals("Y = -alpha * X", -1.0, Statistics.correlation(x, y), EPSILON);
244258
}
259+
double[] x = { 2, 2, 2 };
260+
double[] y = { 2, 1, 3 };
261+
assertEquals(0.0, Statistics.correlation(x, y), 0.0);
262+
assertEquals(0.0, Statistics.correlation(y, x), 0.0);
263+
double[] z = { 4, 1, 1 };
264+
assertEquals(0.0, Statistics.correlation(y, z), 0.0);
245265
}
246266

267+
@Test
268+
public void testCorrelationMatrixOfInts() {
269+
int[][] data = {
270+
{2, 1, 3},
271+
{6, 3, 9},
272+
{4, 1, 1},
273+
{2, 3, 1},
274+
{4, 6, 2}
275+
};
276+
double[][] M = Statistics.correlationMatrix(data);
277+
assertEquals(data.length, M.length);
278+
assertEquals(data.length, M[0].length);
279+
double[][] expected = {
280+
{1.0, 1.0, 0.0, -1.0, -1.0},
281+
{1.0, 1.0, 0.0, -1.0, -1.0},
282+
{0.0, 0.0, 1.0, 0.0, 0.0},
283+
{-1.0, -1.0, 0.0, 1.0, 1.0},
284+
{-1.0, -1.0, 0.0, 1.0, 1.0}
285+
};
286+
for (int i = 0; i < M.length; i++) {
287+
for (int j = 0; j < M.length; j++) {
288+
assertEquals(expected[i][j], M[i][j], EPSILON);
289+
}
290+
}
291+
}
247292

293+
@Test
294+
public void testCorrelationMatrixOfDoubles() {
295+
double[][] data = {
296+
{2, 1, 3},
297+
{6, 3, 9},
298+
{4, 1, 1},
299+
{2, 3, 1},
300+
{4, 6, 2}
301+
};
302+
double[][] M = Statistics.correlationMatrix(data);
303+
assertEquals(data.length, M.length);
304+
assertEquals(data.length, M[0].length);
305+
double[][] expected = {
306+
{1.0, 1.0, 0.0, -1.0, -1.0},
307+
{1.0, 1.0, 0.0, -1.0, -1.0},
308+
{0.0, 0.0, 1.0, 0.0, 0.0},
309+
{-1.0, -1.0, 0.0, 1.0, 1.0},
310+
{-1.0, -1.0, 0.0, 1.0, 1.0}
311+
};
312+
for (int i = 0; i < M.length; i++) {
313+
for (int j = 0; j < M.length; j++) {
314+
assertEquals(expected[i][j], M[i][j], EPSILON);
315+
}
316+
}
317+
}
248318

319+
@Test
320+
public void testWelchsTTestDoubles() {
321+
double[] a1 = {27.5, 21.0, 19.0, 23.6, 17.0, 17.9,
322+
16.9, 20.1, 21.9, 22.6, 23.1, 19.6, 19.0, 21.7, 21.4};
323+
double[] a2 = {27.1, 22.0, 20.8, 23.4, 23.4, 23.5,
324+
25.8, 22.0, 24.8, 20.2, 21.9, 22.1, 22.9, 20.5, 24.4};
325+
assertEquals(-2.46, Statistics.tTestUnequalVariances(a1, a2), 0.01);
326+
assertEquals(2.46, Statistics.tTestUnequalVariances(a2, a1), 0.01);
327+
double[] b1 = {17.2, 20.9, 22.6, 18.1, 21.7, 21.4, 23.5, 24.2, 14.7, 21.8};
328+
double[] b2 = {21.5, 22.8, 21.0, 23.0, 21.6, 23.6, 22.5, 20.7, 23.4, 21.8,
329+
20.7, 21.7, 21.5, 22.5, 23.6, 21.5, 22.5, 23.5, 21.5, 21.8};
330+
assertEquals(-1.57, Statistics.tTestUnequalVariances(b1, b2), 0.01);
331+
assertEquals(1.57, Statistics.tTestUnequalVariances(b2, b1), 0.01);
332+
333+
Number[] result = Statistics.tTestWelch(a1, a2);
334+
assertEquals(-2.46, (Double)result[0], 0.01);
335+
assertEquals(24, ((Integer)result[1]).intValue());
336+
result = Statistics.tTestWelch(a2, a1);
337+
assertEquals(2.46, (Double)result[0], 0.01);
338+
assertEquals(24, ((Integer)result[1]).intValue());
339+
result = Statistics.tTestWelch(b1, b2);
340+
assertEquals(-1.57, (Double)result[0], 0.01);
341+
assertEquals(9, ((Integer)result[1]).intValue());
342+
result = Statistics.tTestWelch(b2, b1);
343+
assertEquals(1.57, (Double)result[0], 0.01);
344+
assertEquals(9, ((Integer)result[1]).intValue());
345+
}
249346

347+
@Test
348+
public void testWelchsTTestInts() {
349+
int[] a1 = {2, 4, 7, 1, 8, 10, 11, 6, 2, 9, 1, 23, 12, 0, 7};
350+
int[] a2 = {17, 2, 8, 19, 12, 4, 2, 0, 22, 7, 7, 1, 9, 12, 13};
351+
assertEquals(-0.9173, Statistics.tTestUnequalVariances(a1, a2), 0.0001);
352+
assertEquals(0.9173, Statistics.tTestUnequalVariances(a2, a1), 0.0001);
353+
int[] b1 = {2, 4, 7, 1, 8, 10, 11, 6, 2, 9};
354+
int[] b2 = {17, 2, 8, 19, 12, 4, 2, 0, 22, 7, 7, 1, 9, 12, 13, 0, 14, 1, 6, 30};
355+
assertEquals(-1.5369, Statistics.tTestUnequalVariances(b1, b2), 0.0001);
356+
assertEquals(1.5369, Statistics.tTestUnequalVariances(b2, b1), 0.0001);
357+
358+
Number[] result = Statistics.tTestWelch(a1, a2);
359+
assertEquals(-0.9173, (Double)result[0], 0.0001);
360+
assertEquals(27, ((Integer)result[1]).intValue());
361+
result = Statistics.tTestWelch(a2, a1);
362+
assertEquals(0.9173, (Double)result[0], 0.0001);
363+
assertEquals(27, ((Integer)result[1]).intValue());
364+
result = Statistics.tTestWelch(b1, b2);
365+
assertEquals(-1.5369, (Double)result[0], 0.0001);
366+
assertEquals(27, ((Integer)result[1]).intValue());
367+
result = Statistics.tTestWelch(b2, b1);
368+
assertEquals(1.5369, (Double)result[0], 0.0001);
369+
assertEquals(27, ((Integer)result[1]).intValue());
370+
}
250371
}

0 commit comments

Comments
 (0)