CorrelatedRandomVectorGenerator implements RandomVectorGenerator { public double[] nextVector() { for (int i = 0; i < normalized.length; ++i) { normalized[i] = generator.nextNormalizedDouble(); } double[] correlated = new double[mean.length]; for (int i = 0; i < correlated.length; ++i) { correlated[i] = mean[i]; for (int j = 0; j < root.getColumnDimension(); ++j) { correlated[i] += root.getEntry(i, j) * normalized[j]; } } return correlated; } CorrelatedRandomVectorGenerator(double[] mean,
                                           RealMatrix covariance, double small,
                                           NormalizedRandomGenerator generator);  CorrelatedRandomVectorGenerator(RealMatrix covariance, double small,
                                           NormalizedRandomGenerator generator); NormalizedRandomGenerator getGenerator(); int getRank(); RealMatrix getRootMatrix(); double[] nextVector();  }
@Test public void testMath226() { double[] mean = { 1, 1, 10, 1 }; double[][] cov = { { 1, 3, 2, 6 }, { 3, 13, 16, 2 }, { 2, 16, 38, -1 }, { 6, 2, -1, 197 } }; RealMatrix covRM = MatrixUtils.createRealMatrix(cov); JDKRandomGenerator jg = new JDKRandomGenerator(); jg.setSeed(5322145245211l); NormalizedRandomGenerator rg = new GaussianRandomGenerator(jg); CorrelatedRandomVectorGenerator sg = new CorrelatedRandomVectorGenerator(mean, covRM, 0.00001, rg); double[] min = new double[mean.length]; Arrays.fill(min, Double.POSITIVE_INFINITY); double[] max = new double[mean.length]; Arrays.fill(max, Double.NEGATIVE_INFINITY); for (int i = 0; i < 10; i++) { double[] generated = sg.nextVector(); for (int j = 0; j < generated.length; ++j) { min[j] = FastMath.min(min[j], generated[j]); max[j] = FastMath.max(max[j], generated[j]); } } for (int j = 0; j < min.length; ++j) { Assert.assertTrue(max[j] - min[j] > 2.0); } }
@Test public void testMeanAndCovariance() { VectorialMean meanStat = new VectorialMean(mean.length); VectorialCovariance covStat = new VectorialCovariance(mean.length, true); for (int i = 0; i < 5000; ++i) { double[] v = generator.nextVector(); meanStat.increment(v); covStat.increment(v); } double[] estimatedMean = meanStat.getResult(); RealMatrix estimatedCovariance = covStat.getResult(); for (int i = 0; i < estimatedMean.length; ++i) { Assert.assertEquals(mean[i], estimatedMean[i], 0.07); for (int j = 0; j <= i; ++j) { Assert.assertEquals(covariance.getEntry(i, j), estimatedCovariance.getEntry(i, j), 0.1 * (1.0 + FastMath.abs(mean[i])) * (1.0 + FastMath.abs(mean[j]))); } } }