G2++ calibration on swaptions
Posted by aincze on Feb 04, 2008; 3:32pm
URL: http://quantlib.414.s1.nabble.com/G2-calibration-on-swaptions-tp5459.html
Dear all,
I would like to know if it is possible to give in input to G2++ calibration some guess for the 5 model parameters and also which are the critical parameters that influence convergence and search for minimum.
Here below, pieces of code I use for calibration:
****
void calibrateModel(
const boost::shared_ptr<ShortRateModel>& model,
const std::vector<boost::shared_ptr<CalibrationHelper> >& helpers) {
LevenbergMarquardt om;
model->calibrate(helpers, om,
EndCriteria(800, 400, 1.0e-15, 1.0e-15, 1.0e-15));
// Output the implied Black volatilities
for (Size i=0; i<numRows; i++) {
for (Size j=0; j<numCols; j++){
Size k = i*numCols + j;
Real npv = helpers[k]->modelValue();
Volatility implied = helpers[k]->impliedVolatility(npv, 1e-8,
1000, 0.0005, 1.0);
Volatility diff = implied - swaptionVols[k];
std::cout << swaptionMaturitiesValues[i] << "x" << swapLenghts[j]
<< std::setprecision(5) << std::noshowpos
<< ": model " << std::setw(7) << io::volatility(implied)
<< ", market " << std::setw(7)
<< io::volatility(swaptionVols[k])
<< " (" << std::setw(7) << std::showpos
<< io::volatility(diff) << " " << io::volatility(diff/swaptionVols[k])
<< std::noshowpos << ")\n";
}
}
}
....
Linear l;
boost::shared_ptr<YieldTermStructure> curve(
new InterpolatedZeroCurve<Linear>(nodes, zcrates, dc30360, l));
RelinkableHandle<YieldTermStructure> rhTermStructure;
rhTermStructure.linkTo(curve);
boost::shared_ptr<IborIndex> indexSixMonths(new
Euribor3M(rhTermStructure));
std::vector<boost::shared_ptr<CalibrationHelper> > swaptions;
// List of times that have to be included in the timegrid
std::list<Time> times;
Size i;
for (i=0; i<numRows; i++) {
for (Size j=0; j<numCols; j++){
Size k = i*numCols + j;
boost::shared_ptr<Quote> vol(new SimpleQuote(swaptionVols[k]));
swaptions.push_back(boost::shared_ptr<CalibrationHelper>(new
SwaptionHelper(swaptionMaturities[i],
Period(swapLenghts[j], Years),
Handle<Quote>(vol),
indexSixMonths,
indexSixMonths->tenor(),
indexSixMonths->dayCounter(),
indexSixMonths->dayCounter(),
rhTermStructure)));
swaptions.back()->addTimesTo(times);
}
}
// Building time-grid
TimeGrid grid(times.begin(), times.end(), 30);
// defining the model
boost::shared_ptr<G2> modelG2(new G2(rhTermStructure));
// model calibration
std::cout << "G2 (analytic formulae) calibration" << std::endl;
for (i=0; i<swaptions.size(); i++)
swaptions[i]->setPricingEngine(boost::shared_ptr<PricingEngine>(
new G2SwaptionEngine(modelG2, 6.0, 20)));
calibrateModel(modelG2, swaptions);
thank you in advance.
Andrei