void vanillaSwapComparaisonExemple() { double strike = 1.6; size_t indexStart = 3; size_t indexEnd = 19; Tenor tenorStruture = Tenor::_6M; Tenor floatingTenor = Tenor::_6M; Tenor fixedTenor = Tenor::_12M; LMMTenorStructure_PTR lmmTenorStructure(new LMMTenorStructure(tenorStruture, 10)); size_t liborIndex = lmmTenorStructure->get_horizon()+1; std::vector<double> myInitialLibor(liborIndex); for (size_t i = 0; i <myInitialLibor.size(); i++) { myInitialLibor[i]=0.02;//+((double)i)*0.01; } //VanillaSwap_Chi_Trang VanillaSwap myVS(strike, indexStart , indexEnd, floatingTenor, fixedTenor, lmmTenorStructure); myVS.print("test_beginer_VanillaSwap.txt"); LmmVanillaSwapPricer myVSP(lmmTenorStructure); double prix_swap=myVSP.swapNPV_Analytical_1(myVS, myInitialLibor); cout << "FirstVersionSwapPrice: "<< prix_swap << endl; //GeneticSwap test GeneticSwap_CONSTPTR vanillaSwap_Genetic=InstrumentFactory::createVanillaSwap( strike,indexStart,indexEnd,floatingTenor,fixedTenor,lmmTenorStructure,1.0); GeneticVanillaSwapPricer_PTR geneticVanillaSwapPricer(new GeneticVanillaSwapPricer()); double geneticPrice=geneticVanillaSwapPricer->geneticVanillaSwap_Analytical(vanillaSwap_Genetic, myInitialLibor); cout << "GeneticSwapTest: "<<geneticPrice << endl; cout << "Difference:" << geneticPrice-prix_swap<< endl; }
void vanillaSwapComparaisonExemple() { double strike = 0.02; size_t indexStart = 3; size_t indexEnd = 19; Tenor tenorStruture = Tenor::_6M; Tenor floatingTenor = Tenor::_6M; Tenor fixedTenor = Tenor::_12M; LMMTenorStructure_PTR lmmTenorStructure(new LMMTenorStructure(tenorStruture, 10)); size_t liborIndex = lmmTenorStructure->get_horizon()+1; std::vector<double> myInitialLibor(liborIndex); for (size_t i = 0; i <myInitialLibor.size(); i++) { myInitialLibor[i]=0.02; //+((double)i)*0.01; } cout << "strike: " << strike << endl; cout << "indexStart: " << indexStart << endl; cout << "indexEnd: " << indexEnd << endl; cout << "tenorStrutureYearFraction: " << lmmTenorStructure->get_tenorType().YearFraction() << endl; cout << "floatingVStenorStrutureRatio: " << floatingTenor.ratioTo(tenorStruture) << endl; cout << "fixedVStenorStrutureRatio: " << fixedTenor.ratioTo(tenorStruture) << endl; cout << "myInitialLibor: "; for (size_t i = 0; i <myInitialLibor.size(); i++) { cout << myInitialLibor[i] << " "; } cout << endl; cout << endl; //VanillaSwap_Chi_Trang VanillaSwap myVS(strike, indexStart , indexEnd, floatingTenor, fixedTenor, lmmTenorStructure); LmmVanillaSwapPricer myVSP(lmmTenorStructure); double prix_swap=myVSP.swapNPV_Analytical_1(myVS, myInitialLibor); //GeneticSwap test //build geneticVanillaSwap GenericSwap_CONSTPTR vanillaSwap_Genetic=InstrumentFactory::createVanillaSwap( strike,indexStart,indexEnd,floatingTenor,fixedTenor,lmmTenorStructure,1.0); GenericVanillaSwapPricer_PTR geneticVanillaSwapPricer(new GenericVanillaSwapPricer()); double geneticPrice=geneticVanillaSwapPricer->genericVanillaSwap_Analytical(vanillaSwap_Genetic, myInitialLibor); cout << "FirstVersionSwapPrice: " << prix_swap << endl; cout << "GeneticSwapTest: " << geneticPrice << endl; cout << "Difference: " << geneticPrice-prix_swap << endl; }
void Test_McGeneticSwapLMMPricer() { //! Parameters double strike = 0.02; LMM::Index indexStart = 0; LMM::Index indexEnd = 20; Tenor floatingTenor = Tenor::_6M; Tenor fixedTenor = Tenor::_12M; Tenor tenorStruture = Tenor::_6M; size_t horizonYear = 10; LMMTenorStructure_PTR lmmTenorStructure( new LMMTenorStructure(tenorStruture, horizonYear)); cout << "strike: " << strike << endl; cout << "indexStart: " << indexStart << endl; cout << "indexEnd: " << indexEnd << endl; cout << "tenorStrutureYearFraction: " << lmmTenorStructure->get_tenorType().YearFraction() << endl; cout << "floatingVStenorStrutureRatio: " << floatingTenor.ratioTo(tenorStruture) << endl; cout << "fixedVStenorStrutureRatio: " << fixedTenor.ratioTo(tenorStruture) << endl; double fwdRate = 0.02; std::vector<double> liborsInitValue(lmmTenorStructure->get_horizon()+1, fwdRate); cout << "myInitialLibor: "; for (size_t i = 0; i <liborsInitValue.size(); i++) { cout << liborsInitValue[i] << " "; } cout << endl; cout << endl; //VanillaSwap_Chi_Trang VanillaSwap firstVersionVanillaSwap(strike, indexStart , indexEnd, floatingTenor, fixedTenor, lmmTenorStructure); LmmVanillaSwapPricer myVSP(lmmTenorStructure); double FirstVersionSwapPrice = myVSP.swapNPV_Analytical_1(firstVersionVanillaSwap, liborsInitValue); //---------------------------Build Lmm and McLmm's structure-------------------------------------- //! Parameter of h double a = -0.06; double b = 0.17; double c = 0.54; double d = 0.17; Shifted_HGVolatilityParam::ABCDParameter abcdParam (a,b,c,d); //Parameter of hg double g_constParam = 1.0; double shift_constParam = -0.01; ConstShifted_HGVolatilityParam_PTR hgParam( new ConstShifted_HGVolatilityParam(lmmTenorStructure,abcdParam,g_constParam,shift_constParam)); //! Correlation 1 size_t nbFactor = 3; // need to test nbFactor = 3, and nbFactor = size_t correlFullRank = lmmTenorStructure->get_horizon()+1; size_t correlReducedRank = nbFactor; //!"Check Parameters(): Condition not implemented yet." std::cout << "checkParams(): "; CorrelationReductionType::CorrelationReductionType correlReductionType = CorrelationReductionType::PCA; double correlAlpha = 0.0; double correlBeta = 0.1; Correlation_PTR correlation(new XY_beta_Correlation(correlFullRank,correlReducedRank, correlReductionType,correlAlpha,correlBeta)); correlation->calculate(); // for print. correlation->print("test_McTerminalLmm_Correlation.csv"); //hgVolatilityFunction ConstShifted_HGVolatilityFunction_PTR hgVolatilityFunction (new ConstShifted_HGVolatilityFunction(lmmTenorStructure, correlation, hgParam)); hgVolatilityFunction->print("test_McTerminalLmm_Volatility.csv"); //! Dispersion Dispersion dispersion(hgVolatilityFunction); unsigned long seed = 5033; RNGenerator_PTR rnGenerator(new McGenerator(seed)); //build lmm and mcLmm model Lmm_PTR shiftedLmm (new Lmm(dispersion)); McLmm_PTR mcLmm(new McTerminalLmm(shiftedLmm, liborsInitValue, rnGenerator, MCSchemeType::EULER)); //build a McGeneticSwapLMMPricer McGeneticSwapLMMPricer_PTR mcGeneticSwapLMMPricer(new McGeneticSwapLMMPricer(mcLmm)); //build the geneticVanillaSwap GeneticSwap_CONSTPTR vanillaSwap_Genetic=InstrumentFactory::createVanillaSwap( strike,indexStart,indexEnd,floatingTenor,fixedTenor,lmmTenorStructure,1.0); //use Monte Carlo Method size_t nbSimulation=10000; double MonteCarloPrice = mcGeneticSwapLMMPricer->swapNPV(vanillaSwap_Genetic, nbSimulation); //ordinaryGeneticVanillaSwapPricer GeneticVanillaSwapPricer_PTR geneticVanillaSwapPricer(new GeneticVanillaSwapPricer()); double OrdinaryGeneticVanillaSwapPrice = geneticVanillaSwapPricer->geneticVanillaSwap_Analytical(vanillaSwap_Genetic,liborsInitValue); //subVanillaSwap LMM::Index subIndexStart = 10; LMM::Index subIndexEnd = 16; GeneticSwap_CONSTPTR subVanillaSwap_Genetic=InstrumentFactory::createVanillaSwap( strike,subIndexStart,subIndexEnd,floatingTenor,fixedTenor,lmmTenorStructure,1.0); double subMonteCarloPrice = mcGeneticSwapLMMPricer->swapNPV(subVanillaSwap_Genetic, nbSimulation); //subOrdinaryGeneticVanillaSwapPrice double subOrdinaryGeneticVanillaSwapPrice = geneticVanillaSwapPricer->geneticVanillaSwap_Analytical(subVanillaSwap_Genetic,liborsInitValue); //subFirstVersionVanillaSwapPrice VanillaSwap subFirstVersionVanillaSwap(strike, subIndexStart , subIndexEnd, floatingTenor, fixedTenor, lmmTenorStructure); double subFirstVersionSwapPrice = myVSP.swapNPV_Analytical_1(subFirstVersionVanillaSwap, liborsInitValue); cout << "MonteCarloPrice: " << MonteCarloPrice << endl; cout << "OrdinaryGeneticVanillaSwapPrice: " << OrdinaryGeneticVanillaSwapPrice << endl; cout << "FirstVersionSwapPrice: " << FirstVersionSwapPrice << endl; cout << "Difference between MonteCarloPrice and OrdinaryGeneticVanillaSwapPrice: " << MonteCarloPrice-OrdinaryGeneticVanillaSwapPrice << endl; cout << "Difference between OrdinaryGeneticVanillaSwapPrice and FirstVersionSwapPrice: " << OrdinaryGeneticVanillaSwapPrice-FirstVersionSwapPrice << endl; cout << "subMonteCarloPrice: " << subMonteCarloPrice << endl; cout << "subOrdinaryGeneticVanillaSwapPrice: " << subOrdinaryGeneticVanillaSwapPrice << endl; cout << "subFirstVersionSwapPrice: " << subFirstVersionSwapPrice << endl; cout << "Difference between subMonteCarloPrice and subOrdinaryGeneticVanillaSwapPrice: " << subMonteCarloPrice-subOrdinaryGeneticVanillaSwapPrice << endl; cout << "Difference between subOrdinaryGeneticVanillaSwapPrice and subFirstVersionSwapPrice: " << subOrdinaryGeneticVanillaSwapPrice-subFirstVersionSwapPrice << endl; ofstream o; o.open("TestResult_GeneticVanillaSwap_05_06.csv", ios::out | ios::app ); o << endl; o << endl; o << endl; o << "strike: " << strike << endl; o << "indexStart: " << indexStart << endl; o << "indexEnd: " << indexEnd << endl; o << "floatingTenorVSLmmStructureTenorRatio: " << floatingTenor.ratioTo(lmmTenorStructure->get_tenorType()) << endl; o << "fixedTenorVSLmmStructureTenorRatio: " << fixedTenor.ratioTo(lmmTenorStructure->get_tenorType()) << endl; o << "floatingTenorYearFraction: " << floatingTenor.YearFraction() << endl; o << "fixedTenorYearFraction: " << fixedTenor.YearFraction() << endl; o << "horizonYear: " << horizonYear << endl; o << "liborsInitValue: "; for(size_t i=0; i<liborsInitValue.size(); i++) { o << liborsInitValue[i] << " "; } o << endl; o << "nbSimulation: " << nbSimulation << endl; o << "PRICES: " << endl; o << "MonteCarloPrice: " << MonteCarloPrice << endl; o << "OrdinaryGeneticVanillaSwapPrice: " << OrdinaryGeneticVanillaSwapPrice << endl; o << "FirstVersionSwapPrice: " << FirstVersionSwapPrice << endl; o << "Difference between MonteCarloPrice and OrdinaryGeneticVanillaSwapPrice: " << MonteCarloPrice-OrdinaryGeneticVanillaSwapPrice << endl; o << "Difference between OrdinaryGeneticVanillaSwapPrice and FirstVersionSwapPrice: " << OrdinaryGeneticVanillaSwapPrice-FirstVersionSwapPrice << endl; o << "SUBPRICES: "<< endl; o << "subIndexStart: " << subIndexStart << endl; o << "subIndexEnd: " << subIndexEnd << endl; o << "subMonteCarloPrice: " << subMonteCarloPrice << endl; o << "subOrdinaryGeneticVanillaSwapPrice: " << subOrdinaryGeneticVanillaSwapPrice << endl; o << "subFirstVersionSwapPrice: " << subFirstVersionSwapPrice << endl; o << "Difference between subMonteCarloPrice and subOrdinaryGeneticVanillaSwapPrice: " << subMonteCarloPrice-subOrdinaryGeneticVanillaSwapPrice << endl; o << "Difference between subOrdinaryGeneticVanillaSwapPrice and subFirstVersionSwapPrice: " << subOrdinaryGeneticVanillaSwapPrice-subFirstVersionSwapPrice << endl; o.close(); }
void Test_evaluation_basis() { LMM::Index indexStart = 2; //1Y LMM::Index indexEnd = 20; //10Y Tenor floatingLegTenorType = Tenor::_6M; Tenor fixedLegTenorType = Tenor::_1YR; assert(indexStart%2==0&&indexEnd%2==0); LMMTenorStructure_PTR lmmTenorStructure( new LMMTenorStructure(floatingLegTenorType, indexEnd/2)); std::vector<std::string> mkt_file_list = InputFileManager::get_VCUB_FileList(); const std::string& mkt_data_file = mkt_file_list.back(); std::string folder_name; // = "TotalCalib\\" ; config.use_positive_constraint_=true; std::string base_name_file = LMMPATH::get_BaseFileName(mkt_data_file) + "\\"; folder_name+=base_name_file; LMMPATH::reset_Output_SubFolder(folder_name ); LmmCalibrationConfig config; config.floatLegTenor_=floatingLegTenorType; config.fixedLegTenor_=fixedLegTenorType; config.model_nbYear_ = indexEnd/2; size_t fixedFloatRatio = config.fixedLegTenor_.ratioTo(config.floatLegTenor_); config.correl_FullRank_ = fixedFloatRatio*config.model_nbYear_+1; LmmSwaptionMarketData_PTR pLmmSwaptionMarketData = get_LmmSwaptionMarketData(config, mkt_data_file); const std::vector<double>& initLiborValues = pLmmSwaptionMarketData->get_LiborQuotes()->get_InitLibor(); double strike = 0.0137; std::vector<LMM::Index> exerciseDates; exerciseDates.push_back(2); //exerciseDates.push_back(4); //exerciseDates.push_back(6); //exerciseDates.push_back(8); //exerciseDates.push_back(10); //exerciseDates.push_back(12); //exerciseDates.push_back(14); //exerciseDates.push_back(16); //exerciseDates.push_back(18); exerciseDates.push_back(20); McLmm_PTR mcLmm_for_pricer = getMcLmmExample(lmmTenorStructure, initLiborValues, LmmCalibrationConfig()); size_t fixedFloatingRatio = fixedLegTenorType.ratioTo(floatingLegTenorType); std::vector<std::vector<size_t>> subset; for(size_t i = 0; i <= 2; i++) { subset.push_back(std::vector<size_t>()); subset.back().push_back(0); subset.back().push_back(0); subset.back().push_back(i); } //for(size_t j = 1; j <= 2; j++) //{ // subset.push_back(std::vector<size_t>()); // subset.back().push_back(0); // subset.back().push_back(j); // subset.back().push_back(0); //} /* for(size_t k = 1; k <= 2; k++) { subset.push_back(std::vector<size_t>()); subset.back().push_back(k); subset.back().push_back(0); subset.back().push_back(0); } */ size_t regressionIndex=2; LMM::Index liborIndex = indexStart + regressionIndex*fixedFloatingRatio; LMM::Index paymentIndex = indexStart + regressionIndex*fixedFloatingRatio + 1; VanillaSwap vanillaSwap( strike, liborIndex, indexEnd, floatingLegTenorType, fixedLegTenorType, lmmTenorStructure); std::vector<Basis_CONSTPTR> basis_vect; std::vector<Basis_Evaluator_CONSTPTR> basis_evaluator_vect; for(size_t basisIndex = 0; basisIndex<subset.size(); basisIndex++) { basis_vect.push_back(getBasis(subset[basisIndex], 1.0, vanillaSwap, strike, liborIndex)); basis_evaluator_vect.push_back(getBasisEvaluator(subset[basisIndex], McLmmVanillaSwapPricer(mcLmm_for_pricer))); } LS::Regression rg(LS::RegressionRepresentation(basis_vect, basis_evaluator_vect)); McLmm_PTR mcLmm = getMcLmmExample(lmmTenorStructure, initLiborValues, LmmCalibrationConfig()); McLmm_LS mcLmm_LS(mcLmm); mcLmm_LS.simulateLMM(1); size_t nb = 30000; clock_t startTime = clock(); std::vector<std::vector<double>> vect(nb); for(size_t i=0; i<nb; i++) { //rg.getRegressionRepresentation().evaluate_basis(mcLmm_LS.lmmSimualtionResults_[0]); vect[i].resize(3); vect[i]=rg.getRegressionRepresentation().getBasis_val_buffer(); vect[i]=std::vector<double>(3, 1.0); } clock_t endTime = clock(); clock_t time = endTime - startTime; double time_in_second = time/(double) CLOCKS_PER_SEC; cout << "time_in_second "<< time_in_second << endl; const matrix& m = mcLmm_LS.lmmSimualtionResults_[0].get_liborMatrix(); const std::vector<double>& numeraire = mcLmm_LS.lmmSimualtionResults_[0].get_numeraire(); std::vector<size_t> basis1; basis1.push_back(1); basis1.push_back(0); basis1.push_back(0); Basis_CONSTPTR basisA=getBasis(basis1, 1.0, vanillaSwap, strike, liborIndex); Basis_Evaluator_CONSTPTR basis_EvaluatorA = getBasisEvaluator(basis1, McLmmVanillaSwapPricer(mcLmm_for_pricer)); basis_EvaluatorA->evaluate(basisA,m, numeraire); clock_t startTime1 = clock(); for(size_t i=0; i<1000; i++) basis_EvaluatorA->evaluate(basisA,m, numeraire); clock_t endTime1 = clock(); clock_t time1 = endTime1 - startTime1; double time1_in_second = time1/(double) CLOCKS_PER_SEC; cout << "time1_in_second "<< time1_in_second << endl; std::vector<size_t> basis2; basis2.push_back(0); basis2.push_back(1); basis2.push_back(0); Basis_CONSTPTR basisB=getBasis(basis2, 1.0, vanillaSwap, strike, liborIndex); Basis_Evaluator_CONSTPTR basis_EvaluatorB = getBasisEvaluator(basis2, McLmmVanillaSwapPricer(mcLmm_for_pricer)); clock_t startTime2 = clock(); for(size_t i=0; i<1000; i++) basis_EvaluatorB->evaluate(basisB, m, numeraire); clock_t endTime2 = clock(); clock_t time2 = endTime2 - startTime2; double time2_in_second = time2/(double) CLOCKS_PER_SEC; cout << "time2_in_second "<< time2_in_second << endl; std::vector<size_t> basis3; basis3.push_back(0); basis3.push_back(0); basis3.push_back(1); Basis_CONSTPTR basisC=getBasis(basis3, 1.0, vanillaSwap, strike, liborIndex); Basis_Evaluator_CONSTPTR basis_EvaluatorC = getBasisEvaluator(basis3, McLmmVanillaSwapPricer(mcLmm_for_pricer)); clock_t startTime3 = clock(); for(size_t i=0; i<1000; i++) basis_EvaluatorC->evaluate(basisC, m, numeraire); clock_t endTime3 = clock(); clock_t time3 = endTime3 - startTime3; double time3_in_second = time3/(double) CLOCKS_PER_SEC; cout << "time3_in_second "<< time3_in_second << endl; EV_Evaluator_CONSTPTR ev_evaluator_libor(new EV_LiborRate_Evaluator()); EV_Evaluator_CONSTPTR ev_evaluator_swaprate(new EV_VanillaSwapRate_Evaluator(McLmmVanillaSwapPricer(mcLmm_for_pricer))); EV_CONSTPTR ev_swaprate(new EV_VanillaSwapRate( Rate1_CONSTPTR( new VanillaSwapRate(VanillaSwap(vanillaSwap))))); EV_CONSTPTR ev_libor(new EV_LiborRate(Rate1_CONSTPTR(new LiborRate(2,Tenor(Tenor::_6M))))); clock_t startTime4 = clock(); for(size_t i=0; i<1000; i++) ev_evaluator_swaprate->evaluate(ev_swaprate,m,numeraire); clock_t endTime4 = clock(); clock_t time4 = endTime4 - startTime4; double time4_in_second = time4/(double) CLOCKS_PER_SEC; cout << "time4_in_second "<< time4_in_second << endl; clock_t startTime5 = clock(); for(size_t i=0; i<1000; i++) ev_evaluator_libor->evaluate(ev_libor,m,numeraire); clock_t endTime5 = clock(); clock_t time5 = endTime5 - startTime5; double time5_in_second = time5/(double) CLOCKS_PER_SEC; cout << "time5_in_second "<< time5_in_second << endl; }