1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.hipparchus.random;
19
20 import org.hipparchus.UnitTestUtils;
21 import org.hipparchus.linear.Array2DRowRealMatrix;
22 import org.hipparchus.linear.RealMatrix;
23 import org.junit.jupiter.api.Test;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26
27 public class UncorrelatedRandomVectorGeneratorTest {
28 private double[] mean;
29 private double[] standardDeviation;
30 private UncorrelatedRandomVectorGenerator generator;
31
32 public UncorrelatedRandomVectorGeneratorTest() {
33 mean = new double[] {0.0, 1.0, -3.0, 2.3};
34 standardDeviation = new double[] {1.0, 2.0, 10.0, 0.1};
35 RandomGenerator rg = new JDKRandomGenerator();
36 rg.setSeed(17399225432l);
37 generator =
38 new UncorrelatedRandomVectorGenerator(mean, standardDeviation,
39 new GaussianRandomGenerator(rg));
40 }
41
42 @Test
43 void testMeanAndCorrelation() {
44 final int n = generator.nextVector().length;
45 final double[] estimatedMean = new double[generator.nextVector().length];
46 final RealMatrix matrix = new Array2DRowRealMatrix(10000, n);
47 for (int i = 0; i < 10000; ++i) {
48 double[] v = generator.nextVector();
49 matrix.setRow(i, v);
50 }
51
52 for (int i = 0; i < n; i++) {
53 estimatedMean[i] = UnitTestUtils.mean(matrix.getColumn(i));
54 }
55
56
57 double scale;
58 RealMatrix estimatedCorrelation = UnitTestUtils.covarianceMatrix(matrix);
59
60 for (int i = 0; i < estimatedMean.length; ++i) {
61 assertEquals(mean[i], estimatedMean[i], 0.07);
62 for (int j = 0; j < i; ++j) {
63 scale = standardDeviation[i] * standardDeviation[j];
64 assertEquals(0, estimatedCorrelation.getEntry(i, j) / scale, 0.03);
65 }
66 scale = standardDeviation[i] * standardDeviation[i];
67 assertEquals(1, estimatedCorrelation.getEntry(i, i) / scale, 0.025);
68 }
69 }
70 }