View Javadoc
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   //https://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.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          // double[] estimatedMean = meanStat.getResult();
56          double scale;
57          RealMatrix estimatedCorrelation = UnitTestUtils.covarianceMatrix(matrix);
58          //RealMatrix estimatedCorrelation = covStat.getResult();
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  }