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.linear.RealMatrixImpl;
23  import org.apache.commons.math.stat.descriptive.moment.VectorialCovariance;
24  import org.apache.commons.math.stat.descriptive.moment.VectorialMean;
25  
26  import junit.framework.*;
27  
28  public class CorrelatedRandomVectorGeneratorTest
29  extends TestCase {
30  
31      public CorrelatedRandomVectorGeneratorTest(String name) {
32          super(name);
33          mean       = null;
34          covariance = null;
35          generator  = null;
36      }
37  
38      public void testRank() {
39          assertEquals(3, generator.getRank());
40      }
41  
42      public void testRootMatrix() {
43          RealMatrix b = generator.getRootMatrix();
44          RealMatrix bbt = b.multiply(b.transpose());
45          for (int i = 0; i < covariance.getRowDimension(); ++i) {
46              for (int j = 0; j < covariance.getColumnDimension(); ++j) {
47                  assertEquals(covariance.getEntry(i, j), bbt.getEntry(i, j), 1.0e-12);
48              }
49          }
50      }
51  
52      public void testMeanAndCovariance() throws DimensionMismatchException {
53  
54          VectorialMean meanStat = new VectorialMean(mean.length);
55          VectorialCovariance covStat = new VectorialCovariance(mean.length, true);
56          for (int i = 0; i < 5000; ++i) {
57              double[] v = generator.nextVector();
58              meanStat.increment(v);
59              covStat.increment(v);
60          }
61  
62          double[] estimatedMean = meanStat.getResult();
63          RealMatrix estimatedCovariance = covStat.getResult();
64          for (int i = 0; i < estimatedMean.length; ++i) {
65              assertEquals(mean[i], estimatedMean[i], 0.07);
66              for (int j = 0; j <= i; ++j) {
67                  assertEquals(covariance.getEntry(i, j),
68                          estimatedCovariance.getEntry(i, j),
69                          0.1 * (1.0 + Math.abs(mean[i])) * (1.0 + Math.abs(mean[j])));
70              }
71          }
72  
73      }
74  
75      public void setUp() {
76          try {
77              mean = new double[] { 0.0, 1.0, -3.0, 2.3};
78  
79              RealMatrixImpl b = new RealMatrixImpl(4, 3);
80              double[][] bData = b.getDataRef();
81              int counter = 0;
82              for (int i = 0; i < bData.length; ++i) {
83                  double[] bi = bData[i];
84                  for (int j = 0; j < b.getColumnDimension(); ++j) {
85                      bi[j] = 1.0 + 0.1 * ++counter;
86                  }
87              }
88              RealMatrix bbt = b.multiply(b.transpose());
89              covariance = new RealMatrixImpl(mean.length, mean.length);
90              double[][] covData = covariance.getDataRef();
91              for (int i = 0; i < covariance.getRowDimension(); ++i) {
92                  covData[i][i] = bbt.getEntry(i, i);
93                  for (int j = 0; j < covariance.getColumnDimension(); ++j) {
94                      double s = bbt.getEntry(i, j);
95                      covData[i][j] = s;
96                      covData[j][i] = s;
97                  }
98              }
99  
100             RandomGenerator rg = new JDKRandomGenerator();
101             rg.setSeed(17399225432l);
102             GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
103             generator = new CorrelatedRandomVectorGenerator(mean,
104                                                             covariance,
105                                                             1.0e-12 * covariance.getNorm(),
106                                                             rawGenerator);
107         } catch (DimensionMismatchException e) {
108             fail(e.getMessage());
109         } catch (NotPositiveDefiniteMatrixException e) {
110             fail("not positive definite matrix");
111         }
112     }
113 
114     public void tearDown() {
115         mean       = null;
116         covariance = null;
117         generator  = null;
118     }
119 
120     public static Test suite() {
121         return new TestSuite(CorrelatedRandomVectorGeneratorTest.class);
122     }
123 
124     private double[] mean;
125     private RealMatrixImpl covariance;
126     private CorrelatedRandomVectorGenerator generator;
127 
128 }