public void testSobol() { //("Testing Sobol sequences up to dimension " // + PPMT_MAX_DIM + "..."); List<double> point; double tolerance = 1.0e-15; // testing max dimensionality int dimensionality =(int)SobolRsg.PPMT_MAX_DIM; ulong seed = 123456; SobolRsg rsg=new SobolRsg(dimensionality, seed); int points = 100, i; for (i=0; i<points; i++) { point = rsg.nextSequence().value; if (point.Count!=dimensionality) { Assert.Fail("Sobol sequence generator returns "+ " a sequence of wrong dimensionality: " + point.Count + " instead of " + dimensionality); } } // testing homogeneity properties dimensionality = 33; seed = 123456; rsg = new SobolRsg(dimensionality, seed); SequenceStatistics stat=new SequenceStatistics(dimensionality); List<double> mean; int k = 0; for (int j=1; j<5; j++) { // five cycle points = (int)(Utils.Pow(2.0, j)-1); // base 2 for (; k<points; k++) { point = rsg.nextSequence().value; stat.add(point); } mean = stat.mean(); for (i=0; i<dimensionality; i++) { double error = Math.Abs(mean[i]-0.5); if (error > tolerance) { Assert.Fail(i+1 + " dimension: " // + QL_FIXED + "mean (" + mean[i] + ") at the end of the " + j+1 + " cycle in Sobol sequence is not " + 0.5 //+ QL_SCIENTIFIC + " (error = " + error + ")"); } } } // testing first dimension (van der Corput sequence) double[] vanderCorputSequenceModuloTwo= { // first cycle (zero excluded) 0.50000, // second cycle 0.75000, 0.25000, // third cycle 0.37500, 0.87500, 0.62500, 0.12500, // fourth cycle 0.18750, 0.68750, 0.93750, 0.43750, 0.31250, 0.81250, 0.56250, 0.06250, // fifth cycle 0.09375, 0.59375, 0.84375, 0.34375, 0.46875, 0.96875, 0.71875, 0.21875, 0.15625, 0.65625, 0.90625, 0.40625, 0.28125, 0.78125, 0.53125, 0.03125 }; dimensionality = 1; rsg = new SobolRsg(dimensionality); points = (int)(Utils.Pow(2.0, 5))-1; // five cycles for (i=0; i<points; i++) { point = rsg.nextSequence().value; double error =Math.Abs(point[0]-vanderCorputSequenceModuloTwo[i]); if (error > tolerance) { Assert.Fail(i+1 + " draw (" //+ QL_FIXED + point[0] + ") in 1-D Sobol sequence is not in the " + "van der Corput sequence modulo two: " + "it should have been " + vanderCorputSequenceModuloTwo[i] //+ QL_SCIENTIFIC + " (error = " + error + ")"); } } }
public void testSobolSkipping() { //("Testing Sobol sequence skipping..."); ulong seed = 42; int[] dimensionality = { 1, 10, 100, 1000 }; ulong[] skip = { 0, 1, 42, 512, 100000 }; SobolRsg.DirectionIntegers[] integers = {SobolRsg.DirectionIntegers.Unit, SobolRsg.DirectionIntegers.Jaeckel, SobolRsg.DirectionIntegers.SobolLevitan, SobolRsg.DirectionIntegers.SobolLevitanLemieux}; for (int i = 0; i < integers.Length; i++) { for (int j = 0; j < dimensionality.Length; j++) { for (int k = 0; k < skip.Length; k++) { // extract n samples SobolRsg rsg1 = new SobolRsg(dimensionality[j], seed, integers[i]); for (int l = 0; l < (int)skip[k]; l++) rsg1.nextInt32Sequence(); // skip n samples at once SobolRsg rsg2 = new SobolRsg(dimensionality[j], seed, integers[i]); rsg2.skipTo(skip[k]); // compare next 100 samples for (int m = 0; m < 100; m++) { List<ulong> s1 = rsg1.nextInt32Sequence(); List<ulong> s2 = rsg2.nextInt32Sequence(); for (int n = 0; n < s1.Count; n++) { if (s1[n] != s2[n]) { Assert.Fail("Mismatch after skipping:" + "\n size: " + dimensionality[j] + "\n integers: " + integers[i] + "\n skipped: " + skip[k] + "\n at index: " + n + "\n expected: " + s1[n] + "\n found: " + s2[n]); } } } } } } }
//typedef SobolRsg generator_type; public SobolFactory(SobolRsg.DirectionIntegers unit) { unit_=unit; }