Hello,
Two days ago I posted a question about differential evolution and the shortratemodel G2.
Meanwhile I learned how to minimize the Rosenbrockfunction with differential evolution.
But my goal is to calibrate the G2 shortratemodel.
For the 5 parameters of the G2++ model I want to have boundedconstraints:
For instance:
0 < a < 6
0 < b < 6
0 < sigma < 0.25
0 < eta < 0.25
-1 < rho < 1
To accomplish this I made a class MyConstraint, see code below.
...
MyConstraint mc;
DifferentialEvolution deOptim(conf);
modelG2pp->calibrate(swaptions, deOptim, EndCriteria(100,10,1e-10,1e-8,Null<Real>()), mc);
...
But still I isn't not working.
Can someone give me a hint of parts of code?
Kind regards,
André
#include <iostream>
#include <vector>
#include <ql/quantlib.hpp>
using namespace QuantLib;
class MyConstraint : public Constraint {
class Impl : public Constraint::Impl {
public:
bool test(const Array& x) const {
Real a1 = x[0], a2 = x[1], a3 = x[2], a4=x[3], a5=x[4];
return (0.0 <= a1 && a1 <= 6.0) &&
(0.0 <= a2 && a2 <= 6.0) &&
(0.0 <= a3 && a3 <= 0.25) &&
(0.0 <= a4 && a4 <= 0.25) &&
(-1.0 <= a5 && a5 <= 1.0);
}
};
public:
MyConstraint()
: Constraint(boost::shared_ptr<Constraint::Impl>(new MyConstraint::Impl)) {}
};
int main(){
...
double a = 0.1;
double b = 0.1;
double sigma = 0.01;
double eta = 0.01;
double rho = -0.75;
boost::shared_ptr<G2> modelG2pp(new G2(yieldCurve, a, b, sigma, eta, rho));
std::cout << "G2 (analytic formulae) calibration" << std::endl;
MyConstraint mc;
DifferentialEvolution::Configuration conf =
DifferentialEvolution::Configuration()
.withStepsizeWeight(0.4)
.withBounds()
.withCrossoverProbability(0.35)
.withPopulationMembers(100)
.withStrategy(DifferentialEvolution::BestMemberWithJitter)
.withCrossoverType(DifferentialEvolution::Normal)
.withAdaptiveCrossover()
.withSeed(0);
MyConstraint mc;
DifferentialEvolution deOptim(conf);
modelG2pp->calibrate(swaptions, deOptim, EndCriteria(100,10,1e-10,1e-8,Null<Real>()), mc);
}