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 public void setUp() {
63 mean = new double[] {0.0, 1.0, -3.0, 2.3};
64 standardDeviation = new double[] {1.0, 2.0, 10.0, 0.1};
65 RandomGenerator rg = new JDKRandomGenerator();
66 rg.setSeed(17399225432l);
67 generator =
68 new UncorrelatedRandomVectorGenerator(mean, standardDeviation,
69 new GaussianRandomGenerator(rg));
70 }
71
72 public void tearDown() {
73 mean = null;
74 standardDeviation = null;
75 generator = null;
76 }
77
78 public static Test suite() {
79 return new TestSuite(UncorrelatedRandomVectorGeneratorTest.class);
80 }
81
82 private double[] mean;
83 private double[] standardDeviation;
84 private UncorrelatedRandomVectorGenerator generator;
85
86 }