001 //Licensed to the Apache Software Foundation (ASF) under one 002 //or more contributor license agreements. See the NOTICE file 003 //distributed with this work for additional information 004 //regarding copyright ownership. The ASF licenses this file 005 //to you under the Apache License, Version 2.0 (the 006 //"License"); you may not use this file except in compliance 007 //with the License. You may obtain a copy of the License at 008 009 //http://www.apache.org/licenses/LICENSE-2.0 010 011 //Unless required by applicable law or agreed to in writing, 012 //software distributed under the License is distributed on an 013 //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 014 //KIND, either express or implied. See the License for the 015 //specific language governing permissions and limitations 016 //under the License. 017 018 package org.apache.commons.math.random; 019 020 import org.apache.commons.math.DimensionMismatchException; 021 import org.apache.commons.math.linear.RealMatrix; 022 import org.apache.commons.math.stat.descriptive.moment.VectorialCovariance; 023 import org.apache.commons.math.stat.descriptive.moment.VectorialMean; 024 025 import junit.framework.*; 026 027 public class UncorrelatedRandomVectorGeneratorTest 028 extends TestCase { 029 030 public UncorrelatedRandomVectorGeneratorTest(String name) { 031 super(name); 032 mean = null; 033 standardDeviation = null; 034 generator = null; 035 } 036 037 public void testMeanAndCorrelation() throws DimensionMismatchException { 038 039 VectorialMean meanStat = new VectorialMean(mean.length); 040 VectorialCovariance covStat = new VectorialCovariance(mean.length, true); 041 for (int i = 0; i < 10000; ++i) { 042 double[] v = generator.nextVector(); 043 meanStat.increment(v); 044 covStat.increment(v); 045 } 046 047 double[] estimatedMean = meanStat.getResult(); 048 double scale; 049 RealMatrix estimatedCorrelation = covStat.getResult(); 050 for (int i = 0; i < estimatedMean.length; ++i) { 051 assertEquals(mean[i], estimatedMean[i], 0.07); 052 for (int j = 0; j < i; ++j) { 053 scale = standardDeviation[i] * standardDeviation[j]; 054 assertEquals(0, estimatedCorrelation.getEntry(i, j) / scale, 0.03); 055 } 056 scale = standardDeviation[i] * standardDeviation[i]; 057 assertEquals(1, estimatedCorrelation.getEntry(i, i) / scale, 0.025); 058 } 059 060 } 061 062 @Override 063 public void setUp() { 064 mean = new double[] {0.0, 1.0, -3.0, 2.3}; 065 standardDeviation = new double[] {1.0, 2.0, 10.0, 0.1}; 066 RandomGenerator rg = new JDKRandomGenerator(); 067 rg.setSeed(17399225432l); 068 generator = 069 new UncorrelatedRandomVectorGenerator(mean, standardDeviation, 070 new GaussianRandomGenerator(rg)); 071 } 072 073 @Override 074 public void tearDown() { 075 mean = null; 076 standardDeviation = null; 077 generator = null; 078 } 079 080 public static Test suite() { 081 return new TestSuite(UncorrelatedRandomVectorGeneratorTest.class); 082 } 083 084 private double[] mean; 085 private double[] standardDeviation; 086 private UncorrelatedRandomVectorGenerator generator; 087 088 }