Пример #1
0
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() );
		}
	}

}
Пример #2
0
QuantLib::Disposable<QuantLib::Matrix> RebonatoAngle::triangularAnglesParametrizationYY( const QuantLib::Array& angles,
																						size_t matrixSize,
																						size_t rank)
{
	// what if rank == 1?
	//QL_REQUIRE((rank-1) * (matrixSize ) == angles.size(),
	//			"rank-1) * (matrixSize ) == angles.size()");

	assert((rank-1) * (matrixSize ) == angles.size());

	QuantLib::Matrix BMatrix(matrixSize, rank);

	size_t k = 0; //angles index
	for (size_t i=0; i<matrixSize; ++i) {
		double sinProduct = 1.0;
		for (size_t j=0; j<rank-1; ++j) {
			BMatrix[i][j] = std::cos(angles[k]);
			BMatrix[i][j] *= sinProduct;
			sinProduct *= std::sin(angles[k]);
			++k;
		}
		BMatrix[i][rank-1] = sinProduct;
	}
	return BMatrix;
}
Пример #3
0
void LmmSkewCostFunction::reset_reference_calib(const QuantLib::Array & true_param)
{
	const size_t nbParam = true_param.size();
	buffer_calib_reference.resize(nbParam);
	for(size_t i=0;i<nbParam;++i)
	{
		buffer_calib_reference[i]=true_param[i];
	}
}
Пример #4
0
QuantLib::Array LmmSkewCostFunction::error_calib(const QuantLib::Array & actual_param) const
{
	size_t nbParam = actual_param.size();
	QuantLib::Array error_calib(nbParam,0.);
	for(size_t i=0;i<nbParam;++i)
	{
		error_calib[i]  = actual_param[i] - buffer_calib_reference[i] ;
	}
	return error_calib;
}
Пример #5
0
bool PCA::checkEigenvalue(QuantLib::Array& eigenvalues, size_t reducedRank)
{ 
	if(eigenvalues.size() < reducedRank)
	{
		return false;
	}

	//! check eigenvalues is in decreasing order.
	for(size_t i=1; i<eigenvalues.size(); ++i)
	{
		if(eigenvalues[i-1] < eigenvalues[i]) 
			return false;
	}

	//! check the first N=recudecRank eigenvalues are positive (=> reducedMatrix is postively defined)
	for(size_t i=0; i<reducedRank; ++i)
	{
		if(eigenvalues[i] < 0.0)
			return false;
	}

	return true;
}
Пример #6
0
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");
	//}

}