Re: calibration G2++ with defferential evolution

Posted by Ralph Schreyer-3 on
URL: http://quantlib.414.s1.nabble.com/calibration-G2-with-defferential-evolution-tp14969p14974.html

Hi Peter, André,

"fixing" the CompositeConstraint in constraint.hpp like this (this is not a real fix, it is just to get the code running)

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    class CompositeConstraint : public Constraint {
      private:
        class Impl : public Constraint::Impl {
          public:
            Impl(const Constraint& c1,
                 const Constraint& c2)
            : c1_(c1), c2_(c2) {}
            bool test(const Array& params) const {
                return c1_.test(params) && c2_.test(params);
            }
            Array upperBound(const Array& params) const {
                Array c1ub = c1_.upperBound(params);
                Array c2ub = c2_.upperBound(params);
Size ubSize = std::max(c1ub.size(), c2ub.size());
                Array rtrnArray(ubSize, 0.0);
if (c1ub.size() == c2ub.size()) {
for (Size iter = 0; iter < c1ub.size(); iter++) {
rtrnArray.at(iter) = std::min(c1ub.at(iter), c2ub.at(iter));
}
}
else if (c1ub.size() > c2ub.size()) {
for (Size iter = 0; iter < c1ub.size(); iter++) {
rtrnArray.at(iter) = c1ub.at(iter);
}
}
else {
for (Size iter = 0; iter < c2ub.size(); iter++) {
rtrnArray.at(iter) = c2ub.at(iter);
}
}
                return rtrnArray;
            }
            Array lowerBound(const Array& params) const {
                Array c1lb = c1_.lowerBound(params);
                Array c2lb = c2_.lowerBound(params);
Size lbSize = std::max(c1lb.size(), c2lb.size());
                Array rtrnArray(lbSize, 0.0);
if (c1lb.size() == c2lb.size()) {
for (Size iter = 0; iter < c1lb.size(); iter++) {
rtrnArray.at(iter) = std::min(c1lb.at(iter), c2lb.at(iter));
}
}
else if (c1lb.size() > c2lb.size()) {
for (Size iter = 0; iter < c1lb.size(); iter++) {
rtrnArray.at(iter) = c1lb.at(iter);
}
}
else {
for (Size iter = 0; iter < c2lb.size(); iter++) {
rtrnArray.at(iter) = c2lb.at(iter);
}
}
                return rtrnArray;
            }
          private:
            Constraint c1_, c2_;
        };
      public:
        CompositeConstraint(const Constraint& c1, const Constraint& c2)
        : Constraint(boost::shared_ptr<Constraint::Impl>(
                                     new CompositeConstraint::Impl(c1,c2))) {}
    };

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

and exchanging the LevenbergMarquardt optimizer in method testDAXCalibration() for the DifferentialEvolution like this

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

void HestonModelTest::testDAXCalibration() {

    BOOST_TEST_MESSAGE(
             "Testing Heston model calibration using DAX volatility data...");

    SavedSettings backup;

    Date settlementDate(5, July, 2002);
    Settings::instance().evaluationDate() = settlementDate;

    CalibrationMarketData marketData = getDAXCalibrationMarketData();
    
    const Handle<YieldTermStructure> riskFreeTS = marketData.riskFreeTS;
    const Handle<YieldTermStructure> dividendTS = marketData.dividendYield;
    const Handle<Quote> s0 = marketData.s0;

    const std::vector<boost::shared_ptr<CalibrationHelper> > options
                                                    = marketData.options;

    const Real v0=0.1;
    const Real kappa=1.0;
    const Real theta=0.1;
    const Real sigma=0.5;
    const Real rho=-0.5;

    boost::shared_ptr<HestonProcess> process(new HestonProcess(
              riskFreeTS, dividendTS, s0, v0, kappa, theta, sigma, rho));

    boost::shared_ptr<HestonModel> model(new HestonModel(process));

    boost::shared_ptr<PricingEngine> engine(
                                         new AnalyticHestonEngine(model, 64));

    for (Size i = 0; i < options.size(); ++i)
        options[i]->setPricingEngine(engine);

    DifferentialEvolution::Configuration conf =
        DifferentialEvolution::Configuration()
        .withStepsizeWeight(1.8)
        .withBounds()
        .withCrossoverProbability(0.9)
        .withPopulationMembers(50)
        .withStrategy(DifferentialEvolution::Rand1SelfadaptiveWithRotation)
        .withCrossoverType(DifferentialEvolution::Normal)
        .withAdaptiveCrossover()
        .withSeed(3242);
    DifferentialEvolution deOptim(conf);

Array lower(5);
lower[0] = 0.001;
lower[1] = 0.001;
lower[2] = 0.001;
lower[3] = -1.0;
lower[4] = 0.001;
Array upper(5);
upper[0] = 10.0;
upper[1] = 50.0;
upper[2] = 10.0;
upper[3] = 1.0;
upper[4] = 10.0;
model->calibrate(options, deOptim, EndCriteria(400, 40, 1.0e-8, 1.0e-8, Null<Real>()), 
NonhomogeneousBoundaryConstraint(lower, upper));

    Real sse = 0;
    for (Size i = 0; i < 13*8; ++i) {
        const Real diff = options[i]->calibrationError()*100.0;
        sse += diff*diff;
    }
    Real expected = 177.2; //see article by A. Sepp.
    if (std::fabs(sse - expected) > 1.0) {
        BOOST_FAIL("Failed to reproduce calibration error"
                   << "\n    calculated: " << sse
                   << "\n    expected:   " << expected);
    }
}

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

I get the code runnning and the test succeeds.

Best
Ralph


2014-02-17 13:00 GMT+01:00 Peter Caspers <[hidden email]>:
Hi André,

can you please post the complete code needed to reproduce the problem
? With a short description of what the problem is (if not obvious) ?

best regards
Peter


On 16 February 2014 17:28, André de Boer <[hidden email]> wrote:
> 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);
>
> }
>
> ------------------------------------------------------------------------------
> Android apps run on BlackBerry 10
> Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
> Now with support for Jelly Bean, Bluetooth, Mapview and more.
> Get your Android app in front of a whole new audience.  Start now.
> http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
> _______________________________________________
> QuantLib-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>

------------------------------------------------------------------------------
Android apps run on BlackBerry 10
Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
Now with support for Jelly Bean, Bluetooth, Mapview and more.
Get your Android app in front of a whole new audience.  Start now.
http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
Android apps run on BlackBerry 10
Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
Now with support for Jelly Bean, Bluetooth, Mapview and more.
Get your Android app in front of a whole new audience.  Start now.
http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users