public void testHalton() { //("Testing Halton sequences..."); List<double> point; double tolerance = 1.0e-15; // testing "high" dimensionality int dimensionality = (int)SobolRsg.PPMT_MAX_DIM; HaltonRsg rsg = new HaltonRsg(dimensionality, 0, false, false); int points = 100, i, k; for (i = 0; i < points; i++) { point = rsg.nextSequence().value; if (point.Count != dimensionality) { Assert.Fail("Halton sequence generator returns "+ " a sequence of wrong dimensionality: " + point.Count + " instead of " + dimensionality) ; } } // testing first and second dimension (van der Corput sequence) double[] vanderCorputSequenceModuloTwo = { // first cycle (zero excluded) 0.50000, // second cycle 0.25000, 0.75000, // third cycle 0.12500, 0.62500, 0.37500, 0.87500, // fourth cycle 0.06250, 0.56250, 0.31250, 0.81250, 0.18750, 0.68750, 0.43750, 0.93750, // fifth cycle 0.03125, 0.53125, 0.28125, 0.78125, 0.15625, 0.65625, 0.40625, 0.90625, 0.09375, 0.59375, 0.34375, 0.84375, 0.21875, 0.71875, 0.46875, 0.96875, }; dimensionality = 1; rsg = new HaltonRsg(dimensionality, 0, false, false); points = (int) (Math.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 Halton sequence is not in the " + "van der Corput sequence modulo two: " + "it should have been " + vanderCorputSequenceModuloTwo[i] //+ QL_SCIENTIFIC + " (error = " + error + ")"); } } double[] vanderCorputSequenceModuloThree = { // first cycle (zero excluded) 1.0/3, 2.0/3, // second cycle 1.0/9, 4.0/9, 7.0/9, 2.0/9, 5.0/9, 8.0/9, // third cycle 1.0/27, 10.0/27, 19.0/27, 4.0/27, 13.0/27, 22.0/27, 7.0/27, 16.0/27, 25.0/27, 2.0/27, 11.0/27, 20.0/27, 5.0/27, 14.0/27, 23.0/27, 8.0/27, 17.0/27, 26.0/27 }; dimensionality = 2; rsg = new HaltonRsg(dimensionality, 0, false, false); points = (int) (Math.Pow(3.0, 3)) - 1; // three cycles of the higher dimension for (i = 0; i < points; i++) { point = rsg.nextSequence().value; double error = Math.Abs(point[0] - vanderCorputSequenceModuloTwo[i]); if (error > tolerance) { Assert.Fail("First component of " + i + 1 + " draw (" + /*QL_FIXED*/ + point[0] + ") in 2-D Halton sequence is not in the " + "van der Corput sequence modulo two: " + "it should have been " + vanderCorputSequenceModuloTwo[i] //+ QL_SCIENTIFIC + " (error = " + error + ")"); } error = Math.Abs(point[1] - vanderCorputSequenceModuloThree[i]); if (error > tolerance) { Assert.Fail("Second component of " + i + 1 + " draw (" + /*QL_FIXED*/ + point[1] + ") in 2-D Halton sequence is not in the " + "van der Corput sequence modulo three: " + "it should have been " + vanderCorputSequenceModuloThree[i] //+ QL_SCIENTIFIC + " (error = " + error + ")"); } } // testing homogeneity properties dimensionality = 33; rsg = new HaltonRsg(dimensionality, 0, false, false); SequenceStatistics stat = new SequenceStatistics(dimensionality); List<double> mean; //, stdev, variance, skewness, kurtosis; k = 0; int j; for (j = 1; j < 5; j++) { // five cycle points = (int) (Math.Pow(2.0, j)) - 1; // base 2 for (; k < points; k++) { point = rsg.nextSequence().value; stat.add(point); } mean = stat.mean(); double error = Math.Abs(mean[0] - 0.5); if (error > tolerance) { Assert.Fail("First dimension mean (" + /*QL_FIXED*/ + mean[0] + ") at the end of the " + j + 1 + " cycle in Halton sequence is not " + 0.5 //+ QL_SCIENTIFIC + " (error = " + error + ")"); } } // reset generator and gaussianstatistics rsg = new HaltonRsg(dimensionality, 0, false, false); stat.reset(dimensionality); k = 0; for (j = 1; j < 3; j++) { // three cycle points = (int) (Math.Pow(3.0, j)) - 1; // base 3 for (; k < points; k++) { point = rsg.nextSequence().value; stat.add(point); } mean = stat.mean(); double error = Math.Abs(mean[1] - 0.5); if (error > tolerance) { Assert.Fail("Second dimension mean (" + /*QL_FIXED*/ + mean[1] + ") at the end of the " + j + 1 + " cycle in Halton sequence is not " + 0.5 //+ QL_SCIENTIFIC + " (error = " + error + ")"); } } }
public override void update() { this.coeff_.updateModelInstance(); // we should also check that y contains positive values only // we must update weights if it is vegaWeighted if (vegaWeighted_) { coeff_.weights_.Clear(); double weightsSum = 0.0; for (int i = 0; i < xBegin_.Count; i++) { double stdDev = Math.Sqrt((yBegin_[i]) * (yBegin_[i]) * this.coeff_.t_); coeff_.weights_.Add(coeff_.model_.weight(xBegin_[i], forward_, stdDev, this.coeff_.addParams_)); weightsSum += coeff_.weights_.Last(); } // weight normalization for (int i = 0; i < coeff_.weights_.Count; i++) { coeff_.weights_[i] /= weightsSum; } } // there is nothing to optimize if (coeff_.paramIsFixed_.Aggregate((a, b) => b && a)) { coeff_.error_ = interpolationError(); coeff_.maxError_ = interpolationMaxError(); coeff_.XABREndCriteria_ = EndCriteria.Type.None; return; } else { XABRError costFunction = new XABRError(this); Vector guess = new Vector(coeff_.model_.dimension()); for (int i = 0; i < guess.size(); ++i) { guess[i] = coeff_.params_[i].Value; } int iterations = 0; int freeParameters = 0; double bestError = double.MaxValue; Vector bestParameters = new Vector(); for (int i = 0; i < coeff_.model_.dimension(); ++i) { if (!coeff_.paramIsFixed_[i]) { ++freeParameters; } } HaltonRsg halton = new HaltonRsg(freeParameters, 42); EndCriteria.Type tmpEndCriteria; double tmpInterpolationError; do { if (iterations > 0) { Sample <List <double> > s = halton.nextSequence(); coeff_.model_.guess(guess, coeff_.paramIsFixed_, forward_, coeff_.t_, s.value, coeff_.addParams_); for (int i = 0; i < coeff_.paramIsFixed_.Count; ++i) { if (coeff_.paramIsFixed_[i]) { guess[i] = coeff_.params_[i].Value; } } } Vector inversedTransformatedGuess = new Vector(coeff_.model_.inverse(guess, coeff_.paramIsFixed_, coeff_.params_, forward_)); ProjectedCostFunction rainedXABRError = new ProjectedCostFunction(costFunction, inversedTransformatedGuess, coeff_.paramIsFixed_); Vector projectedGuess = new Vector(rainedXABRError.project(inversedTransformatedGuess)); NoConstraint raint = new NoConstraint(); Problem problem = new Problem(rainedXABRError, raint, projectedGuess); tmpEndCriteria = optMethod_.minimize(problem, endCriteria_); Vector projectedResult = new Vector(problem.currentValue()); Vector transfResult = new Vector(rainedXABRError.include(projectedResult)); Vector result = coeff_.model_.direct(transfResult, coeff_.paramIsFixed_, coeff_.params_, forward_); tmpInterpolationError = useMaxError_ ? interpolationMaxError() : interpolationError(); if (tmpInterpolationError < bestError) { bestError = tmpInterpolationError; bestParameters = result; coeff_.XABREndCriteria_ = tmpEndCriteria; } } while (++iterations < maxGuesses_ && tmpInterpolationError > errorAccept_); for (int i = 0; i < bestParameters.size(); ++i) { coeff_.params_[i] = bestParameters[i]; } coeff_.error_ = interpolationError(); coeff_.maxError_ = interpolationMaxError(); } }