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.jupiter.api.Test;
24  
25  import static org.junit.jupiter.api.Assertions.assertEquals;
26  
27  public class UncorrelatedRandomVectorGeneratorTest {
28      private double[] mean;
29      private double[] standardDeviation;
30      private UncorrelatedRandomVectorGenerator generator;
31  
32      public UncorrelatedRandomVectorGeneratorTest() {
33          mean              = new double[] {0.0, 1.0, -3.0, 2.3};
34          standardDeviation = new double[] {1.0, 2.0, 10.0, 0.1};
35          RandomGenerator rg = new JDKRandomGenerator();
36          rg.setSeed(17399225432l);
37          generator =
38              new UncorrelatedRandomVectorGenerator(mean, standardDeviation,
39                      new GaussianRandomGenerator(rg));
40      }
41  
42      @Test
43      void testMeanAndCorrelation() {
44          final int n = generator.nextVector().length;
45          final double[] estimatedMean = new double[generator.nextVector().length];
46          final RealMatrix matrix = new Array2DRowRealMatrix(10000, n);
47          for (int i = 0; i < 10000; ++i) {
48              double[] v = generator.nextVector();
49              matrix.setRow(i, v);
50          }
51  
52          for (int i = 0; i < n; i++) {
53              estimatedMean[i] = UnitTestUtils.mean(matrix.getColumn(i));
54          }
55  
56          // double[] estimatedMean = meanStat.getResult();
57          double scale;
58          RealMatrix estimatedCorrelation = UnitTestUtils.covarianceMatrix(matrix);
59          //RealMatrix estimatedCorrelation = covStat.getResult();
60          for (int i = 0; i < estimatedMean.length; ++i) {
61              assertEquals(mean[i], estimatedMean[i], 0.07);
62              for (int j = 0; j < i; ++j) {
63                  scale = standardDeviation[i] * standardDeviation[j];
64                  assertEquals(0, estimatedCorrelation.getEntry(i, j) / scale, 0.03);
65              }
66              scale = standardDeviation[i] * standardDeviation[i];
67              assertEquals(1, estimatedCorrelation.getEntry(i, i) / scale, 0.025);
68          }
69      }
70  }