1   //Licensed to the Apache Software Foundation (ASF) under one
2   //or more contributor license agreements.  See the NOTICE file
3   //distributed with this work for additional information
4   //regarding copyright ownership.  The ASF licenses this file
5   //to you under the Apache License, Version 2.0 (the
6   //"License"); you may not use this file except in compliance
7   //with the License.  You may obtain a copy of the License at
8   
9   //http://www.apache.org/licenses/LICENSE-2.0
10  
11  //Unless required by applicable law or agreed to in writing,
12  //software distributed under the License is distributed on an
13  //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  //KIND, either express or implied.  See the License for the
15  //specific language governing permissions and limitations
16  //under the License.
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  }