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++) {
}
}
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++) {
}
}
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
Get your Android app in front of a whole new audience. Start now.