depositRate, i+1, Months, calendar,
depositRate, i+1, Months, settlementDays, calendar,
int main(int argc, char* argv[]){    try {        QL_IO_INIT        Date todaysDate(15, February, 2002);        Calendar calendar = Calendars::TARGET();        // Date settlementDate = calendar.advance(todaysDate,         //                                        settlementDays, Days);        Date settlementDate(19, February, 2002);        //Instruments used to bootstrap the yield curve:        std::vector<Handle<RateHelper> > instruments;        //Deposit rates        DayCounter depositDayCounter = Thirty360();        Rate weekRates[3] = {3.295, 3.3, 3.3};        Size i;        for (i=0; i<3; i++) {            Handle<MarketElement> depositRate(                new SimpleMarketElement(weekRates[i]*0.01));            Handle<RateHelper> depositHelper(new DepositRateHelper(                depositRate, i+1, Weeks, calendar,                ModifiedFollowing, depositDayCounter));            instruments.push_back(depositHelper);        }        Rate depositRates[12] = {            3.31, 3.32, 3.29, 3.35, 3.315, 3.33,            3.395, 3.41, 3.41, 3.49, 3.54, 3.53};        for (i=0; i<11; i++) {            Handle<MarketElement> depositRate(                new SimpleMarketElement(depositRates[i]*0.01));            Handle<RateHelper> depositHelper(new DepositRateHelper(                depositRate, i+1, Months, calendar,                ModifiedFollowing, depositDayCounter));            instruments.push_back(depositHelper);        }        //Swap rates        Rate swapRates[13] = {            3.6425, 4.0875, 4.38, 4.5815, 4.74325, 4.87375,            4.9775, 5.07, 5.13, 5.1825, 5.36, 5.45125, 5.43875};        int swapYears[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30};        int swFixedLegFrequency = 1;        bool swFixedLegIsAdjusted = false;        DayCounter swFixedLegDayCounter = Thirty360(Thirty360::European);        int swFloatingLegFrequency = 2;        for (i=0; i<13; i++) {            Handle<MarketElement> swapRate(                new SimpleMarketElement(swapRates[i]*0.01));            Handle<RateHelper> swapHelper(new SwapRateHelper(                RelinkableHandle<MarketElement>(swapRate),                swapYears[i], Years, calendar, ModifiedFollowing,                swFixedLegFrequency,                swFixedLegIsAdjusted, swFixedLegDayCounter,                swFloatingLegFrequency));            instruments.push_back(swapHelper);        }        // bootstrapping the yield curve        Handle<PiecewiseFlatForward> myTermStructure(new            PiecewiseFlatForward(todaysDate, settlementDate, instruments,                                  depositDayCounter));        RelinkableHandle<TermStructure > rhTermStructure;        rhTermStructure.linkTo(myTermStructure);        //Define the ATM/OTM/ITM swaps        int fixedLegFrequency = 1;        bool fixedLegIsAdjusted = false;        RollingConvention roll = ModifiedFollowing;        DayCounter fixedLegDayCounter = Thirty360(Thirty360::European);        int floatingLegFrequency = 2;        bool payFixedRate = true;        int fixingDays = 2;        Rate dummyFixedRate = 0.03;        Handle<Xibor> indexSixMonths(new Euribor(6, Months, rhTermStructure));        Handle<SimpleSwap> swap(new SimpleSwap(            payFixedRate, settlementDate.plusYears(1), 5, Years,            calendar, roll, 1000.0, fixedLegFrequency, dummyFixedRate,            fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,            indexSixMonths, fixingDays, 0.0, rhTermStructure));        Rate fixedATMRate = swap->fairRate();        Handle<SimpleSwap> atmSwap(new SimpleSwap(            payFixedRate, settlementDate.plusYears(1), 5, Years,            calendar, roll, 1000.0, fixedLegFrequency, fixedATMRate,            fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,            indexSixMonths, fixingDays, 0.0, rhTermStructure));        Handle<SimpleSwap> otmSwap(new SimpleSwap(            payFixedRate, settlementDate.plusYears(1), 5, Years,            calendar, roll, 1000.0, fixedLegFrequency, fixedATMRate * 1.2,            fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,            indexSixMonths, fixingDays, 0.0, rhTermStructure));        Handle<SimpleSwap> itmSwap(new SimpleSwap(            payFixedRate, settlementDate.plusYears(1), 5, Years,            calendar, roll, 1000.0, fixedLegFrequency, fixedATMRate * 0.8,            fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,            indexSixMonths, fixingDays, 0.0, rhTermStructure));        std::vector<Period> swaptionMaturities;        swaptionMaturities.push_back(Period(1, Months));        swaptionMaturities.push_back(Period(3, Months));        swaptionMaturities.push_back(Period(6, Months));        swaptionMaturities.push_back(Period(1, Years));        swaptionMaturities.push_back(Period(2, Years));        swaptionMaturities.push_back(Period(3, Years));        swaptionMaturities.push_back(Period(4, Years));        swaptionMaturities.push_back(Period(5, Years));        swaptionMaturities.push_back(Period(7, Years));        swaptionMaturities.push_back(Period(10, Years));        CalibrationSet swaptions;        //List of times that have to be included in the timegrid        std::list<Time> times;        for (i=0; i<numRows; i++) {            for (unsigned int j=0; j<numCols; j++) {                unsigned int k = i*10 + j;                Handle<MarketElement> vol(                    new SimpleMarketElement(swaptionVols[k]*0.01));                swaptions.push_back(Handle<CalibrationHelper>(                    new SwaptionHelper(swaptionMaturities[j],                                       Period(swaptionLengths[i], Years),                                       RelinkableHandle<MarketElement>(vol),                                       indexSixMonths,                                       rhTermStructure)));                swaptions.back()->addTimes(times);            }        }        const std::vector<Time> termTimes = myTermStructure->times();        for (i=0; i<termTimes.size(); i++)            times.push_back(termTimes[i]);		// please add a comment here		// wouldn't be safer to have the sort unique in the TimeGrid constructor        times.sort();        times.unique();        //Building time-grid        TimeGrid grid(times, 30);        Handle<Model> modelHW(new HullWhite(rhTermStructure));        Handle<Model> modelHW2(new HullWhite(rhTermStructure));        Handle<Model> modelBK(new BlackKarasinski(rhTermStructure));        std::cout << "Calibrating to swaptions" << std::endl;        std::cout << "Hull-White (analytic formulae):" << std::endl;        swaptions.setPricingEngine(            Handle<PricingEngine>(new JamshidianSwaption(modelHW)));        calibrateModel(modelHW, swaptions, 0.25);        std::cout << "calibrated to "                  << ArrayFormatter::toString(modelHW->params(),6)                  << std::endl                  << std::endl;        std::cout << "Hull-White (numerical calibration):" << std::endl;        swaptions.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelHW2, grid)));        calibrateModel(modelHW2, swaptions, 0.25);        std::cout << "calibrated to "                  << ArrayFormatter::toString(modelHW2->params(),6)                  << std::endl                  << std::endl;        std::cout << "Black-Karasinski: " << std::endl;        swaptions.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelBK, grid)));        calibrateModel(modelBK, swaptions, 0.25);        std::cout << "calibrated to "                  << ArrayFormatter::toString(modelBK->params())                  << std::endl                  << std::endl;        std::cout << "Pricing an ATM bermudan swaption" << std::endl;        //Define the bermudan swaption        std::vector<Date> bermudanDates;        const std::vector<Handle<CashFlow> >& leg = swap->floatingLeg();        for (i=0; i<leg.size(); i++) {            Handle<CashFlows::Coupon> coupon = leg[i];            bermudanDates.push_back(coupon->accrualStartDate());        }        Instruments::Swaption bermudanSwaption(atmSwap,            BermudanExercise(bermudanDates), rhTermStructure,            Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));        //Do the pricing for each model        bermudanSwaption.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));        std::cout << "HW:       " << bermudanSwaption.NPV() << std::endl;        bermudanSwaption.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelHW2, 100)));        std::cout << "HW (num): " << bermudanSwaption.NPV() << std::endl;        bermudanSwaption.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelBK, 100)));        std::cout << "BK:       " << bermudanSwaption.NPV() << std::endl;        std::cout << "Pricing an OTM bermudan swaption" << std::endl;        Instruments::Swaption otmBermudanSwaption(otmSwap,            BermudanExercise(bermudanDates), rhTermStructure,            Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));        //Do the pricing for each model        otmBermudanSwaption.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));        std::cout << "HW:       " << otmBermudanSwaption.NPV() << std::endl;        otmBermudanSwaption.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelHW2, 100)));        std::cout << "HW (num): " << otmBermudanSwaption.NPV() << std::endl;        otmBermudanSwaption.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelBK, 100)));        std::cout << "BK:       " << otmBermudanSwaption.NPV() << std::endl;        std::cout << "Pricing an ITM bermudan swaption" << std::endl;        Instruments::Swaption itmBermudanSwaption(itmSwap,            BermudanExercise(bermudanDates), rhTermStructure,            Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));        //Do the pricing for each model        itmBermudanSwaption.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));        std::cout << "HW:       " << itmBermudanSwaption.NPV() << std::endl;        itmBermudanSwaption.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelHW2, 100)));        std::cout << "HW (num): " << itmBermudanSwaption.NPV() << std::endl;        itmBermudanSwaption.setPricingEngine(            Handle<PricingEngine>(new TreeSwaption(modelBK, 100)));        std::cout << "BK:       " << itmBermudanSwaption.NPV() << std::endl;        return 0;    } catch (std::exception& e) {        std::cout << e.what() << std::endl;        return 1;    } catch (...) {        std::cout << "unknown error" << std::endl;        return 1;    }}