diff --git a/Examples/AdjointBermudanSwaption/AdjointBermudanSwaptionXAD.cpp b/Examples/AdjointBermudanSwaption/AdjointBermudanSwaptionXAD.cpp index 95f6cb6..f114bba 100644 --- a/Examples/AdjointBermudanSwaption/AdjointBermudanSwaptionXAD.cpp +++ b/Examples/AdjointBermudanSwaption/AdjointBermudanSwaptionXAD.cpp @@ -22,10 +22,17 @@ */ /* -This example demonstrates how to use XAD to price a Bermudan Swaption with -sensitivities. - -TODO: Use the implicit function theorem for calibration with AAD. +This example demonstrates how to use XAD to price a Bermudan swaption with +sensitivities to the swaption volatilities, through a Hull-White model +calibrated to 5 of them. + +The sensitivities go through the calibration with the implicit function +theorem, without recording the optimizer: at the calibrated parameters p the +gradient of the calibration objective in p is zero, so +dV/dvol = dV/dvol|p - (Hpp^-1 dV/dp)' Hpvol, with the Hessian blocks Hpp and +Hpvol from central differences of the XAD gradient in p (as in the +AdjointHestonModel example). Recording the optimizer's iterations instead gives +the derivative of where it stopped, not of the optimum. */ #include @@ -36,177 +43,195 @@ TODO: Use the implicit function theorem for calibration with AAD. #include #include #include +#include #include #include -#include +#include #include -#include #include -#include #include -#include #include #include #include #include -#include +#include +#include +#include #include #include -#include using namespace QuantLib; -// Number of swaptions to be calibrated to... -void calibrateModel(const ext::shared_ptr& model, - const std::vector>& swaptions, - const std::vector& swapLengths, - const std::vector& swaptionVols, - Size numRows, - Size numCols) { - - std::vector> helpers(swaptions.begin(), swaptions.end()); - LevenbergMarquardt om; - model->calibrate(helpers, om, EndCriteria(400, 100, 1.0e-8, 1.0e-8, 1.0e-8)); -} - -Handle setupYields(Date settlementDate, Real flatRate) { - // flat yield term structure impling 1x5 swap at 5% - auto rate = ext::make_shared(flatRate); - return Handle( - ext::make_shared(settlementDate, Handle(rate), Actual365Fixed())); -} +namespace { + + // the curve, the calibration swaptions and the Bermudan swaption + struct Market { + Handle curve; + std::vector> swaptions; + ext::shared_ptr bermudan; + }; + + // builds the market from the 5x5 swaption vols (by expiry, then swap length); + // the model is calibrated to the diagonal 1x5, 2x4, 3x3, 4x2, 5x1 + Market build(const std::vector& swaptionVols, Real flatRate) { + Market m; + Calendar calendar = TARGET(); + Date settlementDate(19, February, 2002); + m.curve = Handle( + ext::make_shared(settlementDate, flatRate, Actual365Fixed())); + auto indexSixMonths = ext::make_shared(m.curve); + + const Size numRows = 5, numCols = 5; + const Integer swapLengths[] = {1, 2, 3, 4, 5}; + for (Size i = 0; i < numRows; i++) { + Size j = numCols - i - 1; + auto vol = ext::make_shared(swaptionVols[i * numCols + j]); + m.swaptions.push_back(ext::make_shared( + Period(i + 1, Years), Period(swapLengths[j], Years), Handle(vol), + indexSixMonths, indexSixMonths->tenor(), indexSixMonths->dayCounter(), + indexSixMonths->dayCounter(), m.curve)); + } -Real priceSwaption(const std::vector& swapLengths, - const std::vector& swaptionVols, - Size numRows, - Size numCols, - Real flatRate) { - - Date todaysDate(15, February, 2002); - Calendar calendar = TARGET(); - Date settlementDate(19, February, 2002); - Settings::instance().evaluationDate() = todaysDate; - - auto rhTermStructure = setupYields(settlementDate, flatRate); - - // Define the ITM swap - Frequency fixedLegFrequency = Annual; - BusinessDayConvention fixedLegConvention = Unadjusted; - BusinessDayConvention floatingLegConvention = ModifiedFollowing; - DayCounter fixedLegDayCounter = Thirty360(Thirty360::European); - Frequency floatingLegFrequency = Semiannual; - Swap::Type type = Swap::Payer; - Rate dummyFixedRate = 0.03; - auto indexSixMonths = ext::make_shared(rhTermStructure); - - Date startDate = calendar.advance(settlementDate, 1, Years, floatingLegConvention); - Date maturity = calendar.advance(startDate, 5, Years, floatingLegConvention); - Schedule fixedSchedule(startDate, maturity, Period(fixedLegFrequency), calendar, - fixedLegConvention, fixedLegConvention, DateGeneration::Forward, false); - Schedule floatSchedule(startDate, maturity, Period(floatingLegFrequency), calendar, - floatingLegConvention, floatingLegConvention, DateGeneration::Forward, - false); - - auto swap = ext::make_shared(type, 1000.0, fixedSchedule, dummyFixedRate, - fixedLegDayCounter, floatSchedule, indexSixMonths, - 0.0, indexSixMonths->dayCounter()); - swap->setPricingEngine(ext::make_shared(rhTermStructure)); - Rate fixedITMRate = swap->fairRate() * 0.8; - - auto itmSwap = ext::make_shared(type, 1000.0, fixedSchedule, fixedITMRate, - fixedLegDayCounter, floatSchedule, indexSixMonths, - 0.0, indexSixMonths->dayCounter()); - - // defining the swaptions to be used in model calibration - std::vector swaptionMaturities = {1 * Years, 2 * Years, 3 * Years, 4 * Years, - 5 * Years}; - - std::vector> swaptions; - - // List of times that have to be included in the timegrid - std::list