//! Calibrate to a set of market instruments (caps/swaptions) /*! An additional constraint can be passed which must be * satisfied in addition to the constraints of the model. */ //public void calibrate(List<CalibrationHelper> instruments, OptimizationMethod method, EndCriteria endCriteria, // Constraint constraint = new Constraint(), List<double> weights = new List<double>()) { public void calibrate(List <CalibrationHelper> instruments, OptimizationMethod method, EndCriteria endCriteria, Constraint additionalConstraint, List <double> weights) { if (!(weights.Count == 0 || weights.Count == instruments.Count)) { throw new ApplicationException("mismatch between number of instruments and weights"); } Constraint c; if (additionalConstraint.empty()) { c = constraint_; } else { c = new CompositeConstraint(constraint_, additionalConstraint); } List <double> w = weights.Count == 0 ? new InitializedList <double>(instruments.Count, 1.0): weights; CalibrationFunction f = new CalibrationFunction(this, instruments, w); Problem prob = new Problem(f, c, parameters()); shortRateEndCriteria_ = method.minimize(prob, endCriteria); Vector result = new Vector(prob.currentValue()); setParams(result); // recheck Vector shortRateProblemValues_ = prob.values(result); notifyObservers(); }
//! Calibrate to a set of market instruments (caps/swaptions) /*! An additional constraint can be passed which must be * satisfied in addition to the constraints of the model. */ //public void calibrate(List<CalibrationHelper> instruments, OptimizationMethod method, EndCriteria endCriteria, // Constraint constraint = new Constraint(), List<double> weights = new List<double>()) { public void calibrate(List <CalibrationHelper> instruments, OptimizationMethod method, EndCriteria endCriteria, Constraint additionalConstraint = null, List <double> weights = null, List <bool> fixParameters = null) { if (weights == null) { weights = new List <double>(); } if (additionalConstraint == null) { additionalConstraint = new Constraint(); } Utils.QL_REQUIRE(weights.empty() || weights.Count == instruments.Count, () => "mismatch between number of instruments (" + instruments.Count + ") and weights(" + weights.Count + ")"); Constraint c; if (additionalConstraint.empty()) { c = constraint_; } else { c = new CompositeConstraint(constraint_, additionalConstraint); } List <double> w = weights.Count == 0 ? new InitializedList <double>(instruments.Count, 1.0): weights; Vector prms = parameters(); List <bool> all = new InitializedList <bool>(prms.size(), false); Projection proj = new Projection(prms, fixParameters ?? all); CalibrationFunction f = new CalibrationFunction(this, instruments, w, proj); ProjectedConstraint pc = new ProjectedConstraint(c, proj); Problem prob = new Problem(f, pc, proj.project(prms)); shortRateEndCriteria_ = method.minimize(prob, endCriteria); Vector result = new Vector(prob.currentValue()); setParams(proj.include(result)); Vector shortRateProblemValues_ = prob.values(result); notifyObservers(); //CalibrationFunction f = new CalibrationFunction(this, instruments, w); //Problem prob = new Problem(f, c, parameters()); //shortRateEndCriteria_ = method.minimize(prob, endCriteria); //Vector result = new Vector(prob.currentValue()); //setParams(result); //// recheck //Vector shortRateProblemValues_ = prob.values(result); //notifyObservers(); }
public Vector fcn(int m, int n, Vector x, int iflag) { Vector xt = new Vector(x); Vector fvec; // constraint handling needs some improvement in the future: // starting point should not be close to a constraint violation if (currentProblem_.constraint().test(xt)) { fvec = new Vector(currentProblem_.values(xt)); } else { fvec = new Vector(initCostValues_); } return(fvec); }
//! Calibrate to a set of market instruments (caps/swaptions) /*! An additional constraint can be passed which must be satisfied in addition to the constraints of the model. */ //public void calibrate(List<CalibrationHelper> instruments, OptimizationMethod method, EndCriteria endCriteria, // Constraint constraint = new Constraint(), List<double> weights = new List<double>()) { public void calibrate(List<CalibrationHelper> instruments, OptimizationMethod method, EndCriteria endCriteria, Constraint additionalConstraint, List<double> weights) { if (!(weights.Count == 0 || weights.Count == instruments.Count)) throw new ApplicationException("mismatch between number of instruments and weights"); Constraint c; if (additionalConstraint.empty()) c = constraint_; else c = new CompositeConstraint(constraint_,additionalConstraint); List<double> w = weights.Count == 0 ? new InitializedList<double>(instruments.Count, 1.0): weights; CalibrationFunction f = new CalibrationFunction(this, instruments, w); Problem prob = new Problem(f, c, parameters()); shortRateEndCriteria_ = method.minimize(prob, endCriteria); Vector result = new Vector(prob.currentValue()); setParams(result); // recheck Vector shortRateProblemValues_ = prob.values(result); notifyObservers(); }
public void OptimizersTest() { //("Testing optimizers..."); setup(); // Loop over problems (currently there is only 1 problem) for (int i=0; i<costFunctions_.Count; ++i) { Problem problem = new Problem(costFunctions_[i], constraints_[i], initialValues_[i]); Vector initialValues = problem.currentValue(); // Loop over optimizers for (int j = 0; j < (optimizationMethods_[i]).Count; ++j) { double rootEpsilon = endCriterias_[i].rootEpsilon(); int endCriteriaTests = 1; // Loop over rootEpsilon for(int k=0; k<endCriteriaTests; ++k) { problem.setCurrentValue(initialValues); EndCriteria endCriteria = new EndCriteria(endCriterias_[i].maxIterations(), endCriterias_[i].maxStationaryStateIterations(), rootEpsilon, endCriterias_[i].functionEpsilon(), endCriterias_[i].gradientNormEpsilon()); rootEpsilon *= .1; EndCriteria.Type endCriteriaResult = optimizationMethods_[i][j].optimizationMethod.minimize(problem, endCriteria); Vector xMinCalculated = problem.currentValue(); Vector yMinCalculated = problem.values(xMinCalculated); // Check optimization results vs known solution if (endCriteriaResult==EndCriteria.Type.None || endCriteriaResult==EndCriteria.Type.MaxIterations || endCriteriaResult==EndCriteria.Type.Unknown) Assert.Fail("function evaluations: " + problem.functionEvaluation() + " gradient evaluations: " + problem.gradientEvaluation() + " x expected: " + xMinExpected_[i] + " x calculated: " + xMinCalculated + " x difference: " + (xMinExpected_[i]- xMinCalculated) + " rootEpsilon: " + endCriteria.rootEpsilon() + " y expected: " + yMinExpected_[i] + " y calculated: " + yMinCalculated + " y difference: " + (yMinExpected_[i]- yMinCalculated) + " functionEpsilon: " + endCriteria.functionEpsilon() + " endCriteriaResult: " + endCriteriaResult); } } } }