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.Assert;
24 import org.junit.Test;
25
26 public class UncorrelatedRandomVectorGeneratorTest {
27 private double[] mean;
28 private double[] standardDeviation;
29 private UncorrelatedRandomVectorGenerator generator;
30
31 public UncorrelatedRandomVectorGeneratorTest() {
32 mean = new double[] {0.0, 1.0, -3.0, 2.3};
33 standardDeviation = new double[] {1.0, 2.0, 10.0, 0.1};
34 RandomGenerator rg = new JDKRandomGenerator();
35 rg.setSeed(17399225432l);
36 generator =
37 new UncorrelatedRandomVectorGenerator(mean, standardDeviation,
38 new GaussianRandomGenerator(rg));
39 }
40
41 @Test
42 public void testMeanAndCorrelation() {
43 final int n = generator.nextVector().length;
44 final double[] estimatedMean = new double[generator.nextVector().length];
45 final RealMatrix matrix = new Array2DRowRealMatrix(10000, n);
46 for (int i = 0; i < 10000; ++i) {
47 double[] v = generator.nextVector();
48 matrix.setRow(i, v);
49 }
50
51 for (int i = 0; i < n; i++) {
52 estimatedMean[i] = UnitTestUtils.mean(matrix.getColumn(i));
53 }
54
55
56 double scale;
57 RealMatrix estimatedCorrelation = UnitTestUtils.covarianceMatrix(matrix);
58
59 for (int i = 0; i < estimatedMean.length; ++i) {
60 Assert.assertEquals(mean[i], estimatedMean[i], 0.07);
61 for (int j = 0; j < i; ++j) {
62 scale = standardDeviation[i] * standardDeviation[j];
63 Assert.assertEquals(0, estimatedCorrelation.getEntry(i, j) / scale, 0.03);
64 }
65 scale = standardDeviation[i] * standardDeviation[i];
66 Assert.assertEquals(1, estimatedCorrelation.getEntry(i, i) / scale, 0.025);
67 }
68 }
69 }