1
1
/*
2
- * Copyright 2018 Vincent A. Cicirello, <https://www.cicirello.org/>.
2
+ * Copyright 2018, 2021 Vincent A. Cicirello, <https://www.cicirello.org/>.
3
3
*
4
4
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
5
5
*
@@ -168,6 +168,10 @@ public void testCovarianceOfInts() {
168
168
assertEquals ("covariance of X with itself" , Statistics .variance (data ), Statistics .covariance (data , data .clone ()), EPSILON );
169
169
assertEquals ("covariance of X with itself" , 3 *4 *Statistics .variance (data ), Statistics .covariance (data2 , data3 ), EPSILON );
170
170
}
171
+ IllegalArgumentException thrown = assertThrows (
172
+ IllegalArgumentException .class ,
173
+ () -> Statistics .covariance (new int [3 ], new int [4 ])
174
+ );
171
175
}
172
176
173
177
@ Test
@@ -192,7 +196,11 @@ public void testCovarianceOfDoubles() {
192
196
// assumes variance method already tested... uses it in expected result
193
197
assertEquals ("covariance of X with itself" , Statistics .variance (data ), Statistics .covariance (data , data .clone ()), EPSILON );
194
198
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
+ );
196
204
}
197
205
198
206
@@ -218,6 +226,12 @@ public void testCorrelationOfInts() {
218
226
}
219
227
assertEquals ("Y = -alpha * X" , -1.0 , Statistics .correlation (x , y ), EPSILON );
220
228
}
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 );
221
235
}
222
236
223
237
@ Test
@@ -242,9 +256,116 @@ public void testCorrelationOfDoubles() {
242
256
}
243
257
assertEquals ("Y = -alpha * X" , -1.0 , Statistics .correlation (x , y ), EPSILON );
244
258
}
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 );
245
265
}
246
266
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
+ }
247
292
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
+ }
248
318
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
+ }
249
346
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
+ }
250
371
}
0 commit comments