1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.random;
19
20 import org.apache.commons.math.DimensionMismatchException;
21 import org.apache.commons.math.linear.RealMatrix;
22 import org.apache.commons.math.stat.descriptive.moment.VectorialCovariance;
23 import org.apache.commons.math.stat.descriptive.moment.VectorialMean;
24
25 import junit.framework.*;
26
27 public class UncorrelatedRandomVectorGeneratorTest
28 extends TestCase {
29
30 public UncorrelatedRandomVectorGeneratorTest(String name) {
31 super(name);
32 mean = null;
33 standardDeviation = null;
34 generator = null;
35 }
36
37 public void testMeanAndCorrelation() throws DimensionMismatchException {
38
39 VectorialMean meanStat = new VectorialMean(mean.length);
40 VectorialCovariance covStat = new VectorialCovariance(mean.length, true);
41 for (int i = 0; i < 10000; ++i) {
42 double[] v = generator.nextVector();
43 meanStat.increment(v);
44 covStat.increment(v);
45 }
46
47 double[] estimatedMean = meanStat.getResult();
48 double scale;
49 RealMatrix estimatedCorrelation = covStat.getResult();
50 for (int i = 0; i < estimatedMean.length; ++i) {
51 assertEquals(mean[i], estimatedMean[i], 0.07);
52 for (int j = 0; j < i; ++j) {
53 scale = standardDeviation[i] * standardDeviation[j];
54 assertEquals(0, estimatedCorrelation.getEntry(i, j) / scale, 0.03);
55 }
56 scale = standardDeviation[i] * standardDeviation[i];
57 assertEquals(1, estimatedCorrelation.getEntry(i, i) / scale, 0.025);
58 }
59
60 }
61
62 @Override
63 public void setUp() {
64 mean = new double[] {0.0, 1.0, -3.0, 2.3};
65 standardDeviation = new double[] {1.0, 2.0, 10.0, 0.1};
66 RandomGenerator rg = new JDKRandomGenerator();
67 rg.setSeed(17399225432l);
68 generator =
69 new UncorrelatedRandomVectorGenerator(mean, standardDeviation,
70 new GaussianRandomGenerator(rg));
71 }
72
73 @Override
74 public void tearDown() {
75 mean = null;
76 standardDeviation = null;
77 generator = null;
78 }
79
80 public static Test suite() {
81 return new TestSuite(UncorrelatedRandomVectorGeneratorTest.class);
82 }
83
84 private double[] mean;
85 private double[] standardDeviation;
86 private UncorrelatedRandomVectorGenerator generator;
87
88 }