void test_LocalResetGMatrixMapping() { std::cout<<"hello test_LocalResetGMatrixMapping() "<<std::endl; size_t nbYear = 5; Tenor tenorfixedleg = Tenor::_1YR; Tenor tenorfloatleg = Tenor::_6M; const double increment_matrix = 10; LMMTenorStructure_PTR pLmmTenorStructure( new LMMTenorStructure(tenorfloatleg, nbYear) ); size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); size_t horizon = pLmmTenorStructure->get_horizon() ; size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(horizon,fixedfloatRatio ); UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); UpperTriangleVanillaSwaptionQuotes_ConstPTR atm_swaption_implied_vol_ptr = create_UpperTriangleVanillaSwaptionQuotes(pLmmTenorStructure, tenorfixedleg, tenorfloatleg); GMatrixMapping gMatrixMapping( g_matrix_size , empty_delegate_matrix , atm_swaption_implied_vol_ptr->get_UpperTriangularIndexPairMatrix() ); gMatrixMapping.print("test_LocalResetGMatrixMapping_init.csv"); Initiate_UpperTriangularDoubleMatrix(empty_delegate_matrix, increment_matrix); // loop reset delegate rows size_t max_DelegateRowIndex = gMatrixMapping.get_MaxDelegateRowIndex(); for(size_t iRowDelegate=1;iRowDelegate<=max_DelegateRowIndex;++iRowDelegate) { QuantLib::Array row_values = gMatrixMapping.get_DelegateArray(iRowDelegate); for(size_t jarray=0;jarray<row_values.size();++jarray) { size_t jColDelegate = jarray+1; row_values[jarray] = empty_delegate_matrix(iRowDelegate,jColDelegate); gMatrixMapping.reset_gDelegate(row_values,iRowDelegate); std::ostringstream local_gDelegate_file; local_gDelegate_file<< "test_LocalResetGMatrixMapping_reset_"<<iRowDelegate<<"th-Col.csv"; gMatrixMapping.print( local_gDelegate_file.str() ); } } check_gMatrix(gMatrixMapping.get_g_Ref(), increment_matrix); }
void test_CascadeResetGMatrixMapping() { { size_t nbYear = 5; Tenor tenorfixedleg = Tenor::_1YR; Tenor tenorfloatleg = Tenor::_6M; const double increment_matrix = 10; LMMTenorStructure_PTR pLmmTenorStructure( new LMMTenorStructure(tenorfloatleg, nbYear) ); size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); size_t horizon = pLmmTenorStructure->get_horizon() ; size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(horizon,fixedfloatRatio ); UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); UpperTriangleVanillaSwaptionQuotes_ConstPTR atm_swaption_implied_vol_ptr = create_UpperTriangleVanillaSwaptionQuotes(pLmmTenorStructure, tenorfixedleg, tenorfloatleg); GMatrixMapping gMatrixMapping( g_matrix_size , empty_delegate_matrix , atm_swaption_implied_vol_ptr->get_UpperTriangularIndexPairMatrix() ); gMatrixMapping.print("test_CascadeResetGMatrixMapping_init.csv"); Initiate_UpperTriangularDoubleMatrix(empty_delegate_matrix, increment_matrix); // loop reset delegate rows size_t max_DelegateRowIndex = gMatrixMapping.get_MaxDelegateRowIndex(); for(size_t iRowDelegate=1;iRowDelegate<=max_DelegateRowIndex;++iRowDelegate) { size_t max_DelegateColIndex = delegate_matrix_size - (iRowDelegate+1); for(size_t jColDelegate=1;jColDelegate<=max_DelegateColIndex;++jColDelegate) { std::pair<size_t,size_t> gDelegate_cell(iRowDelegate,jColDelegate); gMatrixMapping.reset_gDelegate(empty_delegate_matrix(iRowDelegate,jColDelegate) ,gDelegate_cell); std::ostringstream local_gDelegate_file; local_gDelegate_file<< "test_CascadeResetGMatrixMapping_reset_"<<iRowDelegate<<"row_"<< jColDelegate<<"col" <<".csv"; gMatrixMapping.print( local_gDelegate_file.str() ); } } } }
void test_TruncatedLocalResetGMatrixMapping() { { size_t nbYear = 6; Tenor tenorfixedleg = Tenor::_1YR; Tenor tenorfloatleg = Tenor::_6M; const double increment_matrix = 10; LMMTenorStructure_PTR pLmmTenorStructure( new LMMTenorStructure(tenorfloatleg, nbYear) ); size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); size_t horizon = pLmmTenorStructure->get_horizon() ; size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(horizon,fixedfloatRatio ); UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); Initiate_UpperTriangularDoubleMatrix(empty_delegate_matrix, increment_matrix); UpperTriangleVanillaSwaptionQuotes_ConstPTR atm_swaption_implied_vol_ptr = create_UpperTriangleVanillaSwaptionQuotes(pLmmTenorStructure, tenorfixedleg, tenorfloatleg); GMatrixMapping gMatrixMapping( g_matrix_size , empty_delegate_matrix , atm_swaption_implied_vol_ptr->get_UpperTriangularIndexPairMatrix() ); std::vector<size_t> truncated_cols; truncated_cols.push_back(2); truncated_cols.push_back(3); std::vector<size_t> truncated_rows; truncated_rows.push_back(2); truncated_rows.push_back(3); gMatrixMapping.reset_Truncated_gDelegate_Rows(truncated_rows); gMatrixMapping.reset_Truncated_gDelegate_Cols(truncated_cols); std::vector<size_t> non_truncated_row = get_NonTruncatedIndices(truncated_rows,nbYear); size_t value_counter=1; for(size_t iRow=0;iRow<non_truncated_row.size();++iRow) { const size_t iDelegate = non_truncated_row[iRow]; QuantLib::Array x = gMatrixMapping.get_DelegateArray(iDelegate); for(size_t jX=0;jX<x.size();++jX) { x[jX]=value_counter*0.1; ++value_counter; } gMatrixMapping.reset_gDelegate(x,iDelegate); std::cout<<" Delegate Row["<<iDelegate<<"] "<<x<<std::endl; std::ostringstream local_gDelegate_file; local_gDelegate_file<< "test_TruncatedLocalResetGMatrixMapping_"<<iDelegate<<"thRow.csv"; gMatrixMapping.print(local_gDelegate_file.str() ); } } }
void test_GlobalResetGMatrixMapping() { std::cout<<"hello test_GlobalResetGMatrixMapping() "<<std::endl; size_t nbYear = 5; Tenor tenorfixedleg = Tenor::_1YR; Tenor tenorfloatleg = Tenor::_6M; const double increment_matrix = 10; LMMTenorStructure_PTR pLmmTenorStructure( new LMMTenorStructure(tenorfloatleg, nbYear) ); size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); size_t horizon = pLmmTenorStructure->get_horizon() ; size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(horizon,fixedfloatRatio ); UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); Initiate_UpperTriangularDoubleMatrix(empty_delegate_matrix, increment_matrix); UpperTriangleVanillaSwaptionQuotes_ConstPTR atm_swaption_implied_vol_ptr = create_UpperTriangleVanillaSwaptionQuotes(pLmmTenorStructure, tenorfixedleg, tenorfloatleg); GMatrixMapping gMatrixMapping( g_matrix_size , empty_delegate_matrix , atm_swaption_implied_vol_ptr->get_UpperTriangularIndexPairMatrix() ); gMatrixMapping.print("test_GlobalResetGMatrixMapping.csv"); //check_gMatrix(gMatrixMapping.get_g_Ref(), increment_matrix); QuantLib::Array x = gMatrixMapping.get_DelegateArray(); std::cout<<std::endl<<std::endl<<x<<std::endl; LmmGnuplotPrinterMatrix gnuplot_printer(pLmmTenorStructure); const LowerTriangularDoubleMatrix& lower_matrix = gMatrixMapping.get_g_Ref(); gnuplot_printer.printVolMatrix(lower_matrix,"test_GlobalResetGMatrixMapping"); }
void test_TruncatedCascadeResetGMatrixMapping() { // use of reset parallelograme truncated row { size_t nbYear = 5; Tenor tenorfixedleg = Tenor::_1YR; Tenor tenorfloatleg = Tenor::_6M; const double increment_matrix = 10; LMMTenorStructure_PTR pLmmTenorStructure( new LMMTenorStructure(tenorfloatleg, nbYear) ); size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); size_t horizon = pLmmTenorStructure->get_horizon() ; size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(horizon,fixedfloatRatio ); UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); UpperTriangleVanillaSwaptionQuotes_ConstPTR atm_swaption_implied_vol_ptr = create_UpperTriangleVanillaSwaptionQuotes(pLmmTenorStructure, tenorfixedleg, tenorfloatleg); GMatrixMapping gMatrixMapping( g_matrix_size , empty_delegate_matrix , atm_swaption_implied_vol_ptr->get_UpperTriangularIndexPairMatrix() ); gMatrixMapping.print("test_TruncatedCascadeResetGMatrixMapping_init.csv"); //Initiate_UpperTriangularDoubleMatrixConstantDiagonal(empty_delegate_matrix, increment_matrix); Initiate_UpperTriangularDoubleMatrix(empty_delegate_matrix, increment_matrix); std::vector<std::pair<size_t,size_t> > truncated_cells; truncated_cells.push_back(std::pair<size_t,size_t>(1,1) ); //truncated_cells.push_back(std::pair<size_t,size_t>(1,4) ); //truncated_cells.push_back(std::pair<size_t,size_t>(2,2) ); //truncated_cells.push_back(std::pair<size_t,size_t>(4,1) ); for(size_t i=0;i<truncated_cells.size();++i) { gMatrixMapping.add_Truncated_gDelegate_Cells(truncated_cells[i]); } // loop reset delegate rows size_t max_DelegateRowIndex = gMatrixMapping.get_MaxDelegateRowIndex(); for(size_t iRowDelegate=1;iRowDelegate<=max_DelegateRowIndex;++iRowDelegate) { size_t max_DelegateColIndex = delegate_matrix_size - (iRowDelegate+1); for(size_t jColDelegate=1;jColDelegate<=max_DelegateColIndex;++jColDelegate) { std::pair<size_t,size_t> gDelegate_cell(iRowDelegate,jColDelegate); if( std::find(truncated_cells.begin(),truncated_cells.end(),gDelegate_cell) == truncated_cells.end() ) { std::pair<size_t,size_t> gDelegate_cell(iRowDelegate,jColDelegate); gMatrixMapping.reset_gDelegate(empty_delegate_matrix(iRowDelegate,jColDelegate) , gDelegate_cell); std::ostringstream local_gDelegate_file; local_gDelegate_file<< "test_TruncatedCascadeResetGMatrixMapping_reset_"<<iRowDelegate<<"row_"<< jColDelegate<<"col" <<".csv"; gMatrixMapping.print( local_gDelegate_file.str() ); } } } } }
void test_TruncatedGlobalResetGMatrixMapping() { { size_t nbYear = 6; Tenor tenorfixedleg = Tenor::_1YR; Tenor tenorfloatleg = Tenor::_6M; const double increment_matrix = 10; LMMTenorStructure_PTR pLmmTenorStructure( new LMMTenorStructure(tenorfloatleg, nbYear) ); size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); size_t horizon = pLmmTenorStructure->get_horizon() ; size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(horizon,fixedfloatRatio ); UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); Initiate_UpperTriangularDoubleMatrix(empty_delegate_matrix, increment_matrix); UpperTriangleVanillaSwaptionQuotes_ConstPTR atm_swaption_implied_vol_ptr = create_UpperTriangleVanillaSwaptionQuotes(pLmmTenorStructure, tenorfixedleg, tenorfloatleg); GMatrixMapping gMatrixMapping( g_matrix_size , empty_delegate_matrix , atm_swaption_implied_vol_ptr->get_UpperTriangularIndexPairMatrix() ); std::vector<std::pair<size_t,size_t> > truncated_cells; truncated_cells.push_back( std::pair<size_t,size_t>(1,1) ); truncated_cells.push_back( std::pair<size_t,size_t>(1,5) ); truncated_cells.push_back( std::pair<size_t,size_t>(5,1) ); for(size_t iCell=0;iCell<truncated_cells.size();++iCell) { gMatrixMapping.add_Truncated_gDelegate_Cells( truncated_cells[iCell] ); } QuantLib::Array x = gMatrixMapping.get_DelegateArray(); for(size_t i=0;i<x.size();++i){ x[i]=(1+i)*0.1;} gMatrixMapping.reset_gDelegate(x); gMatrixMapping.print("test_TruncatedGlobalResetGMatrixMapping_Extrapolation.csv"); } { size_t nbYear = 6; Tenor tenorfixedleg = Tenor::_1YR; Tenor tenorfloatleg = Tenor::_6M; const double increment_matrix = 10; LMMTenorStructure_PTR pLmmTenorStructure( new LMMTenorStructure(tenorfloatleg, nbYear) ); size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); size_t horizon = pLmmTenorStructure->get_horizon() ; size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(horizon,fixedfloatRatio ); UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); Initiate_UpperTriangularDoubleMatrix(empty_delegate_matrix, increment_matrix); UpperTriangleVanillaSwaptionQuotes_ConstPTR atm_swaption_implied_vol_ptr = create_UpperTriangleVanillaSwaptionQuotes(pLmmTenorStructure, tenorfixedleg, tenorfloatleg); GMatrixMapping gMatrixMapping( g_matrix_size , empty_delegate_matrix , atm_swaption_implied_vol_ptr->get_UpperTriangularIndexPairMatrix() ); std::vector<std::pair<size_t,size_t> > truncated_cells; truncated_cells.push_back( std::pair<size_t,size_t>(2,2) ); truncated_cells.push_back( std::pair<size_t,size_t>(1,3) ); truncated_cells.push_back( std::pair<size_t,size_t>(3,1) ); truncated_cells.push_back( std::pair<size_t,size_t>(3,3) ); for(size_t iCell=0;iCell<truncated_cells.size();++iCell) { gMatrixMapping.add_Truncated_gDelegate_Cells( truncated_cells[iCell] ); } QuantLib::Array x = gMatrixMapping.get_DelegateArray(); for(size_t i=0;i<x.size();++i){ x[i]=(1+i)*0.1;} gMatrixMapping.reset_gDelegate(x); gMatrixMapping.print("test_TruncatedGlobalResetGMatrixMapping_Interpolation.csv"); } //{ // size_t nbYear = 6; // Tenor tenorfixedleg = Tenor::_1YR; // Tenor tenorfloatleg = Tenor::_6M; // const double increment_matrix = 10; // LMMTenorStructure_PTR pLmmTenorStructure( new LMMTenorStructure(tenorfloatleg, nbYear) ); // size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); // size_t horizon = pLmmTenorStructure->get_horizon() ; // size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); // size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(horizon,fixedfloatRatio ); // UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); // Initiate_UpperTriangularDoubleMatrix(empty_delegate_matrix, increment_matrix); // UpperTriangleVanillaSwaptionQuotes_ConstPTR atm_swaption_implied_vol_ptr = create_UpperTriangleVanillaSwaptionQuotes(pLmmTenorStructure, tenorfixedleg, tenorfloatleg); // GMatrixMapping gMatrixMapping( // g_matrix_size // , empty_delegate_matrix // , atm_swaption_implied_vol_ptr->get_UpperTriangularIndexPairMatrix() ); // std::vector<size_t> truncated_rows; // truncated_rows.push_back(1); // truncated_rows.push_back(3); // truncated_rows.push_back(5); // gMatrixMapping.reset_Truncated_gDelegate_Rows(truncated_rows); // QuantLib::Array x = gMatrixMapping.get_DelegateArray(); // for(size_t i=0;i<x.size();++i){ x[i]=(1+i)*0.1;} // gMatrixMapping.reset_gDelegate(x); // gMatrixMapping.print("test_TruncatedGlobalResetGMatrixMapping_Row.csv"); //} //{ // size_t nbYear = 6; // Tenor tenorfixedleg = Tenor::_1YR; // Tenor tenorfloatleg = Tenor::_6M; // const double increment_matrix = 10; // LMMTenorStructure_PTR pLmmTenorStructure( new LMMTenorStructure(tenorfloatleg, nbYear) ); // size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); // size_t horizon = pLmmTenorStructure->get_horizon() ; // size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); // size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(horizon,fixedfloatRatio ); // UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); // Initiate_UpperTriangularDoubleMatrix(empty_delegate_matrix, increment_matrix); // UpperTriangleVanillaSwaptionQuotes_ConstPTR atm_swaption_implied_vol_ptr = create_UpperTriangleVanillaSwaptionQuotes(pLmmTenorStructure, tenorfixedleg, tenorfloatleg); // GMatrixMapping gMatrixMapping( // g_matrix_size // , empty_delegate_matrix // , atm_swaption_implied_vol_ptr->get_UpperTriangularIndexPairMatrix() ); // std::vector<size_t> truncated_cols; // truncated_cols.push_back(2); // truncated_cols.push_back(3); // std::vector<size_t> truncated_rows; // truncated_rows.push_back(2); // truncated_rows.push_back(3); // gMatrixMapping.reset_Truncated_gDelegate_Rows(truncated_rows); // gMatrixMapping.reset_Truncated_gDelegate_Cols(truncated_cols); // QuantLib::Array x = gMatrixMapping.get_DelegateArray(); // for(size_t i=0;i<x.size();++i){ x[i]=(1+i)*0.1;} // gMatrixMapping.reset_gDelegate(x); // gMatrixMapping.print("test_TruncatedGlobalResetGMatrixMapping_ColRow.csv"); //} }
GMatrix_Vol_gMapping JB_marketData_LMM_Global_gCalibration( const LmmCalibrationConfig& config , LmmSwaptionMarketData_PTR pLmmSwaptionMarketData , Shifted_HGVolatilityFunction_PTR shifted_HGVolatilityFunction , Correlation_PTR found_correlation_ptr , GMatrixMapping_PTR init_gMapping ) { assert(!config.use_local_calib_); //? size_t nbYear = pLmmSwaptionMarketData->get_nbYear(); //nbYear std::string base_file_name = pLmmSwaptionMarketData->get_MarketDataBaseFileName(); Tenor tenorfixedleg = Tenor::_1YR ; Tenor tenorfloatleg = Tenor::_6M ; size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); std::string base_name; base_name = base_file_name+"_gMatrixGlobalCalibration" ; Shifted_HGVolatilityParam_PTR shifted_HGVolatilityParam = shifted_HGVolatilityFunction->get_ShiftedHGVolatilityParam_PTR(); //create LMM components LMMTenorStructure_PTR pLMMTenorStructure( new LMMTenorStructure(tenorfloatleg,nbYear) ); const double a=shifted_HGVolatilityParam->get_ABCD().a_; const double b=shifted_HGVolatilityParam->get_ABCD().b_; const double c=shifted_HGVolatilityParam->get_ABCD().c_; const double d=shifted_HGVolatilityParam->get_ABCD().d_; QuantLib::Array shiftValues_QL = shifted_HGVolatilityParam->get_ArrayFrom_Shift(); std::vector<double> shiftValues(shiftValues_QL.size()); for(size_t i=0; i<shiftValues_QL.size(); i++) shiftValues[i]=shiftValues_QL[i]; const Shifted_HGVolatilityParam::LowerTriangularMatrix pGMatrix(shifted_HGVolatilityParam->get_gMatrix()); Shifted_HGVolatilityParam::ABCDParameter abcdParam(a,b,c,d); ConstShifted_HGVolatilityParam_PTR pShifted_HGVolatilityParam( new ConstShifted_HGVolatilityParam( pLMMTenorStructure, abcdParam, pGMatrix, shiftValues)); Shifted_HGVolatilityFunction_PTR pVolatilityFunction (new ConstShifted_HGVolatilityFunction(pLMMTenorStructure, found_correlation_ptr, pShifted_HGVolatilityParam)); Dispersion dispersion(pVolatilityFunction); Lmm_PTR lmm_ptr(new Lmm(dispersion) ); LmmVanillaSwaptionApproxPricer_Rebonato_PTR pLmmVanillaSwaptionApproxPricer_Rebonato(new LmmVanillaSwaptionApproxPricer_Rebonato(lmm_ptr)); // create gMatrixMapping size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(pLMMTenorStructure->get_horizon() ,fixedfloatRatio ); UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); GMatrixMapping_PTR pGMatrixMapping; if(init_gMapping) { pGMatrixMapping = init_gMapping; } else { //initiate gMatrixMapping all gDelegate to 1 pGMatrixMapping.reset( new GMatrixMapping(g_matrix_size, empty_delegate_matrix, pLmmSwaptionMarketData->get_SwaptionQuotes_ATM()->get_UpperTriangularIndexPairMatrix()) ); QuantLib::Array g_delegate_vector = pGMatrixMapping->get_DelegateArray(); for(size_t i=0;i<g_delegate_vector.size();++i) g_delegate_vector[i] = 1.; pGMatrixMapping->reset_gDelegate(g_delegate_vector); } pShifted_HGVolatilityParam->reset_g_matrix( pGMatrixMapping->get_g_Ref() ); pLmmVanillaSwaptionApproxPricer_Rebonato->update_VolatilityParam(pShifted_HGVolatilityParam); // Create const function LmmPenalty_PTR pLmmPenalty(new LmmPenalty(config.penalty_time_homogeneity_,config.penalty_libor_) ); LmmBaseCostFunction_PTR pLmmCostFunction(new LmmGlobal_gCostFunction ( pLmmVanillaSwaptionApproxPricer_Rebonato, pLmmSwaptionMarketData->get_LiborQuotes(), pLmmSwaptionMarketData->get_SwaptionQuotes_ATM(), pGMatrixMapping, pShifted_HGVolatilityParam, pLmmPenalty ) ); //costumize swaptions weights UpperTriangularDoubleMatrix swpm_weight_matrix = pLmmCostFunction->get_SwaptionWeightMatrix(); //swpm_weight_matrix(7,1)=1e-6; //swpm_weight_matrix(10,1)=1e-6; //swpm_weight_matrix(5,3)=0.; pLmmCostFunction->reset_SwaptionWeightMatrix(swpm_weight_matrix); //std::ostringstream file_costfunc_stream;file_costfunc_stream<<base_name<<"Calibration_"<<nbYear<<"YR_pel_time"<<penalty_time_homogene<<"_pel_lib"<<penalty_libor <<"_LmmCostFunction.csv"; //pLmmCostFunction->print( file_costfunc_stream.str() ); // Create Calibrator LmmGlobal_gCalibrator lmmCalibrator ( *pGMatrixMapping.get() , 200 //maxIter , 1e-11 //x_epsilon , 1e-11 //f_epsilon , pLmmCostFunction ); if(config.use_positive_constraint_) lmmCalibrator.activate_PositiveConstraint(); lmmCalibrator.solve(); std::string penalty_info_str; if( !pLmmPenalty->isEmpty() ) { const double pT = config.penalty_time_homogeneity_; const double pL = config.penalty_libor_ ; std::ostringstream pel; pel<<"_pT_"<<pT<<"_pL_"<<pL; penalty_info_str = pel.str(); } std::ostringstream file_result_stream;file_result_stream<<base_name<<penalty_info_str<<"_result.csv"; std::string file_calibration_result(file_result_stream.str()); lmmCalibrator.printPlusPlus(file_calibration_result); std::ostringstream file_gDelegate_stream;file_gDelegate_stream<<base_name<<penalty_info_str<<"_gDelegate.csv"; std::string file_gDelegate_vol(file_gDelegate_stream.str() ); pGMatrixMapping->print(file_gDelegate_vol); std::ostringstream file_vol_stream;file_vol_stream<<base_name<<penalty_info_str<<"_vol.csv"; std::string file_calibrated_vol(file_vol_stream.str() ); pShifted_HGVolatilityParam->print( file_calibrated_vol ); config.result_quote_error_l2 = lmmCalibrator.get_QuoteError_L2(); config.result_quote_error_l1 = lmmCalibrator.get_QuoteError_L1(); config.result_quote_error_linf = lmmCalibrator.get_QuoteError_LInf(); if( !pLmmPenalty->isEmpty() ) { config.result_pelTime_error_l2 = lmmCalibrator.get_PenaltyTimeHomogeneity_L2(); config.result_pelTime_error_l1 = lmmCalibrator.get_PenaltyTimeHomogeneity_L1(); config.result_pelTime_error_linf = lmmCalibrator.get_PenaltyTimeHomogeneity_L_INF(); config.result_pelLibor_error_l2 = lmmCalibrator.get_PenaltySmoothMaturity_L2(); config.result_pelLibor_error_l1 = lmmCalibrator.get_PenaltySmoothMaturity_L1(); config.result_pelLibor_error_linf = lmmCalibrator.get_PenaltySmoothMaturity_L_INF(); } { std::string common_result_file_name = "calib_result_gGlobal.csv"; std::string full_common_result_file = LMMPATH::get_Root_OutputPath() + common_result_file_name ; std::ofstream final_result ; final_result.open(full_common_result_file.c_str(), std::ios::app); final_result<<std::endl<<std::endl<< "============= Test At "<<LMMPATH::get_TimeDateNow() <<",,,,,, Error LInf, "<<lmmCalibrator.get_QuoteError_LInf() <<std::endl ; final_result<< lmmCalibrator.get_BaseGeneral_Result_Info(); final_result.close(); } return GMatrix_Vol_gMapping(pVolatilityFunction,pGMatrixMapping); }
void marketData_LMM_Local_gCalibration( const LmmCalibrationConfig& config , LmmSwaptionMarketData_PTR pLmmSwaptionMarketData , const QuantLib::Array& abcd_param , Correlation_PTR found_correlation_ptr , GMatrixMapping_PTR init_gMapping ) { assert(config.use_local_calib_); size_t nbYear = pLmmSwaptionMarketData->get_nbYear(); std::string base_file_name = pLmmSwaptionMarketData->get_MarketDataBaseFileName(); Tenor tenorfixedleg = Tenor::_1YR ; Tenor tenorfloatleg = Tenor::_6M ; size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); std::string base_name; base_name = base_file_name+"_gMatrixLocalCalibration" ; //create LMM components LMMTenorStructure_PTR pLMMTenorStructure( new LMMTenorStructure(tenorfloatleg,nbYear) ); const double a=abcd_param[0]; const double b=abcd_param[1]; const double c=abcd_param[2]; const double d=abcd_param[3]; Shifted_HGVolatilityParam::ABCDParameter abcdParam(a,b,c,d); ConstShifted_HGVolatilityParam_PTR pNoShifted_HGVolatilityParam( new ConstShifted_HGVolatilityParam(pLMMTenorStructure, abcdParam, 1., 0.)); Shifted_HGVolatilityFunction_PTR pVolatilityFunction (new ConstShifted_HGVolatilityFunction(pLMMTenorStructure, found_correlation_ptr , pNoShifted_HGVolatilityParam)); Dispersion dispersion(pVolatilityFunction); Lmm_PTR lmm_ptr(new Lmm(dispersion) ); LmmVanillaSwaptionApproxPricer_Rebonato_PTR pLmmVanillaSwaptionApproxPricer_Rebonato(new LmmVanillaSwaptionApproxPricer_Rebonato(lmm_ptr)); // create gMatrixMapping size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(pLMMTenorStructure->get_horizon() ,fixedfloatRatio ); UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); GMatrixMapping_PTR pGMatrixMapping; if(init_gMapping) { pGMatrixMapping = init_gMapping; } else { //initiate gMatrixMapping all gDelegate to 1 pGMatrixMapping.reset( new GMatrixMapping(g_matrix_size, empty_delegate_matrix, pLmmSwaptionMarketData->get_SwaptionQuotes_ATM()->get_UpperTriangularIndexPairMatrix()) ); QuantLib::Array g_delegate_vector = pGMatrixMapping->get_DelegateArray(); for(size_t i=0;i<g_delegate_vector.size();++i) g_delegate_vector[i] = 1.; pGMatrixMapping->reset_gDelegate(g_delegate_vector); } pNoShifted_HGVolatilityParam->reset_g_matrix( pGMatrixMapping->get_g_Ref() ); pLmmVanillaSwaptionApproxPricer_Rebonato->update_VolatilityParam(pNoShifted_HGVolatilityParam); LmmBaseCostFunction_PTR pLmmCostFunction(new LmmLocal_gCostFunction ( pLmmVanillaSwaptionApproxPricer_Rebonato, pLmmSwaptionMarketData->get_LiborQuotes(), pLmmSwaptionMarketData->get_SwaptionQuotes_ATM(), pGMatrixMapping, pNoShifted_HGVolatilityParam ) ); //costumize swaptions weights UpperTriangularDoubleMatrix swpm_weight_matrix = pLmmCostFunction->get_SwaptionWeightMatrix(); //swpm_weight_matrix(7,1)=1e-6; //swpm_weight_matrix(10,1)=1e-6; //swpm_weight_matrix(5,3)=0.; pLmmCostFunction->reset_SwaptionWeightMatrix(swpm_weight_matrix); // Create Calibrator LmmLocal_gCalibrator lmmCalibrator ( *pGMatrixMapping.get() , 3000 //maxIter , 1e-11 //x_epsilon , 1e-11 //f_epsilon , pLmmCostFunction ); if(config.use_positive_constraint_) lmmCalibrator.activate_PositiveConstraint(); lmmCalibrator.solve(); std::ostringstream file_result_stream;file_result_stream<<base_name<<"_result.csv"; std::string file_calibration_result(file_result_stream.str()); lmmCalibrator.printPlusPlus(file_calibration_result); std::ostringstream file_gDelegate_stream;file_gDelegate_stream<<base_name<<"_gDelegate.csv"; std::string file_gDelegate_vol(file_gDelegate_stream.str() ); pGMatrixMapping->print(file_gDelegate_vol); std::ostringstream file_vol_stream;file_vol_stream<<base_name<<"_vol.csv"; std::string file_calibrated_vol(file_vol_stream.str() ); pNoShifted_HGVolatilityParam->print( file_calibrated_vol ); { std::string common_result_file_name = "calib_result_gLocal.csv"; std::string full_common_result_file = LMMPATH::get_Root_OutputPath() + common_result_file_name ; std::ofstream final_result ; final_result.open(full_common_result_file.c_str(), std::ios::app); final_result<<std::endl<<std::endl<< "============= Test At "<<LMMPATH::get_TimeDateNow() <<",,,,,, Error LInf, "<<lmmCalibrator.get_QuoteError_LInf() <<std::endl ; final_result<< lmmCalibrator.get_BaseGeneral_Result_Info(); final_result.close(); } }
GMatrixMapping_PTR marketData_LMM_CascadeExact_calibration( const LmmCalibrationConfig& config , LmmSwaptionMarketData_PTR pLmmSwaptionMarketData , const QuantLib::Array& abcd_param , Correlation_PTR found_correlation_ptr ) { size_t nbYear = pLmmSwaptionMarketData->get_nbYear(); std::string base_file_name = pLmmSwaptionMarketData->get_MarketDataBaseFileName(); Tenor tenorfixedleg = Tenor::_1YR ; Tenor tenorfloatleg = Tenor::_6M ; size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); std::string base_name=base_file_name +"_gMatrixCascadeCalibration" ; std::ostringstream file_lmm_mkt_stream;file_lmm_mkt_stream<<base_name<<"_Quotation_"<<nbYear<<"YR_.csv"; pLmmSwaptionMarketData->print(file_lmm_mkt_stream.str()); //create LMM components LMMTenorStructure_PTR pLMMTenorStructure( new LMMTenorStructure(tenorfloatleg,nbYear) ); const double a=abcd_param[0]; const double b=abcd_param[1]; const double c=abcd_param[2]; const double d=abcd_param[3]; //const double a=0.0438867,b=0.0179444,c=0.554972,d=0.121429;/// ATTENTION< TEMPORARY, TODELETE and uses abcd calibrator above Shifted_HGVolatilityParam::ABCDParameter abcdParam(a,b,c,d); ConstShifted_HGVolatilityParam_PTR pNoShifted_HGVolatilityParam( new ConstShifted_HGVolatilityParam(pLMMTenorStructure, abcdParam, 1., 0.)); Shifted_HGVolatilityFunction_PTR pVolatilityFunction (new ConstShifted_HGVolatilityFunction(pLMMTenorStructure, found_correlation_ptr, pNoShifted_HGVolatilityParam)); Dispersion dispersion(pVolatilityFunction); Lmm_PTR lmm_ptr(new Lmm(dispersion) ); LmmVanillaSwaptionApproxPricer_Rebonato_PTR pLmmVanillaSwaptionApproxPricer_Rebonato(new LmmVanillaSwaptionApproxPricer_Rebonato(lmm_ptr)); // create gMatrixMapping size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(pLMMTenorStructure->get_horizon() ,fixedfloatRatio ); UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); GMatrixMapping_PTR pGMatrixMapping( new GMatrixMapping(g_matrix_size, empty_delegate_matrix, pLmmSwaptionMarketData->get_SwaptionQuotes_ATM()->get_UpperTriangularIndexPairMatrix()) ); //initiate gMatrixMapping all gDelegate to 1 QuantLib::Array g_delegate_vector = pGMatrixMapping->get_DelegateArray(); for(size_t i=0;i<g_delegate_vector.size();++i) g_delegate_vector[i] = 1.; pGMatrixMapping->reset_gDelegate(g_delegate_vector); pNoShifted_HGVolatilityParam->reset_g_matrix( pGMatrixMapping->get_g_Ref() ); pLmmVanillaSwaptionApproxPricer_Rebonato->update_VolatilityParam(pNoShifted_HGVolatilityParam); LmmBaseCostFunction_PTR pLmmCascadeCostFunction ( new LmmCascade_gCostFunction ( pLmmVanillaSwaptionApproxPricer_Rebonato ,pLmmSwaptionMarketData->get_LiborQuotes() ,pLmmSwaptionMarketData->get_SwaptionQuotes_ATM() ,pGMatrixMapping ,pNoShifted_HGVolatilityParam ) ); UpperTriangularDoubleMatrix copy_weight_matrix = pLmmCascadeCostFunction->get_SwaptionWeightMatrix(); //copy_weight_matrix(2,1)=1000; pLmmCascadeCostFunction->reset_SwaptionWeightMatrix(copy_weight_matrix); LmmCascade_gCalibrator lmmCalibrator ( *pGMatrixMapping.get() , 100000 // config.maxIter , 1e-12 // config.x_epsilon , 1e-10 // config.f_epsilon , pLmmCascadeCostFunction ); if(config.use_positive_constraint_) lmmCalibrator.activate_PositiveConstraint(); lmmCalibrator.solve(); std::ostringstream file_vol_stream;file_vol_stream<<base_name<<"_vol.csv"; std::string file_calibrated_vol(file_vol_stream.str() ); pNoShifted_HGVolatilityParam->print( file_calibrated_vol ); std::ostringstream file_result_stream;file_result_stream<<base_name<<"_result.csv"; std::string file_calibration_result(file_result_stream.str()); lmmCalibrator.printPlusPlus(file_calibration_result); std::ostringstream file_gDelegate_stream;file_gDelegate_stream<<base_name<<"_gDelegate.csv"; std::string file_gDelegate_vol(file_gDelegate_stream.str() ); pGMatrixMapping->print(file_gDelegate_vol); // print in a common file { std::string common_result_file_name = "calib_result_gCascade.csv"; std::string full_common_result_file = LMMPATH::get_Root_OutputPath() + common_result_file_name ; std::ofstream final_result ; final_result.open(full_common_result_file.c_str(), std::ios::app); final_result<<std::endl<<std::endl<< "============= Test At "<<LMMPATH::get_TimeDateNow() <<",,,,,, Error LInf, "<<lmmCalibrator.get_QuoteError_LInf() <<std::endl ; final_result<< lmmCalibrator.get_BaseGeneral_Result_Info(); final_result.close(); } return pGMatrixMapping; }